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;
45 vlast = varr + vnum - 1;
49 for(i=0; i<vnum; i++) {
53 if(vn->y == v->y) continue;
65 slope = (dx << 8) / dy;
67 y0 = (v->y + 0x100) & 0xffffff00; /* start from the next scanline */
68 fy = y0 - v->y; /* fractional part before the next scanline */
69 fx = (fy * slope) >> 8; /* X adjust for the step to the next scanline */
70 x = v->x + fx; /* adjust X */
71 y1 = vn->y & 0xffffff00; /* last scanline of the edge <= vn->y */
74 if(line < top) top = line;
75 if((y1 >> 8) > bot) bot = y1 >> 8;
77 if(line > 0) tab += line;
79 while(line <= (y1 >> 8) && line < fbheight) {
81 int val = x < 0 ? 0 : x >> 8;
82 *tab++ = val < fbwidth ? val : fbwidth - 1;
89 fbptr = fb + top * fbwidth;
90 for(i=top; i<=bot; i++) {
91 start = scantab[0][i];
92 len = scantab[1][i] - start;
95 memset(fbptr + start, col, len);
102 /* ----- line drawing and clipping ------ */
111 static int outcode(int x, int y, int xmin, int ymin, int xmax, int ymax)
117 } else if(x > xmax) {
122 } else if(y > ymax) {
128 #define FIXMUL(a, b) (((a) * (b)) >> 8)
129 #define FIXDIV(a, b) (((a) << 8) / (b))
131 #define LERP(a, b, t) ((a) + FIXMUL((b) - (a), (t)))
133 int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax, int ymax)
137 int oc0 = outcode(*x0, *y0, xmin, ymin, xmax, ymax);
138 int oc1 = outcode(*x1, *y1, xmin, ymin, xmax, ymax);
140 long fx0, fy0, fx1, fy1, fxmin, fymin, fxmax, fymax;
142 if(!(oc0 | oc1)) return 1; /* both points are inside */
156 if(oc0 & oc1) return 0; /* both have points with the same outbit, not visible */
157 if(!(oc0 | oc1)) break; /* both points are inside */
159 oc_out = oc0 ? oc0 : oc1;
162 t = FIXDIV(fymin - fy0, fy1 - fy0);
163 x = LERP(fx0, fx1, t);
165 } else if(oc_out & BOTTOM) {
166 t = FIXDIV(fymax - fy0, fy1 - fy0);
167 x = LERP(fx0, fx1, t);
169 } else if(oc_out & LEFT) {
170 t = FIXDIV(fxmin - fx0, fx1 - fx0);
172 y = LERP(fy0, fy1, t);
173 } else {/*if(oc_out & RIGHT) {*/
174 t = FIXDIV(fxmax - fx0, fx1 - fx0);
176 y = LERP(fy0, fy1, t);
182 oc0 = outcode(fx0 >> 8, fy0 >> 8, xmin, ymin, xmax, ymax);
186 oc1 = outcode(fx1 >> 8, fy1 >> 8, xmin, ymin, xmax, ymax);
197 void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
199 int i, dx, dy, x_inc, y_inc, error;
200 unsigned char *fbptr = fb;
202 fbptr += y0 * fbwidth + x0;
222 for(i=0; i<=dx; i++) {
233 for(i=0; i<=dy; i++) {