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