-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSprite8bpp.cpp
107 lines (86 loc) · 2.13 KB
/
Sprite8bpp.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
//
// sprite8bpp.cpp
//
#include "Sprite8bpp.h"
void Sprite8bpp::fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint8_t color, bool blend) {
if (!_created ) return;
if (x < 0) { w += x; x = 0; }
if ((x < 0) || (y < 0) || (x >= _iwidth) || (y >= _iheight)) return;
if ((x + w) > _iwidth) w = _iwidth - x;
if ((y + h) > _iheight) h = _iheight - y;
if ((w < 1) || (h < 1)) return;
int32_t yp = _iwidth * y + x;
if (!blend) {
while (h--) {
memset(_img8 + yp, color, w);
yp += _iwidth;
}
} else {
while (h--) {
uint8_t *p = _img8 + yp;
for (int i = 0; i < w; i++) {
*p++ |= color;
}
yp += _iwidth;
}
}
}
void Sprite8bpp::fill(uint8_t color) {
memset(_img8, color, _iwidth * _iheight);
}
void Sprite8bpp::blendAlphaBitmap(int32_t x, int32_t y, int32_t w, int32_t h, const uint8_t *bitmap, int ratio) {
if (x < 0) { w += x; x = 0; }
if ((x < 0) || (y < 0) || (x >= _iwidth) || (y >= _iheight)) return;
if ((x + w) > _iwidth) w = _iwidth - x;
if ((y + h) > _iheight) h = _iheight - y;
if ((w < 1) || (h < 1)) return;
int offset = _iwidth * y + x;
const uint8_t *p = bitmap;
while (h--) {
uint8_t *q = _img8 + offset;
for (int i = 0; i < w; i++) {
uint8_t b = pgm_read_byte(p++);
uint16_t d = *q;
if (d == 0) {
*q++ = b;
} else {
d = (b >= d) ? b + d / ratio : b / ratio + d; // TODO
if (d > 255) {
d = 255;
}
*q++ = (uint8_t)d;
}
}
offset += _iwidth;
}
}
void Sprite8bpp::flush(uint16_t *palette) {
if (palette == NULL) {
pushSprite(0, 0);
return;
}
int x = 0;
int y = 0;
int w = _dwidth;
int h = _dheight;
startWrite();
inTransaction = true;
// setAddrWindow(x, y, x + w - 1, y + h - 1);
setWindow(x, y, x + w - 1, y + h - 1);
uint16_t lineBuf[w];
uint8_t *data = _img8 + x + y * w;
int dh = h;
while (dh--) {
int len = w;
uint8_t* p = data;
uint16_t* q = lineBuf;
while(len--) {
*q++ = palette[*p++];
}
pushColors(lineBuf, w, true); // swapByte = true
data += w;
}
CS_H;
inTransaction = false;
endWrite();
}