-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleFonts.cpp
125 lines (96 loc) · 2.95 KB
/
ModuleFonts.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
#include "Application.h"
#include "ModuleTextureManager.h"
#include "ModuleRenderer.h"
#include "ModuleFonts.h"
#include<string.h>
ModuleFonts::ModuleFonts(bool isEnabled) : Module(isEnabled)
{
name = "Fonts";
}
ModuleFonts::~ModuleFonts()
{
}
// Load new texture from file path
int ModuleFonts::Load(const char* texture_path, const char* characters, uint rows)
{
int id = -1;
if (texture_path == nullptr || characters == nullptr || rows == 0)
{
LOG("Could not load font");
return id;
}
tex = App->textures->Load(texture_path);
if (tex == nullptr || strlen(characters) >= MAX_FONT_CHARS)
{
LOG("Could not load font at %s with characters '%s'", texture_path, characters);
return id;
}
id = 0;
for (; id < MAX_FONTS; ++id)
if (fonts[id].texture == nullptr)
break;
if (id == MAX_FONTS)
{
LOG("Cannot load font %s. Array is full (max %d).", texture_path, MAX_FONTS);
return id;
}
Font& font = fonts[id];
font.texture = tex;
font.rows = rows;
// totalLength --- length of the lookup table
// table --------- All characters displayed in the same order as the texture
// columns ------- Amount of chars per row of the texture
// char_w -------- Width of each character
// char_h -------- Height of each character
strcpy_s(font.table, characters);
font.totalLength = strlen(font.table);
font.columns = (font.totalLength / font.rows);
uint texture_w, texture_h;
App->textures->GetSize(font.texture, texture_w, texture_h);
font.char_w = (texture_w / font.columns);
font.char_h = (texture_h / font.rows);
++fontsCount;
LOG("Successfully loaded BMP font from %s", texture_path);
return id;
}
void ModuleFonts::UnLoad(int font_id)
{
if (font_id >= 0 && font_id < MAX_FONTS && fonts[font_id].texture != nullptr)
{
App->textures->Unload(fonts[font_id].texture);
fonts[font_id].texture = nullptr;
--fontsCount;
LOG("Successfully Unloaded BMP font_id %d", font_id);
}
}
void ModuleFonts::BlitText(int x, int y, int font_id, const char* text) const
{
if (text == nullptr || font_id < 0 || font_id >= MAX_FONTS || fonts[font_id].texture == nullptr)
{
LOG("Unable to render text with bmp font id %d", font_id);
return;
}
const Font* font = &fonts[font_id];
SDL_Rect spriteRect;
uint len = strlen(text);
spriteRect.w = font->char_w;
spriteRect.h = font->char_h;
for (uint i = 0; i < len; ++i)
{
// 1 - Find the location of the current character in the lookup table
uint locationInTable = 0;
for (uint j = 0; j < font->totalLength; ++j) {
if (font->table[j] == text[i]) {
locationInTable = j;
break;
}
}
// 2 - Retrieve the position of the current character in the sprite
spriteRect.x = (locationInTable % font->columns) * spriteRect.w;
spriteRect.y = (locationInTable / font->columns) * spriteRect.h;
// 3 - Blit the character at its proper position
App->render->Blit(font->texture, x, y, &spriteRect, 0.0f, false);
// 4 - Advance the position where we blit the next character
x += spriteRect.w;
}
}