commit cac4e5d
Devine Lu Linvega
·
2023-05-05 03:43:06 +0000 UTC
parent f181416
(screen) Faster implementation
3 files changed,
+65,
-57
+55,
-40
1@@ -25,18 +25,27 @@ 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 *s, Uint8 *pixels, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2, Uint8 color)
6+screen_change(int x1, int y1, int x2, int y2)
7 {
8- int x, y, width = s->width, height = s->height;
9+ if(x1 < uxn_screen.x1) uxn_screen.x1 = x1;
10+ if(y1 < uxn_screen.y1) uxn_screen.y1 = y1;
11+ if(x2 > uxn_screen.x2) uxn_screen.x2 = x2;
12+ if(y2 > uxn_screen.y2) uxn_screen.y2 = y2;
13+}
14+
15+static void
16+screen_fill(Uint8 *layer, int x1, int y1, int x2, int y2, int color)
17+{
18+ int x, y, width = uxn_screen.width, height = uxn_screen.height;
19 for(y = y1; y < y2 && y < height; y++)
20 for(x = x1; x < x2 && x < width; x++)
21- pixels[x + y * width] = color;
22+ layer[x + y * width] = color;
23 }
24
25 static void
26-screen_blit(UxnScreen *s, Uint8 *pixels, Uint16 x1, Uint16 y1, Uint8 *ram, Uint16 addr, Uint8 color, Uint8 flipx, Uint8 flipy, Uint8 twobpp)
27+screen_blit(Uint8 *layer, Uint8 *ram, Uint16 addr, int x1, int y1, int color, int flipx, int flipy, int twobpp)
28 {
29- int v, h, width = s->width, height = s->height, opaque = (color % 5) || !color;
30+ int v, h, width = uxn_screen.width, height = uxn_screen.height, opaque = (color % 5) || !color;
31 for(v = 0; v < 8; v++) {
32 Uint16 c = ram[(addr + v) & 0xffff] | (twobpp ? (ram[(addr + v + 8) & 0xffff] << 8) : 0);
33 Uint16 y = y1 + (flipy ? 7 - v : v);
34@@ -45,14 +54,14 @@ screen_blit(UxnScreen *s, Uint8 *pixels, Uint16 x1, Uint16 y1, Uint8 *ram, Uint1
35 if(opaque || ch) {
36 Uint16 x = x1 + (flipx ? 7 - h : h);
37 if(x < width && y < height)
38- pixels[x + y * width] = blending[ch][color];
39+ layer[x + y * width] = blending[ch][color];
40 }
41 }
42 }
43 }
44
45 void
46-screen_palette(UxnScreen *p, Uint8 *addr)
47+screen_palette(Uint8 *addr)
48 {
49 int i, shift;
50 for(i = 0, shift = 4; i < 4; ++i, shift ^= 4) {
51@@ -60,46 +69,52 @@ screen_palette(UxnScreen *p, Uint8 *addr)
52 r = (addr[0 + i / 2] >> shift) & 0xf,
53 g = (addr[2 + i / 2] >> shift) & 0xf,
54 b = (addr[4 + i / 2] >> shift) & 0xf;
55- p->palette[i] = 0x0f000000 | r << 16 | g << 8 | b;
56- p->palette[i] |= p->palette[i] << 4;
57+ uxn_screen.palette[i] = 0x0f000000 | r << 16 | g << 8 | b;
58+ uxn_screen.palette[i] |= uxn_screen.palette[i] << 4;
59 }
60- p->fg.changed = p->bg.changed = 1;
61+ screen_change(0, 0, uxn_screen.width, uxn_screen.height);
62 }
63
64 void
65-screen_resize(UxnScreen *p, Uint16 width, Uint16 height)
66+screen_resize(Uint16 width, Uint16 height)
67 {
68 Uint8 *bg, *fg;
69 Uint32 *pixels;
70 if(width < 0x8 || height < 0x8 || width >= 0x400 || height >= 0x400)
71 return;
72- bg = realloc(p->bg.pixels, width * height),
73- fg = realloc(p->fg.pixels, width * height);
74- pixels = realloc(p->pixels, width * height * sizeof(Uint32) * SCALE * SCALE);
75+ bg = realloc(uxn_screen.bg, width * height),
76+ fg = realloc(uxn_screen.fg, width * height);
77+ pixels = realloc(uxn_screen.pixels, width * height * sizeof(Uint32));
78 if(!bg || !fg || !pixels)
79 return;
80- p->bg.pixels = bg;
81- p->fg.pixels = fg;
82- p->pixels = pixels;
83- p->width = width;
84- p->height = height;
85- screen_fill(p, p->bg.pixels, 0, 0, p->width, p->height, 0);
86- screen_fill(p, p->fg.pixels, 0, 0, p->width, p->height, 0);
87+ uxn_screen.bg = bg;
88+ uxn_screen.fg = fg;
89+ uxn_screen.pixels = pixels;
90+ uxn_screen.width = width;
91+ uxn_screen.height = height;
92+ screen_fill(uxn_screen.bg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
93+ screen_fill(uxn_screen.fg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
94 }
95
96 void
97-screen_redraw(UxnScreen *p)
98+screen_redraw(void)
99 {
100- Uint8 *fg = p->fg.pixels, *bg = p->bg.pixels;
101- Uint32 i, j, x, y, w = p->width * SCALE, h = p->height * SCALE, palette[16], *pixels = p->pixels;
102+ Uint8 *fg = uxn_screen.fg, *bg = uxn_screen.bg;
103+ Uint32 palette[16], *pixels = uxn_screen.pixels;
104+ int i, x, y, w = uxn_screen.width, h = uxn_screen.height;
105+ int x1 = uxn_screen.x1;
106+ int y1 = uxn_screen.y1;
107+ int x2 = uxn_screen.x2 > w ? w : uxn_screen.x2;
108+ int y2 = uxn_screen.y2 > h ? h : uxn_screen.y2;
109 for(i = 0; i < 16; i++)
110- palette[i] = p->palette[(i >> 2) ? (i >> 2) : (i & 3)];
111- for(y = 0; y < h; y++)
112- for(x = 0; x < w; x++) {
113- j = ((x / SCALE) + (y / SCALE) * p->width);
114- pixels[x + y * w] = palette[fg[j] << 2 | bg[j]];
115+ palette[i] = uxn_screen.palette[(i >> 2) ? (i >> 2) : (i & 3)];
116+ for(y = y1; y < y2; y++)
117+ for(x = x1; x < x2; x++) {
118+ i = x + y * w;
119+ pixels[i] = palette[fg[i] << 2 | bg[i]];
120 }
121- p->fg.changed = p->bg.changed = 0;
122+ uxn_screen.x1 = uxn_screen.y1 = 0xffff;
123+ uxn_screen.x2 = uxn_screen.y2 = 0;
124 }
125
126 Uint8
127@@ -119,33 +134,33 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
128 {
129 switch(port) {
130 case 0x3:
131- screen_resize(&uxn_screen, PEEK2(d + 2), uxn_screen.height);
132+ screen_resize(PEEK2(d + 2), uxn_screen.height);
133 break;
134 case 0x5:
135- screen_resize(&uxn_screen, uxn_screen.width, PEEK2(d + 4));
136+ screen_resize(uxn_screen.width, PEEK2(d + 4));
137 break;
138 case 0xe: {
139 Uint8 ctrl = d[0xe];
140 Uint8 color = ctrl & 0x3;
141 Uint16 x = PEEK2(d + 0x8);
142 Uint16 y = PEEK2(d + 0xa);
143- Layer *layer = (ctrl & 0x40) ? &uxn_screen.fg : &uxn_screen.bg;
144+ Uint8 *layer = (ctrl & 0x40) ? uxn_screen.fg : uxn_screen.bg;
145 /* fill mode */
146 if(ctrl & 0x80) {
147 Uint16 x2 = uxn_screen.width;
148 Uint16 y2 = uxn_screen.height;
149 if(ctrl & 0x10) x2 = x, x = 0;
150 if(ctrl & 0x20) y2 = y, y = 0;
151- screen_fill(&uxn_screen, layer->pixels, x, y, x2, y2, color);
152- layer->changed = 1;
153+ screen_fill(layer, x, y, x2, y2, color);
154+ screen_change(x, y, x2, y2);
155 }
156 /* pixel mode */
157 else {
158 Uint16 width = uxn_screen.width;
159 Uint16 height = uxn_screen.height;
160 if(x < width && y < height)
161- layer->pixels[x + y * width] = color;
162- layer->changed = 1;
163+ layer[x + y * width] = color;
164+ screen_change(x, y, x + 1, y + 1);
165 if(d[0x6] & 0x1) POKE2(d + 0x8, x + 1); /* auto x+1 */
166 if(d[0x6] & 0x2) POKE2(d + 0xa, y + 1); /* auto y+1 */
167 }
168@@ -162,12 +177,12 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
169 Uint16 addr = PEEK2(d + 0xc);
170 Uint16 dx = (move & 0x1) << 3;
171 Uint16 dy = (move & 0x2) << 2;
172- Layer *layer = (ctrl & 0x40) ? &uxn_screen.fg : &uxn_screen.bg;
173+ Uint8 *layer = (ctrl & 0x40) ? uxn_screen.fg : uxn_screen.bg;
174 for(i = 0; i <= length; i++) {
175- screen_blit(&uxn_screen, layer->pixels, x + dy * i, y + dx * i, ram, addr, ctrl & 0xf, ctrl & 0x10, ctrl & 0x20, twobpp);
176+ screen_blit(layer, ram, addr, x + dy * i, y + dx * i, ctrl & 0xf, ctrl & 0x10, ctrl & 0x20, twobpp);
177 addr += (move & 0x04) << (1 + twobpp);
178 }
179- layer->changed = 1;
180+ screen_change(x, y, x + dy * length + 8, y + dx * length + 8);
181 if(move & 0x1) POKE2(d + 0x8, x + dx); /* auto x+8 */
182 if(move & 0x2) POKE2(d + 0xa, y + dy); /* auto y+8 */
183 if(move & 0x4) POKE2(d + 0xc, addr); /* auto addr+length */
+5,
-13
1@@ -10,23 +10,15 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2 WITH REGARD TO THIS SOFTWARE.
3 */
4
5-#define SCALE 2
6-
7-typedef struct Layer {
8- Uint8 *pixels, changed;
9-} Layer;
10-
11 typedef struct UxnScreen {
12+ int width, height, x1, y1, x2, y2;
13 Uint32 palette[4], *pixels;
14- Uint16 width, height;
15- Layer fg, bg;
16+ Uint8 *fg, *bg;
17 } UxnScreen;
18
19 extern UxnScreen uxn_screen;
20-
21-void screen_palette(UxnScreen *p, Uint8 *addr);
22-void screen_resize(UxnScreen *p, Uint16 width, Uint16 height);
23-void screen_redraw(UxnScreen *p);
24-
25+void screen_palette(Uint8 *addr);
26+void screen_resize(Uint16 width, Uint16 height);
27+void screen_redraw(void);
28 Uint8 screen_dei(Uxn *u, Uint8 addr);
29 void screen_deo(Uint8 *ram, Uint8 *d, Uint8 port);
+5,
-4
1@@ -33,6 +33,7 @@ static Window window;
2
3 char *rom_path;
4
5+#define SCALE 1
6 #define WIDTH (64 * 8)
7 #define HEIGHT (40 * 8)
8 #define PAD 4
9@@ -59,7 +60,7 @@ uxn_deo(Uxn *u, Uint8 addr)
10 case 0x00:
11 system_deo(u, &u->dev[d], p);
12 if(p > 0x7 && p < 0xe)
13- screen_palette(&uxn_screen, &u->dev[0x8]);
14+ screen_palette(&u->dev[0x8]);
15 break;
16 case 0x10: console_deo(&u->dev[d], p); break;
17 case 0x20: screen_deo(u->ram, &u->dev[d], p); break;
18@@ -71,7 +72,7 @@ uxn_deo(Uxn *u, Uint8 addr)
19 static void
20 emu_draw(void)
21 {
22- screen_redraw(&uxn_screen);
23+ screen_redraw();
24 XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, PAD, PAD, uxn_screen.width * SCALE, uxn_screen.height * SCALE);
25 }
26
27@@ -81,7 +82,7 @@ emu_start(Uxn *u, char *rom)
28 if(!system_load(u, rom))
29 return 0;
30 if(!uxn_screen.width || !uxn_screen.height)
31- screen_resize(&uxn_screen, WIDTH, HEIGHT);
32+ screen_resize(WIDTH, HEIGHT);
33 if(!uxn_eval(u, PAGE_PROGRAM))
34 return system_error("boot", "Failed to start rom.");
35 return 1;
36@@ -237,7 +238,7 @@ main(int argc, char **argv)
37 for(i = 0; i < n; i++)
38 console_input(&u, coninp[i], CONSOLE_STD);
39 }
40- if(uxn_screen.fg.changed || uxn_screen.bg.changed)
41+ if(uxn_screen.x2)
42 emu_draw();
43 }
44 XDestroyImage(ximage);