-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFont.cpp
82 lines (69 loc) · 1.63 KB
/
Font.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
#include "Font.h"
#include "Game.h"
Font::Font(Game* game) : mGame(game)
{
}
Font::~Font()
{
SDL_Log("Font deleted!");
}
bool Font::Load(const std::string& filename)
{
// Support these font sizes
std::vector<int> fontSizes = {
8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28,
30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60,
64, 68, 72
};
//call TTF_OpenFont once per every font size
for (auto& size : fontSizes)
{
TTF_Font* font = TTF_OpenFont(filename.c_str(), size);
if (font == nullptr)
{
SDL_Log("Error from TTF_Font: %s\n", TTF_GetError());
SDL_Log("Failed to load font %s in size %d", filename.c_str(), size);
return false;
}
mFontData.emplace(size, font);
}
return true;
}
void Font::Unload()
{
for (auto& font : mFontData)
{
if (font.second)
{
TTF_CloseFont(font.second);
}
}
}
SDL_Texture* Font::RenderText(const std::string& text, const Vector3& color, int pointSize)
{
SDL_Texture* texture = nullptr;
SDL_Color sdlColor;
sdlColor.r = static_cast<Uint8>(color.x * 255);
sdlColor.g = static_cast<Uint8>(color.y * 255);
sdlColor.b = static_cast<Uint8>(color.z * 255);
sdlColor.a = 255;
// Find the point data for this point size
auto iter = mFontData.find(pointSize);
if (iter != mFontData.end())
{
TTF_Font* font = iter->second;
// draw this to a surface
SDL_Surface* surf = TTF_RenderText_Blended(font, text.c_str(), sdlColor);
if (surf != nullptr)
{
// Convert from surface to texture
texture = SDL_CreateTextureFromSurface(mGame->GetRenderer(), surf);
SDL_FreeSurface(surf);
}
}
else
{
SDL_Log("Point size %d is unsuported.", pointSize);
}
return texture;
}