commit aec2e45

Devie Lu Linvega  ·  2025-12-23 17:12:08 +0000 UTC
parent d690545
(screen) Optimizations
1 files changed,  +36, -59
+36, -59
  1@@ -436,18 +436,6 @@ static const Uint8 blending[16][4] = {
  2 	{0, 3, 1, 2},
  3 };
  4 
  5-void emu_resize(void);
  6-
  7-static unsigned int
  8-screen_changed(void)
  9-{
 10-	clamp(uxn_screen.x1, 0, uxn_screen.width);
 11-	clamp(uxn_screen.y1, 0, uxn_screen.height);
 12-	clamp(uxn_screen.x2, 0, uxn_screen.width);
 13-	clamp(uxn_screen.y2, 0, uxn_screen.height);
 14-	return uxn_screen.x2 > uxn_screen.x1 && uxn_screen.y2 > uxn_screen.y1;
 15-}
 16-
 17 static void
 18 screen_change(const int x1, const int y1, const int x2, const int y2)
 19 {
 20@@ -475,7 +463,7 @@ screen_palette(void)
 21 }
 22 
 23 static void
 24-screen_apply(int width, int height)
 25+screen_resize(int width, int height)
 26 {
 27 	int length;
 28 	clamp(width, 8, 0x800);
 29@@ -492,8 +480,12 @@ screen_apply(int width, int height)
 30 }
 31 
 32 static void
 33-screen_redraw(void)
 34+emu_repaint(void)
 35 {
 36+	const int ax = uxn_screen.x1 * uxn_screen.zoom;
 37+	const int ay = uxn_screen.y1 * uxn_screen.zoom;
 38+	const int aw = uxn_screen.x2 * uxn_screen.zoom - ax;
 39+	const int ah = uxn_screen.y2 * uxn_screen.zoom - ay;
 40 	int i, x, y, k, l;
 41 	for(y = uxn_screen.y1; y < uxn_screen.y2; y++) {
 42 		const int ys = y * uxn_screen.zoom;
 43@@ -506,41 +498,8 @@ screen_redraw(void)
 44 			}
 45 		}
 46 	}
 47-	uxn_screen.x1 = uxn_screen.y1 = uxn_screen.x2 = uxn_screen.y2 = 0;
 48-}
 49-
 50-static void
 51-emu_repaint(void)
 52-{
 53-	const int x = uxn_screen.x1 * uxn_screen.zoom;
 54-	const int y = uxn_screen.y1 * uxn_screen.zoom;
 55-	const int w = uxn_screen.x2 * uxn_screen.zoom - x;
 56-	const int h = uxn_screen.y2 * uxn_screen.zoom - y;
 57-	screen_redraw();
 58-	XPutImage(display, window, DefaultGC(display, 0), ximage, x, y, x, y, w, h);
 59-}
 60-
 61-static void
 62-emu_repaint_all(void)
 63-{
 64-	uxn_screen.x1 = uxn_screen.y1 = 0;
 65-	uxn_screen.x2 = uxn_screen.width;
 66-	uxn_screen.y2 = uxn_screen.height;
 67-	emu_repaint();
 68-}
 69-
 70-void
 71-emu_resize(void)
 72-{
 73-	const int width = uxn_screen.width * uxn_screen.zoom;
 74-	const int height = uxn_screen.height * uxn_screen.zoom;
 75-	XResizeWindow(display, window, width, height);
 76-	uxn_screen.pixels = realloc(uxn_screen.pixels,
 77-		uxn_screen.width * uxn_screen.height * sizeof(unsigned int) * uxn_screen.zoom * uxn_screen.zoom);
 78-	if(ximage)
 79-		free(ximage);
 80-	ximage = XCreateImage(display, DefaultVisual(display, 0), DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)uxn_screen.pixels, width, height, 32, 0);
 81-	emu_repaint_all();
 82+	XPutImage(display, window, DefaultGC(display, 0), ximage, ax, ay, ax, ay, aw, ah);
 83+	uxn_screen.x1 = uxn_screen.y1 = uxn_screen.x2 = uxn_screen.y2 = uxn_screen.redraw = 0;
 84 }
 85 
 86 static void
 87@@ -548,12 +507,30 @@ screen_update()
 88 {
 89 	if(screen_vector)
 90 		uxn_eval(screen_vector);
 91-	if(uxn_screen.resized)
 92-		emu_resize(), uxn_screen.resized = 0;
 93-	if(uxn_screen.redraw)
 94-		emu_repaint_all(), uxn_screen.redraw = 0;
 95-	else if(screen_changed())
 96+	if(uxn_screen.resized) {
 97+		const int width = uxn_screen.width * uxn_screen.zoom;
 98+		const int height = uxn_screen.height * uxn_screen.zoom;
 99+		const int length = uxn_screen.width * uxn_screen.height * sizeof(unsigned int) * uxn_screen.zoom * uxn_screen.zoom;
100+		XResizeWindow(display, window, width, height);
101+		uxn_screen.pixels = realloc(uxn_screen.pixels, length);
102+		if(ximage)
103+			free(ximage);
104+		ximage = XCreateImage(display, DefaultVisual(display, 0), DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)uxn_screen.pixels, width, height, 32, 0);
105+		uxn_screen.redraw = 1;
106+		uxn_screen.resized = 0;
107+	}
108+	if(uxn_screen.redraw) {
109+		uxn_screen.x1 = uxn_screen.y1 = 0;
110+		uxn_screen.x2 = uxn_screen.width;
111+		uxn_screen.y2 = uxn_screen.height;
112+		emu_repaint();
113+	} else if(uxn_screen.x2 > uxn_screen.x1 && uxn_screen.y2 > uxn_screen.y1) {
114+		clamp(uxn_screen.x1, 0, uxn_screen.width);
115+		clamp(uxn_screen.y1, 0, uxn_screen.height);
116+		clamp(uxn_screen.x2, 0, uxn_screen.width);
117+		clamp(uxn_screen.y2, 0, uxn_screen.height);
118 		emu_repaint();
119+	}
120 }
121 
122 /* screen registers */
123@@ -1056,8 +1033,8 @@ emu_deo(const Uint8 port, const Uint8 value)
124 	case 0x1f: console_start_fork(); return;
125 	/* Screen */
126 	case 0x21: screen_vector = PEEK2(&dev[0x20]); return;
127-	case 0x23: screen_apply(PEEK2(&dev[0x22]), uxn_screen.height); return;
128-	case 0x25: screen_apply(uxn_screen.width, PEEK2(&dev[0x24])); return;
129+	case 0x23: screen_resize(PEEK2(&dev[0x22]), uxn_screen.height); return;
130+	case 0x25: screen_resize(uxn_screen.width, PEEK2(&dev[0x24])); return;
131 	case 0x26: rMX = dev[0x26] & 0x1, rMY = dev[0x26] & 0x2, rMA = dev[0x26] & 0x4, rML = dev[0x26] >> 4, rDX = rMX << 3, rDY = rMY << 2; return;
132 	case 0x28:
133 	case 0x29: rX = (dev[0x28] << 8) | dev[0x29], rX = twos(rX); return;
134@@ -1092,7 +1069,7 @@ static void
135 emu_restart(unsigned int soft)
136 {
137 	console_close();
138-	screen_apply(WIDTH, HEIGHT);
139+	screen_resize(WIDTH, HEIGHT);
140 	system_reboot(soft);
141 	uxn_eval(0x100);
142 }
143@@ -1258,13 +1235,13 @@ main(int argc, char **argv)
144 {
145 	int i = 1;
146 	if(argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v')
147-		return !fprintf(stdout, "Uxn11 - Varvara Emulator, 14 Dec 2025.\n");
148+		return !fprintf(stdout, "Uxn11 - Varvara Emulator, 23 Dec 2025.\n");
149 	else if(argc == 1)
150 		return !fprintf(stdout, "usage: %s [-v] file.rom [args..]\n", argv[0]);
151 	else if(!system_boot(argv[i++], argc > 2))
152 		return !fprintf(stdout, "Could not load %s.\n", argv[i - 1]);
153 	uxn_screen.zoom = 1;
154-	screen_apply(WIDTH, HEIGHT);
155+	screen_resize(WIDTH, HEIGHT);
156 	if(uxn_eval(0x100) && console_vector) {
157 		for(; i < argc; i++) {
158 			char *p = argv[i];