chore: use A or B to cycle through sprite shape types

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-05-13 11:52:54 -03:00
parent 2895a3afa8
commit bf59ac0d92
1 changed files with 54 additions and 44 deletions

View File

@ -9,30 +9,12 @@ const Input = @import("gba").Input;
const SquareSpriteTuple = std.meta.Tuple(&[_]type{ Sprite8x8, Sprite16x16, Sprite32x32, Sprite64x64 }); const SquareSpriteTuple = std.meta.Tuple(&[_]type{ Sprite8x8, Sprite16x16, Sprite32x32, Sprite64x64 });
const HorizSpriteTuple = std.meta.Tuple(&[_]type{ Sprite16x8, Sprite32x8, Sprite32x16, Sprite64x32 }); const HorizSpriteTuple = std.meta.Tuple(&[_]type{ Sprite16x8, Sprite32x8, Sprite32x16, Sprite64x32 });
const VertSpriteTuple = std.meta.Tuple(&[_]type{ Sprite8x16, Sprite8x32, Sprite16x32, Sprite32x64 }); 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 // TODO: ZigGBA should support spaces, slashes, underscores, etc. in ROM Title
export var gameHeader linksection(".gbaheader") = GBA.Header.setup("2DSPRITE", "PAOD", "00", 0); 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 { pub fn main() noreturn {
LCD.setupDisplayControl(.{ LCD.setupDisplayControl(.{
.mode = .Mode0, // No Affine BGs .mode = .Mode0, // No Affine BGs
@ -42,49 +24,56 @@ pub fn main() noreturn {
OAM.init(); OAM.init();
const sprites = square_sprites; sprites[0][0].load(); // Copy Mapping to VRAM and Palette to PALRAM
sprites[0].load(); // Copy Mapping to VRAM and Palette to PALRAM
var sprite: *OAM.Attribute = OAM.allocate(); var sprite: *OAM.Attribute = OAM.allocate();
sprite.paletteMode = sprites[0].paletteMode(); sprite.paletteMode = sprites[0][0].paletteMode();
sprite.palette = 0; sprite.palette = 0;
sprite.tileIndex = 0; sprite.tileIndex = 0;
sprite.setSize(sprites[0].size()); sprite.setSize(sprites[0][0].size());
var x: i32 = 96; var x: i32 = 96;
var y: i32 = 32; var y: i32 = 32;
var i: i32 = 0; var i: i32 = 0;
var ii: u2 = 0;
while (true) { while (true) {
LCD.naiveVSync(); LCD.naiveVSync();
Input.readInput(); Input.readInput();
const shoulder = Input.getShoulderJustPressed(); const shoulder = Input.getShoulderJustPressed();
if (shoulder != 0) {
i = (i + shoulder) & 0b11;
// Index must be comptime known unfortunately if (Input.isKeyJustPressed(Input.Keys.A)) ii +%= 1;
// so we need this ugly switch statement if (Input.isKeyJustPressed(Input.Keys.B)) ii -%= 1;
switch (i) {
0 => { if (shoulder != 0 or Input.isKeyJustPressed(Input.Keys.A | Input.Keys.B)) {
sprites[0].load(); i = (i + shoulder) & 0b11;
sprite.setSize(sprites[0].size()); ii %= 3;
},
1 => { switch (ii) {
sprites[1].load(); 0 => switch (i) {
sprite.setSize(sprites[1].size()); 0 => loadSprite(0, 0, sprite),
}, 1 => loadSprite(0, 1, sprite),
2 => { 2 => loadSprite(0, 2, sprite),
sprites[2].load(); 3 => loadSprite(0, 3, sprite),
sprite.setSize(sprites[2].size());
},
3 => {
sprites[3].load();
sprite.setSize(sprites[3].size());
},
else => unreachable, else => unreachable,
},
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 => switch (i) {
0 => loadSprite(2, 0, sprite),
1 => loadSprite(2, 1, sprite),
2 => loadSprite(2, 2, sprite),
3 => loadSprite(2, 3, sprite),
else => unreachable,
},
else => {},
} }
} }
@ -723,3 +712,24 @@ const Sprite32x64 = struct {
GBA.memcpy32(GBA.OBJ_PALETTE_RAM, &Self.pal, Self.pal.len * @sizeOf(u32)); 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());
}