diff options
-rw-r--r-- | src/main.c | 30 |
1 files changed, 16 insertions, 14 deletions
@@ -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). |
53 | typedef u16 Color; | 49 | typedef 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 | // | ||
56 | typedef Color Scanline[SCREEN_WIDTH]; | ||
57 | #define FRAMEBUFFER ((Scanline*)MEM_VRAM) | ||
58 | |||
59 | // | ||
60 | // Colors. | ||
61 | // | ||
62 | |||
55 | static inline Color | 63 | static inline Color |
56 | rgb15(u32 red, u32 green, u32 blue ) { | 64 | rgb15(u32 red, u32 green, u32 blue ) { |
57 | return (blue << 10) | (green << 5) | red; | 65 | return (blue << 10) | (green << 5) | red; |
58 | } | 66 | } |
59 | 67 | ||
60 | static inline void | ||
61 | put_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. |
66 | static inline void | 69 | static inline void |
67 | put_char(int x, int y, Color clr, u8 chr) { | 70 | put_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) { | |||
90 | int main(void) { | 93 | int 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"); |