commit da59482

Devine Lu Linvega  ·  2023-04-14 17:33:51 +0000 UTC
parent 04096fc
(screen.c) Less indirections
1 files changed,  +22, -26
+22, -26
 1@@ -25,27 +25,29 @@ static Uint8 blending[4][16] = {
 2 	{2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2}};
 3 
 4 static void
 5-screen_fill(UxnScreen *p, Layer *layer, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2, Uint8 color)
 6+screen_fill(UxnScreen *s, Uint8 *pixels, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2, Uint8 color)
 7 {
 8-	int x, y;
 9-	for(y = y1; y < y2 && y < p->height; y++)
10-		for(x = x1; x < x2 && x < p->width; x++)
11-			layer->pixels[x + y * p->width] = color;
12+	int x, y, width = s->width, height = s->height;
13+	for(y = y1; y < y2 && y < height; y++)
14+		for(x = x1; x < x2 && x < width; x++)
15+			pixels[x + y * width] = color;
16 }
17 
18 static void
19-screen_blit(UxnScreen *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy, Uint8 twobpp)
20+screen_blit(UxnScreen *s, Uint8 *pixels, Uint16 x1, Uint16 y1, Uint8 *ram, Uint16 addr, Uint8 color, Uint8 flipx, Uint8 flipy, Uint8 twobpp)
21 {
22-	int v, h, opaque = (color % 5) || !color;
23+	int v, h, width = s->width, height = s->height, opaque = (color % 5) || !color;
24+	if(!color)
25+		return screen_fill(s, pixels, x1, y1, x1 + 8, y1 + 8, 0);
26 	for(v = 0; v < 8; v++) {
27-		Uint16 c = sprite[v] | (twobpp ? (sprite[v + 8] << 8) : 0);
28+		Uint16 c = ram[(addr + v) & 0xffff] | (twobpp ? (ram[(addr + v + 8) & 0xffff] << 8) : 0);
29+		Uint16 y = y1 + (flipy ? 7 - v : v);
30 		for(h = 7; h >= 0; --h, c >>= 1) {
31 			Uint8 ch = (c & 1) | ((c >> 7) & 2);
32 			if(opaque || ch) {
33-				Uint16 xx = x + (flipx ? 7 - h : h);
34-				Uint16 yy = y + (flipy ? 7 - v : v);
35-				if(xx < p->width && yy < p->height)
36-					layer->pixels[xx + yy * p->width] = blending[ch][color];
37+				Uint16 x = x1 + (flipx ? 7 - h : h);
38+				if(x < width && y < height)
39+					pixels[x + y * width] = blending[ch][color];
40 			}
41 		}
42 	}
43@@ -83,18 +85,19 @@ screen_resize(UxnScreen *p, Uint16 width, Uint16 height)
44 	p->pixels = pixels;
45 	p->width = width;
46 	p->height = height;
47-	screen_fill(p, &p->bg, 0, 0, p->width, p->height, 0);
48-	screen_fill(p, &p->fg, 0, 0, p->width, p->height, 0);
49+	screen_fill(p, p->bg.pixels, 0, 0, p->width, p->height, 0);
50+	screen_fill(p, p->fg.pixels, 0, 0, p->width, p->height, 0);
51 }
52 
53 void
54 screen_redraw(UxnScreen *p)
55 {
56-	Uint32 i, size = p->width * p->height, palette[16];
57+	Uint32 i, size = p->width * p->height, palette[16], *pixels = p->pixels;
58+	Uint8 *fg = p->fg.pixels, *bg = p->bg.pixels;
59 	for(i = 0; i < 16; i++)
60 		palette[i] = p->palette[(i >> 2) ? (i >> 2) : (i & 3)];
61 	for(i = 0; i < size; i++)
62-		p->pixels[i] = palette[p->fg.pixels[i] << 2 | p->bg.pixels[i]];
63+		pixels[i] = palette[fg[i] << 2 | bg[i]];
64 	p->fg.changed = p->bg.changed = 0;
65 }
66 
67@@ -132,7 +135,7 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
68 			Uint16 y2 = uxn_screen.height;
69 			if(ctrl & 0x10) x2 = x, x = 0;
70 			if(ctrl & 0x20) y2 = y, y = 0;
71-			screen_fill(&uxn_screen, layer, x, y, x2, y2, color);
72+			screen_fill(&uxn_screen, layer->pixels, x, y, x2, y2, color);
73 			layer->changed = 1;
74 		}
75 		/* pixel mode */
76@@ -159,16 +162,9 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
77 		Uint16 dx = (move & 0x1) << 3;
78 		Uint16 dy = (move & 0x2) << 2;
79 		Layer *layer = (ctrl & 0x40) ? &uxn_screen.fg : &uxn_screen.bg;
80-		if(addr > 0x10000 - ((length + 1) << (3 + twobpp)))
81-			return;
82 		for(i = 0; i <= length; i++) {
83-			if(!(ctrl & 0xf)) {
84-				Uint16 ex = x + dy * i, ey = y + dx * i;
85-				screen_fill(&uxn_screen, layer, ex, ey, ex + 8, ey + 8, 0);
86-			} else {
87-				screen_blit(&uxn_screen, layer, x + dy * i, y + dx * i, &ram[addr], ctrl & 0xf, ctrl & 0x10, ctrl & 0x20, twobpp);
88-				addr += (move & 0x04) << (1 + twobpp);
89-			}
90+			screen_blit(&uxn_screen, layer->pixels, x + dy * i, y + dx * i, ram, addr, ctrl & 0xf, ctrl & 0x10, ctrl & 0x20, twobpp);
91+			addr += (move & 0x04) << (1 + twobpp);
92 		}
93 		layer->changed = 1;
94 		if(move & 0x1) POKE2(d + 0x8, x + dx); /* auto x+8 */