Browse code

Split pixels into fg/bg

neauoire authored on 24/12/2021 17:59:18
Showing 3 changed files
... ...
@@ -58,21 +58,25 @@ ppu_palette(Ppu *p, Uint8 *addr)
58 58
 void
59 59
 ppu_resize(Ppu *p, Uint16 width, Uint16 height)
60 60
 {
61
-	Uint8 *pixels;
62
-	if(!(pixels = realloc(p->pixels, width * height / 2)))
61
+	Uint8 *bg, *fg;
62
+	if(!(bg = realloc(p->bg, width * height)))
63 63
 		return;
64
-	p->pixels = pixels;
64
+	if(!(fg = realloc(p->fg, width * height)))
65
+		return;
66
+	p->bg = bg;
67
+	p->fg = fg;
65 68
 	p->width = width;
66 69
 	p->height = height;
67
-	ppu_clear(p, CLEAR_BOTH);
70
+	ppu_clear(p, p->bg);
71
+	ppu_clear(p, p->fg);
68 72
 }
69 73
 
70 74
 void
71
-ppu_clear(Ppu *p, Uint8 mask)
75
+ppu_clear(Ppu *p, Uint8 *layer)
72 76
 {
73
-	Uint32 i, size = p->width * p->height / 2;
77
+	Uint32 i, size = p->width * p->height;
74 78
 	for(i = 0; i < size; ++i)
75
-		p->pixels[i] &= mask;
79
+		layer[i] = 0x00;
76 80
 }
77 81
 
78 82
 void
... ...
@@ -89,34 +93,27 @@ Uint8
89 93
 ppu_read(Ppu *p, Uint16 x, Uint16 y)
90 94
 {
91 95
 	if(x < p->width && y < p->height) {
92
-		Uint32 row = (x + y * p->width) >> 1;
93
-		Uint8 shift = (x & 0x1) << 2;
94
-		Uint8 pix = p->pixels[row] >> shift;
95
-		if(pix & 0x0c)
96
-			pix >>= 2;
97
-		return pix & 0x3;
96
+		Uint32 row = (x + y * p->width);
97
+		return p->fg[row] ? p->fg[row] : p->bg[row];
98 98
 	}
99 99
 	return 0x0;
100 100
 }
101 101
 
102 102
 void
