LuaEngine/Font.cpp

135 lines
3.2 KiB
C++
Raw Normal View History

2020-05-23 01:11:46 +08:00
#include "include.h"
/*
class Font
constructor(filename: string, size: int)
renderText(str: string, r: int, g: int, b: int, a: int): Surface
renderText(rnd: Renderer, str: string, r: int, g: int, b: int, a: int): Texture
*/
int font_close(lua_State* L)
{
auto font = lua_checkpointer<TTF_Font>(L, 1, "LuaEngineFont");
TTF_CloseFont(font);
return 0;
}
int font_rendertext(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_RenderText_Blended(font, str, color);
if (!surf)
{
return TTFError(L, TTF_RenderText_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_RenderText_Blended(font, str, color);
if (!surf)
{
return TTFError(L, TTF_RenderText_Blended);
}
SDL_Texture* text = SDL_CreateTextureFromSurface(rnd, surf);
2020-05-24 05:24:13 +08:00
SDL_FreeSurface(surf);
if (!text)
{
return SDLError(L, SDL_CreateTextureFromSurface);
}
put_texture(L, text);
return 1;
}
}
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);
2020-05-23 01:11:46 +08:00
if (!text)
{
return SDLError(L, SDL_CreateTextureFromSurface);
}
put_texture(L, text);
return 1;
}
}
int font_new(lua_State* L)
{
const char* filename = luaL_checkstring(L, 1);
int fontsize = luaL_checkinteger(L, 2);
TTF_Font* 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_rendertext);
2020-05-24 05:24:13 +08:00
lua_setfield_function(L, "renderUTF8", font_renderutf8);
2020-05-23 01:11:46 +08:00
lua_setfield(L, -2, "__index");
}
lua_setmetatable(L, -2);
return 1;
}
void InitFont(lua_State* L)
{
2020-05-24 05:24:13 +08:00
lua_getglobal(L, "package");
lua_getfield(L, -1, "loaded");
lua_pushcfunction(L, font_new);
lua_setfield(L, -2, "Font");
lua_pop(L, 2);
2020-05-23 01:11:46 +08:00
}