split the polyfiller to a preprocessor-based template file, included
[dosdemo] / src / polyfill.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #if defined(__WATCOMC__) || defined(_MSC_VER)
6 #include <malloc.h>
7 #else
8 #include <alloca.h>
9 #endif
10 #include "polyfill.h"
11 #include "gfxutil.h"
12 #include "demo.h"
13
14 void (*fillfunc[])(struct pvertex*, int) = {
15         polyfill_wire,
16         polyfill_flat,
17         polyfill_gouraud,
18         polyfill_tex,
19         polyfill_tex_gouraud
20 };
21
22 struct pimage pimg_fb, pimg_texture;
23
24 void polyfill(int mode, struct pvertex *verts, int nverts)
25 {
26 #ifndef NDEBUG
27         if(!fillfunc[mode]) {
28                 fprintf(stderr, "polyfill mode %d not implemented\n", mode);
29                 abort();
30         }
31 #endif
32
33         fillfunc[mode](verts, nverts);
34 }
35
36 void polyfill_wire(struct pvertex *verts, int nverts)
37 {
38         int i, x0, y0, x1, y1;
39         struct pvertex *v = verts;
40         unsigned short color = ((v->r << 8) & 0xf800) |
41                 ((v->g << 3) & 0x7e0) | ((v->b >> 3) & 0x1f);
42
43         for(i=0; i<nverts - 1; i++) {
44                 x0 = v->x >> 8;
45                 y0 = v->y >> 8;
46                 ++v;
47                 x1 = v->x >> 8;
48                 y1 = v->y >> 8;
49                 if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pimg_fb.width, pimg_fb.height)) {
50                         draw_line(x0, y0, x1, y1, color);
51                 }
52         }
53         x0 = verts[0].x >> 8;
54         y0 = verts[0].y >> 8;
55         if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pimg_fb.width, pimg_fb.height)) {
56                 draw_line(x1, y1, x0, y0, color);
57         }
58 }
59
60 #define NEXTIDX(x) (((x) - 1 + nverts) % nverts)
61 #define PREVIDX(x) (((x) + 1) % nverts)
62
63 #define POLYFILL polyfill_flat
64 #define SCANEDGE scanedge_flat
65 #undef GOURAUD
66 #undef TEXMAP
67 #include "polytmpl.h"
68 #undef POLYFILL
69 #undef SCANEDGE
70
71 #define POLYFILL polyfill_gouraud
72 #define SCANEDGE scanedge_gouraud
73 #define GOURAUD
74 #undef TEXMAP
75 #include "polytmpl.h"
76 #undef POLYFILL
77 #undef SCANEDGE
78
79 #define POLYFILL polyfill_tex
80 #define SCANEDGE scanedge_tex
81 #undef GOURAUD
82 #define TEXMAP
83 #include "polytmpl.h"
84 #undef POLYFILL
85 #undef SCANEDGE
86
87 #define POLYFILL polyfill_tex_gouraud
88 #define SCANEDGE scanedge_tex_gouraud
89 #define GOURAUD
90 #define TEXMAP
91 #include "polytmpl.h"
92 #undef POLYFILL
93 #undef SCANEDGE