gba-trycatch
view src/polyfill.c @ 5:850be43b3135
sdl version
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 16 Jun 2014 22:01:45 +0300 |
parents | |
children | 73b5f2e5d18a |
line source
1 #include "config.h"
2 #include <string.h>
3 #include "polyfill.h"
4 #include "fixed.h"
5 #include "gbasys.h"
7 static void fill_scanline(int y, int x0, int x1, uint16_t color);
9 void draw_poly(int num, const pvec3 *verts, uint16_t color)
10 {
11 int i, topidx = 0, botidx = 0;
12 int lidx[2], ridx[2];
13 int32_t y, topy, boty;
14 int32_t ldy, rdy, ldxdy, rdxdy;
15 int32_t lx, rx;
16 int start, end;
18 topy = boty = verts[0].y;
19 for(i=1; i<num; i++) {
20 int32_t y = verts[i].y;
21 if(y < topy) {
22 topy = y;
23 topidx = i;
24 }
25 if(y > boty) {
26 boty = y;
27 botidx = i;
28 }
29 }
31 lidx[0] = ridx[0] = topidx;
32 lidx[1] = topidx ? topidx - 1 : num - 1;
33 ridx[1] = (topidx + 1) % num;
35 if(ridx[1] < lidx[1]) {
36 return; /* backface (CCW) */
37 }
39 lx = rx = verts[lidx[0]].x;
41 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
42 ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
44 rdy = verts[ridx[1]].y - verts[ridx[1]].y;
45 rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
47 start = topy >> 16;
48 end = boty >> 16;
50 y = topy;
51 for(i=start; i<end; i++) {
52 unsigned short x0, x1;
54 x0 = lx < 0 ? 0 : (lx >> 16);
55 x1 = rx >= WIDTH ? WIDTH - 1 : (rx >> 16);
57 fill_scanline(i, x0, x1, color);
59 if(y >= verts[lidx[1]].y) {
60 lidx[0] = lidx[1];
61 lidx[1] = lidx[1] ? lidx[1] - 1 : num - 1;
62 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
63 if(ldy < 0) {
64 break;
65 }
66 ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
67 }
68 if(y >= verts[ridx[1]].y) {
69 ridx[0] = ridx[1];
70 ridx[1] = (ridx[1] + 1) % num;
71 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
72 if(rdy < 0) {
73 break;
74 }
75 rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
76 }
78 lx += ldxdy;
79 rx += rdxdy;
80 }
81 }
83 static void fill_scanline(int y, int x0, int x1, uint16_t color)
84 {
85 int i;
86 uint16_t *pixels = (uint16_t*)back_buffer->pixels + y * WIDTH + x0;
88 for(i=x0; i<x1; i++) {
89 *pixels++ = color;
90 }
91 }