Kiritow
bc189b4951
分离各SDL类的包装代码. 事件分发处理全部转移至Lua Init层. C层只提供获取事件的方法. 这样有助于提高性能,以及今后Coroutine scheduler的添加.
95 lines
1.9 KiB
C++
95 lines
1.9 KiB
C++
#include "Font.h"
|
|
#include "Renderer.h"
|
|
#include "Texture.h"
|
|
#include "Surface.h"
|
|
#include "LuaCommon.h"
|
|
#include <iostream>
|
|
using namespace std;
|
|
|
|
#define setfn(cfunc, name) lua_pushcfunction(L, cfunc);lua_setfield(L, -2, name)
|
|
|
|
inline int Font::close(lua_State* L)
|
|
{
|
|
cout << "In Font::close" << endl;
|
|
auto p = (Font*)luaL_checkudata(L, 1, "font");
|
|
p->~Font();
|
|
return 0;
|
|
}
|
|
|
|
inline Font::Font(TTF_Font* f) : font(f, TTF_CloseFont)
|
|
{
|
|
|
|
}
|
|
|
|
inline int Font::renderText(lua_State* L)
|
|
{
|
|
auto p = (Font*)luaL_checkudata(L, 1, "font");
|
|
std::shared_ptr<SDL_Renderer> r;
|
|
const char* msg = nullptr;
|
|
SDL_Color c;
|
|
|
|
if (lua_gettop(L) >= 4)
|
|
{
|
|
r = ((Renderer*)luaL_checkudata(L, 2, "renderer"))->rnd;
|
|
msg = luaL_checkstring(L, 3);
|
|
if (LuaColorToColor(L, 4, c))
|
|
{
|
|
return luaL_error(L, "bad argument #4. rgba expected, got %s", lua_tostring(L, -1));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
msg = luaL_checkstring(L, 2);
|
|
if (LuaColorToColor(L, 3, c))
|
|
{
|
|
return luaL_error(L, "bad argument #3. rgba expected, got %s", lua_tostring(L, -1));
|
|
}
|
|
}
|
|
|
|
SDL_Surface* surf = TTF_RenderText_Blended(p->font.get(), msg, c);
|
|
|
|
if (r.get())
|
|
{
|
|
Texture::create(L, r, SDL_CreateTextureFromSurface(r.get(), surf));
|
|
SDL_FreeSurface(surf);
|
|
}
|
|
else
|
|
{
|
|
Surface::create(L, surf);
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int Font::create(lua_State* L)
|
|
{
|
|
cout << "In Font::create" << endl;
|
|
const char* font = luaL_checkstring(L, 1);
|
|
int fontsz = luaL_checkinteger(L, 2);
|
|
|
|
TTF_Font* ttf = TTF_OpenFont(font, fontsz);
|
|
if (!ttf)
|
|
{
|
|
return LuaSDLError(L);
|
|
}
|
|
|
|
Font* f = new (lua_newuserdata(L, sizeof(Font))) Font(ttf);
|
|
|
|
if (luaL_newmetatable(L, "font"))
|
|
{
|
|
// GC
|
|
lua_pushcfunction(L, close);
|
|
lua_setfield(L, -2, "__gc");
|
|
|
|
// Fields
|
|
lua_newtable(L);
|
|
lua_pushstring(L, "font"); lua_setfield(L, -2, "type");
|
|
setfn(close, "close");
|
|
setfn(renderText, "renderText");
|
|
|
|
// Set __index of metatable.
|
|
lua_setfield(L, -2, "__index");
|
|
}
|
|
lua_setmetatable(L, -2);
|
|
return 1;
|
|
}
|