commit e8c1fe1

Devine Lu Linvega  ·  2025-01-20 00:33:04 +0000 UTC
parent f73141c
(screen.c) Improved screen_resize
1 files changed,  +16, -21
+16, -21
 1@@ -67,33 +67,28 @@ screen_resize(Uint16 width, Uint16 height, int scale)
 2 {
 3 	Uint8 *bg, *fg;
 4 	Uint32 *pixels = NULL;
 5-	int dim_change = uxn_screen.width != width || uxn_screen.height != height;
 6-	if(width < 0x8 || height < 0x8 || width >= 0x800 || height >= 0x800 || scale < 1 || scale >= 4)
 7-		return;
 8-	if(uxn_screen.width == width && uxn_screen.height == height && uxn_screen.scale == scale)
 9-		return;
10+	int dim_change;
11+	clamp(width, 8, 0x800);
12+	clamp(height, 8, 0x800);
13+	clamp(scale, 1, 3);
14+	dim_change = uxn_screen.width != width || uxn_screen.height != height;
15+	/* Pixels */
16+	pixels = realloc(uxn_screen.pixels, width * height * sizeof(Uint32) * scale * scale);
17+	if(!pixels) return;
18+	uxn_screen.pixels = pixels;
19+	uxn_screen.scale = scale;
20+	/* layers */
21 	if(dim_change) {
22-		int wmar = MAR2(width), hmar = MAR2(height);
23-		bg = malloc(wmar * hmar), fg = malloc(wmar * hmar);
24-		if(bg && fg)
25-			pixels = realloc(uxn_screen.pixels, width * height * sizeof(Uint32) * scale * scale);
26+		int i, length = MAR2(width) * MAR2(height);
27+		bg = malloc(length), fg = malloc(length);
28 		if(!bg || !fg || !pixels) {
29 			free(bg), free(fg);
30 			return;
31 		}
32 		free(uxn_screen.bg), free(uxn_screen.fg);
33-	} else {
34-		bg = uxn_screen.bg, fg = uxn_screen.fg;
35-		pixels = realloc(uxn_screen.pixels, width * height * sizeof(Uint32) * scale * scale);
36-		if(!pixels)
37-			return;
38-	}
39-	uxn_screen.bg = bg, uxn_screen.fg = fg;
40-	uxn_screen.pixels = pixels;
41-	uxn_screen.width = width, uxn_screen.height = height, uxn_screen.scale = scale;
42-	if(dim_change) {
43-		int i, len = MAR2(uxn_screen.width) * uxn_screen.height;
44-		for(i = 0; i < len; i++)
45+		uxn_screen.bg = bg, uxn_screen.fg = fg;
46+		uxn_screen.width = width, uxn_screen.height = height;
47+		for(i = 0; i < length; i++)
48 			uxn_screen.bg[i] = uxn_screen.fg[i] = 0;
49 	}
50 	emu_resize(width, height);