From bf59ac0d92248aa65b8a1552a30ffe6d7adf7e65 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 13 May 2022 11:52:54 -0300 Subject: [PATCH] chore: use A or B to cycle through sprite shape types --- src/2d_sprites.zig | 98 +++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/src/2d_sprites.zig b/src/2d_sprites.zig index 99fe8b2..5438641 100644 --- a/src/2d_sprites.zig +++ b/src/2d_sprites.zig @@ -9,30 +9,12 @@ const Input = @import("gba").Input; 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(); // TODO: ZigGBA should support spaces, slashes, underscores, etc. in ROM Title export var gameHeader linksection(".gbaheader") = GBA.Header.setup("2DSPRITE", "PAOD", "00", 0); -// To-do List: -// TODO: 8BPP variants? -// TODO: Tall Sprites - -const square_sprites = initSquareSpriteTuple(); -const horiz_sprites = initHorizSpriteTuple(); -const vert_sprites = 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{} }; -} - pub fn main() noreturn { LCD.setupDisplayControl(.{ .mode = .Mode0, // No Affine BGs @@ -42,49 +24,56 @@ pub fn main() noreturn { OAM.init(); - const sprites = square_sprites; - - sprites[0].load(); // Copy Mapping to VRAM and Palette to PALRAM + sprites[0][0].load(); // Copy Mapping to VRAM and Palette to PALRAM var sprite: *OAM.Attribute = OAM.allocate(); - sprite.paletteMode = sprites[0].paletteMode(); + sprite.paletteMode = sprites[0][0].paletteMode(); sprite.palette = 0; sprite.tileIndex = 0; - sprite.setSize(sprites[0].size()); + sprite.setSize(sprites[0][0].size()); var x: i32 = 96; var y: i32 = 32; var i: i32 = 0; + var ii: u2 = 0; while (true) { LCD.naiveVSync(); Input.readInput(); const shoulder = Input.getShoulderJustPressed(); - if (shoulder != 0) { - i = (i + shoulder) & 0b11; - // Index must be comptime known unfortunately - // so we need this ugly switch statement - switch (i) { - 0 => { - sprites[0].load(); - sprite.setSize(sprites[0].size()); + if (Input.isKeyJustPressed(Input.Keys.A)) ii +%= 1; + if (Input.isKeyJustPressed(Input.Keys.B)) ii -%= 1; + + if (shoulder != 0 or Input.isKeyJustPressed(Input.Keys.A | Input.Keys.B)) { + i = (i + shoulder) & 0b11; + ii %= 3; + + switch (ii) { + 0 => switch (i) { + 0 => loadSprite(0, 0, sprite), + 1 => loadSprite(0, 1, sprite), + 2 => loadSprite(0, 2, sprite), + 3 => loadSprite(0, 3, sprite), + else => unreachable, }, - 1 => { - sprites[1].load(); - sprite.setSize(sprites[1].size()); + 1 => switch (i) { + 0 => loadSprite(1, 0, sprite), + 1 => loadSprite(1, 1, sprite), + 2 => loadSprite(1, 2, sprite), + 3 => loadSprite(1, 3, sprite), + else => unreachable, }, - 2 => { - sprites[2].load(); - sprite.setSize(sprites[2].size()); + 2 => switch (i) { + 0 => loadSprite(2, 0, sprite), + 1 => loadSprite(2, 1, sprite), + 2 => loadSprite(2, 2, sprite), + 3 => loadSprite(2, 3, sprite), + else => unreachable, }, - 3 => { - sprites[3].load(); - sprite.setSize(sprites[3].size()); - }, - else => unreachable, + else => {}, } } @@ -723,3 +712,24 @@ const Sprite32x64 = struct { 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 { + sprites[kind][i].load(); + sprite.setSize(sprites[kind][i].size()); +}