-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleFonts.cpp
113 lines (84 loc) · 2.97 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
#include "Application.h"
#include "ModuleTextures.h"
#include "ModuleRender.h"
#include "ModuleFonts.h"
#include<string.h>
ModuleFonts::ModuleFonts(Application* app, bool start_enabled) : Module(app, start_enabled)
{
}
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;
}
SDL_Texture* 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;
// TODO 1: Finish storing font data
// 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(fonts[id].table, MAX_FONT_CHARS, characters);
font.totalLength = strlen(characters);
font.columns = fonts[id].totalLength / rows;
uint tex_w, tex_h;
App->textures->GetTextureSize(tex, tex_w, tex_h);
font.char_w = tex_w / font.columns;
font.char_h = tex_h / font.rows;
//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;
//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) {
// TODO 2: Find the character in the table and its position in the texture, then Blit
uint charIndex = 0;
// Find the location of the current character in the lookup table
for (uint j = 0; j < font->totalLength; ++j) {
if (font->table[j] == text[i]) {
charIndex = j;
break;
}
}
// Retrieve the position of the current character in the sprite
spriteRect.x = spriteRect.w * (charIndex % font->columns);
spriteRect.y = spriteRect.h * (charIndex / font->columns);
App->renderer->Blit(font->texture, x, y, &spriteRect, 0.0f);
// Advance the position where we blit the next character
x += spriteRect.w;
}
}