From 40c0489920021195877529ab7bfdf597b9468774 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 13 Apr 2021 20:16:02 +0200 Subject: An alternative way of putting pixels on the framebuffer --- src/main.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index fd5d21a..c49ce51 100644 --- a/src/main.c +++ b/src/main.c @@ -20,10 +20,6 @@ // Display modes. // -// Screen settings. -#define SCREEN_WIDTH 240 -#define SCREEN_HEIGHT 160 - // Display modes. #define DISP_MODE_0 0x0000 #define DISP_MODE_1 0x0001 @@ -44,31 +40,38 @@ set_display_mode(u16 value) { *((vu32*)(MEM_IO + 0x0000)) = value; } -// -// Colors. -// +// Screen settings. +#define SCREEN_WIDTH 240 +#define SCREEN_HEIGHT 160 // The GBA in mode 3 expects rbg15 colors in the VRAM, where each component // (RGB) have a 0--31 range. For example, pure red would be rgb15(31, 0, 0). typedef u16 Color; +// We can treat the screen as a HxW matrix. With the following macro we can +// write a pixel to the screen at the (x, y) position using: +// +// FRAMEBUFFER[y][x] = color; +// +typedef Color Scanline[SCREEN_WIDTH]; +#define FRAMEBUFFER ((Scanline*)MEM_VRAM) + +// +// Colors. +// + static inline Color rgb15(u32 red, u32 green, u32 blue ) { return (blue << 10) | (green << 5) | red; } -static inline void -put_pixel(int x, int y, Color clr) { - ((vu16*)MEM_VRAM)[x + y * SCREEN_WIDTH] = clr; -} - // Using bd-font, an 8x8 bitmap font. static inline void put_char(int x, int y, Color clr, u8 chr) { for (size_t i = 0; i < 8; ++i) { for (size_t j = 0; j < 8; ++j) { if ((font[chr][i] >> (7 - j)) & 0x1) { - put_pixel(x + j, y + i, clr); + FRAMEBUFFER[y + i][x + j] = clr; } } } @@ -90,7 +93,6 @@ put_line(int x, int y, Color clr, char *msg) { int main(void) { set_display_mode(DISP_MODE_3 | DISP_BG2); - while(true) { put_line(16, 20 + 16, rgb15(28, 0, 0), "Hello world from the GBA!"); put_line(16, 20 + 32, rgb15(0, 28, 28), "Using my little 8x8 bd-font"); -- cgit v1.2.1