LuaYard/LuaCommon.cpp

179 lines
2.9 KiB
C++

#include "LuaCommon.h"
using namespace std;
bool LuaCompareType(lua_State* L, int index, const char* type, bool leave)
{
int indexType = lua_getfield(L, index, "type");
if (indexType == LUA_TSTRING)
{
if (strcmp(lua_tostring(L, -1), type) == 0)
{
if (!leave) lua_pop(L, 1);
return true;
}
else
{
if (!leave) lua_pop(L, 1);
return false;
}
}
else
{
lua_pop(L, 1);
if (leave) lua_pushstring(L, lua_typename(L, indexType));
return false;
}
}
int LuaRectToRect(lua_State* L, int index, SDL_Rect& rect)
{
if (index < 0)
{
index = lua_gettop(L) + index + 1;
}
if (lua_getfield(L, index, "type") != LUA_TSTRING)
{
int t = lua_type(L, -1);
lua_pop(L, 1);
lua_pushstring(L, lua_typename(L, t));
return 1;
}
if (strcmp(lua_tostring(L, -1), "rect") == 0)
{
lua_pop(L, 1);
lua_getfield(L, index, "x");
rect.x = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "y");
rect.y = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "w");
rect.w = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "h");
rect.h = lua_tointeger(L, -1);
lua_pop(L, 1);
return 0;
}
else
{
return 1;
}
}
int LuaPointToPoint(lua_State* L, int index, SDL_Point& point)
{
if (index < 0)
{
index = lua_gettop(L) + index + 1;
}
if (lua_getfield(L, index, "type") != LUA_TSTRING)
{
int t = lua_type(L, -1);
lua_pop(L, 1);
lua_pushstring(L, lua_typename(L, t));
return 1;
}
if (strcmp(lua_tostring(L, -1), "point") == 0)
{
lua_pop(L, 1);
lua_getfield(L, index, "x");
point.x = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "y");
point.y = lua_tointeger(L, -1);
lua_pop(L, 1);
return 0;
}
else
{
return 1;
}
}
int LuaColorToColor(lua_State* L, int index, SDL_Color& c)
{
if (index < 0)
{
index = lua_gettop(L) + index + 1;
}
if (lua_getfield(L, index, "type") != LUA_TSTRING)
{
int t = lua_type(L, -1);
lua_pop(L, 1);
lua_pushstring(L, lua_typename(L, t));
return 1;
}
if (strcmp(lua_tostring(L, -1), "rgba") == 0)
{
lua_pop(L, 1);
lua_getfield(L, index, "r");
c.r = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "g");
c.g = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "b");
c.b = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "a");
c.a = lua_tointeger(L, -1);
lua_pop(L, 1);
return 0;
}
else
{
return 1;
}
}
int LuaRectPointToRect(lua_State* L, int index, SDL_Rect& r, int default_w, int default_h)
{
if (index < 0)
{
index = lua_gettop(L) + index + 1;
}
if (LuaRectToRect(L, index, r))
{
SDL_Point p;
if (LuaPointToPoint(L, index, p))
{
lua_pop(L, 1);
return 1;
}
lua_pop(L, 1);
r.x = p.x;
r.y = p.y;
r.w = default_w;
r.h = default_h;
return 0;
}
return 0;
}
int LuaSDLError(lua_State* L)
{
return luaL_error(L, "SDLError: %s", SDL_GetError());
}