Browse code

Reverted to a standard SDL Texture for the screen

neauoire authored on 17/09/2021 02:48:00
Showing 3 changed files
... ...
@@ -20,15 +20,23 @@ static Uint8 blending[5][16] = {
20 20
 	{1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0}};
21 21
 
22 22
 void
23
-ppu_pixel(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
23
+ppu_pixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color)
24 24
 {
25
-	Uint8 *pixel = &p->pixels[y * p->width + x], shift = layer * 2;
26
-	if(x < p->width && y < p->height)
27
-		*pixel = (*pixel & ~(0x3 << shift)) | (color << shift);
25
+	int row = (y % 8) + ((x / 8 + y / 8 * p->width / 8) * 16), col = x % 8;
26
+	if(x >= p->width || y >= p->height)
27
+		return;
28
+	if(color == 0 || color == 2)
29
+		layer[row] &= ~(1UL << (7 - col));
30
+	else
31
+		layer[row] |= 1UL << (7 - col);
32
+	if(color == 0 || color == 1)
33
+		layer[row + 8] &= ~(1UL << (7 - col));
34
+	else
35
+		layer[row + 8] |= 1UL << (7 - col);
28 36
 }
29 37
 
30 38
 void
31
-ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
39
+ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
32 40
 {
33 41
 	Uint16 v, h;
34 42
 	for(v = 0; v < 8; v++)
... ...
@@ -44,7 +52,7 @@ ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Ui
44 52
 }
45 53
 
46 54
 void
47
-ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
55
+ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
48 56
 {
49 57
 	Uint16 v, h;
50 58
 	for(v = 0; v < 8; v++)
... ...
@@ -68,5 +76,7 @@ ppu_init(Ppu *p, Uint8 hor, Uint8 ver)
68 76
 {
69 77
 	p->width = 8 * hor;
70 78
 	p->height = 8 * ver;
79
+	p->bg = malloc(p->width / 4 * p->height * sizeof(Uint8));
80
+	p->fg = malloc(p->width / 4 * p->height * sizeof(Uint8));
71 81
 	return 1;
72 82
 }
... ...
@@ -19,10 +19,10 @@ typedef unsigned int Uint32;
19 19
 
20 20
 typedef struct Ppu {
21 21
 	Uint16 width, height;
22
-	Uint8 *pixels;
22
+	Uint8 *pixels, *bg, *fg;
23 23
 } Ppu;
24 24
 
25 25
 int ppu_init(Ppu *p, Uint8 hor, Uint8 ver);
26
-void ppu_pixel(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color);
27
-void ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
28
-void ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
26
+void ppu_pixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color);
27
+void ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
28
+void ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
... ...
@@ -25,14 +25,18 @@ WITH REGARD TO THIS SOFTWARE.
25 25
 #define POLYPHONY 4
26 26
 #define BENCH 0
27 27
 
28
-static SDL_AudioDeviceID audio_id;
29 28
 static SDL_Window *gWindow;
30
-static SDL_Surface *winSurface, *idxSurface, *rgbaSurface;
29
+static SDL_Surface *gSurface;
30
+static SDL_Texture *gTexture;
31
+static SDL_Renderer *gRenderer;
32
+static SDL_AudioDeviceID audio_id;
31 33
 static SDL_Rect gRect;
34
+
35
+/* devices */
32 36
 static Ppu ppu;
33 37
 static Apu apu[POLYPHONY];
34 38
 static Device *devsystem, *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole;
35
-static Uint32 stdin_event, audio0_event;
39
+static Uint32 *ppu_screen, stdin_event, audio0_event, palette[4];
36 40
 
37 41
 static Uint8 zoom = 1, reqdraw = 0;
38 42
 
... ...
@@ -86,71 +90,79 @@ inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
86 90
 	Uint8 i, x, y, b;
