Browse code

Converted PPU to use two textures; moved padding to src/emulator.c

Andrew Alderwick authored on 19/05/2021 22:17:58
Showing 3 changed files
... ...
@@ -41,12 +41,10 @@ readpixel(Uint8 *sprite, Uint8 h, Uint8 v)
41 41
 void
42 42
 clear(Ppu *p)
43 43
 {
44
-	int i, sz = p->height * p->width, rows = sz / 4;
45
-	for(i = 0; i < sz; ++i)
46
-		p->output[i] = p->colors[0];
47
-	for(i = 0; i < rows; i++) {
48
-		p->fg[i] = 0;
49
-		p->bg[i] = 0;
44
+	int i, sz = p->height * p->width;
45
+	for(i = 0; i < sz; ++i) {
46
+		p->fg.pixels[i] = p->fg.colors[0];
47
+		p->bg.pixels[i] = p->bg.colors[0];
50 48
 	}
51 49
 }
52 50
 
... ...
@@ -59,28 +57,23 @@ putcolors(Ppu *p, Uint8 *addr)
59 57
 			r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f,
60 58
 			g = (*(addr + 2 + i / 2) >> (!(i % 2) << 2)) & 0x0f,
61 59
 			b = (*(addr + 4 + i / 2) >> (!(i % 2) << 2)) & 0x0f;
62
-		p->colors[i] = (r << 20) + (r << 16) + (g << 12) + (g << 8) + (b << 4) + b;
60
+		p->bg.colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b;
61
+		p->fg.colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b;
63 62
 	}
63
+	p->fg.colors[0] = 0;
64
+	clear(p);
64 65
 }
65 66
 
66 67
 void
67
-putpixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color)
68
+putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color)
68 69
 {
69
-	Uint16 row = (y % 8) + ((x / 8 + y / 8 * p->hor) * 16), col = 7 - (x % 8);
70
-	if(x >= p->hor * 8 || y >= p->ver * 8 || row > (p->hor * p->ver * 16) - 8)
70
+	if(x >= p->width || y >= p->height)
71 71
 		return;
72
-	if(color == 0 || color == 2)
73
-		layer[row] &= ~(1UL << col);
74
-	else
75
-		layer[row] |= 1UL << col;
76
-	if(color == 0 || color == 1)
77
-		layer[row + 8] &= ~(1UL << col);
78
-	else
79
-		layer[row + 8] |= 1UL << col;
72
+	layer->pixels[y * p->width + x] = layer->colors[color];
80 73
 }
81 74
 
82 75
 void
