-
Notifications
You must be signed in to change notification settings - Fork 0
/
Font.h
122 lines (97 loc) · 2.83 KB
/
Font.h
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
#pragma once
#include "Graphics.h"
#include <fstream>
namespace Font
{
const char* PATH = "font.bin";
constexpr int FONT_SIZE = 10;
constexpr int CHAR_GAP_PX = 2;
constexpr int FONT_SIZE_WITHOUT_GAP = FONT_SIZE - CHAR_GAP_PX;
constexpr int FONT_SIZE_WITH_DOUBLE_GAP = FONT_SIZE + CHAR_GAP_PX;
int scaledWorldFontSize = FONT_SIZE;
constexpr int H_FONT_SIZE = FONT_SIZE >> 1;
constexpr Point POINT_FONT_SIZE = { FONT_SIZE, FONT_SIZE };
constexpr int D_FONT_SIZE = FONT_SIZE << 1;
constexpr int ALPHABET_SIZE = 26;
constexpr int NUMBERS_SIZE = 10;
constexpr int NUMBERS_OFFSET = ALPHABET_SIZE - '0';
constexpr int SYMBOLS_SIZE = 5;
constexpr int BEFORE_SYMBOLS = ALPHABET_SIZE + NUMBERS_SIZE;
constexpr int FC_MINUS = BEFORE_SYMBOLS;
constexpr int FC_PLUS = BEFORE_SYMBOLS + 1;
constexpr int FC_BRACKET_LEFT = BEFORE_SYMBOLS + 2;
constexpr int FC_BRACKET_RIGHT = BEFORE_SYMBOLS + 3;
constexpr int FC_DOUBLE_DOT = BEFORE_SYMBOLS + 4;
constexpr int TOTAL_SIZE = BEFORE_SYMBOLS + SYMBOLS_SIZE;
constexpr int BUFFER_SIZE = FONT_SIZE * FONT_SIZE * TOTAL_SIZE;
constexpr int Y_STEP = FONT_SIZE * (TOTAL_SIZE - 1);
constexpr int FONT_WIDTH = FONT_SIZE * TOTAL_SIZE;
Graphics::Surface* characters;
char* binaryBuffer;
bool init(uint color = Colors::VISIBLE_BLACK)
{
binaryBuffer = new char[BUFFER_SIZE];
std::ifstream file(PATH, std::ios::binary);
if (!file.is_open())
return true;
file.read(binaryBuffer, BUFFER_SIZE);
file.close();
characters = new Graphics::Surface[TOTAL_SIZE];
for (int i = 0; i < TOTAL_SIZE; i++)
{
char* binaryPixel = binaryBuffer + i * FONT_SIZE;
uint* pixel = characters[i].buffer;
for (int y = 0; y < FONT_SIZE; y++)
{
for (int x = 0; x < FONT_SIZE; x++)
{
if (*binaryPixel == 1)
*pixel = color;
else
*pixel = Colors::BLACK;
pixel++;
binaryPixel++;
}
binaryPixel += Y_STEP;
}
}
delete[BUFFER_SIZE] binaryBuffer;
return false;
}
int _getCharIndex(char c)
{
switch (c)
{
case ' ':
return -1;
case '-':
return FC_MINUS;
case '+':
return FC_PLUS;
case '(':
return FC_BRACKET_LEFT;
case ')':
return FC_BRACKET_RIGHT;
case ':':
return FC_DOUBLE_DOT;
}
if (c <= '9')
return c + NUMBERS_OFFSET;
return c - 'a';
}
// Uses Data::pCache[0, 1]
void drawChar(const Point& point, char c, Graphics::Surface& surface = World::sScreen)
{
int index = _getCharIndex(c);
if (index == -1)
return;
if (Data::bFontMode)
{
Graphics::Draw::fillRectSize(point, POINT_FONT_SIZE, Colors::VISIBLE_BLACK, 1.0f, surface);
return;
}
Graphics::blitSurface(surface, characters[index], point);
return;
}
void drawString(Point point, const char* str, Graphics::Surface& surface);
};