split the polyfiller to a preprocessor-based template file, included
[dosdemo] / src / polyfill.c
index 80bfee5..634d525 100644 (file)
@@ -14,7 +14,9 @@
 void (*fillfunc[])(struct pvertex*, int) = {
        polyfill_wire,
        polyfill_flat,
-       0, 0, 0
+       polyfill_gouraud,
+       polyfill_tex,
+       polyfill_tex_gouraud
 };
 
 struct pimage pimg_fb, pimg_texture;
@@ -55,92 +57,37 @@ void polyfill_wire(struct pvertex *verts, int nverts)
        }
 }
 
-static uint32_t 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;
-       int topidx = 0, botidx = 0, sltop = pimg_fb.height, slbot = 0;
-       struct pvertex *left, *right;
-       uint16_t color = PACK_RGB16(pv[0].r, pv[0].g, pv[0].b);
-
-       for(i=1; i<nverts; i++) {
-               if(pv[i].y < pv[topidx].y) topidx = i;
-               if(pv[i].y > 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<nverts; i++) {
-               int next = NEXTIDX(i);
-               int32_t y0 = pv[i].y;
-               int32_t y1 = pv[next].y;
-
-               if((y0 >> 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 {
-                       struct pvertex *edge = y0 > y1 ? left : right;
-                       uint32_t res = scan_edge(pv + i, pv + next, edge);
-                       uint32_t tmp = (res >> 16) & 0xffff;
-                       if(tmp > slbot) slbot = tmp;
-                       if((tmp = res & 0xffff) < sltop) {
-                               sltop = tmp;
-                       }
-               }
-       }
-
-       for(i=sltop; i<=slbot; i++) {
-               int32_t x;
-               uint16_t *pixptr;
-
-               x = left[i].x;
-               pixptr = pimg_fb.pixels + i * pimg_fb.width + (x >> 8);
-
-               while(x <= right[i].x) {
-#ifdef DEBUG_POLYFILL
-                       *pixptr++ += 15;
-#else
-                       *pixptr++ = color;
-#endif
-                       x += 256;
-               }
-       }
-}
-
-static uint32_t scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge)
-{
-       int i;
-       int32_t x, dx, dy, slope;
-       int32_t start_idx, end_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;
-
-       start_idx = v0->y >> 8;
-       end_idx = v1->y >> 8;
-
-       x = v0->x;
-       for(i=start_idx; i<end_idx; i++) {
-               edge[i].x = x;
-               x += slope;
-       }
-
-       return (uint32_t)start_idx | ((uint32_t)(end_idx - 1) << 16);
-}
+#define POLYFILL polyfill_flat
+#define SCANEDGE scanedge_flat
+#undef GOURAUD
+#undef TEXMAP
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE
+
+#define POLYFILL polyfill_gouraud
+#define SCANEDGE scanedge_gouraud
+#define GOURAUD
+#undef TEXMAP
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE
+
+#define POLYFILL polyfill_tex
+#define SCANEDGE scanedge_tex
+#undef GOURAUD
+#define TEXMAP
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE
+
+#define POLYFILL polyfill_tex_gouraud
+#define SCANEDGE scanedge_tex_gouraud
+#define GOURAUD
+#define TEXMAP
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE