-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawlist_render.hpp
447 lines (376 loc) · 12.5 KB
/
drawlist_render.hpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#pragma once
namespace DrawList {
inline bool is_color_visible(uint16_t c) { return c < 0x8000; }
template<int width, int height>
inline bool bounds_check(int x, int y) {
if (y < 0) return false;
if (y >= height) return false;
if (x < 0) return false;
if (x >= width) return false;
return true;
}
template<int width, int height, typename PLOT>
void draw_pixel(int x0, int y0, uint16_t color, PLOT& plot) {
if (!is_color_visible(color))
return;
if (!bounds_check<width, height>(x0, y0))
return;
plot(x0, y0, color);
}
template<int width, int height, typename PLOT>
void draw_hline(int x0, int y0, int w, uint16_t color, PLOT& plot) {
if (!is_color_visible(color))
return;
if (y0 < 0) return;
if (y0 >= height) return;
for (int x = x0; x < x0+w; x++) {
if (x < 0) continue;
if (x >= width) return;
plot(x, y0, color);
}
}
template<int width, int height, typename PLOT>
void draw_vline(int x0, int y0, int h, uint16_t color, PLOT& plot) {
if (!is_color_visible(color))
return;
if (x0 < 0) return;
if (x0 >= width) return;
for (int y = y0; y < y0+h; y++) {
if (y < 0) continue;
if (y >= height) break;
plot(x0, y, color);
}
}
template<int width, int height, typename PLOT>
void draw_rect(int x0, int y0, int w, int h, uint16_t color, PLOT& plot) {
if (!is_color_visible(color))
return;
draw_hline<width, height>(x0, y0, w, color, plot);
draw_hline<width, height>(x0, y0+h-1, w, color, plot);
draw_vline<width, height>(x0, y0+1, h-2, color, plot);
draw_vline<width, height>(x0+w-1, y0+1, h-2, color, plot);
}
template<int width, int height, typename PLOT>
void draw_rect_fill(int x0, int y0, int w, int h, uint16_t fill_color, PLOT& plot) {
if (!is_color_visible(fill_color))
return;
for (int y = y0; y < y0+h; y++) {
if (y < 0) continue;
if (y >= height) break;
for (int x = x0; x < x0+w; x++) {
if (x < 0) continue;
if (x >= width) break;
plot(x, y, fill_color);
}
}
}
template<int width, int height, typename PLOT>
void draw_line(int x1, int y1, int x2, int y2, uint16_t color, PLOT& plot) {
if (!is_color_visible(color))
return;
int x, y, dx, dy, dx1, dy1, mx, my, xe, ye, i;
dx = x2 - x1;
dy = y2 - y1;
dx1 = std::abs(dx);
dy1 = std::abs(dy);
mx = 2 * dy1 - dx1;
my = 2 * dx1 - dy1;
if (dy1 <= dx1) {
if (dx >= 0) {
x = x1;
y = y1;
xe = x2;
} else {
x = x2;
y = y2;
xe = x1;
}
if (bounds_check<width, height>(x, y)) {
plot(x, y, color);
}
for (i = 0; x < xe; i++) {
x = x + 1;
if (mx < 0) {
mx = mx + 2 * dy1;
} else {
if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0)) {
y = y + 1;
} else {
y = y - 1;
}
mx = mx + 2 * (dy1 - dx1);
}
if (bounds_check<width, height>(x, y)) {
plot(x, y, color);
}
}
} else {
if (dy >= 0) {
x = x1;
y = y1;
ye = y2;
} else {
x = x2;
y = y2;
ye = y1;
}
if (bounds_check<width, height>(x, y)) {
plot(x, y, color);
}
for (i = 0; y < ye; i++) {
y = y + 1;
if (my <= 0) {
my = my + 2 * dx1;
} else {
if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0)) {
x = x + 1;
} else {
x = x - 1;
}
my = my + 2 * (dx1 - dy1);
}
if (bounds_check<width, height>(x, y)) {
plot(x, y, color);
}
}
}
}
template<int width, int height, typename PLOT>
uint16_t* draw_image(int x0, int y0, int w, int h, uint16_t* d, PLOT& plot) {
for (int y = y0; y < y0+h; y++) {
if (y < 0) {
d += w;
continue;
}
if (y >= height) {
d += w;
continue;
}
for (int x = x0; x < x0+w; x++, d++) {
if (x < 0) continue;
if (x >= width) continue;
uint16_t c = *d;
if (!is_color_visible(c))
continue;
plot(x, y, c);
}
}
return d;
}
template<unsigned bpp, bool hflip, bool vflip, typename PLOT>
void draw_vram_tile(
int x0, int y0,
int w, int h,
uint16_t vram_addr,
uint8_t palette,
uint8_t* vram,
uint8_t* cgram,
PLOT& plot
) {
// draw tile:
unsigned sy = y0;
for (int ty = 0; ty < h; ty++, sy++) {
sy &= 255;
unsigned sx = x0;
unsigned y = (vflip == false) ? (ty) : (h - 1 - ty);
for(int tx = 0; tx < w; tx++, sx++) {
sx &= 511;
if(sx >= 256) continue;
unsigned x = ((hflip == false) ? tx : (w - 1 - tx));
uint8_t col, d0, d1, d2, d3, d4, d5, d6, d7;
uint8_t mask = 1 << (7-(x&7));
uint8_t *tile_ptr = vram + vram_addr;
switch (bpp) {
case 2:
// 16 bytes per 8x8 tile
tile_ptr += ((x >> 3) << 4);
tile_ptr += ((y >> 3) << 8);
tile_ptr += (y & 7) << 1;
d0 = *(tile_ptr );
d1 = *(tile_ptr + 1);
col = !!(d0 & mask) << 0;
col += !!(d1 & mask) << 1;
break;
case 4:
// 32 bytes per 8x8 tile
tile_ptr += ((x >> 3) << 5);
tile_ptr += ((y >> 3) << 9);
tile_ptr += (y & 7) << 1;
d0 = *(tile_ptr );
d1 = *(tile_ptr + 1);
d2 = *(tile_ptr + 16);
d3 = *(tile_ptr + 17);
col = !!(d0 & mask) << 0;
col += !!(d1 & mask) << 1;
col += !!(d2 & mask) << 2;
col += !!(d3 & mask) << 3;
break;
case 8:
// 64 bytes per 8x8 tile
tile_ptr += ((x >> 3) << 6);
tile_ptr += ((y >> 3) << 10);
tile_ptr += (y & 7) << 1;
d0 = *(tile_ptr );
d1 = *(tile_ptr + 1);
d2 = *(tile_ptr + 16);
d3 = *(tile_ptr + 17);
d4 = *(tile_ptr + 32);
d5 = *(tile_ptr + 33);
d6 = *(tile_ptr + 48);
d7 = *(tile_ptr + 49);
col = !!(d0 & mask) << 0;
col += !!(d1 & mask) << 1;
col += !!(d2 & mask) << 2;
col += !!(d3 & mask) << 3;
col += !!(d4 & mask) << 4;
col += !!(d5 & mask) << 5;
col += !!(d6 & mask) << 6;
col += !!(d7 & mask) << 7;
break;
default:
// TODO: warn
break;
}
// color 0 is always transparent:
if (col == 0)
continue;
col += palette;
// look up color in cgram:
uint16_t bgr = *(cgram + (col<<1)) + (*(cgram + (col<<1) + 1) << 8);
plot(sx, sy, bgr);
}
}
}
template<int width, int height, typename PLOT>
struct Outliner {
explicit Outliner(PLOT& p_plot) : plot(p_plot) {}
PLOT& plot;
void target_updated() {
plot.target_updated();
}
void operator() (int x, int y, uint16_t color) {
if (DrawList::bounds_check<width, height>(x-1, y-1)) {
plot(x-1, y-1, color);
}
if (DrawList::bounds_check<width, height>(x+0, y-1)) {
plot(x+0, y-1, color);
}
if (DrawList::bounds_check<width, height>(x+1, y-1)) {
plot(x+1, y-1, color);
}
if (DrawList::bounds_check<width, height>(x-1, y+0)) {
plot(x-1, y+0, color);
}
if (DrawList::bounds_check<width, height>(x+1, y+0)) {
plot(x+1, y+0, color);
}
if (DrawList::bounds_check<width, height>(x-1, y+1)) {
plot(x-1, y+1, color);
}
if (DrawList::bounds_check<width, height>(x+0, y+1)) {
plot(x+0, y+1, color);
}
if (DrawList::bounds_check<width, height>(x+1, y+1)) {
plot(x+1, y+1, color);
}
}
};
template<int width, int height, typename PLOT>
struct GenericRenderer : public DrawList::Renderer {
explicit GenericRenderer(State& p_state, PLOT& p_plot)
: state(p_state), plot(p_plot), outliner(p_plot), xo(p_state.xOffset), yo(p_state.yOffset)
{
}
State& state;
PLOT& plot;
Outliner<width, height, PLOT> outliner;
int &xo;
int &yo;
void target_updated() override {
plot.target_updated();
}
void draw_pixel(int x0, int y0) override {
DrawList::draw_pixel<width, height>(x0+xo, y0+yo, state.outline_color, outliner);
DrawList::draw_pixel<width, height>(x0+xo, y0+yo, state.stroke_color, plot);
}
void draw_hline(int x0, int y0, int w) override {
DrawList::draw_hline<width, height>(x0+xo, y0+yo, w, state.outline_color, outliner);
DrawList::draw_hline<width, height>(x0+xo, y0+yo, w, state.stroke_color, plot);
}
void draw_vline(int x0, int y0, int h) override {
DrawList::draw_vline<width, height>(x0+xo, y0+yo, h, state.outline_color, outliner);
DrawList::draw_vline<width, height>(x0+xo, y0+yo, h, state.stroke_color, plot);
}
void draw_rect(int x0, int y0, int w, int h) override {
DrawList::draw_rect<width, height>(x0+xo, y0+yo, w, h, state.outline_color, outliner);
DrawList::draw_rect<width, height>(x0+xo, y0+yo, w, h, state.stroke_color, plot);
}
void draw_rect_fill(int x0, int y0, int w, int h) override {
DrawList::draw_rect_fill<width, height>(x0+xo, y0+yo, w, h, state.fill_color, plot);
}
void draw_line(int x0, int y0, int x1, int y1) override {
DrawList::draw_line<width, height>(x0+xo, y0+yo, x1+xo, y1+yo, state.outline_color, outliner);
DrawList::draw_line<width, height>(x0+xo, y0+yo, x1+xo, y1+yo, state.stroke_color, plot);
}
void draw_text_utf8(uint8_t* s, uint16_t len, PixelFont::Font& font, int x0, int y0, text_alignment align) override {
// handle horizontal alignment:
if ((align & DrawList::TEXT_HALIGN_CENTER) || (align & DrawList::TEXT_HALIGN_RIGHT)) {
// determine text width:
int strwidth = font.calc_width(s, len);
// adjust starting x position:
if (align & DrawList::TEXT_HALIGN_CENTER) {
x0 -= strwidth / 2;
} else {
x0 -= strwidth;
}
}
// handle vertical alignment (assuming no newline handling):
if (align & DrawList::TEXT_VALIGN_MIDDLE) {
y0 -= font.m_height / 2;
} else if (align & DrawList::TEXT_VALIGN_BOTTOM) {
y0 -= font.m_height;
}
font.draw_text_utf8<width, height>(s, len, x0+xo, y0+yo, state.outline_color, outliner);
font.draw_text_utf8<width, height>(s, len, x0+xo, y0+yo, state.stroke_color, plot);
}
uint16_t* draw_image(int x0, int y0, int w, int h, uint16_t* d) override {
return DrawList::draw_image<width, height>(x0+xo, y0+yo, w, h, d, plot);
}
void draw_vram_tile(int x0, int y0, int w, int h, bool hflip, bool vflip, uint8_t bpp, uint16_t vram_addr, uint8_t palette, uint8_t* vram, uint8_t* cgram) override {
x0 += xo;
y0 += yo;
switch (bpp) {
case 4:
if (!hflip && !vflip)
DrawList::draw_vram_tile<4, false, false>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
else if (hflip && !vflip)
DrawList::draw_vram_tile<4, true, false>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
else if (!hflip && vflip)
DrawList::draw_vram_tile<4, false, true>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
else if (hflip && vflip)
DrawList::draw_vram_tile<4, true, true>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
break;
case 2:
if (!hflip && !vflip)
DrawList::draw_vram_tile<2, false, false>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
else if (hflip && !vflip)
DrawList::draw_vram_tile<2, true, false>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
else if (!hflip && vflip)
DrawList::draw_vram_tile<2, false, true>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
else if (hflip && vflip)
DrawList::draw_vram_tile<2, true, true>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
break;
case 8:
if (!hflip && !vflip)
DrawList::draw_vram_tile<8, false, false>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
else if (hflip && !vflip)
DrawList::draw_vram_tile<8, true, false>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
else if (!hflip && vflip)
DrawList::draw_vram_tile<8, false, true>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
else if (hflip && vflip)
DrawList::draw_vram_tile<8, true, true>(x0, y0, w, h, vram_addr, palette, vram, cgram, plot);
break;
}
}
};
}