X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fpolyfill.c;h=7bffaa35690460fbdaa12d7e30b9f1337612a936;hb=3616caf014bccbf8814a67cf87ac012a91e7e968;hp=d8facb5a29271e62b29bfb2cbf0a8fc45be21cdf;hpb=921c229affb2b1da97d7be21d5c86ad045a6fcc3;p=dosdemo diff --git a/src/polyfill.c b/src/polyfill.c index d8facb5..7bffaa3 100644 --- a/src/polyfill.c +++ b/src/polyfill.c @@ -1,14 +1,20 @@ #include #include +#include +#include +#include #include "polyfill.h" #include "gfxutil.h" #include "demo.h" void (*fillfunc[])(struct pvertex*, int) = { polyfill_wire, - 0, 0, 0, 0 + polyfill_flat, + 0, 0, 0 }; +struct pimage pimg_fb, pimg_texture; + void polyfill(int mode, struct pvertex *verts, int nverts) { #ifndef NDEBUG @@ -23,19 +29,106 @@ void polyfill(int mode, struct pvertex *verts, int nverts) void polyfill_wire(struct pvertex *verts, int nverts) { - int i; + int i, x0, y0, x1, y1; struct pvertex *v = verts; unsigned short color = ((v->r << 8) & 0xf800) | ((v->g << 3) & 0x7e0) | ((v->b >> 3) & 0x1f); - for(i=0; ix >> 8; y0 = v->y >> 8; ++v; x1 = v->x >> 8; y1 = v->y >> 8; - clip_line(&x0, &y0, &x1, &y1, 0, 0, fb_width, fb_height); - draw_line(x0, y0, x1, y1, color); + if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pimg_fb.width, pimg_fb.height)) { + draw_line(x0, y0, x1, y1, color); + } + } + x0 = verts[0].x >> 8; + y0 = verts[0].y >> 8; + if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pimg_fb.width, pimg_fb.height)) { + draw_line(x1, y1, x0, y0, color); + } +} + +static void scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge); + +#define NEXTIDX(x) (((x) - 1 + nverts) % nverts) +#define PREVIDX(x) (((x) + 1) % nverts) + +void polyfill_flat(struct pvertex *pv, int nverts) +{ + int i; + int32_t y; + int topidx = 0, botidx = 0, sline; + struct pvertex *left, *right; + /*uint16_t color = PACK_RGB16(pv[0].r, pv[0].g, pv[0].b);*/ + + for(i=1; i pv[botidx].y) botidx = i; + } + + left = (struct pvertex*)alloca(pimg_fb.height * sizeof *left); + right = (struct pvertex*)alloca(pimg_fb.height * sizeof *right); + memset(left, 0, pimg_fb.height * sizeof *left); + memset(right, 0, pimg_fb.height * sizeof *right); + + for(i=0; i> 8) == (y1 >> 8)) { + if(y0 > y1) { + int idx = y0 >> 8; + left[idx].x = pv[i].x < pv[next].x ? pv[i].x : pv[next].x; + right[idx].x = pv[i].x < pv[next].x ? pv[next].x : pv[i].x; + } + } else { + scan_edge(pv + i, pv + next, y0 > y1 ? left : right); + } + } + + y = pv[topidx].y; + while(y < pv[botidx].y) { + int32_t x; + uint16_t *pixptr; + + sline = y >> 8; + x = left[sline].x; + + pixptr = pimg_fb.pixels + sline * pimg_fb.width + (x >> 8); + + while(x <= right[sline].x) { + *pixptr++ += 15; + x += 256; + } + y += 256; + } +} + +static void scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge) +{ + int32_t x, y, dx, dy, slope; + int idx; + + if(v0->y > v1->y) { + struct pvertex *tmp = v0; + v0 = v1; + v1 = tmp; + } + + dy = v1->y - v0->y; + dx = v1->x - v0->x; + slope = (dx << 8) / dy; + idx = v0->y >> 8; + + x = v0->x; + y = v0->y; + while(y <= v1->y) { + edge[idx++].x = x; + x += slope; + y += 256; } }