2019-08-18 02:00:57 +08:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 00:09:25 +08:00
|
|
|
int LuaRectToRect(lua_State* L, int index, SDL_Rect& rect)
|
2019-08-18 02:00:57 +08:00
|
|
|
{
|
2019-08-25 00:09:25 +08:00
|
|
|
if (index < 0)
|
2019-08-18 02:00:57 +08:00
|
|
|
{
|
2019-08-25 00:09:25 +08:00
|
|
|
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))
|
2019-08-18 02:00:57 +08:00
|
|
|
{
|
2019-08-25 00:09:25 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
return 1;
|
2019-08-18 02:00:57 +08:00
|
|
|
}
|
2019-08-25 00:09:25 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
r.x = p.x;
|
|
|
|
r.y = p.y;
|
|
|
|
r.w = default_w;
|
|
|
|
r.h = default_h;
|
|
|
|
return 0;
|
2019-08-18 02:00:57 +08:00
|
|
|
}
|
2019-08-25 00:09:25 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaSDLError(lua_State* L)
|
|
|
|
{
|
|
|
|
return luaL_error(L, "SDLError: %s", SDL_GetError());
|
2019-08-18 02:00:57 +08:00
|
|
|
}
|