-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModuleFonts.cpp
More file actions
117 lines (97 loc) · 2.88 KB
/
ModuleFonts.cpp
File metadata and controls
117 lines (97 loc) · 2.88 KB
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
#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;
uint texture_width;
uint texture_height;
if (texture_path == nullptr || characters == nullptr || rows == 0)
{
LOG("Could not load font");
return id;
}
SDL_Texture* tex = App->textures->Load(texture_path);
App->textures->GetSize(tex, texture_width, texture_height);
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
// TODO 1: Finish storing font data
fonts[id].row_chars = fonts[id].len / fonts[id].rows;
fonts[id].char_w = texture_width / fonts[id].row_chars;
fonts[id].char_h = texture_height / rows;
for (int i = 0; i<fonts[id].len; ++i)
{
fonts[id].table[i] = characters[i];
}
// table: array of chars to have the list of characters
// row_chars: amount of chars per row of the texture
// char_w: width of each character
// 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)
{
//lets go through all the font chars
for (int j = 0; j < font->len; ++j)
{
//is this the char u want?
if (font->table[j] == text[i])
{
rect.y = j / font->row_chars;
rect.x = j - rect.y * font->row_chars;
rect.y *= font->char_h;
rect.x *= font->char_w;
}
}
// TODO 2: Find the character in the table and its position in the texture, then Blit
App->render->Blit(font->graphic, x + i * rect.w, y, &rect, false);
}
}