diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 72 |
1 files changed, 64 insertions, 8 deletions
@@ -1,6 +1,7 @@ | |||
1 | #include "shorthand.h" | 1 | #include "shorthand.h" |
2 | #include "bd-font.c" | 2 | #include "bd-font.c" |
3 | #include "gba-buttons.c" | 3 | #include "gba-buttons.c" |
4 | #include "background-tiles.c" | ||
4 | 5 | ||
5 | // | 6 | // |
6 | // Memory sections. | 7 | // Memory sections. |
@@ -46,10 +47,34 @@ | |||
46 | #define DISP_ENABLE_SPRITES DISP_OBJ | DISP_OBJ_1D | 47 | #define DISP_ENABLE_SPRITES DISP_OBJ | DISP_OBJ_1D |
47 | 48 | ||
48 | // Registers to control of BG layers. | 49 | // Registers to control of BG layers. |
49 | #define BG_CTRL_0 *((vu16*)(0x04000008 + 0x0000)) | 50 | #define BG_CTRL_0 *((vu16*)(0x04000008 + 0x0002 * 0)) |
50 | #define BG_CTRL_1 *((vu16*)(0x04000008 + 0x0002)) | 51 | #define BG_CTRL_1 *((vu16*)(0x04000008 + 0x0002 * 1)) |
51 | #define BG_CTRL_2 *((vu16*)(0x04000008 + 0x0004)) | 52 | #define BG_CTRL_2 *((vu16*)(0x04000008 + 0x0002 * 2)) |
52 | #define BG_CTRL_3 *((vu16*)(0x04000008 + 0x0006)) | 53 | #define BG_CTRL_3 *((vu16*)(0x04000008 + 0x0002 * 3)) |
54 | |||
55 | // Bits to control the background. | ||
56 | #define BG_PRIORITY_0 0x0 | ||
57 | #define BG_PRIORITY_1 0x1 | ||
58 | #define BG_PRIORITY_2 0x2 | ||
59 | #define BG_PRIORITY_3 0x3 | ||
60 | #define BG_CHARBLOCK(N) ((N) << 2) | ||
61 | #define BG_MOSAIC (1 << 6) | ||
62 | #define BG_HIGH_COLOR (1 << 7) | ||
63 | #define BG_SCREENBLOCK(N) ((N) << 8) | ||
64 | #define BG_AFFINE (1 << 0xD) | ||
65 | #define BG_SIZE(N) ((N) << 0xE) | ||
66 | |||
67 | // BG registers for horizontal displacement. | ||
68 | #define BG_H_SCROLL_0 *((vu16*)(0x04000010 + 0x0004 * 0)) | ||
69 | #define BG_H_SCROLL_1 *((vu16*)(0x04000010 + 0x0004 * 1)) | ||
70 | #define BG_H_SCROLL_2 *((vu16*)(0x04000010 + 0x0004 * 2)) | ||
71 | #define BG_H_SCROLL_3 *((vu16*)(0x04000010 + 0x0004 * 3)) | ||
72 | |||
73 | // BG registers for vertical displacement. | ||
74 | #define BG_V_SCROLL_0 *((vu16*)(0x04000012 + 0x0004 * 0)) | ||
75 | #define BG_V_SCROLL_1 *((vu16*)(0x04000012 + 0x0004 * 1)) | ||
76 | #define BG_V_SCROLL_2 *((vu16*)(0x04000012 + 0x0004 * 2)) | ||
77 | #define BG_V_SCROLL_3 *((vu16*)(0x04000012 + 0x0004 * 3)) | ||
53 | 78 | ||
54 | // Screen settings. | 79 | // Screen settings. |
55 | #define SCREEN_WIDTH 240 | 80 | #define SCREEN_WIDTH 240 |
@@ -69,9 +94,11 @@ typedef struct Tile { | |||
69 | } Tile; | 94 | } Tile; |
70 | 95 | ||
71 | typedef Tile TileBlock[512]; | 96 | typedef Tile TileBlock[512]; |
72 | |||
73 | #define TILE_MEM ((TileBlock*) MEM_VRAM) | 97 | #define TILE_MEM ((TileBlock*) MEM_VRAM) |
74 | 98 | ||
99 | typedef u16 ScreenBlock[1024]; | ||
100 | #define SCREENBLOCK_MEM ((ScreenBlock*)MEM_VRAM) | ||
101 | |||
75 | // We can treat the screen as a HxW matrix. With the following macro we can | 102 | // We can treat the screen as a HxW matrix. With the following macro we can |
76 | // write a pixel to the screen at the (x, y) position using: | 103 | // write a pixel to the screen at the (x, y) position using: |
77 | // | 104 | // |
@@ -524,9 +551,6 @@ load_packed_sprite_data(u32 *sprite_data, size_t n_tiles, size_t n_frames) { | |||
524 | } | 551 | } |
525 | 552 | ||
526 | int main(void) { | 553 | int main(void) { |
527 | // Configure the display in mode 0 to show OBJs, where tile memory is | ||
528 | // sequential. | ||
529 | DISP_CTRL = DISP_MODE_0 | DISP_ENABLE_SPRITES | DISP_BG_2; | ||
530 | 554 | ||
531 | // Add colors to the sprite color palette. Tiles with color number 0 are | 555 | // Add colors to the sprite color palette. Tiles with color number 0 are |
532 | // treated as transparent. | 556 | // treated as transparent. |
@@ -543,6 +567,21 @@ int main(void) { | |||
543 | sprite_tile_counter = 0; | 567 | sprite_tile_counter = 0; |
544 | sprite_memory = &TILE_MEM[4][sprite_tile_counter]; | 568 | sprite_memory = &TILE_MEM[4][sprite_tile_counter]; |
545 | 569 | ||
570 | // Load background palette. | ||
571 | memcpy(&PAL_BUFFER_BG[0], &bg_palette, 512); | ||
572 | memcpy(&TILE_MEM[0][0], bg_data, 3168); | ||
573 | memcpy(&SCREENBLOCK_MEM[30][0], bg_map, 2048); | ||
574 | |||
575 | // Configure BG0 to use 4bpp, 64x32 tile map in charblock 0 and screenblock | ||
576 | // 31. | ||
577 | BG_CTRL_0 = BG_CHARBLOCK(0) | BG_SCREENBLOCK(30) | BG_SIZE(0); | ||
578 | BG_H_SCROLL_0 = 0; | ||
579 | BG_V_SCROLL_0 = 0; | ||
580 | |||
581 | // Configure the display in mode 0 to show OBJs, where tile memory is | ||
582 | // sequential. | ||
583 | DISP_CTRL = DISP_ENABLE_SPRITES | DISP_MODE_0 | DISP_BG_0; | ||
584 | |||
546 | // Initialize the A/B button sprites. | 585 | // Initialize the A/B button sprites. |
547 | int buttons_x = SCREEN_WIDTH / 2; | 586 | int buttons_x = SCREEN_WIDTH / 2; |
548 | int buttons_y = SCREEN_HEIGHT / 2; | 587 | int buttons_y = SCREEN_HEIGHT / 2; |
@@ -635,6 +674,8 @@ int main(void) { | |||
635 | OBJ_ATTR_2(btn_r.id) = sprites[btn_r.id].tile_start; | 674 | OBJ_ATTR_2(btn_r.id) = sprites[btn_r.id].tile_start; |
636 | 675 | ||
637 | int frame_counter = 0; | 676 | int frame_counter = 0; |
677 | int x = 0; | ||
678 | int y = 0; | ||
638 | while(true) { | 679 | while(true) { |
639 | wait_vsync(); | 680 | wait_vsync(); |
640 | poll_keys(); | 681 | poll_keys(); |
@@ -784,6 +825,19 @@ int main(void) { | |||
784 | } | 825 | } |
785 | } | 826 | } |
786 | 827 | ||
828 | if (key_hold(KEY_DOWN)) { | ||
829 | y += 3; | ||
830 | } | ||
831 | if (key_hold(KEY_UP)) { | ||
832 | y -= 3; | ||
833 | } | ||
834 | if (key_hold(KEY_LEFT)) { | ||
835 | x -= 3; | ||
836 | } | ||
837 | if (key_hold(KEY_RIGHT)) { | ||
838 | x += 3; | ||
839 | } | ||
840 | |||
787 | OBJ_ATTR_2(btn_b.id) = sprites[btn_b.id].tile_start + animation_states[btn_b.state]->tile_offsets[btn_b.frame]; | 841 | OBJ_ATTR_2(btn_b.id) = sprites[btn_b.id].tile_start + animation_states[btn_b.state]->tile_offsets[btn_b.frame]; |
788 | OBJ_ATTR_2(btn_a.id) = sprites[btn_a.id].tile_start + animation_states[btn_a.state]->tile_offsets[btn_a.frame]; | 842 | OBJ_ATTR_2(btn_a.id) = sprites[btn_a.id].tile_start + animation_states[btn_a.state]->tile_offsets[btn_a.frame]; |
789 | OBJ_ATTR_2(btn_up.id) = sprites[btn_up.id].tile_start + animation_states[btn_up.state]->tile_offsets[btn_up.frame]; | 843 | OBJ_ATTR_2(btn_up.id) = sprites[btn_up.id].tile_start + animation_states[btn_up.state]->tile_offsets[btn_up.frame]; |
@@ -793,6 +847,8 @@ int main(void) { | |||
793 | OBJ_ATTR_2(btn_l.id) = sprites[btn_l.id].tile_start + animation_states[btn_l.state]->tile_offsets[btn_l.frame]; | 847 | OBJ_ATTR_2(btn_l.id) = sprites[btn_l.id].tile_start + animation_states[btn_l.state]->tile_offsets[btn_l.frame]; |
794 | OBJ_ATTR_2(btn_r.id) = sprites[btn_r.id].tile_start + animation_states[btn_r.state]->tile_offsets[btn_r.frame]; | 848 | OBJ_ATTR_2(btn_r.id) = sprites[btn_r.id].tile_start + animation_states[btn_r.state]->tile_offsets[btn_r.frame]; |
795 | frame_counter++; | 849 | frame_counter++; |
850 | BG_H_SCROLL_0 = x; | ||
851 | BG_V_SCROLL_0 = y; | ||
796 | }; | 852 | }; |
797 | 853 | ||
798 | return 0; | 854 | return 0; |