2 blender for the Gameboy Advance
3 Copyright (C) 2021 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
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/>.
22 static unsigned char *fb;
23 static int fbwidth, fbheight;
24 static short scantab[2][160] __attribute__((section(".iwram")));
26 void polyfill_framebuffer(unsigned char *ptr, int w, int h)
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))
37 void polyfill_flat(struct pvertex *varr, int vnum, unsigned char col)
39 int i, line, top, bot;
40 struct pvertex *vlast, *v, *vn;
41 int32_t x, y0, y1, dx, dy, slope, fx, fy;
42 short *tab, start, len;
44 uint16_t *pptr, pcol = ((uint16_t)col << 8) | (uint16_t)col;
46 vlast = varr + vnum - 1;
50 for(i=0; i<vnum; i++) {
54 if(vn->y == v->y) continue;
66 slope = (dx << 8) / dy;
68 y0 = (v->y + 0x100) & 0xffffff00; /* start from the next scanline */
69 fy = y0 - v->y; /* fractional part before the next scanline */
70 fx = (fy * slope) >> 8; /* X adjust for the step to the next scanline */
71 x = v->x + fx; /* adjust X */
72 y1 = vn->y & 0xffffff00; /* last scanline of the edge <= vn->y */
75 if(line < top) top = line;
76 if((y1 >> 8) > bot) bot = y1 >> 8;
78 if(line > 0) tab += line;
80 while(line <= (y1 >> 8) && line < fbheight) {
82 int val = x < 0 ? 0 : x >> 8;
83 *tab++ = val < fbwidth ? val : fbwidth - 1;
90 fbptr = fb + top * fbwidth;
91 for(i=top; i<=bot; i++) {
92 start = scantab[0][i];
93 len = scantab[1][i] - start;
97 pptr = (uint16_t*)(fbptr + (start & 0xfffe));
98 *pptr = (*pptr & 0xff) | ((uint16_t)col << 8);
102 pptr = (uint16_t*)(fbptr + start);
108 *pptr = (*pptr & 0xff00) | col;
116 /* ----- line drawing and clipping ------ */
125 static int outcode(int x, int y, int xmin, int ymin, int xmax, int ymax)
131 } else if(x > xmax) {
136 } else if(y > ymax) {
142 #define FIXMUL(a, b) (((a) * (b)) >> 8)
143 #define FIXDIV(a, b) (((a) << 8) / (b))
145 #define LERP(a, b, t) ((a) + FIXMUL((b) - (a), (t)))
147 int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax, int ymax)
151 int oc0 = outcode(*x0, *y0, xmin, ymin, xmax, ymax);
152 int oc1 = outcode(*x1, *y1, xmin, ymin, xmax, ymax);
154 long fx0, fy0, fx1, fy1, fxmin, fymin, fxmax, fymax;
156 if(!(oc0 | oc1)) return 1; /* both points are inside */
170 if(oc0 & oc1) return 0; /* both have points with the same outbit, not visible */
171 if(!(oc0 | oc1)) break; /* both points are inside */
173 oc_out = oc0 ? oc0 : oc1;
176 t = FIXDIV(fymin - fy0, fy1 - fy0);
177 x = LERP(fx0, fx1, t);
179 } else if(oc_out & BOTTOM) {
180 t = FIXDIV(fymax - fy0, fy1 - fy0);
181 x = LERP(fx0, fx1, t);
183 } else if(oc_out & LEFT) {
184 t = FIXDIV(fxmin - fx0, fx1 - fx0);
186 y = LERP(fy0, fy1, t);
187 } else {/*if(oc_out & RIGHT) {*/
188 t = FIXDIV(fxmax - fx0, fx1 - fx0);
190 y = LERP(fy0, fy1, t);
196 oc0 = outcode(fx0 >> 8, fy0 >> 8, xmin, ymin, xmax, ymax);
200 oc1 = outcode(fx1 >> 8, fy1 >> 8, xmin, ymin, xmax, ymax);
212 #define PUTPIXEL(ptr) \
214 if(x0 >= 0 && x0 < fbwidth && y0 >= 0 && y0 < fbheight) { \
215 uint16_t *pptr = (uint16_t*)((uint32_t)ptr & 0xfffffffe); \
216 if((uint32_t)ptr & 1) { \
217 *pptr = (*pptr & 0xff) | (color << 8); \
219 *pptr = (*pptr & 0xff00) | color; \
223 #else /* !ALT_LCLIP */
224 #define PUTPIXEL(ptr) \
226 uint16_t *pptr = (uint16_t*)((uint32_t)ptr & 0xfffffffe); \
227 if((uint32_t)ptr & 1) { \
228 *pptr = (*pptr & 0xff) | (color << 8); \
230 *pptr = (*pptr & 0xff00) | color; \
235 void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
237 int i, dx, dy, x_inc, y_inc, error;
241 unsigned char *fbptr = fb;
243 fbptr += y0 * fbwidth + x0;
269 for(i=0; i<=dx; i++) {
286 for(i=0; i<=dy; i++) {