feat: implement selection between 4bpp and 8bpp sprites

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-06-22 19:20:01 -03:00
parent 06188d3d2b
commit dd57eb3dc3
1 changed files with 40 additions and 11 deletions

View File

@ -8,38 +8,60 @@ const Input = @import("gba").Input;
const sprites = @import("sprites.zig"); const sprites = @import("sprites.zig");
const sprite_attrs = [12]OAM.Attribute{ const sprite_attrs = [24]OAM.Attribute{
sprites.square.d2.bpp4.Sprite8x8.getAttribute(), sprites.square.d2.bpp4.Sprite8x8.getAttribute(),
sprites.square.d2.bpp4.Sprite16x16.getAttribute(), sprites.square.d2.bpp4.Sprite16x16.getAttribute(),
sprites.square.d2.bpp4.Sprite32x32.getAttribute(), sprites.square.d2.bpp4.Sprite32x32.getAttribute(),
sprites.square.d2.bpp4.Sprite64x64.getAttribute(), sprites.square.d2.bpp4.Sprite64x64.getAttribute(),
sprites.horizontal.d2.bpp4.Sprite16x8.getAttribute(), sprites.horizontal.d2.bpp4.Sprite16x8.getAttribute(),
sprites.horizontal.d2.bpp4.Sprite32x8.getAttribute(), sprites.horizontal.d2.bpp4.Sprite32x8.getAttribute(),
sprites.horizontal.d2.bpp4.Sprite32x16.getAttribute(), sprites.horizontal.d2.bpp4.Sprite32x16.getAttribute(),
sprites.horizontal.d2.bpp4.Sprite64x32.getAttribute(), sprites.horizontal.d2.bpp4.Sprite64x32.getAttribute(),
sprites.vertical.d2.bpp4.Sprite8x16.getAttribute(), sprites.vertical.d2.bpp4.Sprite8x16.getAttribute(),
sprites.vertical.d2.bpp4.Sprite8x32.getAttribute(), sprites.vertical.d2.bpp4.Sprite8x32.getAttribute(),
sprites.vertical.d2.bpp4.Sprite16x32.getAttribute(), sprites.vertical.d2.bpp4.Sprite16x32.getAttribute(),
sprites.vertical.d2.bpp4.Sprite32x64.getAttribute(), sprites.vertical.d2.bpp4.Sprite32x64.getAttribute(),
sprites.square.d2.bpp8.Sprite8x8.getAttribute(),
sprites.square.d2.bpp8.Sprite16x16.getAttribute(),
sprites.square.d2.bpp8.Sprite32x32.getAttribute(),
sprites.square.d2.bpp8.Sprite64x64.getAttribute(),
sprites.horizontal.d2.bpp8.Sprite16x8.getAttribute(),
sprites.horizontal.d2.bpp8.Sprite32x8.getAttribute(),
sprites.horizontal.d2.bpp8.Sprite32x16.getAttribute(),
sprites.horizontal.d2.bpp8.Sprite64x32.getAttribute(),
sprites.vertical.d2.bpp8.Sprite8x16.getAttribute(),
sprites.vertical.d2.bpp8.Sprite8x32.getAttribute(),
sprites.vertical.d2.bpp8.Sprite16x32.getAttribute(),
sprites.vertical.d2.bpp8.Sprite32x64.getAttribute(),
}; };
const sprite_load_fns = [12]fn () void{ const sprite_load_fns = [24]fn () void{
sprites.square.d2.bpp4.Sprite8x8.load, sprites.square.d2.bpp4.Sprite8x8.load,
sprites.square.d2.bpp4.Sprite16x16.load, sprites.square.d2.bpp4.Sprite16x16.load,
sprites.square.d2.bpp4.Sprite32x32.load, sprites.square.d2.bpp4.Sprite32x32.load,
sprites.square.d2.bpp4.Sprite64x64.load, sprites.square.d2.bpp4.Sprite64x64.load,
sprites.horizontal.d2.bpp4.Sprite16x8.load, sprites.horizontal.d2.bpp4.Sprite16x8.load,
sprites.horizontal.d2.bpp4.Sprite32x8.load, sprites.horizontal.d2.bpp4.Sprite32x8.load,
sprites.horizontal.d2.bpp4.Sprite32x16.load, sprites.horizontal.d2.bpp4.Sprite32x16.load,
sprites.horizontal.d2.bpp4.Sprite64x32.load, sprites.horizontal.d2.bpp4.Sprite64x32.load,
sprites.vertical.d2.bpp4.Sprite8x16.load, sprites.vertical.d2.bpp4.Sprite8x16.load,
sprites.vertical.d2.bpp4.Sprite8x32.load, sprites.vertical.d2.bpp4.Sprite8x32.load,
sprites.vertical.d2.bpp4.Sprite16x32.load, sprites.vertical.d2.bpp4.Sprite16x32.load,
sprites.vertical.d2.bpp4.Sprite32x64.load, sprites.vertical.d2.bpp4.Sprite32x64.load,
sprites.square.d2.bpp8.Sprite8x8.load,
sprites.square.d2.bpp8.Sprite16x16.load,
sprites.square.d2.bpp8.Sprite32x32.load,
sprites.square.d2.bpp8.Sprite64x64.load,
sprites.horizontal.d2.bpp8.Sprite16x8.load,
sprites.horizontal.d2.bpp8.Sprite32x8.load,
sprites.horizontal.d2.bpp8.Sprite32x16.load,
sprites.horizontal.d2.bpp8.Sprite64x32.load,
sprites.vertical.d2.bpp8.Sprite8x16.load,
sprites.vertical.d2.bpp8.Sprite8x32.load,
sprites.vertical.d2.bpp8.Sprite16x32.load,
sprites.vertical.d2.bpp8.Sprite32x64.load,
}; };
// TODO: ZigGBA should support spaces, slashes, underscores, etc. in ROM Title // TODO: ZigGBA should support spaces, slashes, underscores, etc. in ROM Title
@ -73,6 +95,7 @@ pub fn main() noreturn {
var size_idx: u2 = 0; var size_idx: u2 = 0;
var shape_idx: u2 = 0; var shape_idx: u2 = 0;
var is_4bpp: bool = true;
while (true) { while (true) {
LCD.naiveVSync(); LCD.naiveVSync();
@ -80,14 +103,18 @@ pub fn main() noreturn {
const shoulder = @truncate(u2, @bitCast(u32, Input.getShoulderJustPressed())); const shoulder = @truncate(u2, @bitCast(u32, Input.getShoulderJustPressed()));
if (Input.isKeyJustPressed(Input.Keys.Start)) is_4bpp = !is_4bpp;
if (Input.isKeyJustPressed(Input.Keys.A)) shape_idx +%= 1; if (Input.isKeyJustPressed(Input.Keys.A)) shape_idx +%= 1;
if (Input.isKeyJustPressed(Input.Keys.B)) shape_idx -%= 1; if (Input.isKeyJustPressed(Input.Keys.B)) shape_idx -%= 1;
if (shoulder != 0 or Input.isKeyJustPressed(Input.Keys.A) or Input.isKeyJustPressed(Input.Keys.B)) { const input_cond =
Input.isKeyJustPressed(Input.Keys.A) or Input.isKeyJustPressed(Input.Keys.B) or Input.isKeyJustPressed(Input.Keys.Start);
if (shoulder != 0 or input_cond) {
size_idx +%= shoulder; size_idx +%= shoulder;
shape_idx %= 3; shape_idx %= 3;
loadSprite(shape_idx, size_idx, &sprite); loadSprite(is_4bpp, shape_idx, size_idx, &sprite);
} }
x += Input.getHorizontal() * 2; x += Input.getHorizontal() * 2;
@ -98,9 +125,11 @@ pub fn main() noreturn {
} }
} }
fn loadSprite(shape: usize, size: usize, sprite: *OAM.Attribute) void { fn loadSprite(is_4bpp: bool, shape: usize, size: usize, sprite: *OAM.Attribute) void {
GBA.memset32(GBA.SPRITE_VRAM, 0, 0x8000); // Clear Sprite VRAM GBA.memset32(GBA.SPRITE_VRAM, 0, 0x8000); // Clear Sprite VRAM
sprite.* = sprite_attrs[shape * 4 + size]; const idx = @as(usize, 12) * @boolToInt(is_4bpp) + 4 * shape + size;
sprite_load_fns[shape * 4 + size]();
sprite.* = sprite_attrs[idx];
sprite_load_fns[idx]();
} }