aboutsummaryrefslogtreecommitdiffstats
path: root/src/ppu.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ppu.c')
-rw-r--r--src/ppu.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/src/ppu.c b/src/ppu.c
index 19e530b..a8d7dc6 100644
--- a/src/ppu.c
+++ b/src/ppu.c
@@ -21,7 +21,8 @@ static size_t screen_height = 0;
21static u32 *framebuffer = 0; 21static u32 *framebuffer = 0;
22 22
23static u32 palette[16]; 23static u32 palette[16];
24static u8 *pixels_buf; 24static u8 *pixels_fg;
25static u8 *pixels_bg;
25static u8 *dirty_lines; 26static u8 *dirty_lines;
26static u8 reqdraw = 0; 27static u8 reqdraw = 0;
27// TODO: Probably should consider this 28// TODO: Probably should consider this
@@ -35,17 +36,18 @@ static u8 blending[5][16] = {
35 {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0}}; 36 {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0}};
36 37
37void 38void
38ppu_pixel(u8 layer, u16 x, u16 y, u8 color) { 39ppu_pixel(u8 *layer, u16 x, u16 y, u8 color) {
39 size_t idx = y * screen_width + x; 40 if(x < screen_width && y < screen_height) {
40 u8 *pixel = &pixels_buf[idx], shift = layer * 2; 41 Uint32 i = x + y *screen_width;
41 if(x < screen_width && y < screen_height) { 42 if(color != layer[i]) {
42 *pixel = (*pixel & ~(0x3 << shift)) | (color << shift); 43 layer[i] = color;
43 } 44 }
45 }
44 dirty_lines[y] |= 1; 46 dirty_lines[y] |= 1;
45} 47}
46 48
47void 49void
48ppu_1bpp(u8 layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy) { 50ppu_1bpp(u8 *layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy) {
49 u16 v, h; 51 u16 v, h;
50 for(v = 0; v < 8; v++) 52 for(v = 0; v < 8; v++)
51 for(h = 0; h < 8; h++) { 53 for(h = 0; h < 8; h++) {
@@ -59,7 +61,7 @@ ppu_1bpp(u8 layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy) {
59} 61}
60 62
61void 63void
62ppu_2bpp(u8 layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy) { 64ppu_2bpp(u8 *layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy) {
63 u16 v, h; 65 u16 v, h;
64 for(v = 0; v < 8; v++) 66 for(v = 0; v < 8; v++)
65 for(h = 0; h < 8; h++) { 67 for(h = 0; h < 8; h++) {
@@ -106,7 +108,8 @@ ppu_init(void) {
106 } 108 }
107 109
108 // Allocate intermediate buffers. 110 // Allocate intermediate buffers.
109 pixels_buf = malloc(screen_width * screen_height); 111 pixels_fg = malloc(screen_width * screen_height);
112 pixels_bg = malloc(screen_width * screen_height);
110 dirty_lines = malloc(screen_height); 113 dirty_lines = malloc(screen_height);
111 114
112 // Initialize default palette. 115 // Initialize default palette.
@@ -116,7 +119,8 @@ ppu_init(void) {
116 palette[3] = 0xff7777; 119 palette[3] = 0xff7777;
117 120
118 // Clear pixel buffer memory. 121 // Clear pixel buffer memory.
119 memset(pixels_buf, 0, screen_width * screen_height); 122 memset(pixels_fg, 0, screen_width * screen_height);
123 memset(pixels_bg, 0, screen_width * screen_height);
120 memset(dirty_lines, 1, screen_height); 124 memset(dirty_lines, 1, screen_height);
121 125
122 // Make sure we perform an initial screen drawing. 126 // Make sure we perform an initial screen drawing.
@@ -135,7 +139,7 @@ blit_framebuffer(void) {
135 if (dirty_lines[j] != 0) { 139 if (dirty_lines[j] != 0) {
136 for (size_t i = 0; i < screen_width; i++) { 140 for (size_t i = 0; i < screen_width; i++) {
137 size_t idx = i + j * screen_width; 141 size_t idx = i + j * screen_width;
138 framebuffer[idx] = palette[pixels_buf[idx] % 16]; 142 framebuffer[idx] = palette[pixels_fg[idx] << 2 | pixels_bg[idx]];
139 } 143 }
140 } 144 }
141 dirty_lines[j] = 0; 145 dirty_lines[j] = 0;