diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 207 |
1 files changed, 187 insertions, 20 deletions
@@ -491,7 +491,7 @@ load_sprite_data(u32 *sprite_data, size_t n_tiles, size_t n_frames) { | |||
491 | int main(void) { | 491 | int main(void) { |
492 | // Configure the display in mode 0 to show OBJs, where tile memory is | 492 | // Configure the display in mode 0 to show OBJs, where tile memory is |
493 | // sequential. | 493 | // sequential. |
494 | DISP_CTRL = DISP_MODE_3 | DISP_ENABLE_SPRITES | DISP_BG_2; | 494 | DISP_CTRL = DISP_MODE_0 | DISP_ENABLE_SPRITES | DISP_BG_2; |
495 | 495 | ||
496 | // Add colors to the sprite color palette. Tiles with color number 0 are | 496 | // Add colors to the sprite color palette. Tiles with color number 0 are |
497 | // treated as transparent. | 497 | // treated as transparent. |
@@ -505,16 +505,16 @@ int main(void) { | |||
505 | OBJ_ATTR_0(i) = (1 << 9); | 505 | OBJ_ATTR_0(i) = (1 << 9); |
506 | } | 506 | } |
507 | 507 | ||
508 | sprite_tile_counter = 512; | 508 | sprite_tile_counter = 0; |
509 | sprite_memory = &TILE_MEM[4][sprite_tile_counter]; | 509 | sprite_memory = &TILE_MEM[4][sprite_tile_counter]; |
510 | 510 | ||
511 | // Initialize the A/B button sprites. | 511 | // Initialize the A/B button sprites. |
512 | int buttons_x = SCREEN_WIDTH - 64 - 10; | 512 | int buttons_x = SCREEN_WIDTH / 2; |
513 | int buttons_y = 120; | 513 | int buttons_y = SCREEN_HEIGHT / 2; |
514 | ButtonSprite btn_b = { | 514 | ButtonSprite btn_b = { |
515 | .id = load_sprite_data(&gba_btn_b_data, 16, 7), | 515 | .id = load_sprite_data(&gba_btn_b_data, 16, 7), |
516 | .x = buttons_x, | 516 | .x = buttons_x + 32, |
517 | .y = buttons_y, | 517 | .y = buttons_y + 32, |
518 | .frame = 0, | 518 | .frame = 0, |
519 | .state = BTN_STATE_IDLE, | 519 | .state = BTN_STATE_IDLE, |
520 | }; | 520 | }; |
@@ -524,8 +524,8 @@ int main(void) { | |||
524 | 524 | ||
525 | ButtonSprite btn_a = { | 525 | ButtonSprite btn_a = { |
526 | .id = load_sprite_data(&gba_btn_a_data, 16, 7), | 526 | .id = load_sprite_data(&gba_btn_a_data, 16, 7), |
527 | .x = buttons_x + 20, | 527 | .x = buttons_x + 32 + 20, |
528 | .y = buttons_y - 16, | 528 | .y = buttons_y + 32 - 16, |
529 | .frame = 0, | 529 | .frame = 0, |
530 | .state = BTN_STATE_IDLE, | 530 | .state = BTN_STATE_IDLE, |
531 | }; | 531 | }; |
@@ -533,22 +533,79 @@ int main(void) { | |||
533 | OBJ_ATTR_1(btn_a.id) = btn_a.x | (1 << 0xF); | 533 | OBJ_ATTR_1(btn_a.id) = btn_a.x | (1 << 0xF); |
534 | OBJ_ATTR_2(btn_a.id) = sprites[btn_a.id].tile_start; | 534 | OBJ_ATTR_2(btn_a.id) = sprites[btn_a.id].tile_start; |
535 | 535 | ||
536 | draw_logo(); | 536 | ButtonSprite btn_down = { |
537 | .id = load_sprite_data(&gba_btn_down_data, 16, 7), | ||
538 | .x = buttons_x - 64 - 16, | ||
539 | .y = buttons_y + 32, | ||
540 | .frame = 0, | ||
541 | .state = BTN_STATE_IDLE, | ||
542 | }; | ||
543 | OBJ_ATTR_0(btn_down.id) = btn_down.y; | ||
544 | OBJ_ATTR_1(btn_down.id) = btn_down.x | (1 << 0xF); | ||
545 | OBJ_ATTR_2(btn_down.id) = sprites[btn_down.id].tile_start; | ||
546 | |||
547 | ButtonSprite btn_up = { | ||
548 | .id = load_sprite_data(&gba_btn_up_data, 16, 7), | ||
549 | .x = buttons_x - 64 - 16, | ||
550 | .y = buttons_y + 32 - 18, | ||
551 | .frame = 0, | ||
552 | .state = BTN_STATE_IDLE, | ||
553 | }; | ||
554 | OBJ_ATTR_0(btn_up.id) = btn_up.y; | ||
555 | OBJ_ATTR_1(btn_up.id) = btn_up.x | (1 << 0xF); | ||
556 | OBJ_ATTR_2(btn_up.id) = sprites[btn_up.id].tile_start; | ||
557 | |||
558 | ButtonSprite btn_left = { | ||
559 | .id = load_sprite_data(&gba_btn_left_data, 16, 7), | ||
560 | .x = buttons_x - 64 - 16 - 12, | ||
561 | .y = buttons_y + 32 - 10, | ||
562 | .frame = 0, | ||
563 | .state = BTN_STATE_IDLE, | ||
564 | }; | ||
565 | OBJ_ATTR_0(btn_left.id) = btn_left.y; | ||
566 | OBJ_ATTR_1(btn_left.id) = btn_left.x | (1 << 0xF); | ||
567 | OBJ_ATTR_2(btn_left.id) = sprites[btn_left.id].tile_start; | ||
568 | |||
569 | ButtonSprite btn_right = { | ||
570 | .id = load_sprite_data(&gba_btn_right_data, 16, 7), | ||
571 | .x = buttons_x - 64 - 16 + 11, | ||
572 | .y = buttons_y + 32 - 10, | ||
573 | .frame = 0, | ||
574 | .state = BTN_STATE_IDLE, | ||
575 | }; | ||
576 | OBJ_ATTR_0(btn_right.id) = btn_right.y; | ||
577 | OBJ_ATTR_1(btn_right.id) = btn_right.x | (1 << 0xF); | ||
578 | OBJ_ATTR_2(btn_right.id) = sprites[btn_right.id].tile_start; | ||
579 | |||
580 | ButtonSprite btn_l = { | ||
581 | .id = load_sprite_data(&gba_btn_l_data, 16, 7), | ||
582 | .x = buttons_x - 64 - 28, | ||
583 | .y = buttons_y - 32 - 20, | ||
584 | .frame = 0, | ||
585 | .state = BTN_STATE_IDLE, | ||
586 | }; | ||
587 | OBJ_ATTR_0(btn_l.id) = btn_l.y; | ||
588 | OBJ_ATTR_1(btn_l.id) = btn_l.x | (1 << 0xF); | ||
589 | OBJ_ATTR_2(btn_l.id) = sprites[btn_l.id].tile_start; | ||
590 | |||
591 | ButtonSprite btn_r = { | ||
592 | .id = load_sprite_data(&gba_btn_r_data, 16, 7), | ||
593 | .x = buttons_x + 32 + 20, | ||
594 | .y = buttons_y - 32 - 20, | ||
595 | .frame = 0, | ||
596 | .state = BTN_STATE_IDLE, | ||
597 | }; | ||
598 | OBJ_ATTR_0(btn_r.id) = btn_r.y; | ||
599 | OBJ_ATTR_1(btn_r.id) = btn_r.x | (1 << 0xF); | ||
600 | OBJ_ATTR_2(btn_r.id) = sprites[btn_r.id].tile_start; | ||
601 | |||
602 | // draw_logo(); | ||
537 | 603 | ||
538 | int frame_counter = 0; | 604 | int frame_counter = 0; |
539 | while(true) { | 605 | while(true) { |
540 | wait_vsync(); | 606 | wait_vsync(); |
541 | poll_keys(); | 607 | poll_keys(); |
542 | 608 | ||
543 | // Toggle frame counter when we press down. | ||
544 | if (key_pressed(KEY_DOWN) || key_hold(KEY_DOWN)) { | ||
545 | } | ||
546 | if (key_pressed(KEY_UP) || key_hold(KEY_UP)) { | ||
547 | } | ||
548 | if (key_pressed(KEY_LEFT) || key_hold(KEY_LEFT)) { | ||
549 | } | ||
550 | if (key_pressed(KEY_RIGHT) || key_hold(KEY_RIGHT)) { | ||
551 | } | ||
552 | if (key_pressed(KEY_B)) { | 609 | if (key_pressed(KEY_B)) { |
553 | btn_b.frame = 0; | 610 | btn_b.frame = 0; |
554 | btn_b.state = BTN_STATE_PRESSED; | 611 | btn_b.state = BTN_STATE_PRESSED; |
@@ -558,8 +615,8 @@ int main(void) { | |||
558 | btn_b.frame++; | 615 | btn_b.frame++; |
559 | } | 616 | } |
560 | } else { | 617 | } else { |
561 | size_t n_frames = animation_states[btn_b.state]->n_frames; | ||
562 | // Finish the animation and reset idle state. | 618 | // Finish the animation and reset idle state. |
619 | size_t n_frames = animation_states[btn_b.state]->n_frames; | ||
563 | if (btn_b.frame > 0 && btn_b.frame < n_frames - 1) { | 620 | if (btn_b.frame > 0 && btn_b.frame < n_frames - 1) { |
564 | btn_b.frame++; | 621 | btn_b.frame++; |
565 | } else { | 622 | } else { |
@@ -576,8 +633,8 @@ int main(void) { | |||
576 | btn_a.frame++; | 633 | btn_a.frame++; |
577 | } | 634 | } |
578 | } else { | 635 | } else { |
579 | size_t n_frames = animation_states[btn_a.state]->n_frames; | ||
580 | // Finish the animation and reset idle state. | 636 | // Finish the animation and reset idle state. |
637 | size_t n_frames = animation_states[btn_a.state]->n_frames; | ||
581 | if (btn_a.frame > 0 && btn_a.frame < n_frames - 1) { | 638 | if (btn_a.frame > 0 && btn_a.frame < n_frames - 1) { |
582 | btn_a.frame++; | 639 | btn_a.frame++; |
583 | } else { | 640 | } else { |
@@ -585,13 +642,123 @@ int main(void) { | |||
585 | btn_a.state = BTN_STATE_IDLE; | 642 | btn_a.state = BTN_STATE_IDLE; |
586 | } | 643 | } |
587 | } | 644 | } |
645 | if (key_pressed(KEY_DOWN)) { | ||
646 | btn_down.frame = 0; | ||
647 | btn_down.state = BTN_STATE_PRESSED; | ||
648 | } else if (key_hold(KEY_DOWN)) { | ||
649 | size_t n_frames = animation_states[btn_down.state]->n_frames; | ||
650 | if (btn_down.frame < n_frames - 1) { | ||
651 | btn_down.frame++; | ||
652 | } | ||
653 | } else { | ||
654 | // Finish the animation and reset idle state. | ||
655 | size_t n_frames = animation_states[btn_down.state]->n_frames; | ||
656 | if (btn_down.frame > 0 && btn_down.frame < n_frames - 1) { | ||
657 | btn_down.frame++; | ||
658 | } else { | ||
659 | btn_down.frame = 0; | ||
660 | btn_down.state = BTN_STATE_IDLE; | ||
661 | } | ||
662 | } | ||
663 | if (key_pressed(KEY_UP)) { | ||
664 | btn_up.frame = 0; | ||
665 | btn_up.state = BTN_STATE_PRESSED; | ||
666 | } else if (key_hold(KEY_UP)) { | ||
667 | size_t n_frames = animation_states[btn_up.state]->n_frames; | ||
668 | if (btn_up.frame < n_frames - 1) { | ||
669 | btn_up.frame++; | ||
670 | } | ||
671 | } else { | ||
672 | // Finish the animation and reset idle state. | ||
673 | size_t n_frames = animation_states[btn_up.state]->n_frames; | ||
674 | if (btn_up.frame > 0 && btn_up.frame < n_frames - 1) { | ||
675 | btn_up.frame++; | ||
676 | } else { | ||
677 | btn_up.frame = 0; | ||
678 | btn_up.state = BTN_STATE_IDLE; | ||
679 | } | ||
680 | } | ||
681 | if (key_pressed(KEY_LEFT)) { | ||
682 | btn_left.frame = 0; | ||
683 | btn_left.state = BTN_STATE_PRESSED; | ||
684 | } else if (key_hold(KEY_LEFT)) { | ||
685 | size_t n_frames = animation_states[btn_left.state]->n_frames; | ||
686 | if (btn_left.frame < n_frames - 1) { | ||
687 | btn_left.frame++; | ||
688 | } | ||
689 | } else { | ||
690 | // Finish the animation and reset idle state. | ||
691 | size_t n_frames = animation_states[btn_left.state]->n_frames; | ||
692 | if (btn_left.frame > 0 && btn_left.frame < n_frames - 1) { | ||
693 | btn_left.frame++; | ||
694 | } else { | ||
695 | btn_left.frame = 0; | ||
696 | btn_left.state = BTN_STATE_IDLE; | ||
697 | } | ||
698 | } | ||
699 | if (key_pressed(KEY_RIGHT)) { | ||
700 | btn_right.frame = 0; | ||
701 | btn_right.state = BTN_STATE_PRESSED; | ||
702 | } else if (key_hold(KEY_RIGHT)) { | ||
703 | size_t n_frames = animation_states[btn_right.state]->n_frames; | ||
704 | if (btn_right.frame < n_frames - 1) { | ||
705 | btn_right.frame++; | ||
706 | } | ||
707 | } else { | ||
708 | // Finish the animation and reset idle state. | ||
709 | size_t n_frames = animation_states[btn_right.state]->n_frames; | ||
710 | if (btn_right.frame > 0 && btn_right.frame < n_frames - 1) { | ||
711 | btn_right.frame++; | ||
712 | } else { | ||
713 | btn_right.frame = 0; | ||
714 | btn_right.state = BTN_STATE_IDLE; | ||
715 | } | ||
716 | } | ||
588 | if (key_pressed(KEY_L)) { | 717 | if (key_pressed(KEY_L)) { |
718 | btn_l.frame = 0; | ||
719 | btn_l.state = BTN_STATE_PRESSED; | ||
720 | } else if (key_hold(KEY_L)) { | ||
721 | size_t n_frames = animation_states[btn_l.state]->n_frames; | ||
722 | if (btn_l.frame < n_frames - 1) { | ||
723 | btn_l.frame++; | ||
724 | } | ||
725 | } else { | ||
726 | // Finish the animation and reset idle state. | ||
727 | size_t n_frames = animation_states[btn_l.state]->n_frames; | ||
728 | if (btn_l.frame > 0 && btn_l.frame < n_frames - 1) { | ||
729 | btn_l.frame++; | ||
730 | } else { | ||
731 | btn_l.frame = 0; | ||
732 | btn_l.state = BTN_STATE_IDLE; | ||
733 | } | ||
589 | } | 734 | } |
590 | if (key_pressed(KEY_R)) { | 735 | if (key_pressed(KEY_R)) { |
736 | btn_r.frame = 0; | ||
737 | btn_r.state = BTN_STATE_PRESSED; | ||
738 | } else if (key_hold(KEY_R)) { | ||
739 | size_t n_frames = animation_states[btn_r.state]->n_frames; | ||
740 | if (btn_r.frame < n_frames - 1) { | ||
741 | btn_r.frame++; | ||
742 | } | ||
743 | } else { | ||
744 | // Finish the animation and reset idle state. | ||
745 | size_t n_frames = animation_states[btn_r.state]->n_frames; | ||
746 | if (btn_r.frame > 0 && btn_r.frame < n_frames - 1) { | ||
747 | btn_r.frame++; | ||
748 | } else { | ||
749 | btn_r.frame = 0; | ||
750 | btn_r.state = BTN_STATE_IDLE; | ||
751 | } | ||
591 | } | 752 | } |
592 | 753 | ||
593 | OBJ_ATTR_2(btn_b.id) = sprites[btn_b.id].tile_start + animation_states[btn_b.state]->tile_offsets[btn_b.frame]; | 754 | OBJ_ATTR_2(btn_b.id) = sprites[btn_b.id].tile_start + animation_states[btn_b.state]->tile_offsets[btn_b.frame]; |
594 | OBJ_ATTR_2(btn_a.id) = sprites[btn_a.id].tile_start + animation_states[btn_a.state]->tile_offsets[btn_a.frame]; | 755 | OBJ_ATTR_2(btn_a.id) = sprites[btn_a.id].tile_start + animation_states[btn_a.state]->tile_offsets[btn_a.frame]; |
756 | OBJ_ATTR_2(btn_up.id) = sprites[btn_up.id].tile_start + animation_states[btn_up.state]->tile_offsets[btn_up.frame]; | ||
757 | OBJ_ATTR_2(btn_down.id) = sprites[btn_down.id].tile_start + animation_states[btn_down.state]->tile_offsets[btn_down.frame]; | ||
758 | OBJ_ATTR_2(btn_left.id) = sprites[btn_left.id].tile_start + animation_states[btn_left.state]->tile_offsets[btn_left.frame]; | ||
759 | OBJ_ATTR_2(btn_right.id) = sprites[btn_right.id].tile_start + animation_states[btn_right.state]->tile_offsets[btn_right.frame]; | ||
760 | OBJ_ATTR_2(btn_l.id) = sprites[btn_l.id].tile_start + animation_states[btn_l.state]->tile_offsets[btn_l.frame]; | ||
761 | OBJ_ATTR_2(btn_r.id) = sprites[btn_r.id].tile_start + animation_states[btn_r.state]->tile_offsets[btn_r.frame]; | ||
595 | frame_counter++; | 762 | frame_counter++; |
596 | }; | 763 | }; |
597 | 764 | ||