Browse code

ppu: separate reqdraw for fg and bg

Sigrid Solveig Haflínudóttir authored on 24/12/2021 19:45:31
Showing 3 changed files
... ...
@@ -50,31 +50,31 @@ ppu_palette(Ppu *p, Uint8 *addr)
50 50
 	}
51 51
 	for(i = 4; i < 16; ++i)
52 52
 		p->palette[i] = p->palette[i / 4];
53
-	p->reqdraw = 1;
53
+	p->fg.reqdraw = p->bg.reqdraw = 1;
54 54
 }
55 55
 
56 56
 void
57 57
 ppu_resize(Ppu *p, Uint16 width, Uint16 height)
58 58
 {
59 59
 	Uint8 
60
-	*bg = realloc(p->bg, width * height), 
61
-	*fg = realloc(p->fg, width * height);
60
+	*bg = realloc(p->bg.p, width * height),
61
+	*fg = realloc(p->fg.p, width * height);
62 62
 	if(!bg || !fg)
63 63
 		return;
64
-	p->bg = bg;
65
-	p->fg = fg;
64
+	p->bg.p = bg;
65
+	p->fg.p = fg;
66 66
 	p->width = width;
67 67
 	p->height = height;
68
-	ppu_clear(p, p->bg);
69
-	ppu_clear(p, p->fg);
68
+	ppu_clear(p, &p->bg);
69
+	ppu_clear(p, &p->fg);
70 70
 }
71 71
 
72 72
 void
73
-ppu_clear(Ppu *p, Uint8 *layer)
73
+ppu_clear(Ppu *p, Layer *layer)
74 74
 {
75 75
 	Uint32 i, size = p->width * p->height;
76 76
 	for(i = 0; i < size; ++i)
77
-		layer[i] = 0x00;
77
+		layer->p[i] = 0x00;
78 78
 }
79 79
 
80 80
 void
... ...
@@ -82,25 +82,25 @@ ppu_redraw(Ppu *p, Uint32 *screen)
82 82
 {
83 83
 	Uint32 i, size = p->width * p->height;
84 84
 	for(i = 0; i < size; ++i)
85
-		screen[i] = p->palette[p->fg[i] ? p->fg[i] : p->bg[i]];
86
-	p->reqdraw = 0;
85
+		screen[i] = p->palette[p->fg.p[i] ? p->fg.p[i] : p->bg.p[i]];
86
+	p->fg.reqdraw = p->bg.reqdraw = 0;
87 87
 }
88 88
 
89 89
 void
90
-ppu_write(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color)
90
+ppu_write(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color)
91 91
 {
92 92
 	if(x < p->width && y < p->height) {
93
-		Uint32 row = (x + y * p->width);
94
-		Uint8 prev = layer[row];
93
+		Uint32 i = (x + y * p->width);
94
+		Uint8 prev = layer->p[i];
95 95
 		if(color != prev) {
96
-			layer[row] = color;
97
-			p->reqdraw = 1;
96
+			layer->p[i] = color;
97
+			layer->reqdraw = 1;
98 98
 		}
99 99
 	}
100 100
 }
101 101
 
102 102
 void
