diff options
Diffstat (limited to 'src/renderer.c')
-rw-r--r-- | src/renderer.c | 71 |
1 files changed, 48 insertions, 23 deletions
diff --git a/src/renderer.c b/src/renderer.c index 9fe55b2..ec54411 100644 --- a/src/renderer.c +++ b/src/renderer.c | |||
@@ -1,14 +1,28 @@ | |||
1 | // TODO: For now we pack front/backbuffers together but this make it so that we | 1 | #include "bd-font.c" |
2 | // can only use 2 backgrounds. Instead we can move the backbuffer to the end of | 2 | |
3 | // the VRAM. This will give us 3 backgrounds but eats into the available memory | 3 | // The frontbuffer is located at the beginning of the VRAM, and requires 20KB of |
4 | // for sprites but should be fine for non sprite intensive applications. | 4 | // video memory for 32 * 20 tiles at 4bpp. |
5 | #define FRONTBUFFER ((u32*)(MEM_VRAM)) | 5 | #define FRONTBUF ((u32*)(MEM_VRAM)) |
6 | #define BACKBUFFER ((u32*)(MEM_VRAM + KB(96) - KB(20))) | ||
7 | 6 | ||
8 | // Adjust both of these if the location of the map changes. Each screnblock | 7 | // Adjust both of these if the location of the map changes. Each screnblock |
9 | // requires 2K. | 8 | // requires less than 2KB. |
10 | #define FRONTBUFFER_TILEMAP ((u16*)(MEM_VRAM + KB(20))) | 9 | #define FRONTBUF_TILEMAP ((u16*)(MEM_VRAM + KB(20))) |
11 | #define FRONTBUFFER_SCREENBLOCK 10 | 10 | #define FRONTBUF_SB 10 |
11 | |||
12 | // The backbuffer is located at the end of the VRAM. This can allow us to use | ||
13 | // more backgrounds but eats into the available memory for sprites. This should | ||
14 | // be fine for non sprite intensive applications. If more sprite memory is | ||
15 | // needed, the backbuffer can be located at the end of the background memory | ||
16 | // instead (64KB - 20KB). | ||
17 | #define BACKBUF ((u32*)(MEM_VRAM + KB(96) - KB(20))) | ||
18 | |||
19 | // The font data is located at the end of the frontbuffer memory, after the tile | ||
20 | // map and requires 8KB for 256 8x8 characters at 4bpp. This, along with the | ||
21 | // tilemap information allow us to store the frontbuffer and font for a text | ||
22 | // background in the first 2 charblocks (32KB). | ||
23 | #define FONT_DATA ((u32*)(MEM_VRAM + KB(22))) | ||
24 | #define FONT_TILEMAP ((u16*)(MEM_VRAM + KB(30))) | ||
25 | #define FONT_SB 15 | ||
12 | 26 | ||
13 | static u32 dirty_tiles[21] = {0}; | 27 | static u32 dirty_tiles[21] = {0}; |
14 | 28 | ||
@@ -29,7 +43,7 @@ draw_pixel(u16 x, u16 y, u8 color) { | |||
29 | 43 | ||
30 | // Update backbuffer. | 44 | // Update backbuffer. |
31 | size_t shift = start_col * sizeof(u32); | 45 | size_t shift = start_col * sizeof(u32); |
32 | BACKBUFFER[pos] = (BACKBUFFER[pos] & ~(0xF << shift)) | color << shift; | 46 | BACKBUF[pos] = (BACKBUF[pos] & ~(0xF << shift)) | color << shift; |
33 | 47 | ||
34 | // Mark tile as dirty. | 48 | // Mark tile as dirty. |
35 | dirty_tiles[tile_y] |= 1 << tile_x; | 49 | dirty_tiles[tile_y] |= 1 << tile_x; |
@@ -39,8 +53,8 @@ IWRAM_CODE | |||
39 | void | 53 | void |
40 | flip_buffer(void) { | 54 | flip_buffer(void) { |
41 | // Copy dirty tiles from the backbuffer to the frontbuffer. | 55 | // Copy dirty tiles from the backbuffer to the frontbuffer. |
42 | Tile *dst = FRONTBUFFER; | 56 | Tile *dst = FRONTBUF; |
43 | Tile *src = BACKBUFFER; | 57 | Tile *src = BACKBUF; |
44 | for (size_t j = 0; j < 20; ++j) { | 58 | for (size_t j = 0; j < 20; ++j) { |
45 | if (dirty_tiles[j] == 0) { | 59 | if (dirty_tiles[j] == 0) { |
46 | continue; | 60 | continue; |
@@ -54,19 +68,23 @@ flip_buffer(void) { | |||
54 | } | 68 | } |
55 | } | 69 | } |
56 | 70 | ||
71 | static u16 font_map[256] = {0}; | ||
72 | |||
57 | void | 73 | void |
58 | renderer_init(void) { | 74 | renderer_init(void) { |
59 | // Initialize display mode and bg palette. | 75 | // Initialize display mode and bg palette. |
60 | DISP_CTRL = DISP_MODE_0 | DISP_BG_0 | DISP_OBJ; | 76 | DISP_CTRL = DISP_MODE_0 | DISP_BG_0 | DISP_BG_1 | DISP_OBJ; |
61 | 77 | ||
62 | // Initialize backgrounds. | 78 | // Initialize backgrounds. |
63 | BG_CTRL(0) = BG_CHARBLOCK(0) | BG_SCREENBLOCK(FRONTBUFFER_SCREENBLOCK); | 79 | BG_CTRL(0) = BG_CHARBLOCK(0) | BG_SCREENBLOCK(FRONTBUF_SB) | BG_PRIORITY(1); |
80 | BG_CTRL(1) = BG_CHARBLOCK(1) | BG_SCREENBLOCK(FONT_SB) | BG_PRIORITY(0); | ||
64 | 81 | ||
65 | // TODO: Initialize other backgrounds if needed. | 82 | // Use DMA to clear front and back buffers as well as the font memory map. |
66 | 83 | dma_fill(FRONTBUF, 0, KB(20), 3); | |
67 | // Use DMA to clear front and back buffers. | 84 | dma_fill(FRONTBUF_TILEMAP, 0, KB(2), 3); |
68 | dma_fill(FRONTBUFFER, 0, KB(20), 3); | 85 | dma_fill(FONT_DATA, 0, KB(8), 3); |
69 | dma_fill(BACKBUFFER, 0, KB(20), 3); | 86 | dma_fill(FONT_TILEMAP, 0, KB(2), 3); |
87 | dma_fill(BACKBUF, 0, KB(20), 3); | ||
70 | 88 | ||
71 | // Initialize default palette. | 89 | // Initialize default palette. |
72 | PAL_BUFFER_BG[0] = COLOR_BLACK; | 90 | PAL_BUFFER_BG[0] = COLOR_BLACK; |
@@ -76,11 +94,18 @@ renderer_init(void) { | |||
76 | PAL_BUFFER_BG[4] = COLOR_CYAN; | 94 | PAL_BUFFER_BG[4] = COLOR_CYAN; |
77 | PAL_BUFFER_BG[5] = COLOR_GREY; | 95 | PAL_BUFFER_BG[5] = COLOR_GREY; |
78 | 96 | ||
79 | // Initialize background memory map. | 97 | // Initialize background memory map for frontbuffer and font backgorund. |
80 | for (size_t i = 0; i < 32 * 20; ++i) { | 98 | for (size_t i = 0; i < 32 * 20; ++i) { |
81 | FRONTBUFFER_TILEMAP[i] = i; | 99 | FRONTBUF_TILEMAP[i] = i; |
82 | } | 100 | } |
83 | 101 | ||
84 | // // Load font data into VRAM. | 102 | // Load font data into VRAM. |
85 | // unpack_tiles(&bd_font, FONT_DATA, 256); | 103 | unpack_tiles(&bd_font, FONT_DATA, 256); |
104 | |||
105 | // Initialize the font map translation table. That way we can write | ||
106 | // a character on the text background layer with: | ||
107 | // FONT_TILEMAP[tile_x + 32 * tile_y] = font_map['A']; | ||
108 | for (size_t i = 0; i < 256; ++i) { | ||
109 | font_map[i] = 192 + i; | ||
110 | } | ||
86 | } | 111 | } |