diff options
author | Bad Diode <bd@badd10de.dev> | 2021-04-21 14:43:04 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-04-21 14:43:04 +0200 |
commit | 58d00f8ff54697b6f30cb6e12321ebc5a2377331 (patch) | |
tree | 420e7438474265745d621cf4905ad84af21f7cb1 | |
parent | aa7e7fc98deddaed3b8dbb1942ec954f6b4c016f (diff) | |
download | gba-experiments-58d00f8ff54697b6f30cb6e12321ebc5a2377331.tar.gz gba-experiments-58d00f8ff54697b6f30cb6e12321ebc5a2377331.zip |
Cleanup sprite animation code in gba-buttons
-rw-r--r-- | src/gba-buttons.c | 27 | ||||
-rw-r--r-- | 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] = { | |||
228 | {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}, | 228 | {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}, |
229 | }; | 229 | }; |
230 | 230 | ||
231 | typedef enum {GBA_BTN_STATE_IDLE, GBA_BTN_STATE_PRESSED} GbaBtnState; | ||
232 | typedef struct SpriteAnimation { | 231 | typedef struct SpriteAnimation { |
233 | size_t *tile_offsets; | 232 | size_t *tile_offsets; |
234 | size_t n_frames; | 233 | size_t n_frames; |
235 | } SpriteAnimation; | 234 | } SpriteAnimation; |
236 | 235 | ||
237 | static size_t gba_btn_state_idle[] = {0}; | 236 | typedef enum {BTN_STATE_IDLE, BTN_STATE_PRESSED} BtnState; |
238 | static size_t gba_btn_state_pressed[] = {0, 16, 16, 32, 32, 48, 48, 64, 64, 80, 96}; | ||
239 | 237 | ||
240 | SpriteAnimation anim_idle = { | 238 | static size_t btn_state_idle[] = {0}; |
241 | .tile_offsets = &gba_btn_state_idle, | 239 | static size_t btn_state_pressed[] = {16, 16, 32, 32, 48, 48, 64, 64, 80, 96}; |
242 | .n_frames = sizeof(gba_btn_state_idle) / sizeof(size_t), | ||
243 | }; | ||
244 | |||
245 | SpriteAnimation anim_pressed = { | ||
246 | .tile_offsets = &gba_btn_state_pressed, | ||
247 | .n_frames = sizeof(gba_btn_state_pressed) / sizeof(size_t), | ||
248 | }; | ||
249 | 240 | ||
250 | SpriteAnimation *animation_states[] = { | 241 | static SpriteAnimation *animation_states[] = { |
251 | &anim_idle, | 242 | &(SpriteAnimation){ |
252 | &anim_pressed, | 243 | .tile_offsets = &btn_state_idle, |
244 | .n_frames = sizeof(btn_state_idle) / sizeof(size_t), | ||
245 | }, | ||
246 | &(SpriteAnimation){ | ||
247 | .tile_offsets = &btn_state_pressed, | ||
248 | .n_frames = sizeof(btn_state_pressed) / sizeof(size_t), | ||
249 | }, | ||
253 | }; | 250 | }; |
@@ -460,7 +460,7 @@ typedef struct ButtonSprite { | |||
460 | int x; | 460 | int x; |
461 | int y; | 461 | int y; |
462 | int frame; | 462 | int frame; |
463 | GbaBtnState state; | 463 | BtnState state; |
464 | } ButtonSprite; | 464 | } ButtonSprite; |
465 | 465 | ||
466 | #define NUM_SPRITES 128 | 466 | #define NUM_SPRITES 128 |
@@ -516,7 +516,7 @@ int main(void) { | |||
516 | .x = buttons_x, | 516 | .x = buttons_x, |
517 | .y = buttons_y, | 517 | .y = buttons_y, |
518 | .frame = 0, | 518 | .frame = 0, |
519 | .state = GBA_BTN_STATE_IDLE, | 519 | .state = BTN_STATE_IDLE, |
520 | }; | 520 | }; |
521 | OBJ_ATTR_0(btn_b.id) = btn_b.y; | 521 | OBJ_ATTR_0(btn_b.id) = btn_b.y; |
522 | OBJ_ATTR_1(btn_b.id) = btn_b.x | (1 << 0xF); | 522 | OBJ_ATTR_1(btn_b.id) = btn_b.x | (1 << 0xF); |
@@ -527,7 +527,7 @@ int main(void) { | |||
527 | .x = buttons_x + 20, | 527 | .x = buttons_x + 20, |
528 | .y = buttons_y - 16, | 528 | .y = buttons_y - 16, |
529 | .frame = 0, | 529 | .frame = 0, |
530 | .state = GBA_BTN_STATE_IDLE, | 530 | .state = BTN_STATE_IDLE, |
531 | }; | 531 | }; |
532 | OBJ_ATTR_0(btn_a.id) = btn_a.y; | 532 | OBJ_ATTR_0(btn_a.id) = btn_a.y; |
533 | OBJ_ATTR_1(btn_a.id) = btn_a.x | (1 << 0xF); | 533 | OBJ_ATTR_1(btn_a.id) = btn_a.x | (1 << 0xF); |
@@ -551,7 +551,7 @@ int main(void) { | |||
551 | } | 551 | } |
552 | if (key_pressed(KEY_B)) { | 552 | if (key_pressed(KEY_B)) { |
553 | btn_b.frame = 0; | 553 | btn_b.frame = 0; |
554 | btn_b.state = GBA_BTN_STATE_PRESSED; | 554 | btn_b.state = BTN_STATE_PRESSED; |
555 | } else if (key_hold(KEY_B)) { | 555 | } else if (key_hold(KEY_B)) { |
556 | size_t n_frames = animation_states[btn_b.state]->n_frames; | 556 | size_t n_frames = animation_states[btn_b.state]->n_frames; |
557 | if (btn_b.frame < n_frames - 1) { | 557 | if (btn_b.frame < n_frames - 1) { |
@@ -564,12 +564,12 @@ int main(void) { | |||
564 | btn_b.frame++; | 564 | btn_b.frame++; |
565 | } else { | 565 | } else { |
566 | btn_b.frame = 0; | 566 | btn_b.frame = 0; |
567 | btn_b.state = GBA_BTN_STATE_IDLE; | 567 | btn_b.state = BTN_STATE_IDLE; |
568 | } | 568 | } |
569 | } | 569 | } |
570 | if (key_pressed(KEY_A)) { | 570 | if (key_pressed(KEY_A)) { |
571 | btn_a.frame = 0; | 571 | btn_a.frame = 0; |
572 | btn_a.state = GBA_BTN_STATE_PRESSED; | 572 | btn_a.state = BTN_STATE_PRESSED; |
573 | } else if (key_hold(KEY_A)) { | 573 | } else if (key_hold(KEY_A)) { |
574 | size_t n_frames = animation_states[btn_a.state]->n_frames; | 574 | size_t n_frames = animation_states[btn_a.state]->n_frames; |
575 | if (btn_a.frame < n_frames - 1) { | 575 | if (btn_a.frame < n_frames - 1) { |
@@ -582,7 +582,7 @@ int main(void) { | |||
582 | btn_a.frame++; | 582 | btn_a.frame++; |
583 | } else { | 583 | } else { |
584 | btn_a.frame = 0; | 584 | btn_a.frame = 0; |
585 | btn_a.state = GBA_BTN_STATE_IDLE; | 585 | btn_a.state = BTN_STATE_IDLE; |
586 | } | 586 | } |
587 | } | 587 | } |
588 | if (key_pressed(KEY_L)) { | 588 | if (key_pressed(KEY_L)) { |