-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.cpp
117 lines (96 loc) · 2.91 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
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
#include "font.h"
#include "main.h"
#include "utils.h"
#include "unicode.h"
static Uint16 *convBuf = NULL;
static size_t bufSize = 0;
static Uint16 *strToUint16(const std::wstring &text)
{
const wchar_t *str = text.c_str();
if ((! str) || (sizeof(wchar_t) == sizeof(Uint16)))
return (Uint16*)str;
else {
size_t len = wcslen(str);
if (! convBuf) {
size_t sz = len * 2 + 1;
convBuf = (Uint16*)malloc(sizeof(Uint16) * sz);
bufSize = sz;
} else
if (bufSize < len + 1) {
size_t sz = len * 2 + 1;
convBuf = (Uint16*)realloc(convBuf, sizeof(Uint16) * sz);
// I should check if it is NULL, but I'm too lazy today
bufSize = sz;
}
for (unsigned int i = 0; i <= len; i++)
convBuf[i] = (Uint16)str[i];
return convBuf;
}
}
Font::Font(const std::wstring &name, int ptsize)
{
int size;
data = resources->getRef(name, size);
if (! data)
throw Exception(name + L" not found");
SDL_RWops *op = SDL_RWFromMem(data, size);
font = TTF_OpenFontRW(op, 1, ptsize);
if (! font)
throw Exception(L"Error loading font " + name);
}
Font::~Font()
{
TTF_CloseFont(font);
resources->delRef(data);
}
void Font::draw(SDL_Surface *s, int x, int y, int r, int g, int b,
bool shadow, const std::wstring &text)
{
if (text.length() < 1)
return;
Uint16 *str = strToUint16(text);
if (shadow) {
SDL_Color color = { 1, 1, 1, 1 };
SDL_Surface *surface = TTF_RenderUNICODE_Blended(font, str, color);
SDL_Rect src = { 0, 0, surface->w, surface->h };
SDL_Rect dst = { x+1, y+1, surface->w, surface->h };
SDL_BlitSurface(surface, &src, s, &dst);
SDL_FreeSurface(surface);
}
SDL_Color color = { r, g, b, 0 };
SDL_Surface *surface = TTF_RenderUNICODE_Blended(font, str, color);
SDL_Rect src = { 0, 0, surface->w, surface->h };
SDL_Rect dst = { x, y, surface->w, surface->h };
SDL_BlitSurface(surface, &src, s, &dst);
SDL_FreeSurface(surface);
}
void Font::draw(int x, int y, int r, int g, int b, bool shadow,
const std::wstring &text)
{
draw(screen.getSurface(), x, y, r,g,b, shadow, text);
}
int Font::getWidth(const std::wstring &text)
{
int w, h;
Uint16 *str = strToUint16(text);
TTF_SizeUNICODE(font, str, &w, &h);
return w;
}
int Font::getWidth(wchar_t ch)
{
int minx, maxx, miny, maxy, advance;
TTF_GlyphMetrics(font, (Uint16)ch, &minx, &maxx, &miny, &maxy, &advance);
return advance;
}
int Font::getHeight(const std::wstring &text)
{
int w, h;
Uint16 *str = strToUint16(text);
TTF_SizeUNICODE(font, str, &w, &h);
return h;
}
void Font::getSize(const std::wstring &text, int &width, int &height)
{
Uint16 *str = strToUint16(text);
TTF_SizeUNICODE(font, str, &width, &height);
}