summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-04-13 20:16:02 +0200
committerBad Diode <bd@badd10de.dev>2021-04-13 20:16:02 +0200
commit40c0489920021195877529ab7bfdf597b9468774 (patch)
tree527abe776096e24589182ac389372b3ad652eb4f
parent93daff186c2c5ee681d2e2b1bf4ad1b2a64f7b28 (diff)
downloadgba-experiments-40c0489920021195877529ab7bfdf597b9468774.tar.gz
gba-experiments-40c0489920021195877529ab7bfdf597b9468774.zip
An alternative way of putting pixels on the framebuffer
-rw-r--r--src/main.c30
1 files 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 @@
20// Display modes. 20// Display modes.
21// 21//
22 22
23// Screen settings.
24#define SCREEN_WIDTH 240
25#define SCREEN_HEIGHT 160
26
27// Display modes. 23// Display modes.
28#define DISP_MODE_0 0x0000 24#define DISP_MODE_0 0x0000
29#define DISP_MODE_1 0x0001 25#define DISP_MODE_1 0x0001
@@ -44,31 +40,38 @@ set_display_mode(u16 value) {
44 *((vu32*)(MEM_IO + 0x0000)) = value; 40 *((vu32*)(MEM_IO + 0x0000)) = value;
45} 41}
46 42
47// 43// Screen settings.
48// Colors. 44#define SCREEN_WIDTH 240
49// 45#define SCREEN_HEIGHT 160
50 46
51// The GBA in mode 3 expects rbg15 colors in the VRAM, where each component 47// The GBA in mode 3 expects rbg15 colors in the VRAM, where each component
52// (RGB) have a 0--31 range. For example, pure red would be rgb15(31, 0, 0). 48// (RGB) have a 0--31 range. For example, pure red would be rgb15(31, 0, 0).
53typedef u16 Color; 49typedef u16 Color;
54 50
51// We can treat the screen as a HxW matrix. With the following macro we can
52// write a pixel to the screen at the (x, y) position using:
53//
54// FRAMEBUFFER[y][x] = color;
55//
56typedef Color Scanline[SCREEN_WIDTH];
57#define FRAMEBUFFER ((Scanline*)MEM_VRAM)
58
59//
60// Colors.
61//
62
55static inline Color 63static inline Color
56rgb15(u32 red, u32 green, u32 blue ) { 64rgb15(u32 red, u32 green, u32 blue ) {
57 return (blue << 10) | (green << 5) | red; 65 return (blue << 10) | (green << 5) | red;
58} 66}
59 67
60static inline void
61put_pixel(int x, int y, Color clr) {
62 ((vu16*)MEM_VRAM)[x + y * SCREEN_WIDTH] = clr;
63}
64
65// Using bd-font, an 8x8 bitmap font. 68// Using bd-font, an 8x8 bitmap font.
66static inline void 69static inline void
67put_char(int x, int y, Color clr, u8 chr) { 70put_char(int x, int y, Color clr, u8 chr) {
68 for (size_t i = 0; i < 8; ++i) { 71 for (size_t i = 0; i < 8; ++i) {
69 for (size_t j = 0; j < 8; ++j) { 72 for (size_t j = 0; j < 8; ++j) {
70 if ((font[chr][i] >> (7 - j)) & 0x1) { 73 if ((font[chr][i] >> (7 - j)) & 0x1) {
71 put_pixel(x + j, y + i, clr); 74 FRAMEBUFFER[y + i][x + j] = clr;
72 } 75 }
73 } 76 }
74 } 77 }
@@ -90,7 +93,6 @@ put_line(int x, int y, Color clr, char *msg) {
90int main(void) { 93int main(void) {
91 set_display_mode(DISP_MODE_3 | DISP_BG2); 94 set_display_mode(DISP_MODE_3 | DISP_BG2);
92 95
93
94 while(true) { 96 while(true) {
95 put_line(16, 20 + 16, rgb15(28, 0, 0), "Hello world from the GBA!"); 97 put_line(16, 20 + 16, rgb15(28, 0, 0), "Hello world from the GBA!");
96 put_line(16, 20 + 32, rgb15(0, 28, 28), "Using my little 8x8 bd-font"); 98 put_line(16, 20 + 32, rgb15(0, 28, 28), "Using my little 8x8 bd-font");