-
Notifications
You must be signed in to change notification settings - Fork 2
/
GFX.cpp
397 lines (358 loc) · 13.3 KB
/
GFX.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
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
//------------------------------------------------------------------------
// Copyright(c) 2024 Dad Design.
// Bibliothèque graphique
//
// Inspiré largement de :
// Adafruit-GFX-Library : https://github.com/adafruit/Adafruit-GFX-Library
// eSPI : https://github.com/Bodmer/TFT_eSPI
//------------------------------------------------------------------------
#include "GFX.h"
#include "Debug.h"
//***********************************************************************************
// CFont
// Gestion des polices de caratères
// Utilisation de la structuration des fonts de Adafruit-GFX-Library
// Ce qui permet de profiter des outils de conversion (ex: https://rop.nl/truetype2gfx/)
cFont::cFont(const GFXfont *pFont)
{
m_pFont = pFont; // Pointe sur le descripteur de font
m_pTable = pFont->glyph; // Pointe sur la table des descriteurs de carratères
// On parcours tous les caratères pour déterminer la hauteur max des caratères
// En sortie nous avons deux variables configurées :
// m_NegHeight = hauteur sous la ligne du curseur;
// m_PosHeight = hauteur au dessus de la ligne du curseur;
GFXglyph *pTable = m_pTable;
uint16_t SizeTable = 1 + pFont->last - pFont->first;
m_NegHeight = 0;
m_PosHeight = 0;
for (uint16_t index = 0; index < SizeTable; index++)
{
int8_t Offset = pTable->yOffset;
int8_t NegHeight = pTable->height + Offset;
if (NegHeight > m_NegHeight)
{
m_NegHeight = NegHeight;
}
if (Offset < m_PosHeight)
{
m_PosHeight = Offset;
}
pTable++;
}
}
//***********************************************************************************
// cGFX
// Bibliothèque Graphique
//-----------------------------------------------------------------------------------
// Tracer un rectange vide
void cGFX::drawRect(uint16_t x, uint16_t y, int16_t dx, int16_t dy, cColor Color){
if(dx < 0){ x += dx; dx = -dx;}
if(dy < 0){ y += dy; dy = -dy;}
drawLine(x, y, x+dx, y, Color);
drawLine(x+dx, y, x+dx, y+dy, Color);
drawLine(x+dx, y+dy, x, y+dy, Color);
drawLine(x, y+dy, x, y, Color);
}
//-----------------------------------------------------------------------------------
// Tracer un rectangle plein
void cGFX::drawFillRect(uint16_t x, uint16_t y, int16_t dx, int16_t dy, cColor Color){
if(dx < 0){ x += dx; dx = -dx;}
if(dy < 0){ y += dy; dy = -dy;}
RGB *pFrame;
RGB *pEndLigne;
for (uint16_t PosY = y; PosY < (y+dy); PosY++){
pFrame = getPtr(x, PosY);
pEndLigne = getPtr(x+dx, PosY);
while (pFrame < pEndLigne){
pFrame->set(Color);
pFrame++;
}
}
}
//-----------------------------------------------------------------------------------
// Dessin d'une ligne
// Merci a Frédéric Goset http://fredericgoset.ovh/
// L'algorithme de ligne de Bresenham
void cGFX::drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, cColor Color)
{
int16_t dx = x1 - x0;
int16_t dy = y1 - y0;
int incX = dx < 0 ? -1 : (dx > 0 ? 1 : 0);
int incY = dy < 0 ? -1 : (dy > 0 ? 1 : 0);
if(dx < 0) dx = -dx;
if(dy < 0) dy = -dy;
RGB *pFrame;
RGB *pEndLigne;
if (dy == 0){
// horizontal line
uint16_t Temp;
if(x1 < x0) {Temp = x1; x1 = x0 ; x0= Temp;}
pFrame = getPtr(x0,y0);
pEndLigne = getPtr(x1,y1);
while (pFrame < pEndLigne){
pFrame->set(Color);
pFrame++;
}
}else if (dx == 0){
uint16_t Temp;
if(y1 < y0) {Temp = y1; y1 = y0 ; y0= Temp;}
pFrame = getPtr(x0,y0);
pEndLigne = getPtr(x1,y1);
while (pFrame < pEndLigne){
pFrame->set(Color);
pFrame += getWidth();
}
}else if (dx >= dy) {
// more horizontal than vertical
int slope = 2 * dy;
int error = -dx;
int errorInc = -2 * dx;
int y = y0;
for (int x = x0; x != x1 + incX; x += incX)
{
setPixel(x,y, Color);
error += slope;
if (error >= 0)
{
y += incY;
error += errorInc;
}
}
}else{
// more vertical than horizontal
int slope = 2 * dx;
int error = -dy;
int errorInc = -2 * dy;
int x = x0;
for (int y = y0; y != y1 + incY; y += incY)
{
setPixel(x,y, Color);
error += slope;
if (error >= 0)
{
x += incX;
error += errorInc;
}
}
}
}
//-----------------------------------------------------------------------------------
// Dessin d'un cercle
// Merci a Frédéric Goset http://fredericgoset.ovh/
// L'algorithme de cercle de Bresenham
void cGFX::drawCircle(uint16_t centerX, uint16_t centerY, uint16_t radius, cColor Color)
{
int16_t x = 0;
int16_t y = radius;
int16_t m = 5 - 4 * radius;
while (x <= y)
{
setPixel(centerX + x, centerY + y, Color);
setPixel(centerX + x, centerY - y, Color);
setPixel(centerX - x, centerY + y, Color);
setPixel(centerX - x, centerY - y, Color);
setPixel(centerX + y, centerY + x, Color);
setPixel(centerX + y, centerY - x, Color);
setPixel(centerX - y, centerY + x, Color);
setPixel(centerX - y, centerY - x, Color);
if (m > 0)
{
y--;
m -= 8 * y;
}
x++;
m += 8 * x + 4;
}
}
//-----------------------------------------------------------------------------------
// Dessin d'un arc de cercle
// L'algorithme de cercle de Bresenham
void cGFX::drawArc(uint16_t centerX, uint16_t centerY, uint16_t radius, uint16_t AlphaIn, uint16_t AlphaOut, cColor Color)
{
bool Inv = false;
if(AlphaIn > AlphaOut){
uint16_t Temp = AlphaIn;
AlphaIn = AlphaOut;
AlphaOut = Temp;
Inv = true;
}
int16_t x = 0;
int16_t y = radius;
int16_t m = 5 - 4 * radius;
while (x <= y) {
uint16_t angle = atan2((float) y, (float) x) * 180 / __PI; // Conversion de radians en degrés
if(Inv){
uint16_t angle1 = 90 - angle;
if(!((angle1 >= AlphaIn) && (angle1 <= AlphaOut))) setPixel(centerX + x, centerY - y, Color);
uint16_t angle2 = angle;
if(!((angle2 >= AlphaIn) && (angle2 <= AlphaOut))) setPixel(centerX + y, centerY - x, Color);
angle1 += 90;
if(!((angle1 >= AlphaIn) && (angle1 <= AlphaOut))) setPixel(centerX + y, centerY + x, Color);
angle2 += 90;
if(!((angle2 >= AlphaIn) && (angle2 <= AlphaOut))) setPixel(centerX + x, centerY + y, Color);
angle1 += 90;
if(!((angle1 >= AlphaIn) && (angle1 <= AlphaOut))) setPixel(centerX - x, centerY + y, Color);
angle2 += 90;
if(!((angle2 >= AlphaIn) && (angle2 <= AlphaOut))) setPixel(centerX - y, centerY + x, Color);
angle1 += 90;
if(!((angle1 >= AlphaIn) && (angle1 <= AlphaOut))) setPixel(centerX - y, centerY - x, Color);
angle2 += 90;
if(!((angle2 >= AlphaIn) && (angle2 <= AlphaOut))) setPixel(centerX - x, centerY - y, Color);
}else{
uint16_t angle1 = 90 - angle;
if(((angle1 >= AlphaIn) && (angle1 <= AlphaOut))) setPixel(centerX + x, centerY - y, Color);
uint16_t angle2 = angle;
if(((angle2 >= AlphaIn) && (angle2 <= AlphaOut))) setPixel(centerX + y, centerY - x, Color);
angle1 += 90;
if(((angle1 >= AlphaIn) && (angle1 <= AlphaOut))) setPixel(centerX + y, centerY + x, Color);
angle2 += 90;
if(((angle2 >= AlphaIn) && (angle2 <= AlphaOut))) setPixel(centerX + x, centerY + y, Color);
angle1 += 90;
if(((angle1 >= AlphaIn) && (angle1 <= AlphaOut))) setPixel(centerX - x, centerY + y, Color);
angle2 += 90;
if(((angle2 >= AlphaIn) && (angle2 <= AlphaOut))) setPixel(centerX - y, centerY + x, Color);
angle1 += 90;
if(((angle1 >= AlphaIn) && (angle1 <= AlphaOut))) setPixel(centerX - y, centerY - x, Color);
angle2 += 90;
if(((angle2 >= AlphaIn) && (angle2 <= AlphaOut))) setPixel(centerX - x, centerY - y, Color);
}
if (m > 0)
{
y--;
m -= 8 * y;
}
x++;
m += 8 * x + 4;
}
}
//-----------------------------------------------------------------------------------
// Dessin d'un cercle plein
void cGFX::drawFillCircle(uint16_t centerX, uint16_t centerY, uint16_t radius, cColor Color)
{
int32_t x = 0;
int32_t dx = 1;
int32_t dy = radius+radius;
int32_t p = -(radius>>1);
uint16_t x1 = centerX - radius;
uint16_t x2 = x1 + dy+1;
uint16_t y1 = centerY;
RGB *pFrame = getPtr(x1, y1);
for (int xx = x1; xx <= x2; xx++)
pFrame++->set(Color);
while(x<radius){
if(p>=0) {
x1 = centerX - x;
x2 = x1 + dx;
y1 = centerY + radius;
pFrame = getPtr(x1, y1);
for (int xx = x1; xx <= x2; xx++)
pFrame++->set(Color);
y1 = centerY - radius;
pFrame = getPtr(x1, y1);
for (int xx = x1; xx <= x2; xx++)
pFrame++->set(Color);
dy-=2;
p-=dy;
radius--;
}
dx+=2;
p+=dx;
x++;
x1 = centerX - radius;
x2 = x1 + dy+1;
y1 = centerY + x;
pFrame = getPtr(x1, y1);
for (int xx = x1; xx <= x2; xx++)
pFrame++->set(Color);
y1 = centerY - x;
pFrame = getPtr(x1, y1);
for (int xx = x1; xx <= x2; xx++)
pFrame++->set(Color);
}
}
//-----------------------------------------------------------------------------------
// Tracer une image 8bits par couleurs (depreciated)
void cGFX::drawR8G8B8Image(uint16_t x, uint16_t y, uint16_t dx, uint16_t dy, const uint8_t *pImg){
RGB *pFrame;
RGB *pEndLigne;
const uint8_t *pImgLine;
for (uint16_t PosY = y; PosY < (y+dy); PosY++){
pFrame = getPtr(x, PosY);
pEndLigne = getPtr(x+dx, PosY);
pImgLine = pImg+(dx*(PosY-y)*3);
while (pFrame < pEndLigne){
pFrame->set(cColor((*(pImgLine+2)),(*(pImgLine+1)),(*(pImgLine))));
pImgLine = pImgLine+3;
pFrame++;
}
}
}
//-----------------------------------------------------------------------------------
// Tracer une image
void cGFX::drawImage(uint16_t x, uint16_t y, cImage &Image){
RGB *pFrame;
RGB *pEndLigne;
const uint8_t *pImgLine;
for (uint16_t PosY = y; PosY < (y+Image.getHeight()); PosY++){
pFrame = getPtr(x, PosY);
pEndLigne = getPtr(x+Image.getWith(), PosY);
pImgLine = Image.GetPtrLine(PosY-y);
while (pFrame < pEndLigne){
pFrame->set(Image.getColor(pImgLine));
pImgLine = pImgLine + Image.getPixelSize();
pFrame++;
}
}
}
// ==========================================================================
// Dessiner du texte
//-----------------------------------------------------------------------------------
// Dessiner le caractère c
void cGFX::drawChar(const char c, bool Erase){
const GFXglyph *pTable = m_pFont->getGFXglyph(c);
const uint8_t *pBitmap = m_pFont->getBitmap(c);
uint8_t BitMap = *pBitmap++;
uint8_t numBit = 0;
uint8_t indexX;
RGB * pFrame;
for (uint8_t indexY=0; indexY < pTable->height; indexY ++){
indexX = 0;
pFrame = getPtr(m_xCursor+pTable->xOffset, indexY+m_yCursor+pTable->yOffset);
while (indexX < pTable->width){
if((BitMap&0x80) != 0){
if(Erase){
pFrame->set(m_TextBackColor);
}else{
pFrame->set(m_TextFrontColor);
}
}
pFrame++;
numBit++;
if(numBit > 7){
numBit = 0;
BitMap = *pBitmap++;
}else{
BitMap <<= 1 ;
}
indexX++;
}
}
m_xCursor += pTable->xAdvance;
}
//-----------------------------------------------------------------------------------
// Dessiner le texte
void cGFX::drawText(const char *Text, bool Erase){
drawFillRect(m_xCursor, m_yCursor-m_pFont->getPosHeight(),m_pFont->getTextWidth(Text), m_pFont->getHeight(), m_TextBackColor);
const char *pText = Text;
while(*pText != '\0'){
drawChar(*pText++, Erase);
}
}
//-----------------------------------------------------------------------------------
// Dessiner le texte sans couleur d'arriere plan
void cGFX::drawTransText(const char *Text, bool Erase){
const char *pText = Text;
while(*pText != '\0'){
drawChar(*pText++, Erase);
}
}