This repository was archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.cpp
More file actions
263 lines (242 loc) · 7.57 KB
/
graphics.cpp
File metadata and controls
263 lines (242 loc) · 7.57 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
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
/*
* This file is part of QtLedTest.
*
* QtLedTest is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QtLedTest is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QtLedTest. If not, see <http://www.gnu.org/licenses/>.
*/
#include "graphics.h"
#include <math.h>
#include <stdint.h>
#define toHandle(obj) ((GraphicsHandle)obj)
#define toObject(handle) ((GraphicsObj*)handle)
#include <QApplication>
void update_display(void);
static int _getPixel_1bpp(GraphicsHandle handle, void *buf, int x, int y, int stride)
{
(void)handle;
uint8_t *mem = (uint8_t*)buf;
mem += (y / 8) * stride;
mem += x;
int shift = y % 8;
return ((*mem) >> shift) & 0x01;
}
static void _setPixel_1bpp(GraphicsHandle handle, void *buf, int x, int y, int stride, int color)
{
(void)handle;
uint8_t *mem = (uint8_t*)buf;
int page = (y / 8);
int col = x;
mem += page * stride;
mem += col;
//TODO: Check bounds
int shift = y % 8;
uint8_t m = *mem;
m &= ~(1 << shift);
m |= (color << shift) & (1 << shift);
*mem = m;
}
GraphicsHandle Graphics_init(GraphicsObj *obj, void *gpu_mem, int gpu_size, int width, int height, int depth)
{
obj->gpu_mem = gpu_mem;
obj->gpu_size = gpu_size;
obj->width = width;
obj->height = height;
obj->depth = depth = 1;
obj->stride = width;
obj->offset = 0;
obj->fillColor = 1;
obj->penColor = 1;
switch (depth) {
case 1:
obj->getPixel = _getPixel_1bpp;
obj->setPixel = _setPixel_1bpp;
break;
default:
obj->getPixel = 0;
obj->setPixel = 0;
break;
}
return toHandle(obj);
}
void Graphics_setRenderOffset(GraphicsHandle handle, int offset)
{
GraphicsObj *obj = toObject(handle);
obj->offset = offset;
}
void Graphics_setFill(GraphicsHandle handle, int color)
{
GraphicsObj *obj = toObject(handle);
obj->fillColor = color;
}
void Graphics_setPen(GraphicsHandle handle, int color)
{
GraphicsObj *obj = toObject(handle);
obj->penColor = color;
}
void Graphics_fillRect(GraphicsHandle handle, int x, int y, int w, int h)
{
GraphicsObj *obj = toObject(handle);
uint8_t *dst = (uint8_t*)obj->gpu_mem + obj->offset;
for (int dy = 0; dy < h; ++dy) {
for (int dx = 0; dx < w; ++dx) {
obj->setPixel(handle, dst, x + dx, y + dy, obj->stride, obj->fillColor);
}
}
}
void Graphics_drawRect(GraphicsHandle handle, int x, int y, int w, int h)
{
w--;
h--;
Graphics_drawLine(handle, x, y, x + w, y);
Graphics_drawLine(handle, x + w, y, x + w, y + h);
Graphics_drawLine(handle, x, y + h, x + w, y + h);
Graphics_drawLine(handle, x, y, x, y + h);
}
void Graphics_drawLine(GraphicsHandle handle, int x0, int y0, int x1, int y1)
{
GraphicsObj *obj = toObject(handle);
uint8_t *dst = (uint8_t*)obj->gpu_mem + obj->offset;
int rise = (y1 - y0);
int run = (x1 - x0);
if (run != 0) {
// Horizontal or sloped line
float m = (float)rise / (float)run;
int xf = x0 + run;
int dx = (run > 0) ? 1 : -1;
for (int x = x0; x != xf + dx; x += dx) {
int y = (int)(m * (float)x) + y0;
obj->setPixel(handle, dst, x, y, obj->stride, obj->penColor);
}
} else if (rise != 0) {
// Vertical line
int yf = y0 + rise;
int dy = (rise > 0) ? 1 : -1;
for (int y = y0; y != yf + dy; y += dy) {
obj->setPixel(handle, dst, x0, y, obj->stride, obj->penColor);
}
} else {
obj->setPixel(handle, dst, x0, y0, obj->stride, obj->penColor);
}
}
void Graphics_drawArc(GraphicsHandle handle, int x, int y, int r, int t0, int t1)
{
GraphicsObj *obj = toObject(handle);
uint8_t *dst = (uint8_t*)obj->gpu_mem + obj->offset;
int dt = (t0 < t1) ? 1 : -1;
float fr = (float)r;
for (int t = t0; t != t1 + dt; t += dt) {
float rad = (float)t / 360.0 * (2.0f*3.14f);
float fx = (float)x + fr*cos(rad);
float fy = (float)y + fr*sin(rad);
if (r < 4) {
int xfrac = (int)(fx * 10) % 10;
int yfrac = (int)(fy * 10) % 10;
if (xfrac > 2 && xfrac < 8)
continue;
if (yfrac > 2 && yfrac < 8)
continue;
}
int _x = round(fx);
int _y = round(fy);
obj->setPixel(handle, dst, _x, _y, obj->stride, obj->penColor);
}
}
void Graphics_drawTab(GraphicsHandle handle, int x, int y, int w, int h, int r, bool bottom, bool flat_edge)
{
w--;
h--;
h -= r;
if (bottom) {
y += r;
Graphics_drawLine(handle, x + w, y, x + w, y + h);
Graphics_drawLine(handle, x, y, x, y + h);
if (flat_edge) {
Graphics_drawLine(handle, x, y + h, x + w, y + h);
}
Graphics_drawArc(handle, x + r, y, r, -180, -90);
Graphics_drawArc(handle, x + w - r, y, r, 0, -90);
Graphics_drawLine(handle, x + r, y - r, x + w - r, y - r);
} else {
h -= r;
Graphics_drawLine(handle, x + w, y, x + w, y + h);
Graphics_drawLine(handle, x, y, x + w, y);
if (flat_edge) {
Graphics_drawLine(handle, x, y + h, x + w, y + h);
}
}
}
void Graphics_measureString(GraphicsHandle handle, FontInfo *font, const char *str, int *w, int *h)
{
(void)handle;
const char *s = str;
char c = 0;
*w = 0;
*h = font->height;
while ((c = *s++) != '\0') {
if (c == '\n') {
*h += font->height;
} else {
CharInfo *ci = Font_getCharInfo(font, c);
if (ci) {
*w += ci->width;
} else {
*w += font->size + 2;
}
}
}
}
void Graphics_drawChar(GraphicsHandle handle, FontInfo *font, CharInfo *ci, char c, int x, int y)
{
GraphicsObj *obj = toObject(handle);
if (!ci) {
ci = Font_getCharInfo(font, c);
}
if (ci) {
uint8_t *dst = (uint8_t*)obj->gpu_mem + obj->offset;
int src_x = ci->rect[0];
int src_y = ci->rect[1];
int src_w = ci->rect[2];
int src_h = ci->rect[3];
int dst_x = x + ci->offset[0];
int dst_y = y + ci->offset[1];
for (int dy = 0; dy < src_h; ++dy) {
for (int dx = 0; dx < src_w; ++dx) {
if (obj->getPixel(handle, font->data, src_x + dx, src_y + dy, font->stride) != obj->fillColor)
obj->setPixel(handle, dst, dst_x + dx, dst_y + dy, obj->stride, obj->penColor);
}
}
}
}
void Graphics_drawString(GraphicsHandle handle, FontInfo *font, const char *str, int x, int y)
{
const char *s = str;
char c = 0;
int orig_x = x;
while ((c = *s++) != '\0') {
if (c == '\n') {
x = orig_x;
y += font->size;
} else if (c == '\r') {
x = orig_x;
} else {
CharInfo *ci = Font_getCharInfo(font, c);
if (ci) {
Graphics_drawChar(handle, font, ci, c, x, y);
x += ci->width;
} else {
Graphics_drawRect(handle, x + 1, y + 1, font->size, font->size);
x += font->size + 2;
}
}
}
}