103
-ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
103
+ppu_write(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color)
104 104
 {
105 105
 	if(x < p->width && y < p->height) {
106
-		Uint32 row = (x + y * p->width) >> 1;
107
-		Uint8 shift = ((x & 0x1) << 2) + (layer << 1);
108
-		Uint8 pix = p->pixels[row];
109
-		Uint8 mask = ~(0x3 << shift);
110
-		Uint8 pixnew = (pix & mask) + (color << shift);
111
-		if(pix != pixnew) {
112
-			p->pixels[row] = pixnew;
106
+		Uint32 row = (x + y * p->width);
107
+		Uint8 prev = layer[row];
108
+		if(color != prev) {
109
+			layer[row] = color;
113 110
 			p->reqdraw = 1;
114 111
 		}
115 112
 	}
116 113
 }
117 114
 
118 115
 void
119
-ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
116
+ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
120 117
 {
121 118
 	Uint16 v, h;
122 119
 	for(v = 0; v < 8; ++v)
... ...
@@ -132,7 +129,7 @@ ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Ui
132 129
 }
133 130
 
134 131
 void
135
-ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
132
+ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
136 133
 {
137 134
 	Uint16 v, h;
138 135
 	for(v = 0; v < 8; ++v)
... ...
@@ -156,24 +153,24 @@ ppu_debug(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
156 153
 	for(i = 0; i < 0x20; ++i) {
157 154
 		x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
158 155
 		/* working stack */
159
-		ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
160
-		ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
156
+		ppu_1bpp(p, p->fg, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
157
+		ppu_1bpp(p, p->fg, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
161 158
 		y = 0x28 + (i / 8 + 1) * 8;
162 159
 		b = memory[i];
163 160
 		/* return stack */
164
-		ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
165
-		ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 3, 0, 0);
161
+		ppu_1bpp(p, p->fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
162
+		ppu_1bpp(p, p->fg, x + 8, y, font[b & 0xf], 3, 0, 0);
166 163
 	}
167 164
 	/* return pointer */
168
-	ppu_1bpp(p, 1, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
169
-	ppu_1bpp(p, 1, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
165
+	ppu_1bpp(p, p->fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
166
+	ppu_1bpp(p, p->fg, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
170 167
 	/* guides */
171 168
 	for(x = 0; x < 0x10; ++x) {
172
-		ppu_write(p, 1, x, p->height / 2, 2);
173
-		ppu_write(p, 1, p->width - x, p->height / 2, 2);
174
-		ppu_write(p, 1, p->width / 2, p->height - x, 2);
175
-		ppu_write(p, 1, p->width / 2, x, 2);
176
-		ppu_write(p, 1, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
177
-		ppu_write(p, 1, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
169
+		ppu_write(p, p->fg, x, p->height / 2, 2);
170
+		ppu_write(p, p->fg, p->width - x, p->height / 2, 2);
171
+		ppu_write(p, p->fg, p->width / 2, p->height - x, 2);
172
+		ppu_write(p, p->fg, p->width / 2, x, 2);
173
+		ppu_write(p, p->fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
174
+		ppu_write(p, p->fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
178 175
 	}
179 176
 }
... ...
@@ -21,17 +21,17 @@ typedef unsigned short Uint16;
21 21
 typedef unsigned int Uint32;
22 22
 
23 23
 typedef struct Ppu {
24
-	Uint8 *pixels, reqdraw;
24
+	Uint8 *fg, *bg, reqdraw;
25 25
 	Uint16 width, height;
26 26
 	Uint32 palette[16];
27 27
 } Ppu;
28 28
 
29 29
 void ppu_palette(Ppu *p, Uint8 *addr);
30 30
 void ppu_resize(Ppu *p, Uint16 width, Uint16 height);
31
-void ppu_clear(Ppu *p, Uint8 layer);
31
+void ppu_clear(Ppu *p, Uint8 *layer);
32 32
 void ppu_redraw(Ppu *p, Uint32 *screen);
33 33
 Uint8 ppu_read(Ppu *p, Uint16 x, Uint16 y);
34
-void ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color);
35
-void ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
36
-void ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
34
+void ppu_write(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color);
35
+void ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
36
+void ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
37 37
 void ppu_debug(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory);
... ...
@@ -293,7 +293,7 @@ screen_deo(Device *d, Uint8 port)
293 293
 		Uint16 x = peek16(d->dat, 0x8);
294 294
 		Uint16 y = peek16(d->dat, 0xa);
295 295
 		Uint8 layer = d->dat[0xe] & 0x40;
296
-		ppu_write(&ppu, !!layer, x, y, d->dat[0xe] & 0x3);
296
+		ppu_write(&ppu, layer ? ppu.fg : ppu.bg, x, y, d->dat[0xe] & 0x3);
297 297
 		if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 1); /* auto x+1 */
298 298
 		if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */
299 299
 		break;
... ...
@@ -304,10 +304,10 @@ screen_deo(Device *d, Uint8 port)
304 304
 		Uint8 layer = d->dat[0xf] & 0x40;
305 305
 		Uint8 *addr = &d->mem[peek16(d->dat, 0xc)];
306 306
 		if(d->dat[0xf] & 0x80) {
307
-			ppu_2bpp(&ppu, !!layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
307
+			ppu_2bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
308 308
 			if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 16); /* auto addr+16 */
309 309
 		} else {
310
-			ppu_1bpp(&ppu, !!layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
310
+			ppu_1bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
311 311
 			if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 8); /* auto addr+8 */
312 312
 		}
313 313
 		if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 8); /* auto x+8 */
... ...
@@ -467,7 +467,7 @@ doctrl(Uxn *u, SDL_Event *event, int z)
467 467
 	case SDLK_LEFT: flag = 0x40; break;
468 468
 	case SDLK_RIGHT: flag = 0x80; break;
469 469
 	case SDLK_F1: if(z) set_zoom(zoom > 2 ? 1 : zoom + 1); break;
470
-	case SDLK_F2: if(z) devsystem->dat[0xe] = !devsystem->dat[0xe]; ppu_clear(&ppu, CLEAR_FG); break;
470
+	case SDLK_F2: if(z) devsystem->dat[0xe] = !devsystem->dat[0xe]; ppu_clear(&ppu, ppu.fg); break;
471 471
 	case SDLK_F3: if(z) capture_screen(); break;
472 472
 	case SDLK_AC_BACK:
473 473
 	case SDLK_F4: if(z) restart(u); break;