summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-04-26 20:58:00 +0200
committerBad Diode <bd@badd10de.dev>2021-04-26 20:58:00 +0200
commita0efde919bedfbb427d2e2c5ae34e06489930b0c (patch)
treefc51fd1fa436a7a9886b5074a3903fc2aeb227e6
parent4e51d019fd0c1d21aa8e965fd68dbacec39a2576 (diff)
downloadgba-experiments-a0efde919bedfbb427d2e2c5ae34e06489930b0c.tar.gz
gba-experiments-a0efde919bedfbb427d2e2c5ae34e06489930b0c.zip
Test text drawing on tiled backgrounds
-rw-r--r--src/bd-font.c386
-rw-r--r--src/common.h67
-rw-r--r--src/main.c107
-rw-r--r--src/sprites.h22
-rw-r--r--src/text.h142
5 files changed, 370 insertions, 354 deletions
diff --git a/src/bd-font.c b/src/bd-font.c
index 6173e97..e3fb6cc 100644
--- a/src/bd-font.c
+++ b/src/bd-font.c
@@ -1,258 +1,130 @@
1static const u8 font[][8] = { 1u32 bd_font[] = {
2 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 2 0x00000000, 0x00000000, 0x00002400, 0x423c0000,
3 {0x00, 0x00, 0x24, 0x00, 0x42, 0x3c, 0x00, 0x00}, 3 0x00002400, 0x3c420000, 0x0000363e, 0x3e1c0800,
4 {0x00, 0x00, 0x24, 0x00, 0x3c, 0x42, 0x00, 0x00}, 4 0x00081c3e, 0x3e1c0800, 0x001c1c3e, 0x363e081c,
5 {0x00, 0x00, 0x6c, 0x7c, 0x7c, 0x38, 0x10, 0x00}, 5 0x00081c3e, 0x3e3e081c, 0x00000018, 0x18000000,
6 {0x00, 0x10, 0x38, 0x7c, 0x7c, 0x38, 0x10, 0x00}, 6 0x7e7e7e66, 0x667e7e7e, 0x00001824, 0x24180000,
7 {0x00, 0x38, 0x38, 0x7c, 0x6c, 0x7c, 0x10, 0x38}, 7 0x7e7e665a, 0x5a667e7e, 0x00081c3e, 0x081c221c,
8 {0x00, 0x10, 0x38, 0x7c, 0x7c, 0x7c, 0x10, 0x38}, 8 0x001c221c, 0x08083e08, 0x00183828, 0x08080c0c,
9 {0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00}, 9 0x003c2424, 0x24343606, 0x00082208, 0x1c082208,
10 {0x7e, 0x7e, 0x7e, 0x66, 0x66, 0x7e, 0x7e, 0x7e}, 10 0x040c1c3c, 0x1c0c0400, 0x2030383c, 0x38302000,
11 {0x00, 0x00, 0x18, 0x24, 0x24, 0x18, 0x00, 0x00}, 11 0x081c3e08, 0x083e1c08, 0x00141414, 0x14140014,
12 {0x7e, 0x7e, 0x66, 0x5a, 0x5a, 0x66, 0x7e, 0x7e}, 12 0x003c2a2a, 0x2c282828, 0x0038043c, 0x423c201e,
13 {0x00, 0x10, 0x38, 0x7c, 0x10, 0x38, 0x44, 0x38}, 13 0x00000000, 0x7e000000, 0x081c3e08, 0x3e1c083e,
14 {0x00, 0x38, 0x44, 0x38, 0x10, 0x10, 0x7c, 0x10}, 14 0x081c3e08, 0x08080808, 0x08080808, 0x083e1c08,
15 {0x00, 0x18, 0x1c, 0x14, 0x10, 0x10, 0x30, 0x30}, 15 0x00001030, 0x7e301000, 0x0000080c, 0x7e0c0800,
16 {0x00, 0x3c, 0x24, 0x24, 0x24, 0x2c, 0x6c, 0x60}, 16 0x00000000, 0x0002023e, 0x00002466, 0xff662400,
17 {0x00, 0x10, 0x44, 0x10, 0x38, 0x10, 0x44, 0x10}, 17 0x0008081c, 0x1c3e3e00, 0x003e3e1c, 0x1c080800,
18 {0x20, 0x30, 0x38, 0x3c, 0x38, 0x30, 0x20, 0x00}, 18 0x00000000, 0x00000000, 0x00080808, 0x08080008,
19 {0x04, 0x0c, 0x1c, 0x3c, 0x1c, 0x0c, 0x04, 0x00}, 19 0x14141400, 0x00000000, 0x0000143e, 0x143e1400,
20 {0x10, 0x38, 0x7c, 0x10, 0x10, 0x7c, 0x38, 0x10}, 20 0x00081c02, 0x1c201e08, 0x00002616, 0x08343200,
21 {0x00, 0x28, 0x28, 0x28, 0x28, 0x28, 0x00, 0x28}, 21 0x00081408, 0x34122c00, 0x08080800, 0x00000000,
22 {0x00, 0x3c, 0x54, 0x54, 0x34, 0x14, 0x14, 0x14}, 22 0x08040404, 0x04040408, 0x08101010, 0x10101008,
23 {0x00, 0x1c, 0x20, 0x3c, 0x42, 0x3c, 0x04, 0x78}, 23 0x00001408, 0x3e081400, 0x00000808, 0x3e080800,
24 {0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00}, 24 0x00000000, 0x000c0804, 0x00000000, 0x3e000000,
25 {0x10, 0x38, 0x7c, 0x10, 0x7c, 0x38, 0x10, 0x7c}, 25 0x00000000, 0x000c0c00, 0x00203018, 0x0c060200,
26 {0x10, 0x38, 0x7c, 0x10, 0x10, 0x10, 0x10, 0x10}, 26 0x00001c22, 0x2a221c00, 0x00000c0a, 0x08083e00,
27 {0x10, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x38, 0x10}, 27 0x00001c20, 0x1c023e00, 0x00001c22, 0x18221c00,
28 {0x00, 0x00, 0x08, 0x0c, 0x7e, 0x0c, 0x08, 0x00}, 28 0x00001018, 0x143e1000, 0x00003e02, 0x1e201e00,
29 {0x00, 0x00, 0x10, 0x30, 0x7e, 0x30, 0x10, 0x00}, 29 0x00001c02, 0x1e221c00, 0x00003e20, 0x10080400,
30 {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x7c}, 30 0x00001c22, 0x1c221c00, 0x00001c22, 0x3c201c00,
31 {0x00, 0x00, 0x24, 0x66, 0xff, 0x66, 0x24, 0x00}, 31 0x00000c0c, 0x000c0c00, 0x00000c0c, 0x000c0804,
32 {0x00, 0x10, 0x10, 0x38, 0x38, 0x7c, 0x7c, 0x00}, 32 0x00100804, 0x04081000, 0x0000003e, 0x003e0000,
33 {0x00, 0x7c, 0x7c, 0x38, 0x38, 0x10, 0x10, 0x00}, 33 0x00040810, 0x10080400, 0x001c2220, 0x10080008,
34 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 34 0x001c322a, 0x1a021c00, 0x001c2222, 0x3e222200,
35 {0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10}, 35 0x001e221e, 0x22221e00, 0x001c2202, 0x02221c00,
36 {0x28, 0x28, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00}, 36 0x000e1222, 0x22120e00, 0x003e021e, 0x02023e00,
37 {0x00, 0x00, 0x28, 0x7c, 0x28, 0x7c, 0x28, 0x00}, 37 0x003e021e, 0x02020200, 0x001c2202, 0x32221c00,
38 {0x00, 0x10, 0x38, 0x40, 0x38, 0x04, 0x78, 0x10}, 38 0x00222222, 0x3e222200, 0x003e0808, 0x08083e00,
39 {0x00, 0x00, 0x64, 0x68, 0x10, 0x2c, 0x4c, 0x00}, 39 0x00202020, 0x22221c00, 0x0022120a, 0x0e122200,
40 {0x00, 0x10, 0x28, 0x10, 0x2c, 0x48, 0x34, 0x00}, 40 0x00020202, 0x02023e00, 0x0022362a, 0x22222200,
41 {0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}, 41 0x0022262a, 0x32222200, 0x001c2222, 0x22221c00,
42 {0x10, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x10}, 42 0x001e2222, 0x1e020200, 0x001c2222, 0x2a122c00,
43 {0x10, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x10}, 43 0x001e2222, 0x1e122200, 0x001c021c, 0x20221c00,
44 {0x00, 0x00, 0x28, 0x10, 0x7c, 0x10, 0x28, 0x00}, 44 0x003e0808, 0x08080800, 0x00222222, 0x22221c00,
45 {0x00, 0x00, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x00}, 45 0x00222222, 0x22140800, 0x0022222a, 0x2a2a1400,
46 {0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x10, 0x20}, 46 0x00221408, 0x08142200, 0x00222214, 0x08080800,
47 {0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00}, 47 0x003e1008, 0x04023e00, 0x1c040404, 0x0404041c,
48 {0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00}, 48 0x0002060c, 0x18302000, 0x1c101010, 0x1010101c,
49 {0x00, 0x04, 0x0c, 0x18, 0x30, 0x60, 0x40, 0x00}, 49 0x08142200, 0x00000000, 0x00000000, 0x00003e00,
50 {0x00, 0x00, 0x38, 0x44, 0x54, 0x44, 0x38, 0x00}, 50 0x00040810, 0x00000000, 0x00001c20, 0x3c223c00,
51 {0x00, 0x00, 0x30, 0x50, 0x10, 0x10, 0x7c, 0x00}, 51 0x0002021e, 0x22221e00, 0x00001c22, 0x02221c00,
52 {0x00, 0x00, 0x38, 0x04, 0x38, 0x40, 0x7c, 0x00}, 52 0x0020203c, 0x22223c00, 0x00001c22, 0x1e023c00,
53 {0x00, 0x00, 0x38, 0x44, 0x18, 0x44, 0x38, 0x00}, 53 0x00003c02, 0x021e0202, 0x00003c22, 0x223c201c,
54 {0x00, 0x00, 0x08, 0x18, 0x28, 0x7c, 0x08, 0x00}, 54 0x0002021e, 0x22222200, 0x0008000c, 0x08083e00,
55 {0x00, 0x00, 0x7c, 0x40, 0x78, 0x04, 0x78, 0x00}, 55 0x00200020, 0x2020221c, 0x0002120a, 0x060a3200,
56 {0x00, 0x00, 0x38, 0x40, 0x78, 0x44, 0x38, 0x00}, 56 0x000c0808, 0x08083e00, 0x0000162a, 0x2a2a2a00,
57 {0x00, 0x00, 0x7c, 0x04, 0x08, 0x10, 0x20, 0x00}, 57 0x00000e32, 0x22222200, 0x00001c22, 0x22221c00,
58 {0x00, 0x00, 0x38, 0x44, 0x38, 0x44, 0x38, 0x00}, 58 0x00001e22, 0x221e0202, 0x00003c22, 0x223c2070,
59 {0x00, 0x00, 0x38, 0x44, 0x3c, 0x04, 0x38, 0x00}, 59 0x00001a26, 0x02020200, 0x00001c02, 0x1c201e00,
60 {0x00, 0x00, 0x30, 0x30, 0x00, 0x30, 0x30, 0x00}, 60 0x00043e04, 0x04041800, 0x00002222, 0x22221c00,
61 {0x00, 0x00, 0x30, 0x30, 0x00, 0x30, 0x10, 0x20}, 61 0x00002222, 0x22140800, 0x00002222, 0x2a2a1400,
62 {0x00, 0x08, 0x10, 0x20, 0x20, 0x10, 0x08, 0x00}, 62 0x00002214, 0x08142200, 0x00002222, 0x223c201c,
63 {0x00, 0x00, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0x00}, 63 0x00003e10, 0x08043e00, 0x18040402, 0x02040418,
64 {0x00, 0x20, 0x10, 0x08, 0x08, 0x10, 0x20, 0x00}, 64 0x00080808, 0x08080808, 0x0c101020, 0x2010100c,
65 {0x00, 0x38, 0x44, 0x04, 0x08, 0x10, 0x00, 0x10}, 65 0x00002c1a, 0x00000000, 0x00000814, 0x22223e00,
66 {0x00, 0x38, 0x4c, 0x54, 0x58, 0x40, 0x38, 0x00}, 66 0x001c2202, 0x221c080e, 0x00140022, 0x2222221c,
67 {0x00, 0x38, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x00}, 67 0x1008001c, 0x221e023c, 0x0814001c, 0x203c223c,
68 {0x00, 0x78, 0x44, 0x78, 0x44, 0x44, 0x78, 0x00}, 68 0x0014001c, 0x203c223c, 0x0408001c, 0x203c223c,
69 {0x00, 0x38, 0x44, 0x40, 0x40, 0x44, 0x38, 0x00}, 69 0x0814081c, 0x203c223c, 0x00001c02, 0x221c080e,
70 {0x00, 0x70, 0x48, 0x44, 0x44, 0x48, 0x70, 0x00}, 70 0x0814001c, 0x221e023c, 0x0014001c, 0x221e023c,
71 {0x00, 0x7c, 0x40, 0x78, 0x40, 0x40, 0x7c, 0x00}, 71 0x0408001c, 0x221e023c, 0x0014000c, 0x0808083e,
72 {0x00, 0x7c, 0x40, 0x78, 0x40, 0x40, 0x40, 0x00}, 72 0x0014000c, 0x0808083e, 0x0408000c, 0x0808083e,
73 {0x00, 0x38, 0x44, 0x40, 0x4c, 0x44, 0x38, 0x00}, 73 0x0014001c, 0x223e2222, 0x0814081c, 0x223e2222,
74 {0x00, 0x44, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x00}, 74 0x1008003e, 0x021e023e, 0x00001628, 0x1c0a3400,
75 {0x00, 0x7c, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x00}, 75 0x003c0a1a, 0x0e0a3a00, 0x0814001c, 0x2222221c,
76 {0x00, 0x04, 0x04, 0x04, 0x44, 0x44, 0x38, 0x00}, 76 0x0014001c, 0x2222221c, 0x0408001c, 0x2222221c,
77 {0x00, 0x44, 0x48, 0x50, 0x70, 0x48, 0x44, 0x00}, 77 0x08140022, 0x2222221c, 0x04080022, 0x2222221c,
78 {0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7c, 0x00}, 78 0x00140022, 0x223c201c, 0x14001c22, 0x2222221c,
79 {0x00, 0x44, 0x6c, 0x54, 0x44, 0x44, 0x44, 0x00}, 79 0x14002222, 0x2222221c, 0x00081c22, 0x02221c08,
80 {0x00, 0x44, 0x64, 0x54, 0x4c, 0x44, 0x44, 0x00}, 80 0x0018240e, 0x04043e00, 0x00221408, 0x1c081c08,
81 {0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00}, 81 0x0038041e, 0x041e0438, 0x0030081c, 0x08080806,
82 {0x00, 0x78, 0x44, 0x44, 0x78, 0x40, 0x40, 0x00}, 82 0x1008001c, 0x203c223c, 0x1008000c, 0x0808083e,
83 {0x00, 0x38, 0x44, 0x44, 0x54, 0x48, 0x34, 0x00}, 83 0x1008001c, 0x2222221c, 0x10080022, 0x2222221c,
84 {0x00, 0x78, 0x44, 0x44, 0x78, 0x48, 0x44, 0x00}, 84 0x2c1a000e, 0x32222222, 0x2c1a0022, 0x262a3222,
85 {0x00, 0x38, 0x40, 0x38, 0x04, 0x44, 0x38, 0x00}, 85 0x001c122c, 0x001e0000, 0x000c120c, 0x001e0000,
86 {0x00, 0x7c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00}, 86 0x08000804, 0x02221c00, 0x0000003e, 0x02020000,
87 {0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00}, 87 0x0000003e, 0x20200000, 0x06241468, 0x4422f000,
88 {0x00, 0x44, 0x44, 0x44, 0x44, 0x28, 0x10, 0x00}, 88 0x06241468, 0x54f24000, 0x08000808, 0x08080800,
89 {0x00, 0x44, 0x44, 0x54, 0x54, 0x54, 0x28, 0x00}, 89 0x00482412, 0x24480000, 0x00122448, 0x24120000,
90 {0x00, 0x44, 0x28, 0x10, 0x10, 0x28, 0x44, 0x00}, 90 0x00880022, 0x00880022, 0x55885522, 0x55885522,
91 {0x00, 0x44, 0x44, 0x28, 0x10, 0x10, 0x10, 0x00}, 91 0x55aa55aa, 0x55aa55aa, 0x08080808, 0x08080808,
92 {0x00, 0x7c, 0x08, 0x10, 0x20, 0x40, 0x7c, 0x00}, 92 0x0808080f, 0x08080808, 0x08080f08, 0x0f080808,
93 {0x38, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x38}, 93 0x14141417, 0x14141414, 0x0000001f, 0x14141414,
94 {0x00, 0x40, 0x60, 0x30, 0x18, 0x0c, 0x04, 0x00}, 94 0x00000f08, 0x0f080808, 0x14141710, 0x17141414,
95 {0x38, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x38}, 95 0x14141414, 0x14141414, 0x00001f10, 0x17141414,
96 {0x10, 0x28, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00}, 96 0x14141710, 0x1f000000, 0x1414141f, 0x00000000,
97 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00}, 97 0x08080f08, 0x0f000000, 0x0000000f, 0x08080808,
98 {0x00, 0x20, 0x10, 0x08, 0x00, 0x00, 0x00, 0x00}, 98 0x080808f8, 0x00000000, 0x080808ff, 0x00000000,
99 {0x00, 0x00, 0x38, 0x04, 0x3c, 0x44, 0x3c, 0x00}, 99 0x000000ff, 0x08080808, 0x080808f8, 0x08080808,
100 {0x00, 0x40, 0x40, 0x78, 0x44, 0x44, 0x78, 0x00}, 100 0x000000ff, 0x00000000, 0x080808ff, 0x08080808,
101 {0x00, 0x00, 0x38, 0x44, 0x40, 0x44, 0x38, 0x00}, 101 0x0808f808, 0xf8080808, 0x141414f4, 0x14141414,
102 {0x00, 0x04, 0x04, 0x3c, 0x44, 0x44, 0x3c, 0x00}, 102 0x1414f404, 0xfc000000, 0x0000fc04, 0xf4141414,
103 {0x00, 0x00, 0x38, 0x44, 0x78, 0x40, 0x3c, 0x00}, 103 0x1414f700, 0xff000000, 0x0000ff00, 0xf7141414,
104 {0x00, 0x00, 0x3c, 0x40, 0x40, 0x78, 0x40, 0x40}, 104 0x1414f404, 0xf4141414, 0x0000ff00, 0xff000000,
105 {0x00, 0x00, 0x3c, 0x44, 0x44, 0x3c, 0x04, 0x38}, 105 0x1414f700, 0xf7141414, 0x0808ff00, 0xff000000,
106 {0x00, 0x40, 0x40, 0x78, 0x44, 0x44, 0x44, 0x00}, 106 0x141414ff, 0x00000000, 0x0000ff00, 0xff080808,
107 {0x00, 0x10, 0x00, 0x30, 0x10, 0x10, 0x7c, 0x00}, 107 0x000000ff, 0x14141414, 0x141414fc, 0x00000000,
108 {0x00, 0x04, 0x00, 0x04, 0x04, 0x04, 0x44, 0x38}, 108 0x0808f808, 0xf8000000, 0x0000f808, 0xf8080808,
109 {0x00, 0x40, 0x48, 0x50, 0x60, 0x50, 0x4c, 0x00}, 109 0x000000fc, 0x14141414, 0x141414ff, 0x14141414,
110 {0x00, 0x30, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x00}, 110 0x0808ff08, 0xff080808, 0x0808080f, 0x00000000,
111 {0x00, 0x00, 0x68, 0x54, 0x54, 0x54, 0x54, 0x00}, 111 0x000000f8, 0x08080808, 0xffffffff, 0xffffffff,
112 {0x00, 0x00, 0x70, 0x4c, 0x44, 0x44, 0x44, 0x00}, 112 0x00000000, 0xffffffff, 0x0f0f0f0f, 0x0f0f0f0f,
113 {0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38, 0x00}, 113 0xf0f0f0f0, 0xf0f0f0f0, 0xffffffff, 0x00000000,
114 {0x00, 0x00, 0x78, 0x44, 0x44, 0x78, 0x40, 0x40}, 114 0x00002c12, 0x12122c00, 0x000c120a, 0x12223a02,
115 {0x00, 0x00, 0x3c, 0x44, 0x44, 0x3c, 0x04, 0x0e}, 115 0x003e2202, 0x02020200, 0x00003e14, 0x14141400,
116 {0x00, 0x00, 0x58, 0x64, 0x40, 0x40, 0x40, 0x00}, 116 0x3e220408, 0x04223e00, 0x00003c12, 0x12120c00,
117 {0x00, 0x00, 0x38, 0x40, 0x38, 0x04, 0x78, 0x00}, 117 0x00002222, 0x221e0202, 0x002c1a08, 0x08281000,
118 {0x00, 0x20, 0x7c, 0x20, 0x20, 0x20, 0x18, 0x00}, 118 0x1c081c22, 0x221c081c, 0x1c22223e, 0x22221c00,
119 {0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00}, 119 0x1c222222, 0x22143600, 0x1e041824, 0x22221c00,
120 {0x00, 0x00, 0x44, 0x44, 0x44, 0x28, 0x10, 0x00}, 120 0x0000142a, 0x2a140000, 0x0020142a, 0x2a140200,
121 {0x00, 0x00, 0x44, 0x44, 0x54, 0x54, 0x28, 0x00}, 121 0x00003c02, 0x1c023c00, 0x001c2222, 0x22222202,
122 {0x00, 0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00}, 122 0x00003e00, 0x3e003e00, 0x0000081c, 0x08001c00,
123 {0x00, 0x00, 0x44, 0x44, 0x44, 0x3c, 0x04, 0x38}, 123 0x04081008, 0x04001c00, 0x10080408, 0x10001c00,
124 {0x00, 0x00, 0x7c, 0x08, 0x10, 0x20, 0x7c, 0x00}, 124 0x30480808, 0x08080808, 0x08080808, 0x08080906,
125 {0x18, 0x20, 0x20, 0x40, 0x40, 0x20, 0x20, 0x18}, 125 0x00000800, 0x3e000800, 0x00004c32, 0x004c3200,
126 {0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10}, 126 0x00182418, 0x00000000, 0x00001c1c, 0x1c000000,
127 {0x30, 0x08, 0x08, 0x04, 0x04, 0x08, 0x08, 0x30}, 127 0x00000018, 0x18000000, 0x00f01010, 0x10121418,
128 {0x00, 0x00, 0x34, 0x58, 0x00, 0x00, 0x00, 0x00}, 128 0x0c342424, 0x00000000, 0x1810083c, 0x00000000,
129 {0x00, 0x00, 0x10, 0x28, 0x44, 0x44, 0x7c, 0x00}, 129 0x00003c3c, 0x3c3c0000, 0x00000000, 0x00000000,
130 {0x00, 0x38, 0x44, 0x40, 0x44, 0x38, 0x10, 0x70},
131 {0x00, 0x28, 0x00, 0x44, 0x44, 0x44, 0x44, 0x38},
132 {0x08, 0x10, 0x00, 0x38, 0x44, 0x78, 0x40, 0x3c},
133 {0x10, 0x28, 0x00, 0x38, 0x04, 0x3c, 0x44, 0x3c},
134 {0x00, 0x28, 0x00, 0x38, 0x04, 0x3c, 0x44, 0x3c},
135 {0x20, 0x10, 0x00, 0x38, 0x04, 0x3c, 0x44, 0x3c},
136 {0x10, 0x28, 0x10, 0x38, 0x04, 0x3c, 0x44, 0x3c},
137 {0x00, 0x00, 0x38, 0x40, 0x44, 0x38, 0x10, 0x70},
138 {0x10, 0x28, 0x00, 0x38, 0x44, 0x78, 0x40, 0x3c},
139 {0x00, 0x28, 0x00, 0x38, 0x44, 0x78, 0x40, 0x3c},
140 {0x20, 0x10, 0x00, 0x38, 0x44, 0x78, 0x40, 0x3c},
141 {0x00, 0x28, 0x00, 0x30, 0x10, 0x10, 0x10, 0x7c},
142 {0x00, 0x28, 0x00, 0x30, 0x10, 0x10, 0x10, 0x7c},
143 {0x20, 0x10, 0x00, 0x30, 0x10, 0x10, 0x10, 0x7c},
144 {0x00, 0x28, 0x00, 0x38, 0x44, 0x7c, 0x44, 0x44},
145 {0x10, 0x28, 0x10, 0x38, 0x44, 0x7c, 0x44, 0x44},
146 {0x08, 0x10, 0x00, 0x7c, 0x40, 0x78, 0x40, 0x7c},
147 {0x00, 0x00, 0x68, 0x14, 0x38, 0x50, 0x2c, 0x00},
148 {0x00, 0x3c, 0x50, 0x58, 0x70, 0x50, 0x5c, 0x00},
149 {0x10, 0x28, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38},
150 {0x00, 0x28, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38},
151 {0x20, 0x10, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38},
152 {0x10, 0x28, 0x00, 0x44, 0x44, 0x44, 0x44, 0x38},
153 {0x20, 0x10, 0x00, 0x44, 0x44, 0x44, 0x44, 0x38},
154 {0x00, 0x28, 0x00, 0x44, 0x44, 0x3c, 0x04, 0x38},
155 {0x28, 0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x38},
156 {0x28, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x38},
157 {0x00, 0x10, 0x38, 0x44, 0x40, 0x44, 0x38, 0x10},
158 {0x00, 0x18, 0x24, 0x70, 0x20, 0x20, 0x7c, 0x00},
159 {0x00, 0x44, 0x28, 0x10, 0x38, 0x10, 0x38, 0x10},
160 {0x00, 0x1c, 0x20, 0x78, 0x20, 0x78, 0x20, 0x1c},
161 {0x00, 0x0c, 0x10, 0x38, 0x10, 0x10, 0x10, 0x60},
162 {0x08, 0x10, 0x00, 0x38, 0x04, 0x3c, 0x44, 0x3c},
163 {0x08, 0x10, 0x00, 0x30, 0x10, 0x10, 0x10, 0x7c},
164 {0x08, 0x10, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38},
165 {0x08, 0x10, 0x00, 0x44, 0x44, 0x44, 0x44, 0x38},
166 {0x34, 0x58, 0x00, 0x70, 0x4c, 0x44, 0x44, 0x44},
167 {0x34, 0x58, 0x00, 0x44, 0x64, 0x54, 0x4c, 0x44},
168 {0x00, 0x38, 0x48, 0x34, 0x00, 0x78, 0x00, 0x00},
169 {0x00, 0x30, 0x48, 0x30, 0x00, 0x78, 0x00, 0x00},
170 {0x10, 0x00, 0x10, 0x20, 0x40, 0x44, 0x38, 0x00},
171 {0x00, 0x00, 0x00, 0x7c, 0x40, 0x40, 0x00, 0x00},
172 {0x00, 0x00, 0x00, 0x7c, 0x04, 0x04, 0x00, 0x00},
173 {0x60, 0x24, 0x28, 0x16, 0x22, 0x44, 0x0f, 0x00},
174 {0x60, 0x24, 0x28, 0x16, 0x2a, 0x4f, 0x02, 0x00},
175 {0x10, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00},
176 {0x00, 0x12, 0x24, 0x48, 0x24, 0x12, 0x00, 0x00},
177 {0x00, 0x48, 0x24, 0x12, 0x24, 0x48, 0x00, 0x00},
178 {0x00, 0x11, 0x00, 0x44, 0x00, 0x11, 0x00, 0x44},
179 {0xaa, 0x11, 0xaa, 0x44, 0xaa, 0x11, 0xaa, 0x44},
180 {0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55},
181 {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10},
182 {0x10, 0x10, 0x10, 0xf0, 0x10, 0x10, 0x10, 0x10},
183 {0x10, 0x10, 0xf0, 0x10, 0xf0, 0x10, 0x10, 0x10},
184 {0x28, 0x28, 0x28, 0xe8, 0x28, 0x28, 0x28, 0x28},
185 {0x00, 0x00, 0x00, 0xf8, 0x28, 0x28, 0x28, 0x28},
186 {0x00, 0x00, 0xf0, 0x10, 0xf0, 0x10, 0x10, 0x10},
187 {0x28, 0x28, 0xe8, 0x08, 0xe8, 0x28, 0x28, 0x28},
188 {0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28},
189 {0x00, 0x00, 0xf8, 0x08, 0xe8, 0x28, 0x28, 0x28},
190 {0x28, 0x28, 0xe8, 0x08, 0xf8, 0x00, 0x00, 0x00},
191 {0x28, 0x28, 0x28, 0xf8, 0x00, 0x00, 0x00, 0x00},
192 {0x10, 0x10, 0xf0, 0x10, 0xf0, 0x00, 0x00, 0x00},
193 {0x00, 0x00, 0x00, 0xf0, 0x10, 0x10, 0x10, 0x10},
194 {0x10, 0x10, 0x10, 0x1f, 0x00, 0x00, 0x00, 0x00},
195 {0x10, 0x10, 0x10, 0xff, 0x00, 0x00, 0x00, 0x00},
196 {0x00, 0x00, 0x00, 0xff, 0x10, 0x10, 0x10, 0x10},
197 {0x10, 0x10, 0x10, 0x1f, 0x10, 0x10, 0x10, 0x10},
198 {0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00},
199 {0x10, 0x10, 0x10, 0xff, 0x10, 0x10, 0x10, 0x10},
200 {0x10, 0x10, 0x1f, 0x10, 0x1f, 0x10, 0x10, 0x10},
201 {0x28, 0x28, 0x28, 0x2f, 0x28, 0x28, 0x28, 0x28},
202 {0x28, 0x28, 0x2f, 0x20, 0x3f, 0x00, 0x00, 0x00},
203 {0x00, 0x00, 0x3f, 0x20, 0x2f, 0x28, 0x28, 0x28},
204 {0x28, 0x28, 0xef, 0x00, 0xff, 0x00, 0x00, 0x00},
205 {0x00, 0x00, 0xff, 0x00, 0xef, 0x28, 0x28, 0x28},
206 {0x28, 0x28, 0x2f, 0x20, 0x2f, 0x28, 0x28, 0x28},
207 {0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00},
208 {0x28, 0x28, 0xef, 0x00, 0xef, 0x28, 0x28, 0x28},
209 {0x10, 0x10, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00},
210 {0x28, 0x28, 0x28, 0xff, 0x00, 0x00, 0x00, 0x00},
211 {0x00, 0x00, 0xff, 0x00, 0xff, 0x10, 0x10, 0x10},
212 {0x00, 0x00, 0x00, 0xff, 0x28, 0x28, 0x28, 0x28},
213 {0x28, 0x28, 0x28, 0x3f, 0x00, 0x00, 0x00, 0x00},
214 {0x10, 0x10, 0x1f, 0x10, 0x1f, 0x00, 0x00, 0x00},
215 {0x00, 0x00, 0x1f, 0x10, 0x1f, 0x10, 0x10, 0x10},
216 {0x00, 0x00, 0x00, 0x3f, 0x28, 0x28, 0x28, 0x28},
217 {0x28, 0x28, 0x28, 0xff, 0x28, 0x28, 0x28, 0x28},
218 {0x10, 0x10, 0xff, 0x10, 0xff, 0x10, 0x10, 0x10},
219 {0x10, 0x10, 0x10, 0xf0, 0x00, 0x00, 0x00, 0x00},
220 {0x00, 0x00, 0x00, 0x1f, 0x10, 0x10, 0x10, 0x10},
221 {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
222 {0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff},
223 {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
224 {0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f},
225 {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00},
226 {0x00, 0x00, 0x34, 0x48, 0x48, 0x48, 0x34, 0x00},
227 {0x00, 0x30, 0x48, 0x50, 0x48, 0x44, 0x5c, 0x40},
228 {0x00, 0x7c, 0x44, 0x40, 0x40, 0x40, 0x40, 0x00},
229 {0x00, 0x00, 0x7c, 0x28, 0x28, 0x28, 0x28, 0x00},
230 {0x7c, 0x44, 0x20, 0x10, 0x20, 0x44, 0x7c, 0x00},
231 {0x00, 0x00, 0x3c, 0x48, 0x48, 0x48, 0x30, 0x00},
232 {0x00, 0x00, 0x44, 0x44, 0x44, 0x78, 0x40, 0x40},
233 {0x00, 0x34, 0x58, 0x10, 0x10, 0x14, 0x08, 0x00},
234 {0x38, 0x10, 0x38, 0x44, 0x44, 0x38, 0x10, 0x38},
235 {0x38, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x38, 0x00},
236 {0x38, 0x44, 0x44, 0x44, 0x44, 0x28, 0x6c, 0x00},
237 {0x78, 0x20, 0x18, 0x24, 0x44, 0x44, 0x38, 0x00},
238 {0x00, 0x00, 0x28, 0x54, 0x54, 0x28, 0x00, 0x00},
239 {0x00, 0x04, 0x28, 0x54, 0x54, 0x28, 0x40, 0x00},
240 {0x00, 0x00, 0x3c, 0x40, 0x38, 0x40, 0x3c, 0x00},
241 {0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x44, 0x40},
242 {0x00, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0x7c, 0x00},
243 {0x00, 0x00, 0x10, 0x38, 0x10, 0x00, 0x38, 0x00},
244 {0x20, 0x10, 0x08, 0x10, 0x20, 0x00, 0x38, 0x00},
245 {0x08, 0x10, 0x20, 0x10, 0x08, 0x00, 0x38, 0x00},
246 {0x0c, 0x12, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10},
247 {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x90, 0x60},
248 {0x00, 0x00, 0x10, 0x00, 0x7c, 0x00, 0x10, 0x00},
249 {0x00, 0x00, 0x32, 0x4c, 0x00, 0x32, 0x4c, 0x00},
250 {0x00, 0x18, 0x24, 0x18, 0x00, 0x00, 0x00, 0x00},
251 {0x00, 0x00, 0x38, 0x38, 0x38, 0x00, 0x00, 0x00},
252 {0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00},
253 {0x00, 0x0f, 0x08, 0x08, 0x08, 0x48, 0x28, 0x18},
254 {0x30, 0x2c, 0x24, 0x24, 0x00, 0x00, 0x00, 0x00},
255 {0x18, 0x08, 0x10, 0x3c, 0x00, 0x00, 0x00, 0x00},
256 {0x00, 0x00, 0x3c, 0x3c, 0x3c, 0x3c, 0x00, 0x00},
257 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
258}; 130};
diff --git a/src/common.h b/src/common.h
index 6047e5f..669fb6e 100644
--- a/src/common.h
+++ b/src/common.h
@@ -47,16 +47,10 @@
47#define DISP_ENABLE_SPRITES DISP_OBJ | DISP_OBJ_1D 47#define DISP_ENABLE_SPRITES DISP_OBJ | DISP_OBJ_1D
48 48
49// Registers to control of BG layers. 49// Registers to control of BG layers.
50#define BG_CTRL_0 *((vu16*)(0x04000008 + 0x0002 * 0)) 50#define BG_CTRL(N) *((vu16*)(0x04000008 + 0x0002 * (N)))
51#define BG_CTRL_1 *((vu16*)(0x04000008 + 0x0002 * 1))
52#define BG_CTRL_2 *((vu16*)(0x04000008 + 0x0002 * 2))
53#define BG_CTRL_3 *((vu16*)(0x04000008 + 0x0002 * 3))
54 51
55// Bits to control the background. 52// Bits to control the background.
56#define BG_PRIORITY_0 0x0 53#define BG_PRIORITY(N) ((N) & 0x3)
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) 54#define BG_CHARBLOCK(N) ((N) << 2)
61#define BG_MOSAIC (1 << 6) 55#define BG_MOSAIC (1 << 6)
62#define BG_HIGH_COLOR (1 << 7) 56#define BG_HIGH_COLOR (1 << 7)
@@ -76,20 +70,6 @@
76#define BG_V_SCROLL_2 *((vu16*)(0x04000012 + 0x0004 * 2)) 70#define BG_V_SCROLL_2 *((vu16*)(0x04000012 + 0x0004 * 2))
77#define BG_V_SCROLL_3 *((vu16*)(0x04000012 + 0x0004 * 3)) 71#define BG_V_SCROLL_3 *((vu16*)(0x04000012 + 0x0004 * 3))
78 72
79// Screenblocks for BGs.
80typedef u16 ScreenBlock[1024];
81#define SCREENBLOCK_MEM ((ScreenBlock*)MEM_VRAM)
82
83// Screenblock entry bits.
84#define SCREENBLOCK_ENTRY_H_FLIP (1 << 0xA)
85#define SCREENBLOCK_ENTRY_V_FLIP (1 << 0xB)
86#define SCREENBLOCK_ENTRY_PAL(N) ((N) << 0xC)
87
88size_t se_index(size_t tile_x, size_t tile_y, size_t map_width) {
89 size_t sbb = ((tile_x >> 5) + (tile_y >> 5) * (map_width >> 5));
90 return sbb * 1024 + ((tile_x & 31) + (tile_y & 31) * 32);
91}
92
93// Screen settings. 73// Screen settings.
94#define SCREEN_WIDTH 240 74#define SCREEN_WIDTH 240
95#define SCREEN_HEIGHT 160 75#define SCREEN_HEIGHT 160
@@ -113,6 +93,23 @@ typedef struct Tile {
113typedef Tile TileBlock[512]; 93typedef Tile TileBlock[512];
114#define TILE_MEM ((TileBlock*) MEM_VRAM) 94#define TILE_MEM ((TileBlock*) MEM_VRAM)
115 95
96// Screenblocks and charblocks for backgrounds.
97typedef u16 ScreenBlock[1024];
98typedef Tile CharBlock[512];
99#define CHARBLOCK_MEM ((CharBlock*)MEM_VRAM)
100#define SCREENBLOCK_MEM ((ScreenBlock*)MEM_VRAM)
101
102// Screenblock entry bits.
103#define SCREENBLOCK_ENTRY_H_FLIP (1 << 0xA)
104#define SCREENBLOCK_ENTRY_V_FLIP (1 << 0xB)
105#define SCREENBLOCK_ENTRY_PAL(N) ((N) << 0xC)
106
107size_t se_index(size_t tile_x, size_t tile_y, size_t map_width) {
108 size_t sbb = ((tile_x >> 5) + (tile_y >> 5) * (map_width >> 5));
109 return sbb * 1024 + ((tile_x & 31) + (tile_y & 31) * 32);
110}
111
112
116// We can treat the screen as a HxW matrix. With the following macro we can 113// We can treat the screen as a HxW matrix. With the following macro we can
117// write a pixel to the screen at the (x, y) position using: 114// write a pixel to the screen at the (x, y) position using:
118// 115//
@@ -293,4 +290,30 @@ key_hold(u32 key) {
293// Check if the given key/button is currently pressed. 290// Check if the given key/button is currently pressed.
294#define KEY_PRESSED(key) (~(KEY_INPUTS) & key) 291#define KEY_PRESSED(key) (~(KEY_INPUTS) & key)
295 292
293// Back/unpack bits.
294static inline u32
295unpack_1bb(u8 hex) {
296 const u32 conversion_u32[16] = {
297 0x00000000, 0x00000001, 0x00000010, 0x00000011,
298 0x00000100, 0x00000101, 0x00000110, 0x00000111,
299 0x00001000, 0x00001001, 0x00001010, 0x00001011,
300 0x00001100, 0x00001101, 0x00001110, 0x00001111,
301 };
302 u8 low = hex & 0xF;
303 u8 high = (hex >> 4) & 0xF;
304 return (conversion_u32[high] << 16) | conversion_u32[low];
305}
306
307// Unpack N tiles packed at 1bpp.
308unpack_tiles(u32 *src, u32 *dst, size_t n_tiles) {
309 u32 *target_src = src + n_tiles * 2;
310 while (src != target_src) {
311 *dst++ = unpack_1bb((*src >> 24) & 0xFF);
312 *dst++ = unpack_1bb((*src >> 16) & 0xFF);
313 *dst++ = unpack_1bb((*src >> 8) & 0xFF);
314 *dst++ = unpack_1bb(*src & 0xFF);
315 src++;
316 }
317}
318
296#endif // GBAEXP_COMMON_H 319#endif // GBAEXP_COMMON_H
diff --git a/src/main.c b/src/main.c
index 793baa8..3bd5865 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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
20u16 *bg0_map = SCREENBLOCK_MEM[SBB_0];
21
22void
23init_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
55int main(void) { 16int 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 };
diff --git a/src/sprites.h b/src/sprites.h
index e249e95..88ca787 100644
--- a/src/sprites.h
+++ b/src/sprites.h
@@ -3,19 +3,6 @@
3 3
4#include "common.h" 4#include "common.h"
5 5
6u32
7unpack_1bb(u8 hex) {
8 const u32 conversion_u32[16] = {
9 0x00000000, 0x00000001, 0x00000010, 0x00000011,
10 0x00000100, 0x00000101, 0x00000110, 0x00000111,
11 0x00001000, 0x00001001, 0x00001010, 0x00001011,
12 0x00001100, 0x00001101, 0x00001110, 0x00001111,
13 };
14 u8 low = hex & 0xF;
15 u8 high = (hex >> 4) & 0xF;
16 return (conversion_u32[high] << 16) | conversion_u32[low];
17}
18
19typedef struct Sprite { 6typedef struct Sprite {
20 // A unique sprite identifier. 7 // A unique sprite identifier.
21 size_t id; 8 size_t id;
@@ -54,14 +41,7 @@ load_sprite_data(u32 *sprite_data, size_t n_tiles, size_t n_frames) {
54 41
55size_t 42size_t
56load_packed_sprite_data(u32 *sprite_data, size_t n_tiles, size_t n_frames) { 43load_packed_sprite_data(u32 *sprite_data, size_t n_tiles, size_t n_frames) {
57 size_t counter = 0; 44 unpack_tiles(sprite_data, sprite_memory, n_tiles * n_frames);
58 for (size_t i = 0; i < 8 * n_tiles * n_frames / 4; ++i) {
59 u32 hex = sprite_data[i];
60 sprite_memory[counter++] = unpack_1bb((hex >> 24) & 0xFF);
61 sprite_memory[counter++] = unpack_1bb((hex >> 16) & 0xFF);
62 sprite_memory[counter++] = unpack_1bb((hex >> 8) & 0xFF);
63 sprite_memory[counter++] = unpack_1bb((hex) & 0xFF);
64 }
65 sprite_memory += 8 * n_tiles * n_frames; 45 sprite_memory += 8 * n_tiles * n_frames;
66 Sprite sprite = { 46 Sprite sprite = {
67 .id = sprite_counter, 47 .id = sprite_counter,
diff --git a/src/text.h b/src/text.h
new file mode 100644
index 0000000..e38c220
--- /dev/null
+++ b/src/text.h
@@ -0,0 +1,142 @@
1#ifndef GBAEXP_TILES_H
2#define GBAEXP_TILES_H
3
4#include <stdarg.h>
5
6#include "common.h"
7#include "bd-font.c"
8
9typedef struct TextEngine {
10 // Currently working on tiled backgrounds only. The X and Y positions
11 // correspond to the tile X and Y starting from the top left of the screen.
12 // For a 240x160 screen, we have 30x20 tiles available.
13 size_t cursor_x;
14 size_t cursor_y;
15 // Pointer to the screenblock being used for writing to the screen.
16 u16 *screenblock;
17} TextEngine;
18
19static TextEngine text_engine = {0};
20
21void
22txt_putc(char c) {
23 if (c == '\0') {
24 return;
25 }
26 if (c == '\n') {
27 text_engine.cursor_x = 0;
28 text_engine.cursor_y++;
29 } else {
30 text_engine.screenblock[text_engine.cursor_x + 32 * text_engine.cursor_y] = c;
31 text_engine.cursor_x++;
32 if (text_engine.cursor_x >= 30) {
33 text_engine.cursor_x = 0;
34 text_engine.cursor_y++;
35 }
36 }
37 if (text_engine.cursor_y >= 20) {
38 text_engine.cursor_y = 0;
39 }
40}
41
42static inline void
43txt_puts(char *msg) {
44 while (*msg) {
45 txt_putc(*msg++);
46 }
47}
48
49void
50txt_init(size_t bg, Color clr, size_t cb_idx) {
51 // The screenblock for the tile map should start after the tile memory
52 // (MEM_VRAM + 0x2000 for 256 characters). Since each screenblock is
53 // composed of 1024 screenblock entries of u16 (2 bytes), we need an
54 // screenblock index offset of 8192 / (1024 * 2) = 4 screen blocks.
55 size_t sb_idx = cb_idx * 8 + 4;
56
57 // Set the background parameters for the text layer.
58 BG_CTRL(bg) = BG_CHARBLOCK(cb_idx) | BG_SCREENBLOCK(sb_idx) | BG_PRIORITY(3);
59
60 // Load font data in video memory. Each character is unpacked into a tile of
61 // 8 32bit values (4bpp), meaning that for the full ASCII set of 256
62 // characters, we need 8192 bytes of VRAM (8 * 4 * 256).
63 unpack_tiles(&bd_font, &CHARBLOCK_MEM[cb_idx], 256);
64
65 // Load palette color.
66 PAL_BUFFER_BG[1] = clr;
67
68 // Update text_engine variables.
69 text_engine.screenblock = SCREENBLOCK_MEM[sb_idx];
70}
71
72// Print text to the screen with formatting.
73void
74txt_printf(char *msg, ...) {
75 va_list arg_list;
76 va_start(arg_list, msg);
77 char c;
78 char prev_c;
79 while ((c = *msg++) != '\0') {
80 if (c != '%') {
81 txt_putc(c);
82 prev_c = c;
83 continue;
84 }
85 c = *msg++;
86 switch (c) {
87 case 's': {
88 txt_puts(va_arg(arg_list, char *));
89 } break;
90 case 'd': {
91 char buf[32] = {0};
92 int x = va_arg(arg_list, int);
93 sprintf(buf, "%d", x);
94 txt_puts(buf);
95 } break;
96 case 'u': {
97 char buf[32] = {0};
98 unsigned int x = va_arg(arg_list, unsigned int);
99 sprintf(buf, "%u", x);
100 txt_puts(buf);
101 } break;
102 case 'x': {
103 char buf[32] = {0};
104 unsigned int x = va_arg(arg_list, unsigned int);
105 sprintf(buf, "%x", x);
106 txt_puts(buf);
107 } break;
108 default: {
109 txt_putc('%');
110 txt_putc(c);
111 } break;
112 }
113 }
114}
115
116void
117txt_clear_line(void) {
118 for (size_t i = 0; i < 30; ++i) {
119 text_engine.screenblock[i + 32 * text_engine.cursor_y] = ' ';
120 }
121 text_engine.cursor_x = 0;
122}
123
124void
125txt_clear_screen(void) {
126 for (size_t j = 0; j < 20; ++j) {
127 for (size_t i = 0; i < 30; ++i) {
128 text_engine.screenblock[i + 32 * j] = ' ';
129 }
130 }
131 text_engine.cursor_x = 0;
132 text_engine.cursor_y = 0;
133}
134
135void
136txt_position(size_t tile_x, size_t tile_y) {
137 text_engine.cursor_x = tile_x;
138 text_engine.cursor_y = tile_y;
139}
140
141#endif // GBAEXP_TILES_H
142