Browse code

(screen) Only repaint changed pixels

Devine Lu Linvega authored on 04/05/2023 18:15:16
Showing 4 changed files
... ...
@@ -24,6 +24,15 @@ static Uint8 blending[4][16] = {
24 24
 	{1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1},
25 25
 	{2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2}};
26 26
 
27
+static void
28
+screen_change(UxnScreen *s, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2)
29
+{
30
+	if(x1 < s->x1) s->x1 = x1;
31
+	if(y1 < s->y1) s->y1 = y1;
32
+	if(x2 > s->x2) s->x2 = x2;
33
+	if(y2 > s->y2) s->y2 = y2;
34
+}
35
+
27 36
 static void
28 37
 screen_fill(UxnScreen *s, Uint8 *pixels, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2, Uint8 color)
29 38
 {
... ...
@@ -63,7 +72,7 @@ screen_palette(UxnScreen *p, Uint8 *addr)
63 72
 		p->palette[i] = 0x0f000000 | r << 16 | g << 8 | b;
64 73
 		p->palette[i] |= p->palette[i] << 4;
65 74
 	}
66
-	p->fg.changed = p->bg.changed = 1;
75
+	screen_change(&uxn_screen, 0, 0, p->width, p->height);
67 76
 }
68 77
 
69 78
 void
... ...
@@ -90,13 +99,19 @@ screen_resize(UxnScreen *p, Uint16 width, Uint16 height)
90 99
 void
91 100
 screen_redraw(UxnScreen *p)
92 101
 {
93
-	Uint32 i, size = p->width * p->height, palette[16], *pixels = p->pixels;
102
+	Uint32 i, x, y, w = p->width, palette[16], *pixels = p->pixels;
94 103
 	Uint8 *fg = p->fg.pixels, *bg = p->bg.pixels;
104
+	int x1 = p->x1, y1 = p->y1;
105
+	int x2 = p->x2 > p->width ? p->width : p->x2, y2 = p->y2 > p->height ? p->height : p->y2;
95 106
 	for(i = 0; i < 16; i++)
96 107
 		palette[i] = p->palette[(i >> 2) ? (i >> 2) : (i & 3)];
97
-	for(i = 0; i < size; i++)
98
-		pixels[i] = palette[fg[i] << 2 | bg[i]];
99
-	p->fg.changed = p->bg.changed = 0;
108
+	for(y = y1; y < y2; y++)
109
+		for(x = x1; x < x2; x++) {
110
+			i = x + y * w;
111
+			pixels[i] = palette[fg[i] << 2 | bg[i]];
112
+		}
113
+	p->x1 = p->y1 = 0xffff;
114
+	p->x2 = p->y2 = 0;
100 115
 }
101 116
 
102 117
 Uint8
... ...
@@ -134,7 +149,7 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
134 149
 			if(ctrl & 0x10) x2 = x, x = 0;
135 150
 			if(ctrl & 0x20) y2 = y, y = 0;
136 151
 			screen_fill(&uxn_screen, layer->pixels, x, y, x2, y2, color);
137
-			layer->changed = 1;
152
+			screen_change(&uxn_screen, x, y, x2, y2);
138 153
 		}
139 154
 		/* pixel mode */
140 155
 		else {
... ...
@@ -142,7 +157,7 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
142 157
 			Uint16 height = uxn_screen.height;
143 158
 			if(x < width && y < height)
144 159
 				layer->pixels[x + y * width] = color;
145
-			layer->changed = 1;
160
+			screen_change(&uxn_screen, x, y, x + 1, y + 1);
146 161
 			if(d[0x6] & 0x1) POKE2(d + 0x8, x + 1); /* auto x+1 */
147 162
 			if(d[0x6] & 0x2) POKE2(d + 0xa, y + 1); /* auto y+1 */
148 163
 		}
... ...
@@ -164,7 +179,7 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
164 179
 			screen_blit(&uxn_screen, layer->pixels, x + dy * i, y + dx * i, ram, addr, ctrl & 0xf, ctrl & 0x10, ctrl & 0x20, twobpp);
165 180
 			addr += (move & 0x04) << (1 + twobpp);
166 181
 		}
167
-		layer->changed = 1;
182
+		screen_change(&uxn_screen, x, y, x + dy * length + 8, y + dx * length + 8);
168 183
 		if(move & 0x1) POKE2(d + 0x8, x + dx); /* auto x+8 */
169 184
 		if(move & 0x2) POKE2(d + 0xa, y + dy); /* auto y+8 */
170 185
 		if(move & 0x4) POKE2(d + 0xc, addr);   /* auto addr+length */
... ...
@@ -11,12 +11,12 @@ WITH REGARD TO THIS SOFTWARE.
11 11
 */
12 12
 
13 13
 typedef struct Layer {
14
-	Uint8 *pixels, changed;
14
+	Uint8 *pixels;
15 15
 } Layer;
16 16
 
17 17
 typedef struct UxnScreen {
18 18
 	Uint32 palette[4], *pixels;
19
-	Uint16 width, height;
19
+	Uint16 width, height, x1, y1, x2, y2;
20 20
 	Layer fg, bg;
21 21
 } UxnScreen;
22 22
 
... ...
@@ -84,7 +84,7 @@ system_deo(Uxn *u, Uint8 *d, Uint8 port)
84 84
 		system_cmd(u->ram, PEEK2(d + 2));
85 85
 		break;
86 86
 	case 0x5:
87
-		if(PEEK2(d + 4)){
87
+		if(PEEK2(d + 4)) {
88 88
 			Uxn friend;
89 89
 			uxn_boot(&friend, u->ram);
90 90
 			uxn_eval(&friend, PEEK2(d + 4));
... ...
@@ -262,7 +262,7 @@ start(Uxn *u, char *rom, int queue)
262 262
 static void
263 263
 set_zoom(Uint8 z)
264 264
 {
265
-	if (z >= 1) {
265
+	if(z >= 1) {
266 266
 		zoom = z;
267 267
 		set_window_size(gWindow, (uxn_screen.width + PAD * 2) * zoom, (uxn_screen.height + PAD * 2) * zoom);
268 268
 	}
... ...
@@ -467,7 +467,7 @@ run(Uxn *u)
467 467
 			return 0;
468 468
 		screen_vector = PEEK2(&u->dev[0x20]);
469 469
 		uxn_eval(u, screen_vector);
470
-		if(uxn_screen.fg.changed || uxn_screen.bg.changed)
470
+		if(uxn_screen.x2)
471 471
 			redraw();
472 472
 		now = SDL_GetPerformanceCounter();
473 473
 		if(screen_vector) {