diff options
-rw-r--r-- | src/gba-buttons.c | 24 | ||||
-rw-r--r-- | src/main.c | 46 |
2 files changed, 49 insertions, 21 deletions
diff --git a/src/gba-buttons.c b/src/gba-buttons.c index 0f6218a..ee6458d 100644 --- a/src/gba-buttons.c +++ b/src/gba-buttons.c | |||
@@ -227,3 +227,27 @@ u32 gba_btn_b_data[112][8] = { | |||
227 | {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}, | 227 | {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}, |
228 | {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}, | 228 | {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}, |
229 | }; | 229 | }; |
230 | |||
231 | typedef enum {GBA_BTN_STATE_IDLE, GBA_BTN_STATE_PRESSED} GbaBtnState; | ||
232 | typedef struct SpriteAnimation { | ||
233 | size_t *tile_offsets; | ||
234 | size_t n_frames; | ||
235 | } SpriteAnimation; | ||
236 | |||
237 | static size_t gba_btn_state_idle[] = {0}; | ||
238 | static size_t gba_btn_state_pressed[] = {0, 16, 16, 32, 32, 48, 48, 64, 64, 80, 96}; | ||
239 | |||
240 | SpriteAnimation anim_idle = { | ||
241 | .tile_offsets = &gba_btn_state_idle, | ||
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 | |||
250 | SpriteAnimation *animation_states[] = { | ||
251 | &anim_idle, | ||
252 | &anim_pressed, | ||
253 | }; | ||
@@ -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 | int n_frames; | 463 | GbaBtnState 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 | .n_frames = 6, | 519 | .state = GBA_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 | .n_frames = 6, | 530 | .state = GBA_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); |
@@ -550,35 +550,39 @@ int main(void) { | |||
550 | if (key_pressed(KEY_RIGHT) || key_hold(KEY_RIGHT)) { | 550 | if (key_pressed(KEY_RIGHT) || key_hold(KEY_RIGHT)) { |
551 | } | 551 | } |
552 | if (key_pressed(KEY_B)) { | 552 | if (key_pressed(KEY_B)) { |
553 | btn_b.frame = 1; | 553 | btn_b.frame = 0; |
554 | } else if (key_pressed(KEY_B) || key_hold(KEY_B)) { | 554 | btn_b.state = GBA_BTN_STATE_PRESSED; |
555 | // DEBUG: Slowing animation rate. What would be a better solution? | 555 | } else if (key_hold(KEY_B)) { |
556 | if (frame_counter++ % 3 == 0) { | 556 | size_t n_frames = animation_states[btn_b.state]->n_frames; |
557 | if (btn_b.frame < btn_b.n_frames) { | 557 | if (btn_b.frame < n_frames - 1) { |
558 | btn_b.frame++; | 558 | btn_b.frame++; |
559 | } | ||
560 | } | 559 | } |
561 | } else { | 560 | } else { |
562 | if (btn_b.frame > 0 && btn_b.frame < btn_b.n_frames) { | 561 | size_t n_frames = animation_states[btn_b.state]->n_frames; |
562 | // Finish the animation and reset idle state. | ||
563 | if (btn_b.frame > 0 && btn_b.frame < n_frames - 1) { | ||
563 | btn_b.frame++; | 564 | btn_b.frame++; |
564 | } else { | 565 | } else { |
565 | btn_b.frame = 0; | 566 | btn_b.frame = 0; |
567 | btn_b.state = GBA_BTN_STATE_IDLE; | ||
566 | } | 568 | } |
567 | } | 569 | } |
568 | if (key_pressed(KEY_A)) { | 570 | if (key_pressed(KEY_A)) { |
569 | btn_a.frame = 1; | 571 | btn_a.frame = 0; |
570 | } else if (key_pressed(KEY_A) || key_hold(KEY_A)) { | 572 | btn_a.state = GBA_BTN_STATE_PRESSED; |
571 | // DEBUG: Slowing animation rate. What would be a better solution? | 573 | } else if (key_hold(KEY_A)) { |
572 | if (frame_counter % 3 == 0) { | 574 | size_t n_frames = animation_states[btn_a.state]->n_frames; |
573 | if (btn_a.frame < btn_a.n_frames) { | 575 | if (btn_a.frame < n_frames - 1) { |
574 | btn_a.frame++; | 576 | btn_a.frame++; |
575 | } | ||
576 | } | 577 | } |
577 | } else { | 578 | } else { |
578 | if (btn_a.frame > 0 && btn_a.frame < btn_a.n_frames) { | 579 | size_t n_frames = animation_states[btn_a.state]->n_frames; |
580 | // Finish the animation and reset idle state. | ||
581 | if (btn_a.frame > 0 && btn_a.frame < n_frames - 1) { | ||
579 | btn_a.frame++; | 582 | btn_a.frame++; |
580 | } else { | 583 | } else { |
581 | btn_a.frame = 0; | 584 | btn_a.frame = 0; |
585 | btn_a.state = GBA_BTN_STATE_IDLE; | ||
582 | } | 586 | } |
583 | } | 587 | } |
584 | if (key_pressed(KEY_L)) { | 588 | if (key_pressed(KEY_L)) { |
@@ -586,8 +590,8 @@ int main(void) { | |||
586 | if (key_pressed(KEY_R)) { | 590 | if (key_pressed(KEY_R)) { |
587 | } | 591 | } |
588 | 592 | ||
589 | OBJ_ATTR_2(btn_b.id) = sprites[btn_b.id].tile_start + sprites[btn_b.id].n_tiles * btn_b.frame; | 593 | OBJ_ATTR_2(btn_b.id) = sprites[btn_b.id].tile_start + animation_states[btn_b.state]->tile_offsets[btn_b.frame]; |
590 | OBJ_ATTR_2(btn_a.id) = sprites[btn_a.id].tile_start + sprites[btn_a.id].n_tiles * btn_a.frame; | 594 | OBJ_ATTR_2(btn_a.id) = sprites[btn_a.id].tile_start + animation_states[btn_a.state]->tile_offsets[btn_a.frame]; |
591 | frame_counter++; | 595 | frame_counter++; |
592 | }; | 596 | }; |
593 | 597 | ||