diff --git a/src/sprite2d/main.zig b/src/sprite2d/main.zig index c3dd845..b453bca 100644 --- a/src/sprite2d/main.zig +++ b/src/sprite2d/main.zig @@ -6,13 +6,41 @@ const LCD = @import("gba").LCD; const OAM = @import("gba").OAM; const Input = @import("gba").Input; -const sprite_mod = @import("sprites.zig"); +const sprites = @import("sprites.zig"); -const SquareSpriteTuple = std.meta.Tuple(&[_]type{ Sprite8x8, Sprite16x16, Sprite32x32, Sprite64x64 }); -const HorizSpriteTuple = std.meta.Tuple(&[_]type{ Sprite16x8, Sprite32x8, Sprite32x16, Sprite64x32 }); -const VertSpriteTuple = std.meta.Tuple(&[_]type{ Sprite8x16, Sprite8x32, Sprite16x32, Sprite32x64 }); -const SpriteTupleTuple = std.meta.Tuple(&[_]type{ SquareSpriteTuple, HorizSpriteTuple, VertSpriteTuple }); -const sprites = initSpriteTupleTuple(); +const sprite_attrs = [12]OAM.Attribute{ + sprites.square.d2.bpp4.Sprite8x8.getAttribute(), + sprites.square.d2.bpp4.Sprite16x16.getAttribute(), + sprites.square.d2.bpp4.Sprite32x32.getAttribute(), + sprites.square.d2.bpp4.Sprite64x64.getAttribute(), + + sprites.horizontal.d2.bpp4.Sprite16x8.getAttribute(), + sprites.horizontal.d2.bpp4.Sprite32x8.getAttribute(), + sprites.horizontal.d2.bpp4.Sprite32x16.getAttribute(), + sprites.horizontal.d2.bpp4.Sprite64x32.getAttribute(), + + sprites.vertical.d2.bpp4.Sprite8x16.getAttribute(), + sprites.vertical.d2.bpp4.Sprite8x32.getAttribute(), + sprites.vertical.d2.bpp4.Sprite16x32.getAttribute(), + sprites.vertical.d2.bpp4.Sprite32x64.getAttribute(), +}; + +const sprite_load_fns = [12]fn () void{ + sprites.square.d2.bpp4.Sprite8x8.load, + sprites.square.d2.bpp4.Sprite16x16.load, + sprites.square.d2.bpp4.Sprite32x32.load, + sprites.square.d2.bpp4.Sprite64x64.load, + + sprites.horizontal.d2.bpp4.Sprite16x8.load, + sprites.horizontal.d2.bpp4.Sprite32x8.load, + sprites.horizontal.d2.bpp4.Sprite32x16.load, + sprites.horizontal.d2.bpp4.Sprite64x32.load, + + sprites.vertical.d2.bpp4.Sprite8x16.load, + sprites.vertical.d2.bpp4.Sprite8x32.load, + sprites.vertical.d2.bpp4.Sprite16x32.load, + sprites.vertical.d2.bpp4.Sprite32x64.load, +}; // TODO: ZigGBA should support spaces, slashes, underscores, etc. in ROM Title export var gameHeader linksection(".gbaheader") = GBA.Header.setup("SPRITE", "PAOD", "00", 0); @@ -26,9 +54,6 @@ pub fn main() noreturn { .objVramCharacterMapping = obj_mapping, // 2D Sprite Mapping }); - // Coppy Sprite Tiles to VRAM and Palette to PALRAM - sprites[0][0].load(); - // FIXME: There are issues with OAM.Attribute and the ZigGBA API // investigate what these issues are (could it just be my understanding?) const attr_ptr = @intToPtr([*]OAM.Attribute, 0x0700_0000); @@ -39,12 +64,9 @@ pub fn main() noreturn { // Set bit 9 on all sprites, therefore disabling them all for (oam_slice) |*attr| attr.* |= 0x0200; - var sprite = OAM.Attribute{}; - sprite.paletteMode = sprites[0][0].paletteMode(); - sprite.palette = 0; - sprite.doubleSizeOrVisible = false; - sprite.tileIndex = 0; - sprite.setSize(sprites[0][0].size()); + // Grab Default Sprite (Square 64x64 4bpp) + var sprite = sprite_attrs[3]; + sprite_load_fns[3](); var x: i32 = 96; var y: i32 = 32; @@ -65,684 +87,20 @@ pub fn main() noreturn { size_idx +%= shoulder; shape_idx %= 3; - switch (shape_idx) { - 0 => switch (size_idx) { - 0 => loadSprite(0, 0, &sprite), - 1 => loadSprite(0, 1, &sprite), - 2 => loadSprite(0, 2, &sprite), - 3 => loadSprite(0, 3, &sprite), - }, - 1 => switch (size_idx) { - 0 => loadSprite(1, 0, &sprite), - 1 => loadSprite(1, 1, &sprite), - 2 => loadSprite(1, 2, &sprite), - 3 => loadSprite(1, 3, &sprite), - }, - 2 => switch (size_idx) { - 0 => loadSprite(2, 0, &sprite), - 1 => loadSprite(2, 1, &sprite), - 2 => loadSprite(2, 2, &sprite), - 3 => loadSprite(2, 3, &sprite), - }, - else => {}, - } + loadSprite(shape_idx, size_idx, &sprite); } x += Input.getHorizontal() * 2; y += Input.getVertical() * 2; sprite.setPosition(x, y); - attr_ptr[0] = sprite; } } -const Sprite8x8 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00005EF7, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [8]u32{ - 0x21111112, 0x12111121, 0x11211211, 0x11112111, 0x11121111, 0x11211211, 0x12111121, 0x21111112, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size8x8; - } - - fn load(_: Self) void { - // 8x8 Sprites don't differ in any meaningful way between 1D and 2D mapping - - const sprite_width = 8; // 8x8 - const len = sprite_width * @sizeOf(u32); - - // Copy Tile Mapping - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], len); - - // Copy Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -const Sprite16x16 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00005EF7, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [32]u32{ - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111111, - 0x11111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size16x16; - } - - fn load(_: Self) void { - // In Memory, Tile Map is laid out like: 1 2 - // 3 4 - - const half = Self.tiles.len / 2; - - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], half * @sizeOf(u32)); // Top - GBA.memcpy32(GBA.SPRITE_VRAM + (half * 0x20), &Self.tiles[half], half * @sizeOf(u32)); // Bottom - - // Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -const Sprite32x32 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00005EF7, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [128]u32{ - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size32x32; - } - - fn load(_: Self) void { - // In Memory, Tile Map is laid out like: 1 2 3 4 - // 5 6 7 8 - // 9 10 11 12 - // 13 14 15 16 - - const px_width = 8 * 4; - const offset = 0x40 / 4; - const len = px_width * @sizeOf(u32); - - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * offset), &Self.tiles[px_width], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 2)), &Self.tiles[px_width * 2], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 3)), &Self.tiles[px_width * 3], len); - - // Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -const Sprite64x64 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00005EF7, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [512]u32{ - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size64x64; - } - - fn load(_: Self) void { - // In Memory, Tile Map is laid out like: 1 2 3 4 5 6 7 8 - // 9 10 11 12 13 14 15 16 - // 17 18 19 20 21 22 23 24 - // 25 26 27 28 29 30 31 32 - // 33 34 35 36 37 38 39 40 - // 41 42 43 44 45 46 47 48 - // 49 50 51 52 53 54 55 56 - // 57 58 59 60 61 62 63 64 - - const px_width = 8 * 8; - const offset = 0x40 / 8; - const len = px_width * @sizeOf(u32); - - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * offset), &Self.tiles[px_width], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 2)), &Self.tiles[px_width * 2], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 3)), &Self.tiles[px_width * 3], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 4)), &Self.tiles[px_width * 4], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 5)), &Self.tiles[px_width * 5], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 6)), &Self.tiles[px_width * 6], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 7)), &Self.tiles[px_width * 7], len); - - // Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -const Sprite16x8 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00006318, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [16]u32{ - 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11221111, 0x11112211, 0x11111122, - 0x22111111, 0x11221111, 0x11112211, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size16x8; - } - - fn load(_: Self) void { - // In Memory, Tile Map is laid out like: 1 2 - - const px_width = 8 * 2; - const len = px_width * @sizeOf(u32); - - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], len); - - // Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -const Sprite32x8 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00006318, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [32]u32{ - 0x11112222, 0x22221111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x22221111, 0x11112222, - 0x11111111, 0x11111111, 0x11112222, 0x22221111, 0x11111111, 0x11112222, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x22221111, 0x11111111, 0x11112222, 0x22221111, 0x11111111, 0x11111111, - 0x22221111, 0x11112222, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11112222, 0x22221111, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size32x8; - } - - fn load(_: Self) void { - // In Memory, Tile Map is laid out like: 1 2 3 4 - - const px_width = 8 * 4; - const len = px_width * @sizeOf(u32); - - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], len); - - // Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -const Sprite32x16 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00006318, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [64]u32{ - 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x22111111, 0x11221111, 0x11112211, 0x11111111, - 0x22111111, 0x11221111, 0x11112211, 0x11111122, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x22111111, 0x11221111, 0x11112211, 0x11111122, - 0x11111111, 0x11221111, 0x11112211, 0x11111122, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size32x32; - } - - fn load(_: Self) void { - // 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); - - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * offset), &Self.tiles[px_width], len); - - // Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -const Sprite64x32 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00006318, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [256]u32{ - 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x22111111, 0x11221111, 0x11112211, 0x11111122, - 0x22111111, 0x11221111, 0x11112211, 0x11111122, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x22111111, 0x11221111, 0x11112211, 0x11111111, - 0x22111111, 0x11221111, 0x11112211, 0x11111122, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x22111111, 0x11221111, 0x11112211, 0x11111122, - 0x11111111, 0x11221111, 0x11112211, 0x11111122, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x22111111, 0x11221111, 0x11112211, 0x11111122, - 0x22111111, 0x11221111, 0x11112211, 0x11111122, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size64x32; - } - - fn load(_: Self) void { - // In Memory, Tile Map is laid out like: 1 2 3 4 5 6 7 8 - // 9 10 11 12 13 14 15 16 - // 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); - - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * offset), &Self.tiles[px_width], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 2)), &Self.tiles[px_width * 2], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 3)), &Self.tiles[px_width * 3], len); - - // Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -const Sprite8x16 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00006318, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [16]u32{ - 0x21111112, 0x21111112, 0x12111121, 0x12111121, 0x11211211, 0x11211211, 0x11112111, 0x11112111, - 0x11121111, 0x11121111, 0x11211211, 0x11211211, 0x12111121, 0x12111121, 0x21111112, 0x21111112, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size8x16; - } - - fn load(_: Self) void { - // In Memory, Tile Map is laid out like: 1 - // 2 - - const sprite_len = 8; - - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], sprite_len * @sizeOf(u32)); - GBA.memcpy32(GBA.SPRITE_VRAM + (sprite_len * 0x40), &Self.tiles[sprite_len], sprite_len * @sizeOf(u32)); - - // Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -const Sprite8x32 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00006318, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [32]u32{ - 0x21111112, 0x21111112, 0x21111112, 0x21111112, 0x12111121, 0x12111121, 0x12111121, 0x12111121, - 0x11211211, 0x11211211, 0x11211211, 0x11211211, 0x11112111, 0x11112111, 0x11112111, 0x11112111, - 0x11121111, 0x11121111, 0x11121111, 0x11121111, 0x11211211, 0x11211211, 0x11211211, 0x11211211, - 0x12111121, 0x12111121, 0x12111121, 0x12111121, 0x21111112, 0x21111112, 0x21111112, 0x21111112, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size8x32; - } - - fn load(_: Self) void { - // In Memory, Tile Map is laid out like: 1 - // 2 - // 3 - // 4 - - const px_width = 8; - const offset = 0x40; - const len = px_width * @sizeOf(u32); - - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * offset), &Self.tiles[px_width], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 2)), &Self.tiles[px_width * 2], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 3)), &Self.tiles[px_width * 3], len); - - // Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -const Sprite16x32 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00006318, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [64]u32{ - 0x11111112, 0x11111112, 0x11111121, 0x11111121, 0x11111211, 0x11111211, 0x11112111, 0x11112111, - 0x21111111, 0x21111111, 0x12111111, 0x12111111, 0x11211111, 0x11211111, 0x11121111, 0x11121111, - 0x11121111, 0x11121111, 0x11211111, 0x11211111, 0x12111111, 0x12111111, 0x21111111, 0x21111111, - 0x11112111, 0x11112111, 0x11111211, 0x11111211, 0x11111121, 0x11111121, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x12111111, 0x12111111, 0x11211111, 0x11211111, 0x11121111, 0x11121111, - 0x11111112, 0x11111112, 0x11111121, 0x11111121, 0x11111211, 0x11111211, 0x11112111, 0x11112111, - 0x11112111, 0x11112111, 0x11111211, 0x11111211, 0x11111121, 0x11111121, 0x11111112, 0x11111112, - 0x11121111, 0x11121111, 0x11211111, 0x11211111, 0x12111111, 0x12111111, 0x21111111, 0x21111111, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size16x32; - } - - fn load(_: Self) void { - // In Memory, Tile Map is laid out like: 1 2 - // 3 4 - // 5 6 - // 7 8 - - const px_width = 8 * 2; - const offset = 0x40 / 2; - const len = px_width * @sizeOf(u32); - - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * offset), &Self.tiles[px_width], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 2)), &Self.tiles[px_width * 2], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 3)), &Self.tiles[px_width * 3], len); - - // Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -const Sprite32x64 = struct { - const Self = @This(); - - const pal = [8]u32{ - 0x3DEF0000, 0x00006318, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - }; - - const tiles = [256]u32{ - 0x11111112, 0x11111112, 0x11111121, 0x11111121, 0x11111211, 0x11111211, 0x11112111, 0x11112111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x21111111, 0x21111111, 0x12111111, 0x12111111, 0x11211111, 0x11211111, 0x11121111, 0x11121111, - 0x11121111, 0x11121111, 0x11211111, 0x11211111, 0x12111111, 0x12111111, 0x21111111, 0x21111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11112111, 0x11112111, 0x11111211, 0x11111211, 0x11111121, 0x11111121, 0x11111112, 0x11111112, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111112, 0x11111112, 0x11111121, 0x11111121, 0x11111211, 0x11111211, 0x11112111, 0x11112111, - 0x21111111, 0x21111111, 0x12111111, 0x12111111, 0x11211111, 0x11211111, 0x11121111, 0x11121111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11121111, 0x11121111, 0x11211111, 0x11211111, 0x12111111, 0x12111111, 0x21111111, 0x21111111, - 0x11112111, 0x11112111, 0x11111211, 0x11111211, 0x11111121, 0x11111121, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x12111111, 0x12111111, 0x11211111, 0x11211111, 0x11121111, 0x11121111, - 0x11111112, 0x11111112, 0x11111121, 0x11111121, 0x11111211, 0x11111211, 0x11112111, 0x11112111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11112111, 0x11112111, 0x11111211, 0x11111211, 0x11111121, 0x11111121, 0x11111112, 0x11111112, - 0x11121111, 0x11121111, 0x11211111, 0x11211111, 0x12111111, 0x12111111, 0x21111111, 0x21111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - - 0x21111111, 0x21111111, 0x12111111, 0x12111111, 0x11211111, 0x11211111, 0x11121111, 0x11121111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111112, 0x11111112, 0x11111121, 0x11111121, 0x11111211, 0x11111211, 0x11112111, 0x11112111, - 0x11112111, 0x11112111, 0x11111211, 0x11111211, 0x11111121, 0x11111121, 0x11111112, 0x11111112, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, - 0x11121111, 0x11121111, 0x11211111, 0x11211111, 0x12111111, 0x12111111, 0x21111111, 0x21111111, - }; - - fn paletteMode(_: Self) GBA.PaletteMode { - return .Color16; - } - - fn size(_: Self) OAM.ObjectSize { - return .Size32x64; - } - - fn load(_: Self) void { - // In Memory, Tile Map is laid out like: 1 2 3 4 - // 5 6 7 8 - // 9 10 11 12 - // 13 14 15 16 - // 17 18 19 20 - // 21 22 23 24 - // 25 26 27 28 - // 29 30 31 32 - - const px_width = 8 * 4; - const offset = 0x40 / 4; - const len = px_width * @sizeOf(u32); - - GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tiles[0], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * offset), &Self.tiles[px_width], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 2)), &Self.tiles[px_width * 2], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 3)), &Self.tiles[px_width * 3], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 4)), &Self.tiles[px_width * 4], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 5)), &Self.tiles[px_width * 5], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 6)), &Self.tiles[px_width * 6], len); - GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 7)), &Self.tiles[px_width * 7], len); - - // Palette - GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); - } -}; - -fn initSpriteTupleTuple() SpriteTupleTuple { - return .{ initSquareSpriteTuple(), initHorizSpriteTuple(), initVertSpriteTuple() }; -} - -fn initSquareSpriteTuple() SquareSpriteTuple { - return .{ Sprite8x8{}, Sprite16x16{}, Sprite32x32{}, Sprite64x64{} }; -} - -fn initHorizSpriteTuple() HorizSpriteTuple { - return .{ Sprite16x8{}, Sprite32x8{}, Sprite32x16{}, Sprite64x32{} }; -} - -fn initVertSpriteTuple() VertSpriteTuple { - return .{ Sprite8x16{}, Sprite8x32{}, Sprite16x32{}, Sprite32x64{} }; -} - -fn loadSprite(comptime kind: usize, comptime i: usize, sprite: *OAM.Attribute) void { +fn loadSprite(shape: usize, size: usize, sprite: *OAM.Attribute) void { GBA.memset32(GBA.SPRITE_VRAM, 0, 0x8000); // Clear Sprite VRAM - sprites[kind][i].load(); - sprite.setSize(sprites[kind][i].size()); + sprite.* = sprite_attrs[shape * 4 + size]; + sprite_load_fns[shape * 4 + size](); } diff --git a/src/sprite2d/sprites.zig b/src/sprite2d/sprites.zig index 87004e7..885d9c4 100644 --- a/src/sprite2d/sprites.zig +++ b/src/sprite2d/sprites.zig @@ -1,34 +1,93 @@ +const GBA = @import("gba").GBA; +const Attribute = @import("gba").OAM.Attribute; + /// Square Sprites -const square = struct { +pub const square = struct { /// 1D-Mapped Square Sprites const d1 = struct {}; /// 2D-Mapped Square Sprites - const d2 = struct { + pub const d2 = struct { /// 2D-Mapped Square Sprites using 4-bit Colour - const bpp4 = struct { + pub const bpp4 = struct { pub const palette: [8]u32 = [_]u32{ 0x3DEF0000, 0x00005EF7, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, }; - const Sprite8x8 = struct { + fn loadPalette() void { + GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &palette, palette.len * @sizeOf(u32)); + } + + pub const Sprite8x8 = struct { + const Self = @This(); + const tile = [8]u32{ 0x21111112, 0x12111121, 0x11211211, 0x11112111, 0x11121111, 0x11211211, 0x12111121, 0x21111112, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Square, + .size = 0, + }; + } + + pub fn load() void { + // 8x8 Sprites don't differ in any meaningful way between 1D and 2D mapping + + const sprite_width = 8; // 8x8 + const len = sprite_width * @sizeOf(u32); + + // Copy Tile Mapping + GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], len); + + loadPalette(); + } }; - const Sprite16x16 = struct { + pub const Sprite16x16 = struct { + const Self = @This(); + const tile = [32]u32{ 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, 0x21111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111111, 0x11111111, 0x12111111, 0x11211111, 0x11121111, 0x11112111, 0x11111211, 0x11111121, 0x11111112, 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Square, + .size = 1, + }; + } + + pub fn load() void { + // In Memory, Tile Map is laid out like: 1 2 + // 3 4 + + const half = Self.tile.len / 2; + + GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], half * @sizeOf(u32)); // Top + GBA.memcpy32(GBA.SPRITE_VRAM + (half * 0x20), &Self.tile[half], half * @sizeOf(u32)); // Bottom + + loadPalette(); + } }; - const Sprite32x32 = struct { + pub const Sprite32x32 = struct { + const Self = @This(); + const tile = [128]u32{ 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, @@ -48,9 +107,40 @@ const square = struct { 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Square, + .size = 2, + }; + } + + pub fn load() void { + // In Memory, Tile Map is laid out like: 1 2 3 4 + // 5 6 7 8 + // 9 10 11 12 + // 13 14 15 16 + + const px_width = 8 * 4; + const offset = 0x40 / 4; + const len = px_width * @sizeOf(u32); + + 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); + + loadPalette(); + } }; - const Sprite64x64 = struct { + pub const Sprite64x64 = struct { + const Self = @This(); + const tile = [512]u32{ 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, @@ -124,6 +214,43 @@ const square = struct { 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111112, 0x11111121, 0x11111211, 0x11112111, 0x11121111, 0x11211111, 0x12111111, 0x21111111, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Square, + .size = 3, + }; + } + + pub fn load() void { + // In Memory, Tile Map is laid out like: 1 2 3 4 5 6 7 8 + // 9 10 11 12 13 14 15 16 + // 17 18 19 20 21 22 23 24 + // 25 26 27 28 29 30 31 32 + // 33 34 35 36 37 38 39 40 + // 41 42 43 44 45 46 47 48 + // 49 50 51 52 53 54 55 56 + // 57 58 59 60 61 62 63 64 + + const px_width = 8 * 8; + const offset = 0x40 / 8; + const len = px_width * @sizeOf(u32); + + 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); + GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 4)), &Self.tile[px_width * 4], len); + GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 5)), &Self.tile[px_width * 5], len); + GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 6)), &Self.tile[px_width * 6], len); + GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 7)), &Self.tile[px_width * 7], len); + + loadPalette(); + } }; }; @@ -133,35 +260,89 @@ const square = struct { }; /// Horizontal Sprites -const horizontal = struct { +pub const horizontal = struct { /// 1D-Mapped Horizontal Sprites const d1 = struct {}; /// 2D-Mapped Horizontal Sprites - const d2 = struct { + pub const d2 = struct { /// 2D-Mapped Horizontal Sprites using 4-bit Colour - const bpp4 = struct { + pub const bpp4 = struct { const palette: [8]u32 = [_]u32{ 0x3DEF0000, 0x00006318, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, }; - const Sprite16x8 = struct { + fn loadPalette() void { + GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &palette, palette.len * @sizeOf(u32)); + } + + pub const Sprite16x8 = struct { + const Self = @This(); + const tile = [16]u32{ 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11221111, 0x11112211, 0x11111122, 0x22111111, 0x11221111, 0x11112211, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Horizontal, + .size = 0, + }; + } + + 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); + + GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], len); + + loadPalette(); + } }; - const Sprite32x8 = struct { + pub const Sprite32x8 = struct { + const Self = @This(); + const tile = [32]u32{ 0x11112222, 0x22221111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x22221111, 0x11112222, 0x11111111, 0x11111111, 0x11112222, 0x22221111, 0x11111111, 0x11112222, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x22221111, 0x11111111, 0x11112222, 0x22221111, 0x11111111, 0x11111111, 0x22221111, 0x11112222, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11112222, 0x22221111, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Horizontal, + .size = 1, + }; + } + + 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); + + GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], len); + + loadPalette(); + } }; - const Sprite32x16 = struct { + pub const Sprite32x16 = struct { + const Self = @This(); + const tile = [64]u32{ 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, @@ -172,9 +353,36 @@ const horizontal = struct { 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Horizontal, + .size = 2, + }; + } + + pub fn load() void { + // 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); + + GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], len); + GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * offset), &Self.tile[px_width], len); + + loadPalette(); + } }; - const Sprite64x32 = struct { + pub const Sprite64x32 = struct { + const Self = @This(); + const tile = [256]u32{ 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, @@ -212,41 +420,132 @@ const horizontal = struct { 0x11111122, 0x11112211, 0x11221111, 0x22111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111122, 0x11112211, 0x11221111, 0x22111111, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Horizontal, + .size = 3, + }; + } + + pub fn load() void { + // In Memory, Tile Map is laid out like: 1 2 3 4 5 6 7 8 + // 9 10 11 12 13 14 15 16 + // 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); + + 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); + + loadPalette(); + } }; }; }; }; /// Vertical Sprites -const vertical = struct { +pub const vertical = struct { /// 1D-Mapped Vertical Sprites const d1 = struct {}; /// 2D-Mapped Vertical Sprites - const d2 = struct { + pub const d2 = struct { /// 2D-Mapped Vertical Sprites using 4-bit Colour - const bpp4 = struct { - const palette: [8]u32 = [_]u32{ + pub const bpp4 = struct { + pub const palette: [8]u32 = [_]u32{ 0x3DEF0000, 0x00006318, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, }; - const Sprite8x16 = struct { + fn loadPalette() void { + GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &palette, palette.len * @sizeOf(u32)); + } + + pub const Sprite8x16 = struct { + const Self = @This(); + const tile = [16]u32{ 0x21111112, 0x21111112, 0x12111121, 0x12111121, 0x11211211, 0x11211211, 0x11112111, 0x11112111, 0x11121111, 0x11121111, 0x11211211, 0x11211211, 0x12111121, 0x12111121, 0x21111112, 0x21111112, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Vertical, + .size = 0, + }; + } + + pub fn load() void { + // In Memory, Tile Map is laid out like: 1 + // 2 + + const sprite_len = 8; + + GBA.memcpy32(GBA.SPRITE_VRAM, &Self.tile[0], sprite_len * @sizeOf(u32)); + GBA.memcpy32(GBA.SPRITE_VRAM + (sprite_len * 0x40), &Self.tile[sprite_len], sprite_len * @sizeOf(u32)); + + loadPalette(); + } }; - const Sprite8x32 = struct { + pub const Sprite8x32 = struct { + const Self = @This(); + const tile = [32]u32{ 0x21111112, 0x21111112, 0x21111112, 0x21111112, 0x12111121, 0x12111121, 0x12111121, 0x12111121, 0x11211211, 0x11211211, 0x11211211, 0x11211211, 0x11112111, 0x11112111, 0x11112111, 0x11112111, 0x11121111, 0x11121111, 0x11121111, 0x11121111, 0x11211211, 0x11211211, 0x11211211, 0x11211211, 0x12111121, 0x12111121, 0x12111121, 0x12111121, 0x21111112, 0x21111112, 0x21111112, 0x21111112, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Vertical, + .size = 1, + }; + } + + pub fn load() void { + // In Memory, Tile Map is laid out like: 1 + // 2 + // 3 + // 4 + + const px_width = 8; + const offset = 0x40; + const len = px_width * @sizeOf(u32); + + 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); + + loadPalette(); + } }; - const Sprite16x32 = struct { + pub const Sprite16x32 = struct { + const Self = @This(); + const tile = [64]u32{ 0x11111112, 0x11111112, 0x11111121, 0x11111121, 0x11111211, 0x11111211, 0x11112111, 0x11112111, 0x21111111, 0x21111111, 0x12111111, 0x12111111, 0x11211111, 0x11211111, 0x11121111, 0x11121111, @@ -257,9 +556,40 @@ const vertical = struct { 0x11112111, 0x11112111, 0x11111211, 0x11111211, 0x11111121, 0x11111121, 0x11111112, 0x11111112, 0x11121111, 0x11121111, 0x11211111, 0x11211111, 0x12111111, 0x12111111, 0x21111111, 0x21111111, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Vertical, + .size = 2, + }; + } + + pub fn load() void { + // In Memory, Tile Map is laid out like: 1 2 + // 3 4 + // 5 6 + // 7 8 + + const px_width = 8 * 2; + const offset = 0x40 / 2; + const len = px_width * @sizeOf(u32); + + 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); + + loadPalette(); + } }; - const Sprite32x64 = struct { + pub const Sprite32x64 = struct { + const Self = @This(); + const tile = [256]u32{ 0x11111112, 0x11111112, 0x11111121, 0x11111121, 0x11111211, 0x11111211, 0x11112111, 0x11112111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, @@ -297,6 +627,43 @@ const vertical = struct { 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11121111, 0x11121111, 0x11211111, 0x11211111, 0x12111111, 0x12111111, 0x21111111, 0x21111111, }; + + pub fn getAttribute() Attribute { + return Attribute{ + .paletteMode = .Color16, + .doubleSizeOrVisible = false, + .palette = 0, + .tileIndex = 0, + .shape = .Vertical, + .size = 3, + }; + } + + pub fn load() void { + // In Memory, Tile Map is laid out like: 1 2 3 4 + // 5 6 7 8 + // 9 10 11 12 + // 13 14 15 16 + // 17 18 19 20 + // 21 22 23 24 + // 25 26 27 28 + // 29 30 31 32 + + const px_width = 8 * 4; + const offset = 0x40 / 4; + const len = px_width * @sizeOf(u32); + + 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); + GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 4)), &Self.tile[px_width * 4], len); + GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 5)), &Self.tile[px_width * 5], len); + GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 6)), &Self.tile[px_width * 6], len); + GBA.memcpy32(GBA.SPRITE_VRAM + (px_width * (offset * 7)), &Self.tile[px_width * 7], len); + + loadPalette(); + } }; }; };