103
-ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
103
+ppu_1bpp(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
104 104
 {
105 105
 	Uint16 v, h;
106 106
 	for(v = 0; v < 8; ++v)
... ...
@@ -116,7 +116,7 @@ ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U
116 116
 }
117 117
 
118 118
 void
119
-ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
119
+ppu_2bpp(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
120 120
 {
121 121
 	Uint16 v, h;
122 122
 	for(v = 0; v < 8; ++v)
... ...
@@ -140,24 +140,24 @@ ppu_debug(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
140 140
 	for(i = 0; i < 0x20; ++i) {
141 141
 		x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
142 142
 		/* working stack */
143
-		ppu_1bpp(p, p->fg, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
144
-		ppu_1bpp(p, p->fg, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
143
+		ppu_1bpp(p, &p->fg, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
144
+		ppu_1bpp(p, &p->fg, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
145 145
 		y = 0x28 + (i / 8 + 1) * 8;
146 146
 		b = memory[i];
147 147
 		/* return stack */
148
-		ppu_1bpp(p, p->fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
149
-		ppu_1bpp(p, p->fg, x + 8, y, font[b & 0xf], 3, 0, 0);
148
+		ppu_1bpp(p, &p->fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
149
+		ppu_1bpp(p, &p->fg, x + 8, y, font[b & 0xf], 3, 0, 0);
150 150
 	}
151 151
 	/* return pointer */
152
-	ppu_1bpp(p, p->fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
153
-	ppu_1bpp(p, p->fg, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
152
+	ppu_1bpp(p, &p->fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
153
+	ppu_1bpp(p, &p->fg, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
154 154
 	/* guides */
155 155
 	for(x = 0; x < 0x10; ++x) {
156
-		ppu_write(p, p->fg, x, p->height / 2, 2);
157
-		ppu_write(p, p->fg, p->width - x, p->height / 2, 2);
158
-		ppu_write(p, p->fg, p->width / 2, p->height - x, 2);
159
-		ppu_write(p, p->fg, p->width / 2, x, 2);
160
-		ppu_write(p, p->fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
161
-		ppu_write(p, p->fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
156
+		ppu_write(p, &p->fg, x, p->height / 2, 2);
157
+		ppu_write(p, &p->fg, p->width - x, p->height / 2, 2);
158
+		ppu_write(p, &p->fg, p->width / 2, p->height - x, 2);
159
+		ppu_write(p, &p->fg, p->width / 2, x, 2);
160
+		ppu_write(p, &p->fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
161
+		ppu_write(p, &p->fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
162 162
 	}
163 163
 }
... ...
@@ -20,18 +20,23 @@ typedef unsigned char Uint8;
20 20
 typedef unsigned short Uint16;
21 21
 typedef unsigned int Uint32;
22 22
 
23
+typedef struct Layer {
24
+	Uint8 *p;
25
+	Uint8 reqdraw;
26
+} Layer;
27
+
23 28
 typedef struct Ppu {
24
-	Uint8 *fg, *bg, reqdraw;
29
+	Layer fg, bg;
25 30
 	Uint16 width, height;
26 31
 	Uint32 palette[16];
27 32
 } Ppu;
28 33
 
29 34
 void ppu_palette(Ppu *p, Uint8 *addr);
30 35
 void ppu_resize(Ppu *p, Uint16 width, Uint16 height);
31
-void ppu_clear(Ppu *p, Uint8 *layer);
36
+void ppu_clear(Ppu *p, Layer *layer);
32 37
 void ppu_redraw(Ppu *p, Uint32 *screen);
33 38
 
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);
39
+void ppu_write(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color);
40
+void ppu_1bpp(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
41
+void ppu_2bpp(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
37 42
 void ppu_debug(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory);
... ...
@@ -105,7 +105,7 @@ set_zoom(Uint8 scale)
105 105
 	if(!gWindow)
106 106
 		return;
107 107
 	set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
108
-	ppu.reqdraw = 1;
108
+	ppu.fg.reqdraw = ppu.bg.reqdraw = 1;
109 109
 }
110 110
 
111 111
 static int
... ...
@@ -128,7 +128,7 @@ set_size(Uint16 width, Uint16 height, int is_resize)
128 128
 		return error("SDL_UpdateTexture", SDL_GetError());
129 129
 	if(is_resize)
130 130
 		set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
131
-	ppu.reqdraw = 1;
131
+	ppu.fg.reqdraw = ppu.bg.reqdraw = 1;
132 132
 	return 1;
133 133
 }
134 134
 
... ...
@@ -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 ? ppu.fg : ppu.bg, 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 ? ppu.fg : ppu.bg, 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 ? ppu.fg : ppu.bg, 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, ppu.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;
... ...
@@ -550,7 +550,7 @@ run(Uxn *u)
550 550
 		}
551 551
 	breakout:
552 552
 		uxn_eval(u, devscreen->vector);
553
-		if(ppu.reqdraw || devsystem->dat[0xe])
553
+		if(ppu.fg.reqdraw || ppu.bg.reqdraw || devsystem->dat[0xe])
554 554
 			redraw(u);
555 555
 		if(!BENCH) {
556 556
 			elapsed = (SDL_GetPerformanceCounter() - begin) / (double)SDL_GetPerformanceFrequency() * 1000.0f;