From 58d00f8ff54697b6f30cb6e12321ebc5a2377331 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Wed, 21 Apr 2021 14:43:04 +0200 Subject: Cleanup sprite animation code in gba-buttons --- src/gba-buttons.c | 27 ++++++++++++--------------- src/main.c | 14 +++++++------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/gba-buttons.c b/src/gba-buttons.c index ee6458d..de62212 100644 --- a/src/gba-buttons.c +++ b/src/gba-buttons.c @@ -228,26 +228,23 @@ u32 gba_btn_b_data[112][8] = { {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}, }; -typedef enum {GBA_BTN_STATE_IDLE, GBA_BTN_STATE_PRESSED} GbaBtnState; typedef struct SpriteAnimation { size_t *tile_offsets; size_t n_frames; } SpriteAnimation; -static size_t gba_btn_state_idle[] = {0}; -static size_t gba_btn_state_pressed[] = {0, 16, 16, 32, 32, 48, 48, 64, 64, 80, 96}; +typedef enum {BTN_STATE_IDLE, BTN_STATE_PRESSED} BtnState; -SpriteAnimation anim_idle = { - .tile_offsets = &gba_btn_state_idle, - .n_frames = sizeof(gba_btn_state_idle) / sizeof(size_t), -}; - -SpriteAnimation anim_pressed = { - .tile_offsets = &gba_btn_state_pressed, - .n_frames = sizeof(gba_btn_state_pressed) / sizeof(size_t), -}; +static size_t btn_state_idle[] = {0}; +static size_t btn_state_pressed[] = {16, 16, 32, 32, 48, 48, 64, 64, 80, 96}; -SpriteAnimation *animation_states[] = { - &anim_idle, - &anim_pressed, +static SpriteAnimation *animation_states[] = { + &(SpriteAnimation){ + .tile_offsets = &btn_state_idle, + .n_frames = sizeof(btn_state_idle) / sizeof(size_t), + }, + &(SpriteAnimation){ + .tile_offsets = &btn_state_pressed, + .n_frames = sizeof(btn_state_pressed) / sizeof(size_t), + }, }; diff --git a/src/main.c b/src/main.c index 8c1e82d..2015ee0 100644 --- a/src/main.c +++ b/src/main.c @@ -460,7 +460,7 @@ typedef struct ButtonSprite { int x; int y; int frame; - GbaBtnState state; + BtnState state; } ButtonSprite; #define NUM_SPRITES 128 @@ -516,7 +516,7 @@ int main(void) { .x = buttons_x, .y = buttons_y, .frame = 0, - .state = GBA_BTN_STATE_IDLE, + .state = BTN_STATE_IDLE, }; OBJ_ATTR_0(btn_b.id) = btn_b.y; OBJ_ATTR_1(btn_b.id) = btn_b.x | (1 << 0xF); @@ -527,7 +527,7 @@ int main(void) { .x = buttons_x + 20, .y = buttons_y - 16, .frame = 0, - .state = GBA_BTN_STATE_IDLE, + .state = BTN_STATE_IDLE, }; OBJ_ATTR_0(btn_a.id) = btn_a.y; OBJ_ATTR_1(btn_a.id) = btn_a.x | (1 << 0xF); @@ -551,7 +551,7 @@ int main(void) { } if (key_pressed(KEY_B)) { btn_b.frame = 0; - btn_b.state = GBA_BTN_STATE_PRESSED; + btn_b.state = BTN_STATE_PRESSED; } else if (key_hold(KEY_B)) { size_t n_frames = animation_states[btn_b.state]->n_frames; if (btn_b.frame < n_frames - 1) { @@ -564,12 +564,12 @@ int main(void) { btn_b.frame++; } else { btn_b.frame = 0; - btn_b.state = GBA_BTN_STATE_IDLE; + btn_b.state = BTN_STATE_IDLE; } } if (key_pressed(KEY_A)) { btn_a.frame = 0; - btn_a.state = GBA_BTN_STATE_PRESSED; + btn_a.state = BTN_STATE_PRESSED; } else if (key_hold(KEY_A)) { size_t n_frames = animation_states[btn_a.state]->n_frames; if (btn_a.frame < n_frames - 1) { @@ -582,7 +582,7 @@ int main(void) { btn_a.frame++; } else { btn_a.frame = 0; - btn_a.state = GBA_BTN_STATE_IDLE; + btn_a.state = BTN_STATE_IDLE; } } if (key_pressed(KEY_L)) { -- cgit v1.2.1