commit c75f75e
Devine Lu Linvega
·
2024-11-20 00:25:37 +0000 UTC
parent 1a84d39
(screen.c) Macroed boundary checking
1 files changed,
+19,
-34
+19,
-34
1@@ -17,6 +17,17 @@ WITH REGARD TO THIS SOFTWARE.
2
3 UxnScreen uxn_screen;
4
5+#define BOUNDARY(a, b, c, d, e) \
6+ { \
7+ if(a > 0xfff8) { \
8+ b = 0, c = 0 - a; \
9+ if(d > 0x8000) d = 0; \
10+ } else if(a > e) \
11+ return; \
12+ else if(a > e - 8) \
13+ d = e; \
14+ }
15+
16 /* c = !ch ? (color % 5 ? color >> 2 : 0) : color % 4 + ch == 1 ? 0 : (ch - 2 + (color & 3)) % 3 + 1; */
17
18 static Uint8 blending[4][16] = {
19@@ -45,42 +56,16 @@ screen_2bpp(Uint8 *layer, Uint8 *addr, Uint16 x1, Uint16 y1, Uint16 color, int f
20 static void
21 screen_1bpp(Uint8 *layer, Uint8 *addr, Uint16 x1, Uint16 y1, Uint16 color, int fx, int fy)
22 {
23- int x, y;
24- Uint16 top = y1, bottom = y1 + 8, left = x1, right = x1 + 8, offset2 = 0, offset3 = 0;
25- int row, opaque = (color % 5);
26- /* find vertical bounds */
27- if(y1 > 0xfff8){
28- top = 0;
29- if(bottom > 0x8000) bottom = 0;
30- offset2 = 0 - y1;
31- }
32- else if(y1 > uxn_screen.height)
33- return;
34- else if(y1 > uxn_screen.height - 8){
35- bottom = uxn_screen.height;
36- }
37-
38- /* find hor bounds */
39- if(x1 > 0xfff8){
40- left = 0;
41- if(right > 0x8000) right = 0;
42- offset3 = 0 - x1;
43- }
44- else if(x1 > uxn_screen.width)
45- return;
46- else if(x1 > uxn_screen.width - 8){
47- right = uxn_screen.width;
48- }
49-
50+ int x, y, opaque = (color % 5);
51+ Uint16 top = y1, bottom = y1 + 8, left = x1, right = x1 + 8, offx = 0, offy = 0;
52+ BOUNDARY(y1, top, offy, bottom, uxn_screen.height)
53+ BOUNDARY(x1, left, offx, right, uxn_screen.width)
54 for(y = top; y < bottom; y++) {
55- int yi = y-top;
56- int offset = fy < 0 ? 7 - yi : yi;
57- int byte = addr[offset+(offset2*fy)];
58- row = y * uxn_screen.width;
59+ int byte = addr[fy < 0 ? 7 - (y - top) : y - top + offy * fy];
60+ int row = y * uxn_screen.width;
61 for(x = left; x < right; x++) {
62- int xi = x - left + offset3;
63- int offx = fx > 0 ? (7-xi) : xi;
64- Uint8 ch = (byte >> offx) & 1;
65+ int shift = (fx > 0 ? 7 - (x - left) : x - left) + offx;
66+ int ch = (byte >> shift) & 1;
67 if((opaque || ch))
68 layer[x + row] = blending[ch][color];
69 }