diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 59 |
1 files changed, 31 insertions, 28 deletions
@@ -61,6 +61,7 @@ typedef u16 Color; | |||
61 | typedef Color Scanline[SCREEN_WIDTH]; | 61 | typedef 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 | ||
230 | int 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. | ||
238 | static inline void | ||
239 | put_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. | 250 | static inline void |
234 | for (int j = 30; j < SCREEN_HEIGHT - 30; j += 8) { | 251 | draw_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. | 260 | int 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(); |