Browse code

Replaced duplicate parts of PPU init with resize functions

Andrew Alderwick authored on 19/09/2021 22:34:03
Showing 3 changed files
... ...
@@ -93,22 +93,13 @@ ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U
93 93
 /* output */
94 94
 
95 95
 int
96
-ppu_init(Ppu *p, Uint8 hor, Uint8 ver)
96
+ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
97 97
 {
98
-	p->width = 8 * hor;
99
-	p->height = 8 * ver;
100
-	p->bg = calloc(1, p->width / 4 * p->height * sizeof(Uint8));
101
-	p->fg = calloc(1, p->width / 4 * p->height * sizeof(Uint8));
102
-	ppu_clear(p);
103
-	return p->bg && p->fg;
104
-}
105
-
106
-int
107
-ppu_resize(Ppu *p, Uint8 hor, Uint8 ver)
108
-{
109
-	ppu_clear(p);
110
-	p->width = 8 * hor;
111
-	p->height = 8 * ver;
98
+	/* round width and height up to nearest multiple of 8 */
99
+	width = ((width - 1) | 0x7) + 1;
100
+	height = ((height - 1) | 0x7) + 1;
101
+	p->width = width;
102
+	p->height = height;
112 103
 	p->bg = realloc(p->bg, p->width / 4 * p->height * sizeof(Uint8));
113 104
 	p->fg = realloc(p->fg, p->width / 4 * p->height * sizeof(Uint8));
114 105
 	ppu_clear(p);
... ...
@@ -22,8 +22,7 @@ typedef struct Ppu {
22 22
 	Uint8 *bg, *fg;
23 23
 } Ppu;
24 24
 
25
-int ppu_init(Ppu *p, Uint8 hor, Uint8 ver);
26
-int ppu_resize(Ppu *p, Uint8 hor, Uint8 ver);
25
+int ppu_set_size(Ppu *p, Uint16 width, Uint16 height);
27 26
 int ppu_pixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color);
28 27
 int ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
29 28
 int ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
... ...
@@ -185,9 +185,32 @@ quit(void)
185 185
 	exit(0);
186 186
 }
187 187
 
188
+static int
189
+set_size(Uint16 width, Uint16 height, int is_resize)
190
+{
191
+	ppu_set_size(&ppu, width, height);
192
+	gRect.x = PAD;
193
+	gRect.y = PAD;
194
+	gRect.w = ppu.width;
195
+	gRect.h = ppu.height;
196
+	if(!(ppu_screen = realloc(ppu_screen, ppu.width * ppu.height * sizeof(Uint32))))
197
+		return error("ppu_screen", "Memory failure");
198
+	memset(ppu_screen, 0, ppu.width * ppu.height * sizeof(Uint32));
199
+	if(gTexture != NULL) SDL_DestroyTexture(gTexture);
200
+	SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
201
+	gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
202
+	if(gTexture == NULL || SDL_SetTextureBlendMode(gTexture, SDL_BLENDMODE_NONE))
203
+		return error("sdl_texture", SDL_GetError());
204
+	SDL_UpdateTexture(gTexture, NULL, ppu_screen, sizeof(Uint32));
205
+	if(is_resize) SDL_SetWindowSize(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
206
+	reqdraw = 1;
207
+	return 1;
208
+}
209
+
188 210
 static int
189 211
 init(void)
190 212
 {
213
+	const Uint16 width = 64 * 8, height = 40 * 8;
191 214
 	SDL_AudioSpec as;
192 215
 	SDL_zero(as);
193 216
 	as.freq = SAMPLE_FREQUENCY;
... ...
@@ -196,8 +219,6 @@ init(void)
196 219
 	as.callback = audio_callback;
197 220
 	as.samples = 512;
198 221
 	as.userdata = NULL;
199
-	if(!ppu_init(&ppu, 64, 40))
200
-		return error("ppu", "Init failure");
201 222
 	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
202 223
 		error("sdl", SDL_GetError());
203 224
 		if(SDL_Init(SDL_INIT_VIDEO) < 0)
... ...
@@ -207,25 +228,16 @@ init(void)
207 228
 		if(!audio_id)
208 229
 			error("sdl_audio", SDL_GetError());
209 230
 	}
210
-	gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom, SDL_WINDOW_SHOWN);
231
+	gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (width + PAD * 2) * zoom, (height + PAD * 2) * zoom, SDL_WINDOW_SHOWN);
211 232
 	if(gWindow == NULL)
212 233
 		return error("sdl_window", SDL_GetError());
213 234
 	gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
214 235
 	if(gRenderer == NULL)
215 236
 		return error("sdl_renderer", SDL_GetError());
216
-	SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
217
-	gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
218
-	if(gTexture == NULL || SDL_SetTextureBlendMode(gTexture, SDL_BLENDMODE_NONE))
219
-		return error("sdl_texture", SDL_GetError());
220
-	if(!(ppu_screen = calloc(1, ppu.width * ppu.height * sizeof(Uint32))))
237
+	if(!set_size(width, height, 0))
221 238
 		return 0;
222
-	SDL_UpdateTexture(gTexture, NULL, ppu_screen, sizeof(Uint32));
223 239
 	SDL_StartTextInput();
224 240
 	SDL_ShowCursor(SDL_DISABLE);
225
-	gRect.x = PAD;
226
-	gRect.y = PAD;
227
-	gRect.w = ppu.width;
228
-	gRect.h = ppu.height;
229 241
 	return 1;
230 242
 }
231 243
 
... ...
@@ -296,21 +308,6 @@ update_palette(Uint8 *addr)
296 308
 	reqdraw = 1;
297 309
 }
298 310
 
299
-void
300
-set_size(Uint16 width, Uint16 height)
301
-{
302
-	ppu_resize(&ppu, width / 8, height / 8);
303
-	gRect.w = ppu.width;
304
-	gRect.h = ppu.height;
305
-	if(!(ppu_screen = realloc(ppu_screen, ppu.width * ppu.height * sizeof(Uint32))))
306
-		return;
307
-	SDL_DestroyTexture(gTexture);
308
-	SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
309
-	gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
310
-	SDL_SetWindowSize(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
311
-	reqdraw = 1;
312
-}
313
-
314 311
 #pragma mark - Devices
315 312
 
316 313
 static int
... ...
@@ -353,7 +350,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
353 350
 	else
354 351
 		switch(b0) {
355 352
 		case 0x5:
356
-			if(!FIXED_SIZE) set_size(peek16(d->dat, 0x2), peek16(d->dat, 0x4));
353
+			if(!FIXED_SIZE) return set_size(peek16(d->dat, 0x2), peek16(d->dat, 0x4), 1);
357 354
 			break;
358 355
 		case 0xe: {
359 356
 			Uint16 x = peek16(d->dat, 0x8);