-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicGraphics.c
535 lines (430 loc) · 11.9 KB
/
basicGraphics.c
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
* basicGraphics.c
*
* Created on: Dec 18, 2018
* Author: Ronald Sousa https://hashdefineelectronics.com
*
* This is the a simple graphics library for drawing primitives, fonts and bitmaps
*/
#include "basicGraphics.h"
#include <stdlib.h>
#include "../common.h"
/**
* Holds the current fonts
*/
static const GFXfont * CurrentFont = ((void*)0);
/**
* Holds the display driver
*/
static DisplayInterfaceType *Driver;
/**
* configure our graphics display
*/
static void Init(DisplayInterfaceType * driver, const GFXfont * font) {
if(!driver || !font) {
return;
}
CurrentFont = font;
Driver = driver;
}
static void getStringJustificationPos(basicStringBoundType * TextBounds, GraphicsTextPostEnumType justification, uint32_t containerWidth, uint32_t containerHeight) {
if(containerWidth) {
if(containerWidth < TextBounds->width) {
containerWidth = TextBounds->width;
}
switch(justification) {
case Text_Center:
TextBounds->x = ((containerWidth - TextBounds->width) / 2);
break;
case Text_Right:
case Text_RightCenter:
case Text_RightTop:
TextBounds->x = (containerWidth - TextBounds->width);
break;
case Text_Left:
case Text_LeftCenter:
case Text_LeftTop:
default:
TextBounds->x = 0;
break;
}
}
if(containerHeight) {
switch(justification) {
case Text_Center:
case Text_LeftCenter:
case Text_RightCenter:
if(containerHeight < TextBounds->height) {
containerHeight = TextBounds->height;
}
TextBounds->y = ((containerHeight + TextBounds->height) / 2);
break;
case Text_RightTop:
case Text_LeftTop:
TextBounds->y = 0;
break;
case Text_Right:
case Text_Left:
default:
// default to bottom
TextBounds->y = containerHeight;
break;
}
}
}
/**
* this function will return the give text height and width bound.
*
* @param text is the pointer to the string to find screen bound
* @param font is the font pointer
* @param bounds pointer to return the new bound values
*/
static void getStringBounds(uint8_t * text, GFXfont * font, basicStringBoundType * bounds) {
GFXglyph *Glyph;
uint8_t *Bitmap;
uint8_t TempChar;
GFXfont * Font = font;
uint32_t YTemp = 0;
uint32_t XTemp = 0;
bounds->x = 0;
bounds->y = 0;
bounds->width = 0;
bounds->height = 0;
if(!bounds) {
return;
}
if(!Font) {
if(!CurrentFont) {
/// @TODO return invalid pointer error
return;
}
Font = (GFXfont *)CurrentFont;
}
bounds->height = Font->maxHeight;
while(*text) {
TempChar = *text;
// make sure that we can render the character
if(TempChar != '\r' && TempChar >= Font->first && TempChar <= Font->last ) {
TempChar -= Font->first;
Glyph = &Font->glyph[TempChar];
bounds->width += Glyph->xAdvance;
}
text++;
}
if(Driver && bounds->width > Driver->Width) {
bounds->width = Driver->Width;
}
}
/**
* handles rendering the character with the given font.
*
* @param glyph pointer to the character data structure
* @param bitmap pointer to the character bitmap data buffer
* @param character the ascii character to render
* @param xPos pointer to the X axis position
* @param yPos pointer to the Y axis position
* @param colour this is the colour that the text will be drawn as. Current only support monotone which is TRUE or FALSE.
*/
static GraphicsReturnType renderCharacter( GFXglyph *glyph,
uint8_t *bitmap,
uint8_t character,
uint32_t *xPos,
uint32_t *yPos,
uint_fast8_t colour) {
if(!CurrentFont || !Driver || !Driver->SetPixel) {
// invalid pointer
return RBasicGReturned_InvalidPointer;
}
uint_fast8_t Height = glyph->height;
uint_fast8_t Width = glyph->width;
int32_t xOffset = glyph->xOffset;
int32_t yOffset = glyph->yOffset;
uint8_t BitIndex = 0;
uint16_t SegmentIndex = glyph->bitmapOffset;
// this has the section of bits that make up the character.
uint8_t Segment = 0;
int32_t YIndex;
int32_t XIndex;
/// @Todo Add character clipping here
for(YIndex = 0; YIndex < Height; YIndex++) {
for(XIndex = 0; XIndex < Width; XIndex++) {
// check if we need to get the next 8bit of data for this character
if(!(BitIndex++ & 7)) {
Segment = bitmap[SegmentIndex++];
}
if(Segment & 0x80) {
Driver->SetPixel(*xPos + xOffset + XIndex, *yPos + yOffset + YIndex, colour);
}
Segment <<= 1;
}
}
return BasicGReturned_OK;
}
/*
* Handles drawing string onto the string
*
* @param *text is the pointer to a null terminated string
* @param xPos is the X axis position
* @param yPos is the Y axis position. Note that yPos is draw from bottom up which mean that the minimum yPos should be the text height
* @param colour this is the colour that the text will be drawn as. Current only support monotone which is TRUE or FALSE.
*/
static void WriteString(uint8_t * text, uint32_t xPos, uint32_t yPos, uint_fast8_t colour, const GFXfont * fontToUse) {
GFXglyph *Glyph;
uint8_t *Bitmap;
uint8_t TempChar;
GFXfont * Font = (GFXfont *)fontToUse;
if(!CurrentFont || !Driver || !Driver->SetPixel) {
/// @TODO return invalid pointer error
return;
}
if(!Font) {
Font = (GFXfont *)CurrentFont;
}
while(*text) {
TempChar = *text;
// make sure that we can render the character
if(TempChar >= Font->first && TempChar <= Font->last ) {
TempChar -= Font->first;
Glyph = &Font->glyph[TempChar];
// make sure that we aren't trying to render an empty character
if(Glyph->width && Glyph->height) {
Bitmap = Font->bitmap;
renderCharacter(Glyph, Bitmap, TempChar, &xPos, &yPos, colour);
}
xPos += Glyph->xAdvance;
}
text++;
}
}
/**
* this function should be call to clean and free up resources
*/
static void Destroy(void) {
if(!Driver || !Driver->Close) {
return;
}
Driver->Close(true);
}
/**
* Clears the screen buffer
*/
static void Clear(void) {
if(!Driver || !Driver->Clear) {
return;
}
Driver->Clear();
}
/**
* Send the current draw buffer to the screen
*/
static void Flush(void) {
if(!Driver || !Driver->Sync) {
return;
}
Driver->Sync();
}
/**
* Sanitases the X and Y coordinates to ensure that it doesn't surpases the screen range
*/
static void SanitisePositionRange(int32_t *x, int32_t *y) {
if(!Driver || !Driver->SetPixel) {
return;
}
if(*x < 0) {
*x = 0;
}
if(*y < 0) {
*y = 0;
}
if(*x > (Driver->Width - 1)) {
*x = Driver->Width -1;
}
if(*y > (Driver->Height - 1)) {
*y = Driver->Height -1;
}
}
/**
* This function implements the line drawing using Bresenham's line algorithm.
*
* @note This code was inspired by https://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#C
*
* @param xStart This is the X start position.
* @param yStart This is the Y start position.
* @param xEnd This is the X end position.
* @param yEnd This is the Y end position.
* @param colour the pixel colour value
*/
static void drawLine(int32_t xStart, int32_t yStart, int32_t xEnd, int32_t yEnd, uint_fast8_t colour) {
// make sure that have a driver that we can use
if(!Driver || !Driver->SetPixel) {
return;
}
SanitisePositionRange(&xStart, &yStart);
SanitisePositionRange(&xEnd, &yEnd);
// take the difference between the start and end axis
int32_t DeltaX = abs(xEnd - xStart);
int32_t DeltaY = abs(yEnd - yStart);
// This get which direction we draw the line.
int32_t OffestX = xStart < xEnd ? 1 : -1;
int32_t OffestY = yStart < yEnd ? 1 : -1;
// this is the expected worse error
int32_t DeltaError = ((DeltaX > DeltaY ? DeltaX : -DeltaY) / 2);
// this is the current line error
int32_t Error;
for(;;) {
Driver->SetPixel(xStart, yStart, colour);
if (xStart == xEnd && yStart == yEnd) {
break; // we made it to the end point, let get out
}
// update our error
Error = DeltaError;
// update Y axis error and position
if (Error > -DeltaX) {
DeltaError -= DeltaY;
xStart += OffestX;
}
// update Y axis error and position
if (Error < DeltaY) {
DeltaError += DeltaX;
yStart += OffestY;
}
}
}
void drawRectagle(int32_t xStart, int32_t yStart, int32_t xEnd, int32_t yEnd, uint_fast8_t colour, uint_fast8_t fill) {
// make sure that have a driver that we can use
int32_t Counter;
int32_t Diff;
if(!Driver || !Driver->SetPixel) {
return;
}
SanitisePositionRange(&xStart, &yStart);
SanitisePositionRange(&xEnd, &yEnd);
if(fill) {
Diff = xEnd < xStart ? -1 : 1;
for( ;xEnd != xStart;) {
drawLine(xStart, yStart, xStart, yEnd, colour);
xStart += Diff;
}
} else {
drawLine(xStart, yStart, xStart, yEnd, colour);
drawLine(xStart, yStart, xEnd, yStart, colour);
drawLine(xEnd, yEnd, xEnd, yStart, colour);
drawLine(xEnd, yEnd, xStart, yEnd, colour);
}
}
/**
* @note this implementation was taken from https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
*
* @param x0 circle's x position for its center.
* @param y0 circle's y position for its center.
* @param radius circle's radius
* @param colour the circle colour
* @param fill if true then fill else just draw the line
*/
void drawCircle(int32_t x0, int32_t y0, int32_t radius, uint_fast8_t colour, uint_fast8_t fill) {
// make sure that have a driver that we can use
if(!Driver || !Driver->SetPixel) {
return;
}
SanitisePositionRange(&x0, &y0);
int32_t x = radius-1;
int32_t y = 0;
int32_t dx = 1;
int32_t dy = 1;
int32_t err = dx - (radius << 1);
while (x >= y) {
if(fill) {
drawLine(x0 + x, y0 + y, x0 - x, y0 + y, colour);
drawLine(x0 + y, y0 + x, x0 - y, y0 + x, colour);
drawLine(x0 - x, y0 - y, x0 + x, y0 - y, colour);
drawLine(x0 - y, y0 - x, x0 + y, y0 - x, colour);
} else {
Driver->SetPixel(x0 + x, y0 + y, colour);
Driver->SetPixel(x0 - x, y0 + y, colour);
Driver->SetPixel(x0 + y, y0 + x, colour);
Driver->SetPixel(x0 - y, y0 + x, colour);
Driver->SetPixel(x0 - x, y0 - y, colour);
Driver->SetPixel(x0 + x, y0 - y, colour);
Driver->SetPixel(x0 - y, y0 - x, colour);
Driver->SetPixel(x0 + y, y0 - x, colour);
}
if (err <= 0) {
y++;
err += dy;
dy += 2;
}
if (err > 0) {
x--;
dx += 2;
err += dx - (radius << 1);
}
}
}
/**
* this is the data size for the icon array
*/
static const uint_fast8_t IconDataBitSize = 32;
/**
* Draws directly into the screen buffer. the image should be the same size as the screen buffer
*/
static void drawFullScreen(uint8_t *source) {
if(Driver->directWriteToBuffer) {
Driver->directWriteToBuffer(source);
}
}
/**
* Draw an icon
*
* @param x start position x
* @param y start position x
* @param height icon height
* @param width icon width
* @param source pointer to the data array
*/
static void drawIcon(int32_t x, int32_t y, uint32_t height, uint32_t width, uint_fast8_t colour, uint32_t *source) {
uint32_t HeightIndex;
uint32_t WidthIndex;
uint32_t BitIndex = 0;
uint32_t Value = *source;
for(WidthIndex = 0 ; WidthIndex < width; WidthIndex++) {
for(HeightIndex = 0 ;HeightIndex < height; HeightIndex++) {
Driver->SetPixel(x + WidthIndex, y + HeightIndex, (Value & 0x80000000) ? colour : 0);
if(BitIndex < (IconDataBitSize -1)) {
BitIndex++;
Value = Value << 1;
} else {
BitIndex = 0;
source++;
Value = *source;
}
}
}
}
static void Fill(uint8_t value) {
if(!Driver || !Driver->Fill) {
return;
}
Driver->Fill(value);
}
static void Reset(uint_fast8_t resetBuffer) {
Driver->Reset(resetBuffer);
}
/**
* This is our graphics instance
*/
SimpleGraphcisType GraphicsInstance = {
Init: Init,
Reset: Reset,
Destroy: Destroy,
Clear: Clear,
Flush: Flush,
WriteString: WriteString,
GetStringBounds: getStringBounds,
getStringJustificationPos : getStringJustificationPos,
drawLine: drawLine,
drawCircle: drawCircle,
drawRectagle: drawRectagle,
drawIcon : drawIcon,
drawFullScreen : drawFullScreen,
Fill: Fill
};