-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.cpp
214 lines (186 loc) · 4.45 KB
/
fix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <algorithm>
int32_t SDL_Init();
extern bool gKeepGoing;
void* RenderThread(void* buffer);
uint32_t gTexture[WIDTH*HEIGHT];
extern Arduboy2Base arduboy;
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define DEBUG_PORT 8080
uint16_t gReportedVerts = 0;
struct point
{
int16_t x;
int16_t y;
};
struct triangle
{
point a;
point b;
point c;
uint8_t color;
int8_t order;
uintptr_t texture;
};
void init()
{
if(SDL_Init() < 0) return;
}
void post()
{
RenderThread(gTexture);
}
inline void setup();
inline void loop();
void Arduboy2Audio::begin()
{
}
void fillTriangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3, uint8_t color)
{
uintptr_t texture = 0; // Always empty for this path
// Fill a triangle - Bresenham method
// Original from http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html
uint8_t e1,e2;
int8_t signx1,signx2,dx1,dy1,dx2,dy2;
uint8_t t1x,t2x,y,minx,maxx,t1xp,t2xp;
bool changed1 = false;
bool changed2 = false;
if(y1>y2) { std::swap(y1,y2); std::swap(x1,x2); }
if(y1>y3) { std::swap(y1,y3); std::swap(x1,x3); }
if(y2>y3) { std::swap(y2,y3); std::swap(x2,x3); }
t1x=t2x=x1; y=y1;
dx1 = (int8_t)(x2 - x1); if(dx1<0) { dx1=-dx1; signx1=-1; } else signx1=1;
dy1 = (int8_t)(y2 - y1);
dx2 = (int8_t)(x3 - x1); if(dx2<0) { dx2=-dx2; signx2=-1; } else signx2=1;
dy2 = (int8_t)(y3 - y1);
if(dy1 > dx1)
{
std::swap(dx1,dy1);
changed1 = true;
}
if(dy2 > dx2)
{
std::swap(dy2,dx2);
changed2 = true;
}
e2 = (uint8_t)(dx2>>1);
if(y1 == y2) goto next;
e1 = (uint8_t)(dx1>>1);
for(uint8_t i = 0; i < dx1;)
{
t1xp=0; t2xp=0;
if(t1x<t2x)
{
minx=t1x; maxx=t2x;
}
else
{
minx=t2x;
maxx=t1x;
}
while(i<dx1)
{
i++;
e1 += dy1;
while (e1 >= dx1)
{
e1 -= dx1;
if(changed1) t1xp=signx1;
else goto next1;
}
if(changed1) break;
else t1x += signx1;
}
next1:
while(true)
{
e2 += dy2;
while(e2 >= dx2)
{
e2 -= dx2;
if(changed2) t2xp=signx2;
else goto next2;
}
if(changed2) break;
else t2x += signx2;
}
next2:
if(minx>t1x) minx=t1x; if(minx>t2x) minx=t2x;
if(maxx<t1x) maxx=t1x; if(maxx<t2x) maxx=t2x;
arduboy.drawFastHLine(minx, y, maxx-minx, color, texture);
if(!changed1) t1x += signx1;
t1x+=t1xp;
if(!changed2) t2x += signx2;
t2x+=t2xp;
y += 1;
if(y == y2) break;
}
next:
dx1 = (int8_t)(x3 - x2); if(dx1<0) { dx1=-dx1; signx1=-1; } else signx1=1;
dy1 = (int8_t)(y3 - y2);
t1x=x2;
if(dy1 > dx1)
{
std::swap(dy1,dx1);
changed1 = true;
} else changed1=false;
e1 = (uint8_t)(dx1>>1);
for(uint8_t i = 0; i<=dx1; i++)
{
t1xp=0; t2xp=0;
if(t1x<t2x)
{
minx=t1x;
maxx=t2x;
}
else
{
minx=t2x;
maxx=t1x;
}
while(i<dx1)
{
e1 += dy1;
while (e1 >= dx1)
{
e1 -= dx1;
if(changed1)
{
t1xp=signx1; break;
}
else goto next3;
}
if(changed1) break;
else t1x += signx1;
if(i < dx1) i++;
}
next3:
while(t2x != x3)
{
e2 += dy2;
while(e2 >= dx2)
{
e2 -= dx2;
if(changed2) t2xp=signx2;
else goto next4;
}
if(changed2) break;
else t2x += signx2;
}
next4:
if(minx>t1x) minx=t1x; if(minx>t2x) minx=t2x;
if(maxx<t1x) maxx=t1x; if(maxx<t2x) maxx=t2x;
arduboy.drawFastHLine(minx, y, maxx-minx, color, texture);
if(!changed1) t1x += signx1;
t1x += t1xp;
if(!changed2) t2x += signx2;
t2x+=t2xp;
y += 1;
if(y > y3) return;
}
}
void fillTriangle(triangle t)
{
arduboy.fillTriangle(t.a.x, t.a.y, t.b.x, t.b.y, t.c.x, t.c.y, t.color, t.texture);
}