83
-puticn(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
76
+puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
84 77
 {
85 78
 	Uint16 v, h;
86 79
 	for(v = 0; v < 8; v++)
... ...
@@ -96,7 +89,7 @@ puticn(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uin
96 89
 }
97 90
 
98 91
 void
99
-putchr(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
92
+putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
100 93
 {
101 94
 	Uint16 v, h;
102 95
 	for(v = 0; v < 8; v++)
... ...
@@ -113,63 +106,35 @@ putchr(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uin
113 106
 
114 107
 /* output */
115 108
 
116
-void
117
-drawpixel(Ppu *p, Uint16 x, Uint16 y, Uint8 color)
118
-{
119
-	if(x >= p->pad && x <= p->width - p->pad - 1 && y >= p->pad && y <= p->height - p->pad - 1)
120
-		p->output[y * p->width + x] = p->colors[color];
121
-}
122
-
123 109
 void
124 110
 drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr)
125 111
 {
126 112
 	Uint8 i, x, y, b;
127 113
 	for(i = 0; i < 0x20; ++i) { /* memory */
128 114
 		x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
129
-		puticn(p, p->bg, x, y, font[(b >> 4) & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
130
-		puticn(p, p->bg, x + 8, y, font[b & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
115
+		puticn(p, &p->bg, x, y, font[(b >> 4) & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
116
+		puticn(p, &p->bg, x + 8, y, font[b & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
131 117
 	}
132 118
 	for(x = 0; x < 0x20; ++x) {
133
-		drawpixel(p, x, p->height / 2, 2);
134
-		drawpixel(p, p->width - x, p->height / 2, 2);
135
-		drawpixel(p, p->width / 2, p->height - x, 2);
136
-		drawpixel(p, p->width / 2, x, 2);
137
-		drawpixel(p, p->width / 2 - 16 + x, p->height / 2, 2);
138
-		drawpixel(p, p->width / 2, p->height / 2 - 16 + x, 2);
119
+		putpixel(p, &p->bg, x, p->height / 2, 2);
120
+		putpixel(p, &p->bg, p->width - x, p->height / 2, 2);
121
+		putpixel(p, &p->bg, p->width / 2, p->height - x, 2);
122
+		putpixel(p, &p->bg, p->width / 2, x, 2);
123
+		putpixel(p, &p->bg, p->width / 2 - 16 + x, p->height / 2, 2);
124
+		putpixel(p, &p->bg, p->width / 2, p->height / 2 - 16 + x, 2);
139 125
 	}
140 126
 }
141 127
 
142
-void
143
-drawppu(Ppu *p)
144
-{
145
-	Uint16 x, y;
146
-	for(y = 0; y < p->ver; ++y)
147
-		for(x = 0; x < p->hor; ++x) {
148
-			Uint8 v, h;
149
-			Uint16 key = (y * p->hor + x) * 16;
150
-			for(v = 0; v < 8; v++)
151
-				for(h = 0; h < 8; h++) {
152
-					Uint8 color = readpixel(&p->fg[key], h, v);
153
-					if(color == 0)
154
-						color = readpixel(&p->bg[key], h, v);
155
-					drawpixel(p, x * 8 + p->pad + 7 - h, y * 8 + p->pad + v, color);
156
-				}
157
-		}
158
-}
159
-
160 128
 int
161
-initppu(Ppu *p, Uint8 hor, Uint8 ver, Uint8 pad)
129
+initppu(Ppu *p, Uint8 hor, Uint8 ver)
162 130
 {
163 131
 	p->hor = hor;
164 132
 	p->ver = ver;
165
-	p->pad = pad;
166
-	p->width = (8 * p->hor + p->pad * 2);
167
-	p->height = (8 * p->ver + p->pad * 2);
168
-	if(!(p->output = malloc(p->width * p->height * sizeof(Uint32))))
169
-		return 0;
170
-	if(!(p->bg = malloc(p->width * p->height * sizeof(Uint8) / 4)))
133
+	p->width = 8 * p->hor;
134
+	p->height = 8 * p->ver;
135
+	if(!(p->bg.pixels = malloc(p->width * p->height * sizeof(Uint32))))
171 136
 		return 0;
172
-	if(!(p->fg = malloc(p->width * p->height * sizeof(Uint8) / 4)))
137
+	if(!(p->fg.pixels = malloc(p->width * p->height * sizeof(Uint32))))
173 138
 		return 0;
174 139
 	clear(p);
175 140
 	return 1;
... ...
@@ -17,16 +17,18 @@ typedef unsigned char Uint8;
17 17
 typedef unsigned short Uint16;
18 18
 typedef unsigned int Uint32;
19 19
 
20
+typedef struct Layer {
21
+	Uint32 *pixels, colors[4];
22
+} Layer;
23
+
20 24
 typedef struct Ppu {
21
-	Uint8 *bg, *fg;
22
-	Uint16 hor, ver, pad, width, height;
23
-	Uint32 *output, colors[4];
25
+	Uint16 hor, ver, width, height;
26
+	Layer fg, bg;
24 27
 } Ppu;
25 28
 
26
-int initppu(Ppu *p, Uint8 hor, Uint8 ver, Uint8 pad);
29
+int initppu(Ppu *p, Uint8 hor, Uint8 ver);
27 30
 void putcolors(Ppu *p, Uint8 *addr);
28
-void putpixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color);
29
-void puticn(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
30
-void putchr(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
31
-void drawppu(Ppu *p);
31
+void putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color);
32
+void puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
33
+void putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
32 34
 void drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr);
... ...
@@ -20,12 +20,15 @@ WITH REGARD TO THIS SOFTWARE.
20 20
 static SDL_AudioDeviceID audio_id;
21 21
 static SDL_Window *gWindow;
22 22
 static SDL_Renderer *gRenderer;
23
-static SDL_Texture *gTexture;
23
+static SDL_Texture *fgTexture, *bgTexture;
24
+static SDL_Rect gRect;
24 25
 static Ppu ppu;
25 26
 static Apu apu[POLYPHONY];
26 27
 static Mpu mpu;
27 28
 static Device *devscreen, *devmouse, *devctrl, *devmidi, *devaudio0;
28 29
 
30
+#define PAD 16
31
+
29 32
 Uint8 zoom = 0, debug = 0, reqdraw = 0, bench = 0;
30 33
 
31 34
 int
... ...
@@ -53,14 +56,15 @@ audio_callback(void *u, Uint8 *stream, int len)
53 56
 }
54 57
 
55 58
 void
56
-redraw(Uint32 *dst, Uxn *u)
59
+redraw(Uxn *u)
57 60
 {
58
-	drawppu(&ppu);
59 61
 	if(debug)
60 62
 		drawdebugger(&ppu, u->wst.dat, u->wst.ptr);
61
-	SDL_UpdateTexture(gTexture, NULL, dst, ppu.width * sizeof(Uint32));
63
+	SDL_UpdateTexture(bgTexture, &gRect, ppu.bg.pixels, ppu.width * sizeof(Uint32));
64
+	SDL_UpdateTexture(fgTexture, &gRect, ppu.fg.pixels, ppu.width * sizeof(Uint32));
62 65
 	SDL_RenderClear(gRenderer);
63
-	SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
66
+	SDL_RenderCopy(gRenderer, bgTexture, NULL, NULL);
67
+	SDL_RenderCopy(gRenderer, fgTexture, NULL, NULL);
64 68
 	SDL_RenderPresent(gRenderer);
65 69
 	reqdraw = 0;
66 70
 }
... ...
@@ -69,26 +73,27 @@ void
69 73
 toggledebug(Uxn *u)
70 74
 {
71 75
 	debug = !debug;
72
-	redraw(ppu.output, u);
76
+	redraw(u);
73 77
 }
74 78
 
75 79
 void
76 80
 togglezoom(Uxn *u)
77 81
 {
78 82
 	zoom = zoom == 3 ? 1 : zoom + 1;
79
-	SDL_SetWindowSize(gWindow, ppu.width * zoom, ppu.height * zoom);
80
-	redraw(ppu.output, u);
83
+	SDL_SetWindowSize(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
84
+	redraw(u);
81 85
 }
82 86
 
83 87
 void
84 88
 quit(void)
85 89
 {
86
-	free(ppu.output);
87
-	free(ppu.fg);
88
-	free(ppu.bg);
90
+	free(ppu.fg.pixels);
91
+	free(ppu.bg.pixels);
89 92
 	SDL_UnlockAudioDevice(audio_id);
90
-	SDL_DestroyTexture(gTexture);
91
-	gTexture = NULL;
93
+	SDL_DestroyTexture(bgTexture);
94
+	bgTexture = NULL;
95
+	SDL_DestroyTexture(fgTexture);
96
+	fgTexture = NULL;
92 97
 	SDL_DestroyRenderer(gRenderer);
93 98
 	gRenderer = NULL;
94 99
 	SDL_DestroyWindow(gWindow);
... ...
@@ -101,22 +106,31 @@ int
101 106
 init(void)
102 107
 {
103 108
 	SDL_AudioSpec as;
104
-	if(!initppu(&ppu, 48, 32, 16))
109
+	if(!initppu(&ppu, 48, 32))
105 110
 		return error("PPU", "Init failure");
111
+	gRect.x = PAD;
112
+	gRect.y = PAD;
113
+	gRect.w = ppu.width;
114
+	gRect.h = ppu.height;
106 115
 	if(!initmpu(&mpu, 1))
107 116
 		return error("MPU", "Init failure");
108 117
 	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
109 118
 		return error("Init", SDL_GetError());
110
-	gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ppu.width * zoom, ppu.height * zoom, SDL_WINDOW_SHOWN);
119
+	gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom, SDL_WINDOW_SHOWN);
111 120
 	if(gWindow == NULL)
112 121
 		return error("Window", SDL_GetError());
113 122
 	gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
114 123
 	if(gRenderer == NULL)
115 124
 		return error("Renderer", SDL_GetError());
116
-	SDL_RenderSetLogicalSize(gRenderer, ppu.width, ppu.height);
117
-	gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width, ppu.height);
118
-	if(gTexture == NULL)
125
+	SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
126
+	bgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
127
+	if(bgTexture == NULL || SDL_SetTextureBlendMode(bgTexture, SDL_BLENDMODE_NONE))
128
+		return error("Texture", SDL_GetError());
129
+	fgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
130
+	if(fgTexture == NULL || SDL_SetTextureBlendMode(fgTexture, SDL_BLENDMODE_BLEND))
119 131
 		return error("Texture", SDL_GetError());
132
+	SDL_UpdateTexture(bgTexture, NULL, ppu.bg.pixels, 4);
133
+	SDL_UpdateTexture(fgTexture, NULL, ppu.fg.pixels, 4);
120 134
 	SDL_StartTextInput();
121 135
 	SDL_ShowCursor(SDL_DISABLE);
122 136
 	SDL_zero(as);
... ...
@@ -137,8 +151,8 @@ void
137 151
 domouse(SDL_Event *event)
138 152
 {
139 153
 	Uint8 flag = 0x00;
140
-	Uint16 x = clamp(event->motion.x - ppu.pad, 0, ppu.hor * 8 - 1);
141
-	Uint16 y = clamp(event->motion.y - ppu.pad, 0, ppu.ver * 8 - 1);
154
+	Uint16 x = clamp(event->motion.x - PAD, 0, ppu.hor * 8 - 1);
155
+	Uint16 y = clamp(event->motion.y - PAD, 0, ppu.ver * 8 - 1);
142 156
 	mempoke16(devmouse->dat, 0x2, x);
143 157
 	mempoke16(devmouse->dat, 0x4, y);
144 158
 	devmouse->dat[7] = 0x00;
... ...
@@ -223,7 +237,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
223 237
 		Uint16 x = mempeek16(d->dat, 0x8);
224 238
 		Uint16 y = mempeek16(d->dat, 0xa);
225 239
 		Uint8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
226
-		Uint8 *layer = d->dat[0xe] >> 4 & 0x1 ? ppu.fg : ppu.bg;
240
+		Layer *layer = d->dat[0xe] >> 4 & 0x1 ? &ppu.fg : &ppu.bg;
227 241
 		Uint8 mode = d->dat[0xe] >> 5;
228 242
 		if(!mode)
229 243
 			putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3);
... ...
@@ -319,11 +333,11 @@ int
319 333
 start(Uxn *u)
320 334
 {
321 335
 	evaluxn(u, 0x0100);
322
-	redraw(ppu.output, u);
336
+	redraw(u);
323 337
 	while(1) {
324 338
 		int i;
325 339
 		SDL_Event event;
326
-		double elapsed, start;
340
+		double elapsed, start = 0;
327 341
 		if(!bench)
328 342
 			start = SDL_GetPerformanceCounter();
329 343
 		while(SDL_PollEvent(&event) != 0) {
... ...
@@ -348,7 +362,7 @@ start(Uxn *u)
348 362
 				break;
349 363
 			case SDL_WINDOWEVENT:
350 364
 				if(event.window.event == SDL_WINDOWEVENT_EXPOSED)
351
-					redraw(ppu.output, u);
365
+					redraw(u);
352 366
 				break;
353 367
 			}
354 368
 		}
... ...
@@ -361,7 +375,7 @@ start(Uxn *u)
361 375
 		}
362 376
 		evaluxn(u, mempeek16(devscreen->dat, 0));
363 377
 		if(reqdraw)
364
-			redraw(ppu.output, u);
378
+			redraw(u);
365 379
 		if(!bench) {
366 380
 			elapsed = (SDL_GetPerformanceCounter() - start) / (double)SDL_GetPerformanceFrequency() * 1000.0f;
367 381
 			SDL_Delay(clamp(16.666f - elapsed, 0, 1000));