From 1bf10f6d76445753d920f906d0c8e4fa8e481c60 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Wed, 22 Jun 2022 18:54:06 -0300 Subject: [PATCH] chore: cleanup 2d-mapping code for 4bpp horizontal sprites --- src/sprite2d/sprites.zig | 46 ++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/sprite2d/sprites.zig b/src/sprite2d/sprites.zig index 63f355d..a436d8f 100644 --- a/src/sprite2d/sprites.zig +++ b/src/sprite2d/sprites.zig @@ -908,10 +908,11 @@ pub const horizontal = struct { pub fn load() void { // In Memory, Tile Map is laid out like: 1 2 - const px_width = 8 * 2; - const len = px_width * @sizeOf(u32); + const tile_width = 8 * 4; + const tiles_per_row = 2; - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], len); + const sprite_width = tile_width * tiles_per_row; + GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], sprite_width); loadPalette(); } @@ -941,10 +942,11 @@ pub const horizontal = struct { pub fn load() void { // In Memory, Tile Map is laid out like: 1 2 3 4 - const px_width = 8 * 4; - const len = px_width * @sizeOf(u32); + const tile_width = 8 * 4; + const tiles_per_row = 4; - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], len); + const sprite_width = tile_width * tiles_per_row; + GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], sprite_width); loadPalette(); } @@ -979,12 +981,16 @@ pub const horizontal = struct { // In Memory, Tile Map is laid out like: 1 2 3 4 // 5 6 7 8 - const px_width = 8 * 4; - const offset = 0x40 / 4; - const len = px_width * @sizeOf(u32); + const tile_width = 8 * 4; + const tiles_per_row = 4; - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * offset), &Self.tile[px_width], len); + const sprite_width = tile_width * tiles_per_row; + const offset = 0x10 / tiles_per_row; + + const tile_idx = sprite_width / @sizeOf(u32); + + GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], sprite_width); + GBA.memcpy32(GBA.SPRITE_VRAM + sprite_width * (offset * 1), &Self.tile[tile_idx], sprite_width); loadPalette(); } @@ -1048,14 +1054,18 @@ pub const horizontal = struct { // 17 18 19 20 21 22 23 24 // 25 26 27 28 29 30 31 32 - const px_width = 8 * 8; - const offset = 0x40 / 8; - const len = px_width * @sizeOf(u32); + const tile_width = 8 * 4; + const tiles_per_row = 8; - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * offset), &Self.tile[px_width], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 2)), &Self.tile[px_width * 2], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 3)), &Self.tile[px_width * 3], len); + const sprite_width = tile_width * tiles_per_row; + const offset = 0x10 / tiles_per_row; + + const tile_idx = sprite_width / @sizeOf(u32); + + GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], sprite_width); + GBA.memcpy32(GBA.SPRITE_VRAM + sprite_width * (offset * 1), &Self.tile[tile_idx * 1], sprite_width); + GBA.memcpy32(GBA.SPRITE_VRAM + sprite_width * (offset * 2), &Self.tile[tile_idx * 2], sprite_width); + GBA.memcpy32(GBA.SPRITE_VRAM + sprite_width * (offset * 3), &Self.tile[tile_idx * 3], sprite_width); loadPalette(); }