summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-04-15 17:24:25 +0200
committerBad Diode <bd@badd10de.dev>2021-04-15 17:24:25 +0200
commit37e72d68fe6fbbdc211a6ef2b7756877e31e8061 (patch)
tree66d34c5f6c292079418060ee6aaaaf8d85a2a7fb
parentf220b02578d5f6549cb002bf69ad43ab2d2f923c (diff)
downloadgba-experiments-37e72d68fe6fbbdc211a6ef2b7756877e31e8061.tar.gz
gba-experiments-37e72d68fe6fbbdc211a6ef2b7756877e31e8061.zip
Add initial Mode4 tests, copied from the TONC tutorial
-rw-r--r--src/main.c59
1 files changed, 31 insertions, 28 deletions
diff --git a/src/main.c b/src/main.c
index 1596ba4..cbfa2a7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -61,6 +61,7 @@ typedef u16 Color;
61typedef Color Scanline[SCREEN_WIDTH]; 61typedef Color Scanline[SCREEN_WIDTH];
62#define FRAMEBUFFER ((Scanline*)MEM_VRAM) 62#define FRAMEBUFFER ((Scanline*)MEM_VRAM)
63#define SCREEN_BUFFER ((vu16*) MEM_VRAM) 63#define SCREEN_BUFFER ((vu16*) MEM_VRAM)
64#define PAL_BUFFER ((vu16*) MEM_PAL)
64 65
65// 66//
66// Colors. 67// Colors.
@@ -227,39 +228,41 @@ wait_vsync() {
227// Main functions. 228// Main functions.
228// 229//
229 230
230int main(void) { 231// In Mode4 the buffer is of 8 bytes per pixel instead of 16. We can't write the
231 set_display_mode(DISP_MODE_3 | DISP_BG2); 232// color directly, instead the color is stored in the palette memory at
233// `MEM_PAL`. Note that in this mode MEM_PAL[0] is the background color. This
234// plotter takes an index to a color stored in MEM_PAL[col_index]. Because the
235// GBA needs to meet memory alignment requirements, we can't write a u8 into
236// memory, instead we need to read a u16 word, mask and or the corresponding
237// bits and wave the updated u16.
238static inline void
239put_pixel_m4(int x, int y, u8 col_index) {
240 int buffer_index = (y * SCREEN_WIDTH + x) / 2;
241 u16 *destination = &SCREEN_BUFFER[buffer_index];
242 int odd = x & 0x1;
243 if(odd) {
244 *destination= (*destination & 0xFF) | (col_index << 8);
245 } else {
246 *destination= (*destination & ~0xFF) | col_index;
247 }
248}
232 249
233 // Draw a grid pattern. 250static inline void
234 for (int j = 30; j < SCREEN_HEIGHT - 30; j += 8) { 251draw_fill_rect_m4(int x0, int y0, int x1, int y1, u8 col_index) {
235 for (int i = 30; i < 50; i += 8) { 252 int ix, iy;
236 draw_rect(i, j, 0 + i + 8, j + 8, COLOR_RED); 253 for(iy = y0; iy < y1; iy++) {
254 for(ix = x0; ix < x1; ix++) {
255 put_pixel_m4(ix, iy, col_index);
237 } 256 }
238 } 257 }
258}
239 259
240 // Draw the text line. 260int main(void) {
241 for (int i = 0; i < SCREEN_WIDTH; i += 8) { 261 set_display_mode(DISP_MODE_4 | DISP_BG2);
242 draw_fill_rect(i, 7, 0 + i + 7, 16, COLOR_RED);
243 }
244 put_text(8, 8, COLOR_BLACK, "Testing other patterns");
245 262
246 // Test line drawings. 263 PAL_BUFFER[1] = COLOR_RED;
247 draw_fill_rect(69, 29, SCREEN_WIDTH - 69, SCREEN_HEIGHT - 29, COLOR_BLACK); 264 PAL_BUFFER[2] = COLOR_BLUE;
248 int step = 10; 265 draw_fill_rect_m4(0, 0, 20, 20, 1);
249 for (int k = 0; k < (SCREEN_WIDTH - 70 - 70) / step; k++) {
250 int x0 = 70;
251 int y0 = 30 + k * step;
252 int x1 = x0 + k * step + step;
253 int y1 = SCREEN_HEIGHT - 30;
254 draw_line(x0, y0, x1, y1, COLOR_BLUE);
255 }
256 for (int k = 0; k < (SCREEN_WIDTH - 70 - 70) / step; k++) {
257 int x0 = 70 + k * step;
258 int y0 = 30;
259 int x1 = SCREEN_WIDTH - 70;
260 int y1 = y0 + k * step + step;
261 draw_line(x0, y0, x1, y1, COLOR_RED);
262 }
263 266
264 while(true) { 267 while(true) {
265 wait_vsync(); 268 wait_vsync();