-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfont.c
More file actions
104 lines (86 loc) · 2.71 KB
/
Copy pathfont.c
File metadata and controls
104 lines (86 loc) · 2.71 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
#include <stdint.h>
#include <string.h>
#include "graphics.h"
#include "memory.h"
#include "font.h"
#include "assets/basic_font.h"
#include "tinybit.h"
int cursorX = 0;
int cursorY = 0;
const int fontWidth = 4;
const int fontHeight = 6;
uint16_t textColor = 0xFFFF;
char characters[16 * 8] = {
'?', '"', '%', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '!', ' ', ' ', ' ',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
'@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '[', '\\', ']', '^', '_',
'`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
};
// Reset all font state to defaults (called from tinybit_init)
void font_init() {
cursorX = 0;
cursorY = 0;
textColor = 0xFFFF;
}
// Set the text color for font rendering
void font_text_color(uint16_t color) {
textColor = color;
}
// Set the cursor position for text rendering
void font_cursor(int x, int y) {
cursorX = x;
cursorY = y;
}
// Print text string at current cursor position using bitmap font
void font_print(const char* str) {
const char* ptr = str;
int location = 0;
int startX = cursorX;
while (*ptr) {
// process newline character
if (*ptr == '\n') {
cursorY += fontHeight;
cursorX = startX;
ptr++;
continue;
}
// get character location
location = 0;
for (int i = 0; i < 16 * 8; i++) {
if (*ptr == characters[i]) {
location = i;
break; // Stop at the first match
}
}
// draw the character using direct pixel blending
int charRow = location / 16;
int charCol = location % 16;
for (int y = 0; y < fontHeight; y++) {
for (int x = 0; x < fontWidth; x++) {
int px = cursorX + x;
int py = cursorY + y;
if (px >= 0 && px < TB_SCREEN_WIDTH && py >= 0 && py < TB_SCREEN_HEIGHT) {
int byteIndex = (charRow * (fontHeight+2)+y) * 16 + charCol;
// Bounds check for font array access
if (byteIndex >= 0 && byteIndex < sizeof(basic_font)) {
int bitPosition = 7 - x;
uint8_t font_byte = basic_font[byteIndex];
uint16_t* pixel = &tinybit_memory->display[py * TB_SCREEN_WIDTH + px];
if ((font_byte >> bitPosition) & 1) {
blend(pixel, textColor);
}
else {
blend(pixel, fillColor);
}
}
}
}
}
cursorX += fontWidth;
ptr++;
}
}