LuaYard/Window.cpp
Kiritow bc189b4951 完整重构
分离各SDL类的包装代码.
事件分发处理全部转移至Lua Init层. C层只提供获取事件的方法.
这样有助于提高性能,以及今后Coroutine scheduler的添加.
2019-08-25 00:09:28 +08:00

79 lines
1.6 KiB
C++

#include "Window.h"
#include <iostream>
using namespace std;
#define setfn(cfunc, name) lua_pushcfunction(L, cfunc);lua_setfield(L, -2, name)
inline int Window::close(lua_State* L)
{
cout << "In Window::close" << endl;
auto p = (Window*)luaL_checkudata(L, 1, "window");
p->~Window();
return 0;
}
inline int Window::showWindow(lua_State* L)
{
auto p = (Window*)luaL_checkudata(L, 1, "window");
SDL_ShowWindow(p->wnd.get());
return 0;
}
inline int Window::hideWindow(lua_State* L)
{
auto p = (Window*)luaL_checkudata(L, 1, "window");
SDL_HideWindow(p->wnd.get());
return 0;
}
int Window::getWindowID(lua_State* L)
{
auto p = (Window*)luaL_checkudata(L, 1, "window");
lua_pushinteger(L, SDL_GetWindowID(p->wnd.get()));
return 1;
}
int Window::create(lua_State* L)
{
auto title = luaL_checkstring(L, 1);
int w = luaL_checkinteger(L, 2);
int h = luaL_checkinteger(L, 3);
cout << "In Window::create" << endl;
auto p = new (lua_newuserdata(L, sizeof(Window))) Window(w, h, title);
// Data table (for storing lua values)
lua_newtable(L);
lua_setuservalue(L, -2);
// Metatable
if (luaL_newmetatable(L, "window"))
{
// Type
lua_pushstring(L, "window");
lua_setfield(L, -2, "type");
// GC
lua_pushcfunction(L, close);
lua_setfield(L, -2, "__gc");
// Fields
lua_newtable(L);
setfn(close, "close");
setfn(showWindow, "show");
setfn(hideWindow, "hide");
setfn(getWindowID, "getid");
lua_setfield(L, -2, "__index");
}
lua_setmetatable(L, -2);
return 1;
}
Window::Window(int w, int h, const char* title) : wnd(SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_HIDDEN), SDL_DestroyWindow)
{
}