-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprite.c
More file actions
218 lines (208 loc) · 5.97 KB
/
sprite.c
File metadata and controls
218 lines (208 loc) · 5.97 KB
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
215
216
217
218
#include "harvey_platform.h"
#include "sprite.h"
void fill_rectangle(int x, int y, int width, int height, uint32_t color)
{
const int screen_width = VIDEO->WIDTH;
const int screen_height = VIDEO->HEIGHT;
uint32_t *framebuffer = (uint32_t *)VIDEO->DMA_ADDR;
if (x >= screen_width || y >= screen_width || (x + width) <= 0 || (y + height) <= 0)
return;
int ystart = y >= 0 ? y : 0;
int ystop = (y + height) < screen_height ? (y + height) : screen_height;
int xstart = x >= 0 ? x : 0;
int xstop = (x + width) < screen_width ? (x + width) : screen_width;
for (int y = ystart; y < ystop; y++)
{
for (int x = xstart; x < xstop; x++)
{
framebuffer[y * screen_width + x] = color;
}
}
}
void draw_sprite(int x, int y, const sprite_t *sprite)
{
const int screen_width = VIDEO->WIDTH;
const int screen_height = VIDEO->HEIGHT;
uint32_t *framebuffer = (uint32_t *)VIDEO->DMA_ADDR;
if (!sprite)
return;
int width = sprite->width;
int height = sprite->height;
const uint32_t *data = sprite->pixel_data;
if (x >= screen_width || y >= screen_width || (x + width) <= 0 || (y + height) <= 0)
return;
int ystart = y;
int yc = 0;
if (y < 0)
{
ystart = 0;
yc = -y;
}
int ystop = (y + height) <= screen_height ? (y + height) : screen_height;
int xstart = x;
int xc = 0;
if (x < 0)
{
xstart = 0;
xc = -x;
}
int xstop = (x + width) < screen_width ? (x + width) : screen_width;
data = &data[yc * width];
for (int y = ystart; y < ystop; y++, data += width)
{
int p = xc;
for (int x = xstart; x < xstop; x++)
{
uint32_t new_color = data[p++];
uint32_t alpha = new_color >> 24;
if (alpha == 255)
{
framebuffer[y * screen_width + x] = new_color;
}
else if (alpha != 0)
{
if (alpha > 0)
alpha += 1;
uint32_t one_minus_alpha = 256 - alpha;
uint32_t previous_color = framebuffer[y * screen_width + x];
uint32_t r = ((((previous_color >> 16) & 0xff) * one_minus_alpha) >> 8) + ((((new_color >> 16) & 0xff) * alpha) >> 8);
uint32_t g = ((((previous_color >> 8) & 0xff) * one_minus_alpha) >> 8) + ((((new_color >> 8) & 0xff) * alpha) >> 8);
uint32_t b = ((((previous_color >> 0) & 0xff) * one_minus_alpha) >> 8) + ((((new_color >> 0) & 0xff) * alpha) >> 8);
framebuffer[y * screen_width + x] = (r << 16) | (g << 8) | b;
}
}
}
}
static void draw_line_horizontal(int x, int y, int length, uint32_t color)
{
const int screen_width = VIDEO->WIDTH;
const int screen_height = VIDEO->HEIGHT;
uint32_t *framebuffer = (uint32_t *)VIDEO->DMA_ADDR;
if (length < 0)
{
x += length;
length = -length;
}
if (y < 0 || y >= screen_height || x >= screen_width || (x + length) <= 0)
return;
int xstart = x >= 0 ? x : 0;
int xstop = (x + length) <= screen_width ? (x + length) : screen_width;
for (x = xstart; x < xstop; x++)
framebuffer[y * screen_width + x] = color;
}
static void draw_line_vertical(int x, int y, int length, uint32_t color)
{
const int screen_width = VIDEO->WIDTH;
const int screen_height = VIDEO->HEIGHT;
uint32_t *framebuffer = (uint32_t *)VIDEO->DMA_ADDR;
if (length < 0)
{
y += length;
length = -length;
}
if (x < 0 || x >= screen_width || y >= screen_height || (y + length) <= 0)
return;
int ystart = y >= 0 ? y : 0;
int ystop = (y + length) <= screen_height ? (y + length) : screen_height;
for (y = ystart; y < ystop; y++)
framebuffer[y * screen_width + x] = color;
}
static void bresenham_low(int x0, int y0, int x1, int y1, uint32_t color)
{
const int screen_width = VIDEO->WIDTH;
const int screen_height = VIDEO->HEIGHT;
uint32_t *framebuffer = (uint32_t *)VIDEO->DMA_ADDR;
int dx = x1 - x0;
int dy = y1 - y0;
int yi = 1;
if (dy < 0)
{
yi = -1;
dy = -dy;
}
int d = 2 * dy - dx;
int y = y0;
for (int x = x0; x <= x1; x++)
{
if (x >= 0 && x < screen_width && y >= 0 && y < screen_height)
{
framebuffer[y * screen_width + x] = color;
}
if (d > 0)
{
y += yi;
d += 2 * (dy - dx);
}
else
{
d += 2 * dy;
}
}
}
static void bresenham_high(int x0, int y0, int x1, int y1, uint32_t color)
{
const int screen_width = VIDEO->WIDTH;
const int screen_height = VIDEO->HEIGHT;
uint32_t *framebuffer = (uint32_t *)VIDEO->DMA_ADDR;
int dx = x1 - x0;
int dy = y1 - y0;
int xi = 1;
if (dx < 0)
{
xi = -1;
dx = -dx;
}
int d = 2 * dx - dy;
int x = x0;
for (int y = y0; y <= y1; y++)
{
if (x >= 0 && x < screen_width && y >= 0 && y < screen_height)
{
framebuffer[y * screen_width + x] = color;
}
if (d > 0)
{
x += xi;
d += 2 * (dx - dy);
}
else
{
d += 2 * dx;
}
}
}
void draw_line(int x0, int y0, int x1, int y1, uint32_t color)
{
int absy = (y1 > y0) ? (y1 - y0) : (y0 - y1);
int absx = (x1 > x0) ? (x1 - x0) : (x0 - x1);
if (absy == 0)
{
draw_line_horizontal(x0, y0, x1 - x0, color);
}
else if (absx == 0)
{
draw_line_vertical(x0, y0, y1 - y0, color);
}
else if (absy < absx)
{
if (x0 > x1)
{
bresenham_low(x1, y1, x0, y0, color);
}
else
{
bresenham_low(x0, y0, x1, y1, color);
}
}
else
{
if (y0 > y1)
{
bresenham_high(x1, y1, x0, y0, color);
}
else
{
bresenham_high(x0, y0, x1, y1, color);
}
}
}