-
Notifications
You must be signed in to change notification settings - Fork 2
/
Font.cpp
212 lines (204 loc) · 5.7 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "include.h"
/*
class Font
constructor(filename: string, size: int)
constructor(data: string, size: int, flag: int) flag is not used currently.
renderText(str: utf-8 string, r: int, g: int, b: int, a: int): Surface
renderText(rnd: Renderer, str: utf-8 string, r: int, g: int, b: int, a: int): Texture
renderTextSolid(str: utf-8 string, r: int, g: int, b: int, a: int): Surface
renderTextSolid(rnd: Renderer, str: utf-8 string, r: int, g: int, b: int, a: int): Texture
renderTextShaded(str: utf-8 string, fr: int, fg: int, fb: int, fa: int, br: int, bg: int, bb: int, ba: int): Surface
renderTextShaded(rnd: Renderer, str: utf-8 string, fr: int, fg: int, fb: int, fa: int, br: int, bg: int, bb: int, ba: int): Texture
*/
int font_close(lua_State* L)
{
auto font = lua_checkpointer<TTF_Font>(L, 1, "LuaEngineFont");
TTF_CloseFont(font);
return 0;
}
int font_renderutf8(lua_State* L)
{
auto font = lua_checkpointer<TTF_Font>(L, 1, "LuaEngineFont");
const char* str;
SDL_Color color;
if (lua_isstring(L, 2))
{
str = luaL_checkstring(L, 2);
color.r = luaL_checkinteger(L, 3);
color.g = luaL_checkinteger(L, 4);
color.b = luaL_checkinteger(L, 5);
color.a = luaL_checkinteger(L, 6);
SDL_Surface* surf = TTF_RenderUTF8_Blended(font, str, color);
if (!surf)
{
return TTFError(L, TTF_RenderUTF8_Blended);
}
put_surface(L, surf);
return 1;
}
else
{
auto rnd = lua_checkpointer<SDL_Renderer>(L, 2, "LuaEngineRenderer");
str = luaL_checkstring(L, 3);
color.r = luaL_checkinteger(L, 4);
color.g = luaL_checkinteger(L, 5);
color.b = luaL_checkinteger(L, 6);
color.a = luaL_checkinteger(L, 7);
SDL_Surface* surf = TTF_RenderUTF8_Blended(font, str, color);
if (!surf)
{
return TTFError(L, TTF_RenderUTF8_Blended);
}
SDL_Texture* text = SDL_CreateTextureFromSurface(rnd, surf);
SDL_FreeSurface(surf);
if (!text)
{
return SDLError(L, SDL_CreateTextureFromSurface);
}
put_texture(L, text);
return 1;
}
}
int font_renderutf8_solid(lua_State* L)
{
auto font = lua_checkpointer<TTF_Font>(L, 1, "LuaEngineFont");
const char* str;
SDL_Color color;
if (lua_isstring(L, 2))
{
str = luaL_checkstring(L, 2);
color.r = luaL_checkinteger(L, 3);
color.g = luaL_checkinteger(L, 4);
color.b = luaL_checkinteger(L, 5);
color.a = luaL_checkinteger(L, 6);
SDL_Surface* surf = TTF_RenderUTF8_Solid(font, str, color);
if (!surf)
{
return TTFError(L, TTF_RenderUTF8_Solid);
}
put_surface(L, surf);
return 1;
}
else
{
auto rnd = lua_checkpointer<SDL_Renderer>(L, 2, "LuaEngineRenderer");
str = luaL_checkstring(L, 3);
color.r = luaL_checkinteger(L, 4);
color.g = luaL_checkinteger(L, 5);
color.b = luaL_checkinteger(L, 6);
color.a = luaL_checkinteger(L, 7);
SDL_Surface* surf = TTF_RenderUTF8_Solid(font, str, color);
if (!surf)
{
return TTFError(L, TTF_RenderUTF8_Solid);
}
SDL_Texture* text = SDL_CreateTextureFromSurface(rnd, surf);
SDL_FreeSurface(surf);
if (!text)
{
return SDLError(L, SDL_CreateTextureFromSurface);
}
put_texture(L, text);
return 1;
}
}
int font_renderutf8_shaded(lua_State* L)
{
auto font = lua_checkpointer<TTF_Font>(L, 1, "LuaEngineFont");
const char* str;
SDL_Color frontColor;
SDL_Color backColor;
if (lua_isstring(L, 2))
{
str = luaL_checkstring(L, 2);
frontColor.r = luaL_checkinteger(L, 3);
frontColor.g = luaL_checkinteger(L, 4);
frontColor.b = luaL_checkinteger(L, 5);
frontColor.a = luaL_checkinteger(L, 6);
backColor.r = luaL_checkinteger(L, 7);
backColor.g = luaL_checkinteger(L, 8);
backColor.b = luaL_checkinteger(L, 9);
backColor.a = luaL_checkinteger(L, 10);
SDL_Surface* surf = TTF_RenderUTF8_Shaded(font, str, frontColor, backColor);
if (!surf)
{
return TTFError(L, TTF_RenderUTF8_Shaded);
}
put_surface(L, surf);
return 1;
}
else
{
auto rnd = lua_checkpointer<SDL_Renderer>(L, 2, "LuaEngineRenderer");
str = luaL_checkstring(L, 3);
frontColor.r = luaL_checkinteger(L, 4);
frontColor.g = luaL_checkinteger(L, 5);
frontColor.b = luaL_checkinteger(L, 6);
frontColor.a = luaL_checkinteger(L, 7);
backColor.r = luaL_checkinteger(L, 8);
backColor.g = luaL_checkinteger(L, 9);
backColor.b = luaL_checkinteger(L, 10);
backColor.a = luaL_checkinteger(L, 11);
SDL_Surface* surf = TTF_RenderUTF8_Shaded(font, str, frontColor, backColor);
if (!surf)
{
return TTFError(L, TTF_RenderUTF8_Shaded);
}
SDL_Texture* text = SDL_CreateTextureFromSurface(rnd, surf);
SDL_FreeSurface(surf);
if (!text)
{
return SDLError(L, SDL_CreateTextureFromSurface);
}
put_texture(L, text);
return 1;
}
}
int font_new(lua_State* L)
{
TTF_Font* font;
if (lua_gettop(L) >= 3)
{
size_t datalen;
const char* data = luaL_checklstring(L, 1, &datalen);
int fontsize = luaL_checkinteger(L, 2);
int flag = luaL_checkinteger(L, 3);
SDL_RWops* src = SDL_RWFromConstMem(data, datalen);
font = TTF_OpenFontRW(src, 0, fontsize);
SDL_RWclose(src);
if (!font)
{
return TTFError(L, TTF_OpenFontRW);
}
}
else
{
const char* filename = luaL_checkstring(L, 1);
int fontsize = luaL_checkinteger(L, 2);
font = TTF_OpenFont(filename, fontsize);
if (!font)
{
return TTFError(L, TTF_OpenFont);
}
}
lua_newpointer(L, font);
if (luaL_newmetatable(L, "LuaEngineFont"))
{
lua_setfield_function(L, "__gc", font_close);
lua_newtable(L);
lua_setfield_function(L, "renderText", font_renderutf8);
lua_setfield_function(L, "renderTextSolid", font_renderutf8_solid);
lua_setfield_function(L, "renderTextShaded", font_renderutf8_shaded);
lua_setfield(L, -2, "__index");
}
lua_setmetatable(L, -2);
return 1;
}
void InitFont(lua_State* L)
{
lua_getglobal(L, "package");
lua_getfield(L, -1, "loaded");
lua_pushcfunction(L, font_new);
lua_setfield(L, -2, "Font");
lua_pop(L, 2);
}