add missing tools/pngdump to the repo
[gbajam22] / src / polyfill.c
1 /*
2 gbajam22 entry for the Gameboy Advance
3 Copyright (C) 2022  John Tsiombikas <nuclear@mutantstargoat.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <string.h>
19 #include "polyfill.h"
20 #include "debug.h"
21
22 static unsigned char *fb;
23 static int fbwidth, fbheight;
24 static short scantab[2][160];
25
26 void polyfill_framebuffer(void *ptr, int w, int h)
27 {
28         fb = ptr;
29         fbwidth = w;
30         fbheight = h;
31 }
32
33 #define VNEXT(p)        (((p) == vlast) ? varr : (p) + 1)
34 #define VPREV(p)        ((p) == varr ? vlast : (p) - 1)
35 #define VSUCC(p, side)  ((side) == 0 ? VNEXT(p) : VPREV(p))
36
37 ARM_IWRAM
38 void polyfill_flat(struct pvertex *varr, int vnum, unsigned char col)
39 {
40         int i, line, top, bot;
41         struct pvertex *vlast, *v, *vn;
42         int32_t x, y0, y1, dx, dy, slope, fx, fy;
43         short *tab, start, len;
44         unsigned char *fbptr;
45         uint16_t *pptr, pcol = ((uint16_t)col << 8) | (uint16_t)col;
46
47         vlast = varr + vnum - 1;
48         top = fbheight;
49         bot = 0;
50
51         for(i=0; i<vnum; i++) {
52                 v = varr + i;
53                 vn = VNEXT(v);
54
55                 if(vn->y == v->y) continue;
56
57                 if(vn->y > v->y) {
58                         tab = scantab[0];
59                 } else {
60                         tab = scantab[1];
61                         v = vn;
62                         vn = varr + i;
63                 }
64
65                 dx = vn->x - v->x;
66                 dy = vn->y - v->y;
67                 slope = (dx << 8) / dy;
68
69                 y0 = (v->y + 0x100) & 0xffffff00;       /* start from the next scanline */
70                 fy = y0 - v->y;                                         /* fractional part before the next scanline */
71                 fx = (fy * slope) >> 8;                         /* X adjust for the step to the next scanline */
72                 x = v->x + fx;                                          /* adjust X */
73                 y1 = vn->y & 0xffffff00;                        /* last scanline of the edge <= vn->y */
74
75                 line = y0 >> 8;
76                 if(line < top) top = line;
77                 if((y1 >> 8) > bot) bot = y1 >> 8;
78
79                 if(line > 0) tab += line;
80
81                 while(line <= (y1 >> 8) && line < fbheight) {
82                         if(line >= 0) {
83                                 int val = x < 0 ? 0 : x >> 8;
84                                 *tab++ = val < fbwidth ? val : fbwidth - 1;
85                         }
86                         x += slope;
87                         line++;
88                 }
89         }
90
91         if(top < 0) top = 0;
92         if(bot >= fbheight) bot = fbheight - 1;
93
94         fbptr = fb + top * fbwidth;
95         for(i=top; i<=bot; i++) {
96                 start = scantab[0][i];
97                 len = scantab[1][i] - start;
98
99                 if(len > 0) {
100                         if(start & 1) {
101                                 pptr = (uint16_t*)(fbptr + (start & 0xfffe));
102                                 *pptr = (*pptr & 0xff) | ((uint16_t)col << 8);
103                                 len--;
104                                 start++;
105                         }
106                         pptr = (uint16_t*)(fbptr + start);
107                         while(len > 1) {
108                                 *pptr++ = pcol;
109                                 len -= 2;
110                         }
111                         if(len) {
112                                 *pptr = (*pptr & 0xff00) | col;
113                         }
114                 }
115                 fbptr += fbwidth;
116         }
117 }
118
119
120 /* ----- line drawing and clipping ------ */
121 enum {
122         IN              = 0,
123         LEFT    = 1,
124         RIGHT   = 2,
125         TOP             = 4,
126         BOTTOM  = 8
127 };
128
129 static int outcode(int x, int y, int xmin, int ymin, int xmax, int ymax)
130 {
131         int code = 0;
132
133         if(x < xmin) {
134                 code |= LEFT;
135         } else if(x > xmax) {
136                 code |= RIGHT;
137         }
138         if(y < ymin) {
139                 code |= TOP;
140         } else if(y > ymax) {
141                 code |= BOTTOM;
142         }
143         return code;
144 }
145
146 #define FIXMUL(a, b)    (((a) * (b)) >> 8)
147 #define FIXDIV(a, b)    (((a) << 8) / (b))
148
149 #define LERP(a, b, t)   ((a) + FIXMUL((b) - (a), (t)))
150
151 int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax, int ymax)
152 {
153         int oc_out;
154
155         int oc0 = outcode(*x0, *y0, xmin, ymin, xmax, ymax);
156         int oc1 = outcode(*x1, *y1, xmin, ymin, xmax, ymax);
157
158         long fx0, fy0, fx1, fy1, fxmin, fymin, fxmax, fymax;
159
160         if(!(oc0 | oc1)) return 1;      /* both points are inside */
161
162         fx0 = *x0 << 8;
163         fy0 = *y0 << 8;
164         fx1 = *x1 << 8;
165         fy1 = *y1 << 8;
166         fxmin = xmin << 8;
167         fymin = ymin << 8;
168         fxmax = xmax << 8;
169         fymax = ymax << 8;
170
171         for(;;) {
172                 long x, y, t;
173
174                 if(oc0 & oc1) return 0;         /* both have points with the same outbit, not visible */
175                 if(!(oc0 | oc1)) break;         /* both points are inside */
176
177                 oc_out = oc0 ? oc0 : oc1;
178
179                 if(oc_out & TOP) {
180                         t = FIXDIV(fymin - fy0, fy1 - fy0);
181                         x = LERP(fx0, fx1, t);
182                         y = fymin;
183                 } else if(oc_out & BOTTOM) {
184                         t = FIXDIV(fymax - fy0, fy1 - fy0);
185                         x = LERP(fx0, fx1, t);
186                         y = fymax;
187                 } else if(oc_out & LEFT) {
188                         t = FIXDIV(fxmin - fx0, fx1 - fx0);
189                         x = fxmin;
190                         y = LERP(fy0, fy1, t);
191                 } else {/*if(oc_out & RIGHT) {*/
192                         t = FIXDIV(fxmax - fx0, fx1 - fx0);
193                         x = fxmax;
194                         y = LERP(fy0, fy1, t);
195                 }
196
197                 if(oc_out == oc0) {
198                         fx0 = x;
199                         fy0 = y;
200                         oc0 = outcode(fx0 >> 8, fy0 >> 8, xmin, ymin, xmax, ymax);
201                 } else {
202                         fx1 = x;
203                         fy1 = y;
204                         oc1 = outcode(fx1 >> 8, fy1 >> 8, xmin, ymin, xmax, ymax);
205                 }
206         }
207
208         *x0 = fx0 >> 8;
209         *y0 = fy0 >> 8;
210         *x1 = fx1 >> 8;
211         *y1 = fy1 >> 8;
212         return 1;
213 }
214
215 #ifdef ALT_LCLIP
216 #define PUTPIXEL(ptr) \
217         do { \
218                 if(x0 >= 0 && x0 < fbwidth && y0 >= 0 && y0 < fbheight) { \
219                         uint16_t *pptr = (uint16_t*)((uint32_t)ptr & 0xfffffffe); \
220                         if((uint32_t)ptr & 1) { \
221                                 *pptr = (*pptr & 0xff) | (color << 8); \
222                         } else { \
223                                 *pptr = (*pptr & 0xff00) | color; \
224                         } \
225                 } \
226         } while(0)
227 #else   /* !ALT_LCLIP */
228 #define PUTPIXEL(ptr) \
229         do { \
230                 uint16_t *pptr = (uint16_t*)((uint32_t)ptr & 0xfffffffe); \
231                 if((uint32_t)ptr & 1) { \
232                         *pptr = (*pptr & 0xff) | (color << 8); \
233                 } else { \
234                         *pptr = (*pptr & 0xff00) | color; \
235                 } \
236         } while(0)
237 #endif
238
239 void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
240 {
241         int i, dx, dy, x_inc, y_inc, error;
242 #ifdef ALT_LCLIP
243         int y0inc;
244 #endif
245         unsigned char *fbptr = fb;
246
247         fbptr += y0 * fbwidth + x0;
248
249         dx = x1 - x0;
250         dy = y1 - y0;
251
252         if(dx >= 0) {
253                 x_inc = 1;
254         } else {
255                 x_inc = -1;
256                 dx = -dx;
257         }
258         if(dy >= 0) {
259                 y_inc = fbwidth;
260 #ifdef ALT_LCLIP
261                 y0inc = 1;
262 #endif
263         } else {
264                 y_inc = -fbwidth;
265 #ifdef ALT_LCLIP
266                 y0inc = -1;
267 #endif
268                 dy = -dy;
269         }
270
271         if(dx > dy) {
272                 error = dy * 2 - dx;
273                 for(i=0; i<=dx; i++) {
274                         PUTPIXEL(fbptr);
275                         if(error >= 0) {
276                                 error -= dx * 2;
277                                 fbptr += y_inc;
278 #ifdef ALT_LCLIP
279                                 y0 += y0inc;
280 #endif
281                         }
282                         error += dy * 2;
283                         fbptr += x_inc;
284 #ifdef ALT_LCLIP
285                         x0 += x_inc;
286 #endif
287                 }
288         } else {
289                 error = dx * 2 - dy;
290                 for(i=0; i<=dy; i++) {
291                         PUTPIXEL(fbptr);
292                         if(error >= 0) {
293                                 error -= dy * 2;
294                                 fbptr += x_inc;
295 #ifdef ALT_LCLIP
296                                 x0 += x_inc;
297 #endif
298                         }
299                         error += dx * 2;
300                         fbptr += y_inc;
301 #ifdef ALT_LCLIP
302                         y0 += y0inc;
303 #endif
304                 }
305         }
306 }
307