summaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c140
1 files changed, 76 insertions, 64 deletions
diff --git a/src/main.c b/src/main.c
index 740dbbf..d78dbe0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,5 +1,6 @@
1#include "shorthand.h" 1#include "shorthand.h"
2#include "bd-font.c" 2#include "bd-font.c"
3#include "gba-buttons.c"
3 4
4// 5//
5// Memory sections. 6// Memory sections.
@@ -249,7 +250,7 @@ draw_fill_rect(int x0, int y0, int x1, int y1, Color clr) {
249} 250}
250 251
251static inline void 252static inline void
252wait_vsync() { 253wait_vsync(void) {
253 while(DISP_VCOUNT >= 160); 254 while(DISP_VCOUNT >= 160);
254 while(DISP_VCOUNT < 160); 255 while(DISP_VCOUNT < 160);
255} 256}
@@ -290,7 +291,7 @@ draw_fill_rect_m4(int x0, int y0, int x1, int y1, u8 col_index, vu16 *buffer) {
290} 291}
291 292
292static inline void 293static inline void
293flip_page() { 294flip_page(void) {
294 DISP_CTRL ^= DISP_PAGE; 295 DISP_CTRL ^= DISP_PAGE;
295} 296}
296 297
@@ -323,7 +324,7 @@ flip_page() {
323// functions is called. Don't use if the code we are trying to profile make use 324// functions is called. Don't use if the code we are trying to profile make use
324// of these timers. 325// of these timers.
325static inline 326static inline
326void profile_start() { 327void profile_start(void) {
327 TIMER_DATA_2 = 0; 328 TIMER_DATA_2 = 0;
328 TIMER_DATA_3 = 0; 329 TIMER_DATA_3 = 0;
329 TIMER_CTRL_2 = 0; 330 TIMER_CTRL_2 = 0;
@@ -333,7 +334,7 @@ void profile_start() {
333} 334}
334 335
335static inline 336static inline
336u32 profile_stop() { 337u32 profile_stop(void) {
337 TIMER_CTRL_2 = 0; 338 TIMER_CTRL_2 = 0;
338 return (TIMER_DATA_3 << 16) | TIMER_DATA_2; 339 return (TIMER_DATA_3 << 16) | TIMER_DATA_2;
339} 340}
@@ -364,7 +365,7 @@ static u16 key_curr = 0;
364static u16 key_prev = 0; 365static u16 key_prev = 0;
365 366
366static inline void 367static inline void
367poll_keys() { 368poll_keys(void) {
368 key_prev = key_curr; 369 key_prev = key_curr;
369 key_curr = ~KEY_INPUTS & KEY_MASK; 370 key_curr = ~KEY_INPUTS & KEY_MASK;
370} 371}
@@ -388,7 +389,7 @@ key_hold(u32 key) {
388#define KEY_PRESSED(key) (~(KEY_INPUTS) & key) 389#define KEY_PRESSED(key) (~(KEY_INPUTS) & key)
389 390
390void 391void
391draw_logo() { 392draw_logo(void) {
392 int side = 60; 393 int side = 60;
393 int line = 35; 394 int line = 35;
394 int height = side * 0.5; 395 int height = side * 0.5;
@@ -472,20 +473,14 @@ typedef struct FontSprite {
472 u32 pal_bank; 473 u32 pal_bank;
473} FontSprite; 474} FontSprite;
474 475
475 476typedef struct ButtonSprite {
476static u32 test_tiles[][8] = { 477 int x;
477 {0x33322111, 0x33322111, 0x33322111, 0x33322111, 0x33322111, 0x33322111, 0x33322111, 0x33322111}, 478 int y;
478 {0x00044000, 0x00044000, 0x00044000, 0x44444444, 0x44444444, 0x00044000, 0x00044000, 0x00044000}, 479 int frame;
479 {0x44400444, 0x44400444, 0x44400444, 0x00000000, 0x00000000, 0x44400444, 0x44400444, 0x44400444}, 480 int n_frames;
480 {0x11122333, 0x11122333, 0x11122333, 0x11122333, 0x11122333, 0x11122333, 0x11122333, 0x11122333}, 481 u32 tile_index;
481}; 482 u32 pal_bank;
482 483} ButtonSprite;
483static u16 test_palette[16] = {
484 0x0000, 0x001f, 0x03e0, 0x7c00,
485 0x7fff, 0x0000, 0x0000, 0x0000,
486 0x0000, 0x0000, 0x0000, 0x0000,
487 0x0000, 0x0000, 0x0000, 0x0000
488};
489 484
490int main(void) { 485int main(void) {
491 // Configure the display in mode 0 to show OBJs, where tile memory is 486 // Configure the display in mode 0 to show OBJs, where tile memory is
@@ -495,8 +490,7 @@ int main(void) {
495 // Add colors to the sprite color palette. Tiles with color number 0 are 490 // Add colors to the sprite color palette. Tiles with color number 0 are
496 // treated as transparent. 491 // treated as transparent.
497 for (size_t i = 0; i < 16; ++i) { 492 for (size_t i = 0; i < 16; ++i) {
498 PAL_BUFFER_SPRITES[i] = test_palette[i]; 493 PAL_BUFFER_SPRITES[i] = COLOR_WHITE;
499 PAL_BUFFER_SPRITES[i + 16] = COLOR_WHITE;
500 } 494 }
501 495
502 // Initialize all attributes by disabling rendering. If we don't do this, 496 // Initialize all attributes by disabling rendering. If we don't do this,
@@ -508,45 +502,38 @@ int main(void) {
508 size_t initial_tile = 512; 502 size_t initial_tile = 512;
509 Tile *tile_mem = &TILE_MEM[4][initial_tile]; 503 Tile *tile_mem = &TILE_MEM[4][initial_tile];
510 504
511 // Load bd-font as tiles. The full extended ASCII range (256 characters)
512 // will be available as an offset of the initial tile. For example, the
513 // uppercase A letter will be at tile index of 512 + 65.
514 copy_font_to_tile_memory(tile_mem);
515
516 // Test copying the exported tiles. 505 // Test copying the exported tiles.
517 for (size_t i = 0; i < 4; ++i) { 506 for (size_t i = 0; i < 224; ++i) {
518 for (size_t j = 0; j < 8; ++j) { 507 for (size_t j = 0; j < 8; ++j) {
519 (tile_mem + i)->data[j] = test_tiles[i][j]; 508 (tile_mem + i)->data[j] = gba_buttons_tiles[i][j];
520 } 509 }
521 } 510 }
522 511
523 // Initialize character sprites for all font chars. 512 // Initialize the A/B button sprites.
524 FontSprite font_sprites[256] = {0}; 513 int buttons_x = SCREEN_WIDTH - 64 - 10;
525 size_t x = 49; 514 int buttons_y = 120;
526 size_t y = 8; 515 ButtonSprite btn_b = {
527 for (size_t i = 0; i < 256; ++i) { 516 .x = buttons_x,
528 if ((i % 16) == 0 && i != 0) { 517 .y = buttons_y,
529 y += 16; 518 .frame = 0,
530 x = 49; 519 .n_frames = 6,
531 } 520 .tile_index = initial_tile,
532 font_sprites[i].x = x; 521 .pal_bank = 0,
533 font_sprites[i].y = y; 522 };
534 font_sprites[i].tile_index = initial_tile + i; 523 OBJ_ATTR_0(0) = btn_b.y;
535 font_sprites[i].animation_state = i % 56; 524 OBJ_ATTR_1(0) = btn_b.x | (1 << 0xF);
536 x += 8; 525 OBJ_ATTR_2(0) = btn_b.tile_index;
537 } 526 ButtonSprite btn_a = {
538 527 .x = buttons_x + 20,
539 // Number of characters to become sprites. 528 .y = buttons_y - 16,
540 size_t n_chars = 128; 529 .frame = 0,
541 for (size_t i = 0; i < n_chars; ++i) { 530 .n_frames = 6,
542 OBJ_ATTR_0(i) = font_sprites[i].y; 531 .tile_index = initial_tile + 16 * 7,
543 OBJ_ATTR_1(i) = font_sprites[i].x; 532 .pal_bank = 0,
544 if (i < 4) { 533 };
545 OBJ_ATTR_2(i) = font_sprites[i].tile_index; 534 OBJ_ATTR_0(1) = btn_a.y;
546 } else { 535 OBJ_ATTR_1(1) = btn_a.x | (1 << 0xF);
547 OBJ_ATTR_2(i) = font_sprites[i].tile_index | (1 << 0xC); 536 OBJ_ATTR_2(1) = btn_a.tile_index;
548 }
549 }
550 537
551 // draw_logo(); 538 // draw_logo();
552 539
@@ -565,20 +552,45 @@ int main(void) {
565 if (key_pressed(KEY_RIGHT) || key_hold(KEY_RIGHT)) { 552 if (key_pressed(KEY_RIGHT) || key_hold(KEY_RIGHT)) {
566 } 553 }
567 if (key_pressed(KEY_B)) { 554 if (key_pressed(KEY_B)) {
555 btn_b.frame = 1;
556 } else if (key_pressed(KEY_B) || key_hold(KEY_B)) {
557 // DEBUG: Slowing animation rate. What would be a better solution?
558 if (frame_counter++ % 3 == 0) {
559 if (btn_b.frame < btn_b.n_frames) {
560 btn_b.frame++;
561 }
562 }
563 } else {
564 if (btn_b.frame > 0 && btn_b.frame < btn_b.n_frames) {
565 btn_b.frame++;
566 } else {
567 btn_b.frame = 0;
568 }
569 }
570 if (key_pressed(KEY_A)) {
571 btn_a.frame = 1;
572 } else if (key_pressed(KEY_A) || key_hold(KEY_A)) {
573 // DEBUG: Slowing animation rate. What would be a better solution?
574 if (frame_counter++ % 3 == 0) {
575 if (btn_a.frame < btn_a.n_frames) {
576 btn_a.frame++;
577 }
578 }
579 } else {
580 if (btn_a.frame > 0 && btn_a.frame < btn_a.n_frames) {
581 btn_a.frame++;
582 } else {
583 btn_a.frame = 0;
584 }
568 } 585 }
569 if (key_pressed(KEY_L)) { 586 if (key_pressed(KEY_L)) {
570 } 587 }
571 if (key_pressed(KEY_R)) { 588 if (key_pressed(KEY_R)) {
572 } 589 }
573 590
574 for (size_t i = 0; i < 128; ++i) { 591 OBJ_ATTR_2(0) = btn_b.tile_index + 16 * btn_b.frame;
575 int y = font_sprites[i].y; 592 OBJ_ATTR_2(1) = btn_a.tile_index + 16 * btn_a.frame;
576 y -= bouncing_animation[font_sprites[i].animation_state++]; 593
577 if (font_sprites[i].animation_state > 56) {
578 font_sprites[i].animation_state = 0;
579 }
580 OBJ_ATTR_0(i) = (OBJ_ATTR_0(i) & ~0xFF) | (y & 0xFF);
581 }
582 }; 594 };
583 595
584 return 0; 596 return 0;