gba-trycatch
view src/polyfill.c @ 10:23f716fa7f10
changed to mode 5 again, this time with hardware scaling (looks kinda crap)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 22 Jun 2014 06:31:14 +0300 |
parents | fb0a0d6a8b52 |
children | c398d834d64a |
line source
1 #include "config.h"
2 #include <string.h>
3 #include <assert.h>
4 #include "polyfill.h"
5 #include "fixed.h"
6 #include "gbasys.h"
8 #define VNEXT(x, n) (((x) + 1) % (n))
9 #define VPREV(x, n) ((x) > 0 ? (x) - 1 : (n) - 1)
11 static void fill_scanline_pal(int y, int x0, int x1, uint8_t color);
12 static void fill_scanline_rgb(int y, int x0, int x1, uint16_t color);
14 void draw_poly(int num, const pvec3 *verts, uint16_t color)
15 {
16 int i, topidx = 0, botidx = 0;
17 int lidx[2] = {-1, -1}, ridx[2] = {-1, -1};
18 int32_t y, topy, boty;
19 int32_t ldy = 0, rdy = 0, ldxdy, rdxdy;
20 int32_t lx, rx;
21 int start, end;
23 topy = boty = verts[0].y;
24 for(i=1; i<num; i++) {
25 int32_t y = verts[i].y;
26 if(y < topy) {
27 topy = y;
28 topidx = i;
29 }
30 if(y > boty) {
31 boty = y;
32 botidx = i;
33 }
34 }
36 lidx[0] = ridx[0] = topidx;
38 /* find starting left edge */
39 lidx[1] = VPREV(lidx[0], num);
40 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
42 while(ldy == 0) {
43 lidx[0] = lidx[1];
44 lidx[1] = VPREV(lidx[1], num);
46 if(lidx[1] == topidx) {
47 return; /* degenerate */
48 }
50 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
51 }
52 lx = verts[lidx[0]].x;
53 ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
55 /* find starting right edge */
56 ridx[1] = VNEXT(ridx[0], num);
57 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
59 while(rdy == 0) {
60 ridx[0] = ridx[1];
61 ridx[1] = VNEXT(ridx[1], num);
63 if(ridx[1] == topidx) {
64 return; /* degenerate */
65 }
67 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
68 }
69 rx = verts[ridx[0]].x;
70 rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
73 if(verts[ridx[1]].x < verts[lidx[1]].x) {
74 return; /* backface (CCW) */
75 }
77 start = topy >> 16;
78 end = boty >> 16;
80 if(end >= HEIGHT) end = HEIGHT - 1;
82 y = topy;
83 for(i=start; i<end; i++) {
84 short x0, x1;
86 if(y >= verts[lidx[1]].y) {
87 lx = verts[lidx[1]].x;
88 lidx[0] = lidx[1];
89 lidx[1] = VPREV(lidx[1], num);
90 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
91 if(ldy < 0) {
92 break;
93 }
94 ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
95 }
96 if(y >= verts[ridx[1]].y) {
97 rx = verts[ridx[1]].x;
98 ridx[0] = ridx[1];
99 ridx[1] = VNEXT(ridx[1], num);
100 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
101 if(rdy < 0) {
102 break;
103 }
104 rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
105 }
107 x0 = lx < 0 ? 0 : (lx >> 16);
108 x1 = (rx >> 16) >= WIDTH ? WIDTH - 1 : (rx >> 16);
110 if(i >= 0 && x1 > x0) {
111 #ifdef PALMODE
112 fill_scanline_pal(i, x0, x1, (uint8_t)color);
113 #else
114 fill_scanline_rgb(i, x0, x1, color);
115 #endif
116 }
118 lx += ldxdy;
119 rx += rdxdy;
120 y += 65536;
121 }
122 }
125 static void fill_scanline_pal(int y, int x0, int x1, uint8_t color)
126 {
127 #if 1
128 int i, num_pairs, num_pix = x1 - x0;
129 uint16_t *pixels = (uint16_t*)back_buffer->pixels + (y * WIDTH + x0) / 2;
130 uint16_t colpair = (uint16_t)color | ((uint16_t)color << 8);
132 if(x0 & 1) {
133 uint16_t pix = *pixels & 0xff;
134 *pixels++ = pix | ((uint16_t)color << 8);
135 --num_pix;
136 }
138 num_pairs = (num_pix & 0xfffe) / 2;
140 for(i=0; i<num_pairs; i++) {
141 *pixels++ = colpair;
142 }
144 if(num_pix & 1) {
145 uint16_t pix = *pixels & 0xff00;
146 *pixels = pix | color;
147 }
148 #else
149 int i;
150 uint8_t *pixels = (uint8_t*)back_buffer->pixels + y * WIDTH + x0;
152 for(i=x0; i<x1; i++) {
153 *pixels++ = color;
154 }
155 #endif
156 }
158 static void fill_scanline_rgb(int y, int x0, int x1, uint16_t color)
159 {
160 int i;
161 uint16_t *pixels = (uint16_t*)back_buffer->pixels + y * WIDTH + x0;
163 for(i=x0; i<x1; i++) {
164 *pixels++ = color;
165 }
166 }
169 void draw_point(const pvec3 *v, uint16_t color)
170 {
171 int x = v->x >> 16;
172 int y = v->y >> 16;
173 uint16_t *pixels = (uint16_t*)back_buffer->pixels;
175 if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
176 return;
177 }
179 #ifdef PALMODE
180 pixels += (y * WIDTH + x) / 2;
181 if(x & 1) {
182 *pixels = (*pixels & 0xff) | (color << 8);
183 } else {
184 *pixels = (*pixels & 0xff00) | color;
185 }
186 #else
187 pixels[y * WIDTH + x] = color;
188 #endif
189 }