Skip to content

Commit

Permalink
feat: Swaping to new texture on the backend (#79)
Browse files Browse the repository at this point in the history
* feat: Swaping to new texture on the backend

* fix format
  • Loading branch information
erickzanardo authored Mar 14, 2024
1 parent 108cc24 commit f603289
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
Binary file modified api/packages/board_renderer/assets/letters.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions api/packages/board_renderer/lib/src/board_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ typedef CreateCommand = img.Command Function();
typedef CreateImage = img.Image Function({
required int width,
required int height,
int numChannels,
img.Color backgroundColor,
});

/// A function that draws a rectangle in an image.
Expand Down Expand Up @@ -131,6 +133,8 @@ class BoardRenderer {
final image = _createImage(
width: totalWidth + cellSize,
height: totalHeight + cellSize,
numChannels: 4,
backgroundColor: img.ColorRgba8(0, 255, 255, 255),
);

for (final word in words) {
Expand Down Expand Up @@ -168,6 +172,7 @@ class BoardRenderer {

final createdCommand = _createCommand()
..image(image)
..convert(numChannels: 4, alpha: 0)
..encodePng();

await createdCommand.execute();
Expand All @@ -194,14 +199,16 @@ class BoardRenderer {
Future<Uint8List> renderSection(BoardSection section) async {
final words = [...section.words, ...section.borderWords];

const cellSize = 40;
const cellSize = 80;

final totalWidth = section.size * cellSize;
final totalHeight = section.size * cellSize;

final image = _createImage(
width: totalWidth,
height: totalHeight,
numChannels: 4,
backgroundColor: img.ColorRgba8(0, 255, 255, 255),
);

const url = 'http://127.0.0.1:8080/assets/letters.png';
Expand Down Expand Up @@ -233,14 +240,17 @@ class BoardRenderer {
: position.$2 * cellSize
);
if (dstX < totalWidth && dstY < totalHeight && dstX >= 0 && dstY >= 0) {
final srcX =
word.solvedTimestamp == null ? 2080 : charIndex * cellSize;

_compositeImage(
image,
texture,
dstX: dstX,
dstY: dstY,
dstW: cellSize,
dstH: cellSize,
srcX: charIndex * cellSize,
srcX: srcX,
srcY: 0,
srcW: cellSize,
srcH: cellSize,
Expand Down
48 changes: 42 additions & 6 deletions api/packages/board_renderer/test/src/board_renderer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ void main() {

final renderer = BoardRenderer(
createCommand: () => command,
createImage: ({required width, required height}) => image,
createImage: ({
required width,
required height,
int numChannels = 4,
img.Color? backgroundColor,
}) =>
image,
drawRect: (
img.Image dst, {
required int x1,
Expand Down Expand Up @@ -84,7 +90,13 @@ void main() {

final renderer = BoardRenderer(
createCommand: () => command,
createImage: ({required width, required height}) => image,
createImage: ({
required width,
required height,
int numChannels = 4,
img.Color? backgroundColor,
}) =>
image,
drawRect: (
img.Image dst, {
required int x1,
Expand Down Expand Up @@ -165,7 +177,13 @@ void main() {

final renderer = BoardRenderer(
createCommand: () => command,
createImage: ({required width, required height}) => image,
createImage: ({
required width,
required height,
int numChannels = 4,
img.Color? backgroundColor,
}) =>
image,
drawRect: (
img.Image dst, {
required int x1,
Expand Down Expand Up @@ -225,7 +243,13 @@ void main() {

final renderer = BoardRenderer(
createCommand: () => command,
createImage: ({required width, required height}) => image,
createImage: ({
required width,
required height,
int numChannels = 4,
img.Color? backgroundColor,
}) =>
image,
drawRect: (
img.Image dst, {
required int x1,
Expand Down Expand Up @@ -293,7 +317,13 @@ void main() {

final renderer = BoardRenderer(
createCommand: () => command,
createImage: ({required width, required height}) => image,
createImage: ({
required width,
required height,
int numChannels = 4,
img.Color? backgroundColor,
}) =>
image,
drawRect: (
img.Image dst, {
required int x1,
Expand Down Expand Up @@ -359,7 +389,13 @@ void main() {

final renderer = BoardRenderer(
createCommand: () => command,
createImage: ({required width, required height}) => image,
createImage: ({
required width,
required height,
int numChannels = 4,
img.Color? backgroundColor,
}) =>
image,
drawRect: (
img.Image dst, {
required int x1,
Expand Down
Binary file modified api/public/assets/letters.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f603289

Please sign in to comment.