80bfee5f29307b2393a6cd918df8c704db10b5e6
[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         0, 0, 0
18 };
19
20 struct pimage pimg_fb, pimg_texture;
21
22 void polyfill(int mode, struct pvertex *verts, int nverts)
23 {
24 #ifndef NDEBUG
25         if(!fillfunc[mode]) {
26                 fprintf(stderr, "polyfill mode %d not implemented\n", mode);
27                 abort();
28         }
29 #endif
30
31         fillfunc[mode](verts, nverts);
32 }
33
34 void polyfill_wire(struct pvertex *verts, int nverts)
35 {
36         int i, x0, y0, x1, y1;
37         struct pvertex *v = verts;
38         unsigned short color = ((v->r << 8) & 0xf800) |
39                 ((v->g << 3) & 0x7e0) | ((v->b >> 3) & 0x1f);
40
41         for(i=0; i<nverts - 1; i++) {
42                 x0 = v->x >> 8;
43                 y0 = v->y >> 8;
44                 ++v;
45                 x1 = v->x >> 8;
46                 y1 = v->y >> 8;
47                 if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pimg_fb.width, pimg_fb.height)) {
48                         draw_line(x0, y0, x1, y1, color);
49                 }
50         }
51         x0 = verts[0].x >> 8;
52         y0 = verts[0].y >> 8;
53         if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pimg_fb.width, pimg_fb.height)) {
54                 draw_line(x1, y1, x0, y0, color);
55         }
56 }
57
58 static uint32_t scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge);
59
60 #define NEXTIDX(x) (((x) - 1 + nverts) % nverts)
61 #define PREVIDX(x) (((x) + 1) % nverts)
62
63 void polyfill_flat(struct pvertex *pv, int nverts)
64 {
65         int i;
66         int topidx = 0, botidx = 0, sltop = pimg_fb.height, slbot = 0;
67         struct pvertex *left, *right;
68         uint16_t color = PACK_RGB16(pv[0].r, pv[0].g, pv[0].b);
69
70         for(i=1; i<nverts; i++) {
71                 if(pv[i].y < pv[topidx].y) topidx = i;
72                 if(pv[i].y > pv[botidx].y) botidx = i;
73         }
74
75         left = (struct pvertex*)alloca(pimg_fb.height * sizeof *left);
76         right = (struct pvertex*)alloca(pimg_fb.height * sizeof *right);
77         memset(left, 0, pimg_fb.height * sizeof *left);
78         memset(right, 0, pimg_fb.height * sizeof *right);
79
80         for(i=0; i<nverts; i++) {
81                 int next = NEXTIDX(i);
82                 int32_t y0 = pv[i].y;
83                 int32_t y1 = pv[next].y;
84
85                 if((y0 >> 8) == (y1 >> 8)) {
86                         if(y0 > y1) {
87                                 int idx = y0 >> 8;
88                                 left[idx].x = pv[i].x < pv[next].x ? pv[i].x : pv[next].x;
89                                 right[idx].x = pv[i].x < pv[next].x ? pv[next].x : pv[i].x;
90                         }
91                 } else {
92                         struct pvertex *edge = y0 > y1 ? left : right;
93                         uint32_t res = scan_edge(pv + i, pv + next, edge);
94                         uint32_t tmp = (res >> 16) & 0xffff;
95                         if(tmp > slbot) slbot = tmp;
96                         if((tmp = res & 0xffff) < sltop) {
97                                 sltop = tmp;
98                         }
99                 }
100         }
101
102         for(i=sltop; i<=slbot; i++) {
103                 int32_t x;
104                 uint16_t *pixptr;
105
106                 x = left[i].x;
107                 pixptr = pimg_fb.pixels + i * pimg_fb.width + (x >> 8);
108
109                 while(x <= right[i].x) {
110 #ifdef DEBUG_POLYFILL
111                         *pixptr++ += 15;
112 #else
113                         *pixptr++ = color;
114 #endif
115                         x += 256;
116                 }
117         }
118 }
119
120 static uint32_t scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge)
121 {
122         int i;
123         int32_t x, dx, dy, slope;
124         int32_t start_idx, end_idx;
125
126         if(v0->y > v1->y) {
127                 struct pvertex *tmp = v0;
128                 v0 = v1;
129                 v1 = tmp;
130         }
131
132         dy = v1->y - v0->y;
133         dx = v1->x - v0->x;
134         slope = (dx << 8) / dy;
135
136         start_idx = v0->y >> 8;
137         end_idx = v1->y >> 8;
138
139         x = v0->x;
140         for(i=start_idx; i<end_idx; i++) {
141                 edge[i].x = x;
142                 x += slope;
143         }
144
145         return (uint32_t)start_idx | ((uint32_t)(end_idx - 1) << 16);
146 }