-
Notifications
You must be signed in to change notification settings - Fork 1
/
ModuleFonts.cpp
111 lines (92 loc) · 2.42 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
#include "Globals.h"
#include "Application.h"
#include "ModuleTextures.h"
#include "ModuleRender.h"
#include "ModuleFonts.h"
#include <string.h>
#ifdef _WIN32
#define strncpy strncpy_s
#endif
// 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;
}
// if we reach this point we can safely create a new bmp font
Font* f = &fonts[id];
f->graphic = tex;
strncpy(fonts[id].table, characters, MAX_FONT_CHARS);
f->rows = rows;
f->len = strlen(characters);
f->row_chars = fonts[id].len / rows;
uint w, h;
App->textures->GetSize(tex, w, h);
f->row_chars = fonts[id].len / rows;
f->char_w = w / fonts[id].row_chars;
f->char_h = h / 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].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 bmp 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)
{
// we could use strchr instead ?
for (uint j = 0; j < font->len; ++j)
{
if (font->table[j] == text[i])
{
rect.x = rect.w * (j % font->row_chars);
rect.y = rect.h * (j / font->row_chars);
App->render->Blit(font->graphic, x, y, &rect, 0.0f, false);
x += rect.w;
break;
}
}
}
}