diff options
author | Bad Diode <bd@badd10de.dev> | 2021-04-26 20:58:00 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-04-26 20:58:00 +0200 |
commit | a0efde919bedfbb427d2e2c5ae34e06489930b0c (patch) | |
tree | fc51fd1fa436a7a9886b5074a3903fc2aeb227e6 /src/main.c | |
parent | 4e51d019fd0c1d21aa8e965fd68dbacec39a2576 (diff) | |
download | gba-experiments-a0efde919bedfbb427d2e2c5ae34e06489930b0c.tar.gz gba-experiments-a0efde919bedfbb427d2e2c5ae34e06489930b0c.zip |
Test text drawing on tiled backgrounds
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 107 |
1 files changed, 53 insertions, 54 deletions
@@ -4,6 +4,7 @@ | |||
4 | #include "gba-buttons.c" | 4 | #include "gba-buttons.c" |
5 | #include "background-tiles.c" | 5 | #include "background-tiles.c" |
6 | #include "sprites.h" | 6 | #include "sprites.h" |
7 | #include "text.h" | ||
7 | 8 | ||
8 | // | 9 | // |
9 | // Main functions. | 10 | // Main functions. |
@@ -11,62 +12,25 @@ | |||
11 | 12 | ||
12 | // TODO: Cleanup OBJ/OAM memory copying and access. | 13 | // TODO: Cleanup OBJ/OAM memory copying and access. |
13 | // | 14 | // |
14 | #define CBB_0 0 | ||
15 | #define SBB_0 28 | ||
16 | |||
17 | #define CROSS_TX 15 | ||
18 | #define CROSS_TY 10 | ||
19 | |||
20 | u16 *bg0_map = SCREENBLOCK_MEM[SBB_0]; | ||
21 | |||
22 | void | ||
23 | init_map() { | ||
24 | // initialize a background | ||
25 | BG_CTRL_0 = BG_CHARBLOCK(CBB_0) | BG_SCREENBLOCK(SBB_0) | BG_SIZE(3); | ||
26 | BG_H_SCROLL_0 = 0; | ||
27 | BG_V_SCROLL_0 = 0; | ||
28 | |||
29 | // (1) create the tiles: basic tile and a cross | ||
30 | const Tile tiles[2] = { | ||
31 | {{0x11111111, 0x01111111, 0x01111111, 0x01111111, | ||
32 | 0x01111111, 0x01111111, 0x01111111, 0x00000001}}, | ||
33 | {{0x00000000, 0x00100100, 0x01100110, 0x00011000, | ||
34 | 0x00011000, 0x01100110, 0x00100100, 0x00000000}}, | ||
35 | }; | ||
36 | TILE_MEM[CBB_0][0] = tiles[0]; | ||
37 | TILE_MEM[CBB_0][1] = tiles[1]; | ||
38 | |||
39 | // (2) create a palette | ||
40 | PAL_BANK_BG[0][1] = rgb15(31, 0, 0); | ||
41 | PAL_BANK_BG[1][1] = rgb15( 0, 31, 0); | ||
42 | PAL_BANK_BG[2][1] = rgb15( 0, 0, 31); | ||
43 | PAL_BANK_BG[3][1] = rgb15(16, 16, 16); | ||
44 | |||
45 | // (3) Create a map: four contingent blocks of | ||
46 | // 0x0000, 0x1000, 0x2000, 0x3000. | ||
47 | u16 *pse = bg0_map; | ||
48 | for(int i = 0; i < 4; i++) { | ||
49 | for(int j = 0; j < 32 * 32; j++) { | ||
50 | *pse++ = SCREENBLOCK_ENTRY_PAL(i) | 0; | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | 15 | ||
55 | int main(void) { | 16 | int main(void) { |
56 | init_map(); | ||
57 | // Configure the display in mode 0 to show OBJs, where tile memory is | 17 | // Configure the display in mode 0 to show OBJs, where tile memory is |
58 | // sequential. | 18 | // sequential. |
59 | DISP_CTRL = DISP_ENABLE_SPRITES | DISP_MODE_0 | DISP_BG_0; | 19 | DISP_CTRL = DISP_ENABLE_SPRITES | DISP_MODE_0 | DISP_BG_0; |
60 | 20 | ||
61 | u32 tx, ty, se_curr, se_prev= CROSS_TY * 32 + CROSS_TX; | ||
62 | |||
63 | bg0_map[se_prev]++; // initial position of cross | ||
64 | |||
65 | // Initialize sprite button overlay. | 21 | // Initialize sprite button overlay. |
66 | init_sprite_pal(0, COLOR_WHITE); | 22 | init_sprite_pal(0, COLOR_WHITE); |
67 | init_sprites(0); | 23 | init_sprites(0); |
68 | init_button_sprites(); | 24 | init_button_sprites(); |
69 | 25 | ||
26 | // Initialize text engine. | ||
27 | txt_init(0, COLOR_RED, 0); | ||
28 | txt_position(0, 6); | ||
29 | txt_printf("Strings: %s\n", "Hello World"); | ||
30 | txt_printf("Signed ints: %d\n", -100000); | ||
31 | txt_printf("Unsigned ints: %u\n", 1234567); | ||
32 | txt_printf("Hex values: 0x%x\n", 1024); | ||
33 | |||
70 | int frame_counter = 0; | 34 | int frame_counter = 0; |
71 | int x = 0; | 35 | int x = 0; |
72 | int y = 0; | 36 | int y = 0; |
@@ -74,6 +38,9 @@ int main(void) { | |||
74 | wait_vsync(); | 38 | wait_vsync(); |
75 | poll_keys(); | 39 | poll_keys(); |
76 | 40 | ||
41 | txt_position(0, 0); | ||
42 | txt_printf("Frame counter: %d\n", frame_counter); | ||
43 | |||
77 | if (key_hold(KEY_DOWN)) { | 44 | if (key_hold(KEY_DOWN)) { |
78 | y += 3; | 45 | y += 3; |
79 | } | 46 | } |
@@ -86,19 +53,51 @@ int main(void) { | |||
86 | if (key_hold(KEY_RIGHT)) { | 53 | if (key_hold(KEY_RIGHT)) { |
87 | x += 3; | 54 | x += 3; |
88 | } | 55 | } |
89 | tx = ((x >> 3) + CROSS_TX) & 0x3F; | ||
90 | ty = ((y >> 3) + CROSS_TY) & 0x3F; | ||
91 | 56 | ||
92 | se_curr = se_index(tx, ty, 64); | 57 | if (key_pressed(KEY_A)) { |
93 | if(se_curr != se_prev) { | 58 | txt_clear_line(); |
94 | bg0_map[se_prev]--; | 59 | txt_printf("Pressed key: A"); |
95 | bg0_map[se_curr]++; | 60 | } |
96 | se_prev= se_curr; | 61 | if (key_pressed(KEY_B)) { |
62 | txt_clear_line(); | ||
63 | txt_printf("Pressed key: B"); | ||
64 | } | ||
65 | if (key_pressed(KEY_L)) { | ||
66 | txt_clear_line(); | ||
67 | txt_printf("Pressed key: L"); | ||
68 | } | ||
69 | if (key_pressed(KEY_R)) { | ||
70 | txt_clear_line(); | ||
71 | txt_printf("Pressed key: R"); | ||
72 | } | ||
73 | if (key_pressed(KEY_START)) { | ||
74 | txt_clear_line(); | ||
75 | txt_printf("Pressed key: Start"); | ||
76 | } | ||
77 | if (key_pressed(KEY_SELECT)) { | ||
78 | txt_clear_line(); | ||
79 | txt_printf("Pressed key: Select"); | ||
80 | } | ||
81 | if (key_pressed(KEY_UP)) { | ||
82 | txt_clear_line(); | ||
83 | txt_printf("Pressed key: Up"); | ||
84 | } | ||
85 | if (key_pressed(KEY_DOWN)) { | ||
86 | txt_clear_line(); | ||
87 | txt_printf("Pressed key: Down"); | ||
88 | } | ||
89 | if (key_pressed(KEY_LEFT)) { | ||
90 | txt_clear_line(); | ||
91 | txt_printf("Pressed key: Left"); | ||
92 | } | ||
93 | if (key_pressed(KEY_RIGHT)) { | ||
94 | txt_clear_line(); | ||
95 | txt_printf("Pressed key: Right"); | ||
97 | } | 96 | } |
98 | 97 | ||
99 | frame_counter++; | 98 | frame_counter++; |
100 | BG_H_SCROLL_0 = x; | 99 | // BG_H_SCROLL_0 = x; |
101 | BG_V_SCROLL_0 = y; | 100 | // BG_V_SCROLL_0 = y; |
102 | 101 | ||
103 | update_button_sprites(); | 102 | update_button_sprites(); |
104 | }; | 103 | }; |