87 91
 	for(i = 0; i < 0x20; ++i) { /* stack */
88 92
 		x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
89
-		ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
90
-		ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
93
+		ppu_1bpp(p, ppu.fg, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
94
+		ppu_1bpp(p, ppu.fg, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
91 95
 	}
92 96
 	/* return pointer */
93
-	ppu_1bpp(p, 1, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
94
-	ppu_1bpp(p, 1, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
97
+	ppu_1bpp(p, ppu.fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
98
+	ppu_1bpp(p, ppu.fg, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
95 99
 	for(i = 0; i < 0x20; ++i) { /* memory */
96 100
 		x = ((i % 8) * 3 + 1) * 8, y = 0x38 + (i / 8 + 1) * 8, b = memory[i];
97
-		ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
98
-		ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 3, 0, 0);
101
+		ppu_1bpp(p, ppu.fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
102
+		ppu_1bpp(p, ppu.fg, x + 8, y, font[b & 0xf], 3, 0, 0);
99 103
 	}
100 104
 	for(x = 0; x < 0x10; ++x) { /* guides */
101
-		ppu_pixel(p, 1, x, p->height / 2, 2);
102
-		ppu_pixel(p, 1, p->width - x, p->height / 2, 2);
103
-		ppu_pixel(p, 1, p->width / 2, p->height - x, 2);
104
-		ppu_pixel(p, 1, p->width / 2, x, 2);
105
-		ppu_pixel(p, 1, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
106
-		ppu_pixel(p, 1, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
105
+		ppu_pixel(p, ppu.fg, x, p->height / 2, 2);
106
+		ppu_pixel(p, ppu.fg, p->width - x, p->height / 2, 2);
107
+		ppu_pixel(p, ppu.fg, p->width / 2, p->height - x, 2);
108
+		ppu_pixel(p, ppu.fg, p->width / 2, x, 2);
109
+		ppu_pixel(p, ppu.fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
110
+		ppu_pixel(p, ppu.fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
111
+	}
112
+}
113
+
114
+static Uint8
115
+get_pixel(int x, int y)
116
+{
117
+	int ch1, ch2, r = (y % 8) + ((x / 8 + y / 8 * ppu.width / 8) * 16);
118
+	ch1 = (ppu.fg[r] >> (7 - x % 8)) & 1;
119
+	ch2 = (ppu.fg[r + 8] >> (7 - x % 8)) & 1;
120
+	if(!ch1 && !ch2) {
121
+		ch1 = (ppu.bg[r] >> (7 - x % 8)) & 1;
122
+		ch2 = (ppu.bg[r + 8] >> (7 - x % 8)) & 1;
107 123
 	}
124
+	return ch1 + (ch2 << 1);
108 125
 }
109 126
 
110 127
 static void
111 128
 redraw(Uxn *u)
112 129
 {
130
+	Uint16 x, y;
113 131
 	if(devsystem->dat[0xe])
114 132
 		inspect(&ppu, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram.dat);
115
-	if(rgbaSurface == NULL)
116
-		SDL_BlitScaled(idxSurface, NULL, winSurface, &gRect);
117
-	else if(zoom == 1)
118
-		SDL_BlitSurface(idxSurface, NULL, winSurface, &gRect);
119
-	else {
120
-		SDL_BlitSurface(idxSurface, NULL, rgbaSurface, NULL);
121
-		SDL_BlitScaled(rgbaSurface, NULL, winSurface, &gRect);
122
-	}
123
-	SDL_UpdateWindowSurface(gWindow);
133
+	for(y = 0; y < ppu.height; ++y)
134
+		for(x = 0; x < ppu.width; ++x)
135
+			ppu_screen[x + y * ppu.width] = palette[get_pixel(x, y)];
136
+	SDL_UpdateTexture(gTexture, &gRect, ppu_screen, ppu.width * sizeof(Uint32));
137
+	SDL_RenderClear(gRenderer);
138
+	SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
139
+	SDL_RenderPresent(gRenderer);
124 140
 	reqdraw = 0;
125 141
 }
126 142
 
127 143
 static void
128
-toggledebug(Uxn *u)
144
+toggle_debug(Uxn *u)
129 145
 {
130 146
 	devsystem->dat[0xe] = !devsystem->dat[0xe];
131 147
 	redraw(u);
132 148
 }
133 149
 
134 150
 static void
135
-togglezoom(Uxn *u)
151
+toggle_zoom(Uxn *u)
136 152
 {
137 153
 	zoom = zoom == 3 ? 1 : zoom + 1;
138 154
 	SDL_SetWindowSize(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
139
-	winSurface = SDL_GetWindowSurface(gWindow);
140
-	gRect.x = zoom * PAD;
141
-	gRect.y = zoom * PAD;
142
-	gRect.w = zoom * ppu.width;
143
-	gRect.h = zoom * ppu.height;
155
+	gSurface = SDL_GetWindowSurface(gWindow);
144 156
 	redraw(u);
145 157
 }
146 158
 
147 159
 static void
148
-screencapture(void)
160
+capture_screen(void)
149 161
 {
150 162
 	time_t t = time(NULL);
151 163
 	char fname[64];
152 164
 	strftime(fname, sizeof(fname), "screenshot-%Y%m%d-%H%M%S.bmp", localtime(&t));
153
-	SDL_SaveBMP(winSurface, fname);
165
+	SDL_SaveBMP(gSurface, fname);
154 166
 	fprintf(stderr, "Saved %s\n", fname);
155 167
 }
156 168
 
... ...
@@ -158,9 +170,11 @@ static void
158 170
 quit(void)
159 171
 {
160 172
 	SDL_UnlockAudioDevice(audio_id);
161
-	SDL_FreeSurface(winSurface);
162
-	SDL_FreeSurface(idxSurface);
163
-	if(rgbaSurface) SDL_FreeSurface(rgbaSurface);
173
+	SDL_FreeSurface(gSurface);
174
+	SDL_DestroyTexture(gTexture);
175
+	gTexture = NULL;
176
+	SDL_DestroyRenderer(gRenderer);
177
+	gRenderer = NULL;
164 178
 	SDL_DestroyWindow(gWindow);
165 179
 	SDL_Quit();
166 180
 	exit(0);
... ...
@@ -191,27 +205,25 @@ init(void)
191 205
 	gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom, SDL_WINDOW_SHOWN);
192 206
 	if(gWindow == NULL)
193 207
 		return error("sdl_window", SDL_GetError());
194
-	winSurface = SDL_GetWindowSurface(gWindow);
195
-	if(winSurface == NULL)
208
+	gSurface = SDL_GetWindowSurface(gWindow);
209
+	if(gSurface == NULL)
196 210
 		return error("sdl_surface win", SDL_GetError());
197
-	idxSurface = SDL_CreateRGBSurfaceWithFormat(0, ppu.width, ppu.height, 8, SDL_PIXELFORMAT_INDEX8);
198
-	if(idxSurface == NULL || SDL_SetSurfaceBlendMode(idxSurface, SDL_BLENDMODE_NONE))
199
-		return error("sdl_surface idx", SDL_GetError());
200
-	if(SDL_MUSTLOCK(idxSurface) == SDL_TRUE)
201
-		return error("sdl_surface idx", "demands locking");
202
-	gRect.x = zoom * PAD;
203
-	gRect.y = zoom * PAD;
204
-	gRect.w = zoom * ppu.width;
205
-	gRect.h = 2 * ppu.height; /* force non-1:1 scaling for BlitScaled test */
206
-	if(SDL_BlitScaled(idxSurface, NULL, winSurface, &gRect) < 0) {
207
-		rgbaSurface = SDL_CreateRGBSurfaceWithFormat(0, ppu.width, ppu.height, 32, SDL_PIXELFORMAT_RGB24);
208
-		if(rgbaSurface == NULL || SDL_SetSurfaceBlendMode(rgbaSurface, SDL_BLENDMODE_NONE))
209
-			return error("sdl_surface rgba", SDL_GetError());
210
-	}
211
-	gRect.h = zoom * ppu.height;
212
-	ppu.pixels = idxSurface->pixels;
211
+	gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
212
+	if(gRenderer == NULL)
213
+		return error("sdl_renderer", SDL_GetError());
214
+	SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
215
+	gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
216
+	if(gTexture == NULL || SDL_SetTextureBlendMode(gTexture, SDL_BLENDMODE_NONE))
217
+		return error("sdl_texture", SDL_GetError());
218
+	SDL_UpdateTexture(gTexture, NULL, ppu_screen, 4);
219
+	if(!(ppu_screen = malloc(ppu.width * ppu.height * sizeof(Uint32))))
220
+		return 0;
213 221
 	SDL_StartTextInput();
214 222
 	SDL_ShowCursor(SDL_DISABLE);
223
+	gRect.x = PAD;
224
+	gRect.y = PAD;
225
+	gRect.w = ppu.width;
226
+	gRect.h = ppu.height;
215 227
 	return 1;
216 228
 }
217 229
 
... ...
@@ -219,8 +231,8 @@ static void
219 231
 domouse(SDL_Event *event)
220 232
 {
221 233
 	Uint8 flag = 0x00;
222
-	Uint16 x = clamp(event->motion.x / zoom - PAD, 0, ppu.width - 1);
223
-	Uint16 y = clamp(event->motion.y / zoom - PAD, 0, ppu.height - 1);
234
+	Uint16 x = clamp(event->motion.x - PAD, 0, ppu.width - 1);
235
+	Uint16 y = clamp(event->motion.y - PAD, 0, ppu.height - 1);
224 236
 	poke16(devmouse->dat, 0x2, x);
225 237
 	poke16(devmouse->dat, 0x4, y);
226 238
 	switch(event->button.button) {
... ...
@@ -253,9 +265,9 @@ doctrl(Uxn *u, SDL_Event *event, int z)
253 265
 	case SDLK_DOWN: flag = 0x20; break;
254 266
 	case SDLK_LEFT: flag = 0x40; break;
255 267
 	case SDLK_RIGHT: flag = 0x80; break;
256
-	case SDLK_F1: if(z) togglezoom(u); break;
257
-	case SDLK_F2: if(z) toggledebug(u); break;
258
-	case SDLK_F3: if(z) screencapture(); break;
268
+	case SDLK_F1: if(z) toggle_zoom(u); break;
269
+	case SDLK_F2: if(z) toggle_debug(u); break;
270
+	case SDLK_F3: if(z) capture_screen(); break;
259 271
 	}
260 272
 	/* clang-format on */
261 273
 	if(z) {
... ...
@@ -268,22 +280,17 @@ doctrl(Uxn *u, SDL_Event *event, int z)
268 280
 		devctrl->dat[2] &= ~flag;
269 281
 }
270 282
 
271
-static void
272
-docolors(Device *d)
283
+void
284
+update_palette(Uint8 *addr)
273 285
 {
274
-	SDL_Color pal[16];
275 286
 	int i;
276 287
 	for(i = 0; i < 4; ++i) {
277
-		pal[i].r = ((d->dat[0x8 + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11;
278
-		pal[i].g = ((d->dat[0xa + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11;
279
-		pal[i].b = ((d->dat[0xc + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11;
280
-	}
281
-	for(i = 4; i < 16; ++i) {
282
-		pal[i].r = pal[i / 4].r;
283
-		pal[i].g = pal[i / 4].g;
284
-		pal[i].b = pal[i / 4].b;
288
+		Uint8
289
+			r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f,
290
+			g = (*(addr + 2 + i / 2) >> (!(i % 2) << 2)) & 0x0f,
291
+			b = (*(addr + 4 + i / 2) >> (!(i % 2) << 2)) & 0x0f;
292
+		palette[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b;
285 293
 	}
286
-	SDL_SetPaletteColors(idxSurface->format->palette, pal, 0, 16);
287 294
 	reqdraw = 1;
288 295
 }
289 296
 
... ...
@@ -303,7 +310,8 @@ system_talk(Device *d, Uint8 b0, Uint8 w)
303 310
 		case 0x3: d->u->rst.ptr = d->dat[0x3]; break;
304 311
 		case 0xf: return 0;
305 312
 		}
306
-		if(b0 > 0x7 && b0 < 0xe) docolors(d);
313
+		if(b0 > 0x7 && b0 < 0xe)
314
+			update_palette(&d->dat[0x8]);
307 315
 	}
308 316
 	return 1;
309 317
 }
... ...
@@ -323,7 +331,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
323 331
 		Uint16 x = peek16(d->dat, 0x8);
324 332
 		Uint16 y = peek16(d->dat, 0xa);
325 333
 		Uint8 layer = d->dat[0xe] & 0x40;
326
-		ppu_pixel(&ppu, !!layer, x, y, d->dat[0xe] & 0x3);
334
+		ppu_pixel(&ppu, layer ? ppu.fg : ppu.bg, x, y, d->dat[0xe] & 0x3);
327 335
 		if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 1); /* auto x+1 */
328 336
 		if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */
329 337
 		reqdraw = 1;
... ...
@@ -333,10 +341,10 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
333 341
 		Uint8 layer = d->dat[0xf] & 0x40;
334 342
 		Uint8 *addr = &d->mem[peek16(d->dat, 0xc)];
335 343
 		if(d->dat[0xf] & 0x80) {
336
-			ppu_2bpp(&ppu, !!layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
344
+			ppu_2bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
337 345
 			if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 16); /* auto addr+16 */
338 346
 		} else {
339
-			ppu_1bpp(&ppu, !!layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
347
+			ppu_1bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
340 348
 			if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 8); /* auto addr+8 */
341 349
 		}
342 350
 		if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 8); /* auto x+8 */
... ...
@@ -488,7 +496,7 @@ run(Uxn *u)
488 496
 				if(event.window.event == SDL_WINDOWEVENT_EXPOSED)
489 497
 					redraw(u);
490 498
 				else if(event.window.event == SDL_WINDOWEVENT_RESIZED)
491
-					winSurface = SDL_GetWindowSurface(gWindow);
499
+					gSurface = SDL_GetWindowSurface(gWindow);
492 500
 				break;
493 501
 			default:
494 502
 				if(event.type == stdin_event) {