forked from bertrik/scarlettscroller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.cpp
97 lines (82 loc) · 1.81 KB
/
draw.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
#include <string.h>
#include <math.h>
#include "font.h"
#include "framebuffer.h"
#include "draw.h"
static uint8_t *_framebuffer;
static uint8_t gamma_corr[256];
static bool fliph, flipv;
void draw_init(uint8_t *framebuffer, double gamma)
{
_framebuffer = framebuffer;
for (int i = 0; i < 256; i++) {
gamma_corr[i] = round(pow(i / 255.0, gamma) * 255);
}
fliph = false;
flipv = false;
}
void draw_flip(bool horizontal, bool vertical)
{
fliph = horizontal;
flipv = vertical;
}
void draw_clear(void)
{
memset(_framebuffer, 0, LED_WIDTH * LED_HEIGHT);
}
bool draw_pixel(int x, int y, uint8_t c)
{
if ((x < 0) || (x >= LED_WIDTH) || (y < 0) || (y >= LED_HEIGHT)) {
return false;
}
if (fliph) {
x = LED_WIDTH - 1 - x;
}
if (flipv) {
y = LED_HEIGHT - 1 - y;
}
_framebuffer[y * LED_WIDTH + x] = gamma_corr[c];
return true;
}
void draw_vline(int x, uint8_t c)
{
for (int y = 0; y < LED_HEIGHT; y++) {
draw_pixel(x, y, c);
}
}
int draw_glyph(int g, int x, uint8_t fg, uint8_t bg)
{
// ASCII?
if (g > 127) {
return x;
}
// draw glyph
unsigned char aa = 0;
for (int col = 0; col < 5; col++) {
unsigned char a = font[g][col];
// skip repeating space
if ((aa == 0) && (a == 0)) {
continue;
}
aa = a;
// draw column
for (int y = 0; y < 7; y++) {
draw_pixel(x, y, (a & 1) ? fg : bg);
a >>= 1;
}
x++;
}
// draw space until next character
if (aa != 0) {
draw_vline(x, bg);
x++;
}
return x;
}
int draw_text(const char *text, int x, uint8_t fg, uint8_t bg)
{
for (size_t i = 0; i < strlen(text); i++) {
x = draw_glyph(text[i], x, fg, bg);
}
return x;
}