-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleFonts.cpp
108 lines (87 loc) · 2.66 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
#include "Globals.h"
#include "Application.h"
#include "ModuleTextures.h"
#include "ModuleRender.h"
#include "ModuleFonts.h"
#include<string.h>
// Constructor
ModuleFonts::ModuleFonts() : Module()
{}
// Destructor
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].graphic == nullptr)
break;
if (id == MAX_FONTS)
{
LOG("Cannot load font %s. Array is full (max %d).", texture_path, MAX_FONTS);
return id;
}
fonts[id].graphic = tex; // graphic: pointer to the texture
fonts[id].rows = rows; // rows: rows of characters in the texture
fonts[id].len = strlen(characters); // len: length of the table
strcpy_s(fonts[id].table, characters); // table: array of chars to have the list of characters
fonts[id].row_chars = fonts[id].len / rows; // row_chars: amount of chars per row of the texture
App->textures->GetSize(tex, fonts[id].char_w, fonts[id].char_h);
fonts[id].char_w /= fonts[id].row_chars;//(fonts[id].len/fonts[id].rows); // char_w: width of each character
fonts[id].char_h /= rows; // char_h: height of each character
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].graphic != nullptr)
{
App->textures->Unload(fonts[font_id].graphic);
fonts[font_id].graphic = nullptr;
LOG("Successfully Unloaded BMP font_id %d", font_id);
}
}
// Render text using a bitmap font
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].graphic == nullptr)
{
LOG("Unable to render text with bmp font id %d", font_id);
return;
}
const Font* font = &fonts[font_id];
SDL_Rect rect;
uint len = strlen(text);
rect.w = font->char_w;
rect.h = font->char_h;
for (uint i = 0; i < len; ++i)
{
for (uint j = 0; j < font->len; j++) {
if (font->table[j] == text[i]) {
rect.x = j * font->char_w;
for (uint k = 0; k < font->rows; k++) {
if (j > font->row_chars) j -= font->row_chars;
else {
rect.y = k * font->char_h;
break;
}
}
}
}
App->render->Blit(font->graphic, x, y, &rect);
x += font->char_w;
}
}