From 436568ee6320ee7507c01cd6978c078aec2335bd Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 23 Nov 2018 13:20:48 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 17 + bin/Animation.exe | Bin 0 -> 1057280 bytes bin/Jin.exe | Bin 2565120 -> 2565120 bytes bin/ParticleSystem.exe | Bin 0 -> 1052672 bytes bin/game/anim2.png | Bin 0 -> 648800 bytes bin/jin.exe | Bin 0 -> 2565120 bytes bin/particle.png | Bin 0 -> 1821 bytes build/vc++/jin.vcxproj | 1 - build/vc++/jin.vcxproj.filters | 9 - examples/particle_system/main.cpp | 8 +- src/3rdparty/luax/luax.h | 449 ++++++++++++++++++++++++++ src/lua/common/je_lua.h | 2 +- src/lua/common/je_lua_proxy.h | 8 +- src/lua/common/je_lua_shared.cpp | 1 + src/lua/libraries/luax/luax.h | 449 -------------------------- src/lua/modules/graphics/je_lua_animation.cpp | 3 +- 16 files changed, 479 insertions(+), 468 deletions(-) create mode 100644 .gitignore create mode 100644 bin/Animation.exe create mode 100644 bin/ParticleSystem.exe create mode 100644 bin/game/anim2.png create mode 100644 bin/jin.exe create mode 100644 bin/particle.png create mode 100644 src/3rdparty/luax/luax.h delete mode 100644 src/lua/libraries/luax/luax.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d7d42cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ + +*.ilk +*.pdb +*.lib +*.exp +*.obj +*.tlog +*.lastbuildstate +*.log +*.ipch +*.idb +*.res +*.db +*.suo +*.opendb +*.db-shm +*.db-wal diff --git a/bin/Animation.exe b/bin/Animation.exe new file mode 100644 index 0000000..4bda616 Binary files /dev/null and b/bin/Animation.exe differ diff --git a/bin/Jin.exe b/bin/Jin.exe index 0c72f2d..e48b1f6 100644 Binary files a/bin/Jin.exe and b/bin/Jin.exe differ diff --git a/bin/ParticleSystem.exe b/bin/ParticleSystem.exe new file mode 100644 index 0000000..6284228 Binary files /dev/null and b/bin/ParticleSystem.exe differ diff --git a/bin/game/anim2.png b/bin/game/anim2.png new file mode 100644 index 0000000..63c1f63 Binary files /dev/null and b/bin/game/anim2.png differ diff --git a/bin/jin.exe b/bin/jin.exe new file mode 100644 index 0000000..e48b1f6 Binary files /dev/null and b/bin/jin.exe differ diff --git a/bin/particle.png b/bin/particle.png new file mode 100644 index 0000000..632fcc1 Binary files /dev/null and b/bin/particle.png differ diff --git a/build/vc++/jin.vcxproj b/build/vc++/jin.vcxproj index 025c309..e687894 100644 --- a/build/vc++/jin.vcxproj +++ b/build/vc++/jin.vcxproj @@ -205,7 +205,6 @@ - diff --git a/build/vc++/jin.vcxproj.filters b/build/vc++/jin.vcxproj.filters index 6b1e8f9..b613a33 100644 --- a/build/vc++/jin.vcxproj.filters +++ b/build/vc++/jin.vcxproj.filters @@ -7,12 +7,6 @@ {2264d908-20ee-41c2-8bdc-bc1117959c4f} - - {269451af-b8e9-451f-940a-9b679e6116d3} - - - {93b29ece-8318-4b20-a65d-2e79c1a2f816} - {923b475a-00e2-4b8f-925a-ca1e5f9b4689} @@ -203,9 +197,6 @@ resource - - source\libraries\luax - source\resources diff --git a/examples/particle_system/main.cpp b/examples/particle_system/main.cpp index 6ab3448..b337722 100644 --- a/examples/particle_system/main.cpp +++ b/examples/particle_system/main.cpp @@ -37,7 +37,7 @@ const char* shader_code = R"( const float Pi = 3.14f; void onLoad() { - tex = Texture::createTexture("texture.png"); + tex = Texture::createTexture("particle.png"); spr = new Sprite(tex, Origin::BottomCenter); shader = Shader::createShader(shader_code); ParticleSystemDef def; @@ -46,15 +46,15 @@ void onLoad() def.emitterDef.emitRateDef.rate.rate = 0.01f; def.emitterDef.forceDef.force.force = 50.f; def.emitterDef.positionDef.enableRandom = true; - def.emitterDef.positionDef.position.random.floor = Vector2(-20, 0); - def.emitterDef.positionDef.position.random.ceil = Vector2(20, 0); + def.emitterDef.positionDef.position.random.floor = Vector2(0, 0); + def.emitterDef.positionDef.position.random.ceil = Vector2(0, 0); def.emitterDef.directionDef.enableRandom = true; def.emitterDef.directionDef.direction.random.floor = -Pi / 2 ; def.emitterDef.directionDef.direction.random.ceil = -Pi / 2 ; def.particleDef.colorOverTimeDef.enable = true; - def.particleDef.colorOverTimeDef.colorStart = Color(255, 100, 0, 255); + def.particleDef.colorOverTimeDef.colorStart = Color(255, 255, 0, 255); def.particleDef.colorOverTimeDef.colorEnd = Color(255, 0, 0, 0); /* def.particleDef.angularSpeedDef.enableRandom = true; diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h new file mode 100644 index 0000000..0055176 --- /dev/null +++ b/src/3rdparty/luax/luax.h @@ -0,0 +1,449 @@ +/** +* Copyright (C) 2018 chai +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is furnished +* to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +/** +* include following 3 files before this +* lua.h +* lauxlib.h +* lualib.h +*/ +#ifndef __LUAX_H +#define __LUAX_H +// only for lua 5.1 +#if LUA_VERSION_NUM == 501 + +#define LUAX_VERSION "0.1.0" + +#define luax_newstate luaL_newstate +#define luax_close lua_close +#define luax_openlibs luaL_openlibs +// load chunk but dont run it +#define luax_loadbuffer luaL_loadbuffer +#define luax_dostring luaL_dostring +#define luax_pcall lua_pcall +#define luax_setglobal lua_setglobal +#define luax_setglobali(L, i, name)\ + lua_pushvalue(L, i);\ + lua_setglobal(L, name); +#define luax_pop lua_pop +#define luax_remove lua_remove +#define luax_newtable lua_newtable +#define luax_getglobal lua_getglobal + +#define luax_clearstack(L) lua_settop(L, 0) +/** +* +*/ +#define luax_setglobalstring(L, n, v) (lua_pushstring(L, v), lua_setglobal(L, n)) + +/** +* Get number of args +*/ +#define luax_gettop lua_gettop + +#define luax_gettable lua_gettable + +#define luax_settable lua_settable + +/** +* Check userdata type. +*/ +#define luax_checktype luaL_checkudata +#define luax_checknumber luaL_checknumber +#define luax_checkinteger luaL_checkinteger +#define luax_checkstring luaL_checkstring +#define luax_checklstring luaL_checklstring +#define luax_touserdata lua_touserdata +#define luax_tolightuserdata lua_touserdata +//#define luax_checkbool luaL_checkinteger +inline bool luax_checkbool(lua_State *L, int numArg) +{ + bool b = false; + if (lua_type(L, numArg) == LUA_TBOOLEAN) + { + b = lua_toboolean(L, numArg); + } + else + { + luaL_typerror(L, numArg, lua_typename(L, LUA_TBOOLEAN)); + } + return b; +} + +/** +* Oprating tables. +*/ +/* get value and leaves it on top of stack */ +#define luax_rawgetnumber(L, i, k) (lua_rawgeti(L,i, k), lua_tonumber(L, -1)) +inline float luax_rawgetnumberthenpop(lua_State* L, int i, int k) +{ + float n = luax_rawgetnumber(L, i, k); + luax_pop(L, 1); + return n; +} + +#define luax_rawgeti lua_rawgeti + +/** +* +*/ +#define luax_typerror luaL_typerror + + +/** +* Error checking +*/ +#define luax_error luaL_error + +/** +* Push value on the top of stack. +*/ +#define luax_pushnumber lua_pushnumber +#define luax_pushstring lua_pushstring +#define luax_pushlstring lua_pushlstring +#define luax_pushinteger lua_pushinteger +#define luax_pushboolean lua_pushboolean +#define luax_pushlightuserdata lua_pushlightuserdata +#define luax_pushnil lua_pushnil +#define luax_pushvalue lua_pushvalue +#define luax_pushliteral lua_pushliteral + +#define luax_setmetatable lua_setmetatable + +//inline void luax_pushuserdata(lua_State* L, void* p) +//{ +// /** +// * https://stackoverflow.com/questions/15038796/lua-c-api-push-existing-pointers +// */ +// void** box = (void**)lua_newuserdata(L, sizeof(p)); +// *box = p; +// +//} + +#define luax_rawseti lua_rawseti + +/** +* Set field +*/ +#define luax_setfield lua_setfield +#define luax_setfield_(T, L, k, v)\ + do { lua_push##T(L, v); lua_setfield(L, -2, k); } while (0) + +#define luax_setfieldnumber(L, k, v) luax_setfield_(number, L, k, v) +#define luax_setfieldinteger(L, k, v) luax_setfield_(integer, L, k, v) +#define luax_setfieldstring(L, k, v) luax_setfield_(string, L, k, v) +#define luax_setfieldbool(L, k, v) luax_setfield_(boolean, L, k, v) +#define luax_setfieldudata(L, k, v) luax_setfield_(lightuserdata, L, k, v) +#define luax_setfieldcfunc(L, k, v) luax_setfield_(cfunction, L, k, v) +#define luax_setfieldfstring(L, k, ...)\ + do { lua_pushfstring(L, __VA_ARGS__); lua_setfield(L, -2, k); } while (0) + +/** +* If nosuch field push a nil at the top of stack. +*/ +#define luax_getfield(L, I, N) lua_getfield(L, I, N) +inline float luax_getfieldnumber(lua_State* L, int I, const char* N) +{ + luax_getfield(L, I, N); + float n = luax_checknumber(L, -1); + return n; +} +inline int luax_getfieldinteger(lua_State* L, int I, const char* N) +{ + luax_getfield(L, I, N); + int bin = luax_checkinteger(L, -1); + return bin; +} +inline const char* luax_getfieldstring(lua_State* L, int I, const char* N) +{ + luax_getfield(L, I, N); + const char* str = luax_checkstring(L, -1); + return str; +} +inline char luax_getfieldbool(lua_State* L, int I, const char* N) +{ + luax_getfield(L, I, N); + char bin = lua_toboolean(L, -1); + return bin; +} +/** +* +*/ +#define luax_call lua_call + +/** +* Set raw +*/ +#define luax_setraw(T, L, idx, i, v)\ + (lua_push##T(L, v), lua_rawseti(L, idx, i)) + +#define luax_setrawstring(L, idx, i, v) luax_setraw(string, L, idx, i, v) +#define luax_setrawnumber(L, idx, i, v) luax_setraw(number, L, idx, i, v) +#define luax_setrawbool(L, idx, i, v) luax_setraw(boolean, L, idx, i, v) + +/** +* +*/ +#define luax_optboolean(L, i, x)\ + (!lua_isnoneornil(L, i) ? lua_toboolean(L, i) : (x)) +#define luax_optudata(L, i, name, x)\ + (!lua_isnoneornil(L, i) ? luaL_checkudata(L, i, name) : (x)) +#define luax_optnumber luaL_optnumber +#define luax_optinteger luaL_optinteger + +inline int luax_newlib(lua_State* L, const luaL_Reg* f) +{ + lua_createtable(L, 0, sizeof(f)); + for (; f && f->name; ++f) + { + lua_pushcfunction(L, f->func); + lua_setfield(L, -2, f->name); + } + return 1; // leave lib table on top of stack +} + +/** +* Register a userdefined lua type with given type name. +*/ +inline void luax_newtype(lua_State* L, const char* tname, const luaL_Reg* f) +{ + luaL_newmetatable(L, tname); + + // m.__index = m + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + + if (f != 0) + luaL_register(L, 0, f); + + lua_pop(L, 1); // Pops metatable. +} + +/** +* Instance of a type. +*/ +inline void* luax_newinstance(lua_State* L, const char* tname, int size) +{ + void* p = lua_newuserdata(L, size); + + luaL_newmetatable(L, tname); + lua_setmetatable(L, -2); + + return p; +} + +/** +* require a module +*/ +inline int luax_require(lua_State* L, const char* mname, lua_CFunction openf, int glb) +{ + openf(L); + if (glb) + { + lua_setglobal(L, mname); + return 0; + } + return 1; +} + +/** +* Return 1 if match. +*/ +inline int luax_istype(lua_State* L, int idx, const char* tname) +{ + if (lua_getmetatable(L, idx)) + { + /* get metatable called tname */ + lua_getfield(L, LUA_REGISTRYINDEX, tname); + if (lua_rawequal(L, -1, -2)) + { + lua_pop(L, 2); // pop both metatables + return 1; + } + lua_pop(L, 2); + return 0; + } + /* value at idx has no metatable */ + return 0; +} + +#define luax_is(T, L, i) (lua_is##T(L, i)) +#define luax_isnumber(L, i) luax_is(number, L, i) +#define luax_isstring(L, i) luax_is(string, L, i) +#define luax_istable(L, i) luax_is(table, L, i) +#define luax_isnil(L, i) luax_is(nil, L, i) +#define luax_isboolean(L, i) luax_is(boolean, L, i) +#define luax_isfunction(L, i) luax_is(function, L, i) +#define luax_isuserdata lua_isuserdata +#define luax_islightuserdata lua_islightuserdata +inline int luax_isinteger(lua_State* L, int i) +{ + if (!luax_isnumber(L, i)) + return 0; + return lua_tonumber(L, i) == lua_tointeger(L, i); +} +inline int luax_isfloat(lua_State* L, int i) +{ + if (!luax_isnumber(L, i)) + return 0; + return lua_tonumber(L, i) != lua_tointeger(L, i); +} + +#define luax_isnumberstrict(L, i) (lua_type(L, i) == LUA_TNUMBER) +#define luax_isstringstrict(L, i) (lua_type(L, i) == LUA_TSTRING) +#define luax_isbooleanstrict(L, i) (lua_type(L, i) == LUA_TBOOLEAN) +#define luax_istablestrict(L, i) (lua_type(L, i) == LUA_TTABLE) +#define luax_isnilstrict(L, i) (lua_type(L, i) == LUA_TNIL) +inline int luax_isintegerstrict(lua_State* L, int i) +{ + if (!luax_isnumberstrict(L, i)) + return 0; + return lua_tonumber(L, i) == lua_tointeger(L, i); +} +inline int luax_isfloatstrict(lua_State* L, int i) +{ + if (!luax_isnumberstrict(L, i)) + return 0; + return lua_tonumber(L, i) != lua_tointeger(L, i); +} + +/** +* To userdata. +*/ +inline void* luax_toudata(lua_State* L, int idx) +{ + void* p = lua_touserdata(L, idx); + if (p == NULL) + luaL_typerror(L, idx, "userdata"); // if p is not userdata + return p; +} + +/** +* Get table index size. +*/ +inline int luax_tableidxlen(lua_State* L, int i) +{ + return lua_objlen(L, i); +} + +/** +* Get table hash size +inline int luax_tbalehashlen(lua_State* L, int i) +{ + +} +*/ + +/** +* Get table hash and index size +inline int luax_tablelen(lua_State* L, int i) +{ + +} +*/ + +/* create a global tbale and stay it on the top of stack */ +#define luax_globaltable(L, name)\ +lua_newtable(L);\ +lua_pushvalue(L, 1);\ +lua_setglobal(L, name); + +inline int luax_tableinsert(lua_State * L, int tindex, int vindex, int pos) +{ + if (tindex < 0) + tindex = lua_gettop(L) + 1 + tindex; + if (vindex < 0) + vindex = lua_gettop(L) + 1 + vindex; + if (pos == -1) + { + lua_pushvalue(L, vindex); + lua_rawseti(L, tindex, lua_objlen(L, tindex) + 1); + return 0; + } + else if (pos < 0) + pos = lua_objlen(L, tindex) + 1 + pos; + for (int i = lua_objlen(L, tindex) + 1; i > pos; --i) + { + lua_rawgeti(L, tindex, i - 1); + lua_rawseti(L, tindex, i); + } + lua_pushvalue(L, vindex); + lua_rawseti(L, tindex, pos); + return 0; +} + +/** +* Add the package loader to the package.loaders table. +*/ +inline int luax_registersearcher(lua_State * L, lua_CFunction f, int pos) +{ + lua_getglobal(L, "package"); + + if (lua_isnil(L, -1)) + return luaL_error(L, "Can't register searcher: package table does not exist."); + + lua_getfield(L, -1, "loaders"); + + if (lua_isnil(L, -1)) + return luaL_error(L, "Can't register searcher: package.loaders table does not exist."); + + lua_pushcfunction(L, f); + luax_tableinsert(L, -2, -1, pos); + lua_pop(L, 3); + return 0; +} + +typedef struct luax_Str +{ + const char* name; + const char* value; +} luax_Str; + +inline void luax_setfieldstrings(lua_State* L, const luax_Str* strs) +{ + for (int i = 0; strs[i].name != 0; ++i) + { + luax_setfieldstring(L, strs[i].name, strs[i].value); + } +} + +typedef struct luax_Num +{ + const char* name; + float number; +}; + +inline void luax_setfieldnumbers(lua_State* L, const luax_Num* strs) +{ + for (int i = 0; strs[i].name != 0; ++i) + { + luax_setfieldnumber(L, strs[i].name, strs[i].number); + } +} + +typedef luaL_Reg luax_Reg; + +#define luax_ref luaL_ref + +#define luax_unref luaL_unref + +#endif // #if LUA_VERSION_NUM == 501 + +#endif // __LUAX_H \ No newline at end of file diff --git a/src/lua/common/je_lua.h b/src/lua/common/je_lua.h index 56a5090..05cf917 100644 --- a/src/lua/common/je_lua.h +++ b/src/lua/common/je_lua.h @@ -4,7 +4,7 @@ #include #include "LuaJIT/lua.hpp" -#include "libraries/luax/luax.h" +#include "luax/luax.h" #include "je_lua_shared.hpp" #include "je_lua_proxy.h" diff --git a/src/lua/common/je_lua_proxy.h b/src/lua/common/je_lua_proxy.h index aa03fa0..6b79b9d 100644 --- a/src/lua/common/je_lua_proxy.h +++ b/src/lua/common/je_lua_proxy.h @@ -20,6 +20,8 @@ namespace JinEngine void release(); + const char* getObjectType(); + template Shared& getShared() { @@ -36,9 +38,9 @@ namespace JinEngine return shared.getObject(); } - const char* getObjectType(); - - // Bind shared object. + /// + /// + /// SharedBase* shared; }; diff --git a/src/lua/common/je_lua_shared.cpp b/src/lua/common/je_lua_shared.cpp index a3478dd..241aec2 100644 --- a/src/lua/common/je_lua_shared.cpp +++ b/src/lua/common/je_lua_shared.cpp @@ -15,6 +15,7 @@ namespace JinEngine { if (--mCount <= 0) { + // Remove game object reference. luax_removeobject(mL, this); delete this; } diff --git a/src/lua/libraries/luax/luax.h b/src/lua/libraries/luax/luax.h deleted file mode 100644 index 0055176..0000000 --- a/src/lua/libraries/luax/luax.h +++ /dev/null @@ -1,449 +0,0 @@ -/** -* Copyright (C) 2018 chai -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is furnished -* to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ -/** -* include following 3 files before this -* lua.h -* lauxlib.h -* lualib.h -*/ -#ifndef __LUAX_H -#define __LUAX_H -// only for lua 5.1 -#if LUA_VERSION_NUM == 501 - -#define LUAX_VERSION "0.1.0" - -#define luax_newstate luaL_newstate -#define luax_close lua_close -#define luax_openlibs luaL_openlibs -// load chunk but dont run it -#define luax_loadbuffer luaL_loadbuffer -#define luax_dostring luaL_dostring -#define luax_pcall lua_pcall -#define luax_setglobal lua_setglobal -#define luax_setglobali(L, i, name)\ - lua_pushvalue(L, i);\ - lua_setglobal(L, name); -#define luax_pop lua_pop -#define luax_remove lua_remove -#define luax_newtable lua_newtable -#define luax_getglobal lua_getglobal - -#define luax_clearstack(L) lua_settop(L, 0) -/** -* -*/ -#define luax_setglobalstring(L, n, v) (lua_pushstring(L, v), lua_setglobal(L, n)) - -/** -* Get number of args -*/ -#define luax_gettop lua_gettop - -#define luax_gettable lua_gettable - -#define luax_settable lua_settable - -/** -* Check userdata type. -*/ -#define luax_checktype luaL_checkudata -#define luax_checknumber luaL_checknumber -#define luax_checkinteger luaL_checkinteger -#define luax_checkstring luaL_checkstring -#define luax_checklstring luaL_checklstring -#define luax_touserdata lua_touserdata -#define luax_tolightuserdata lua_touserdata -//#define luax_checkbool luaL_checkinteger -inline bool luax_checkbool(lua_State *L, int numArg) -{ - bool b = false; - if (lua_type(L, numArg) == LUA_TBOOLEAN) - { - b = lua_toboolean(L, numArg); - } - else - { - luaL_typerror(L, numArg, lua_typename(L, LUA_TBOOLEAN)); - } - return b; -} - -/** -* Oprating tables. -*/ -/* get value and leaves it on top of stack */ -#define luax_rawgetnumber(L, i, k) (lua_rawgeti(L,i, k), lua_tonumber(L, -1)) -inline float luax_rawgetnumberthenpop(lua_State* L, int i, int k) -{ - float n = luax_rawgetnumber(L, i, k); - luax_pop(L, 1); - return n; -} - -#define luax_rawgeti lua_rawgeti - -/** -* -*/ -#define luax_typerror luaL_typerror - - -/** -* Error checking -*/ -#define luax_error luaL_error - -/** -* Push value on the top of stack. -*/ -#define luax_pushnumber lua_pushnumber -#define luax_pushstring lua_pushstring -#define luax_pushlstring lua_pushlstring -#define luax_pushinteger lua_pushinteger -#define luax_pushboolean lua_pushboolean -#define luax_pushlightuserdata lua_pushlightuserdata -#define luax_pushnil lua_pushnil -#define luax_pushvalue lua_pushvalue -#define luax_pushliteral lua_pushliteral - -#define luax_setmetatable lua_setmetatable - -//inline void luax_pushuserdata(lua_State* L, void* p) -//{ -// /** -// * https://stackoverflow.com/questions/15038796/lua-c-api-push-existing-pointers -// */ -// void** box = (void**)lua_newuserdata(L, sizeof(p)); -// *box = p; -// -//} - -#define luax_rawseti lua_rawseti - -/** -* Set field -*/ -#define luax_setfield lua_setfield -#define luax_setfield_(T, L, k, v)\ - do { lua_push##T(L, v); lua_setfield(L, -2, k); } while (0) - -#define luax_setfieldnumber(L, k, v) luax_setfield_(number, L, k, v) -#define luax_setfieldinteger(L, k, v) luax_setfield_(integer, L, k, v) -#define luax_setfieldstring(L, k, v) luax_setfield_(string, L, k, v) -#define luax_setfieldbool(L, k, v) luax_setfield_(boolean, L, k, v) -#define luax_setfieldudata(L, k, v) luax_setfield_(lightuserdata, L, k, v) -#define luax_setfieldcfunc(L, k, v) luax_setfield_(cfunction, L, k, v) -#define luax_setfieldfstring(L, k, ...)\ - do { lua_pushfstring(L, __VA_ARGS__); lua_setfield(L, -2, k); } while (0) - -/** -* If nosuch field push a nil at the top of stack. -*/ -#define luax_getfield(L, I, N) lua_getfield(L, I, N) -inline float luax_getfieldnumber(lua_State* L, int I, const char* N) -{ - luax_getfield(L, I, N); - float n = luax_checknumber(L, -1); - return n; -} -inline int luax_getfieldinteger(lua_State* L, int I, const char* N) -{ - luax_getfield(L, I, N); - int bin = luax_checkinteger(L, -1); - return bin; -} -inline const char* luax_getfieldstring(lua_State* L, int I, const char* N) -{ - luax_getfield(L, I, N); - const char* str = luax_checkstring(L, -1); - return str; -} -inline char luax_getfieldbool(lua_State* L, int I, const char* N) -{ - luax_getfield(L, I, N); - char bin = lua_toboolean(L, -1); - return bin; -} -/** -* -*/ -#define luax_call lua_call - -/** -* Set raw -*/ -#define luax_setraw(T, L, idx, i, v)\ - (lua_push##T(L, v), lua_rawseti(L, idx, i)) - -#define luax_setrawstring(L, idx, i, v) luax_setraw(string, L, idx, i, v) -#define luax_setrawnumber(L, idx, i, v) luax_setraw(number, L, idx, i, v) -#define luax_setrawbool(L, idx, i, v) luax_setraw(boolean, L, idx, i, v) - -/** -* -*/ -#define luax_optboolean(L, i, x)\ - (!lua_isnoneornil(L, i) ? lua_toboolean(L, i) : (x)) -#define luax_optudata(L, i, name, x)\ - (!lua_isnoneornil(L, i) ? luaL_checkudata(L, i, name) : (x)) -#define luax_optnumber luaL_optnumber -#define luax_optinteger luaL_optinteger - -inline int luax_newlib(lua_State* L, const luaL_Reg* f) -{ - lua_createtable(L, 0, sizeof(f)); - for (; f && f->name; ++f) - { - lua_pushcfunction(L, f->func); - lua_setfield(L, -2, f->name); - } - return 1; // leave lib table on top of stack -} - -/** -* Register a userdefined lua type with given type name. -*/ -inline void luax_newtype(lua_State* L, const char* tname, const luaL_Reg* f) -{ - luaL_newmetatable(L, tname); - - // m.__index = m - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - - if (f != 0) - luaL_register(L, 0, f); - - lua_pop(L, 1); // Pops metatable. -} - -/** -* Instance of a type. -*/ -inline void* luax_newinstance(lua_State* L, const char* tname, int size) -{ - void* p = lua_newuserdata(L, size); - - luaL_newmetatable(L, tname); - lua_setmetatable(L, -2); - - return p; -} - -/** -* require a module -*/ -inline int luax_require(lua_State* L, const char* mname, lua_CFunction openf, int glb) -{ - openf(L); - if (glb) - { - lua_setglobal(L, mname); - return 0; - } - return 1; -} - -/** -* Return 1 if match. -*/ -inline int luax_istype(lua_State* L, int idx, const char* tname) -{ - if (lua_getmetatable(L, idx)) - { - /* get metatable called tname */ - lua_getfield(L, LUA_REGISTRYINDEX, tname); - if (lua_rawequal(L, -1, -2)) - { - lua_pop(L, 2); // pop both metatables - return 1; - } - lua_pop(L, 2); - return 0; - } - /* value at idx has no metatable */ - return 0; -} - -#define luax_is(T, L, i) (lua_is##T(L, i)) -#define luax_isnumber(L, i) luax_is(number, L, i) -#define luax_isstring(L, i) luax_is(string, L, i) -#define luax_istable(L, i) luax_is(table, L, i) -#define luax_isnil(L, i) luax_is(nil, L, i) -#define luax_isboolean(L, i) luax_is(boolean, L, i) -#define luax_isfunction(L, i) luax_is(function, L, i) -#define luax_isuserdata lua_isuserdata -#define luax_islightuserdata lua_islightuserdata -inline int luax_isinteger(lua_State* L, int i) -{ - if (!luax_isnumber(L, i)) - return 0; - return lua_tonumber(L, i) == lua_tointeger(L, i); -} -inline int luax_isfloat(lua_State* L, int i) -{ - if (!luax_isnumber(L, i)) - return 0; - return lua_tonumber(L, i) != lua_tointeger(L, i); -} - -#define luax_isnumberstrict(L, i) (lua_type(L, i) == LUA_TNUMBER) -#define luax_isstringstrict(L, i) (lua_type(L, i) == LUA_TSTRING) -#define luax_isbooleanstrict(L, i) (lua_type(L, i) == LUA_TBOOLEAN) -#define luax_istablestrict(L, i) (lua_type(L, i) == LUA_TTABLE) -#define luax_isnilstrict(L, i) (lua_type(L, i) == LUA_TNIL) -inline int luax_isintegerstrict(lua_State* L, int i) -{ - if (!luax_isnumberstrict(L, i)) - return 0; - return lua_tonumber(L, i) == lua_tointeger(L, i); -} -inline int luax_isfloatstrict(lua_State* L, int i) -{ - if (!luax_isnumberstrict(L, i)) - return 0; - return lua_tonumber(L, i) != lua_tointeger(L, i); -} - -/** -* To userdata. -*/ -inline void* luax_toudata(lua_State* L, int idx) -{ - void* p = lua_touserdata(L, idx); - if (p == NULL) - luaL_typerror(L, idx, "userdata"); // if p is not userdata - return p; -} - -/** -* Get table index size. -*/ -inline int luax_tableidxlen(lua_State* L, int i) -{ - return lua_objlen(L, i); -} - -/** -* Get table hash size -inline int luax_tbalehashlen(lua_State* L, int i) -{ - -} -*/ - -/** -* Get table hash and index size -inline int luax_tablelen(lua_State* L, int i) -{ - -} -*/ - -/* create a global tbale and stay it on the top of stack */ -#define luax_globaltable(L, name)\ -lua_newtable(L);\ -lua_pushvalue(L, 1);\ -lua_setglobal(L, name); - -inline int luax_tableinsert(lua_State * L, int tindex, int vindex, int pos) -{ - if (tindex < 0) - tindex = lua_gettop(L) + 1 + tindex; - if (vindex < 0) - vindex = lua_gettop(L) + 1 + vindex; - if (pos == -1) - { - lua_pushvalue(L, vindex); - lua_rawseti(L, tindex, lua_objlen(L, tindex) + 1); - return 0; - } - else if (pos < 0) - pos = lua_objlen(L, tindex) + 1 + pos; - for (int i = lua_objlen(L, tindex) + 1; i > pos; --i) - { - lua_rawgeti(L, tindex, i - 1); - lua_rawseti(L, tindex, i); - } - lua_pushvalue(L, vindex); - lua_rawseti(L, tindex, pos); - return 0; -} - -/** -* Add the package loader to the package.loaders table. -*/ -inline int luax_registersearcher(lua_State * L, lua_CFunction f, int pos) -{ - lua_getglobal(L, "package"); - - if (lua_isnil(L, -1)) - return luaL_error(L, "Can't register searcher: package table does not exist."); - - lua_getfield(L, -1, "loaders"); - - if (lua_isnil(L, -1)) - return luaL_error(L, "Can't register searcher: package.loaders table does not exist."); - - lua_pushcfunction(L, f); - luax_tableinsert(L, -2, -1, pos); - lua_pop(L, 3); - return 0; -} - -typedef struct luax_Str -{ - const char* name; - const char* value; -} luax_Str; - -inline void luax_setfieldstrings(lua_State* L, const luax_Str* strs) -{ - for (int i = 0; strs[i].name != 0; ++i) - { - luax_setfieldstring(L, strs[i].name, strs[i].value); - } -} - -typedef struct luax_Num -{ - const char* name; - float number; -}; - -inline void luax_setfieldnumbers(lua_State* L, const luax_Num* strs) -{ - for (int i = 0; strs[i].name != 0; ++i) - { - luax_setfieldnumber(L, strs[i].name, strs[i].number); - } -} - -typedef luaL_Reg luax_Reg; - -#define luax_ref luaL_ref - -#define luax_unref luaL_unref - -#endif // #if LUA_VERSION_NUM == 501 - -#endif // __LUAX_H \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_animation.cpp b/src/lua/modules/graphics/je_lua_animation.cpp index cd048e3..d2c806a 100644 --- a/src/lua/modules/graphics/je_lua_animation.cpp +++ b/src/lua/modules/graphics/je_lua_animation.cpp @@ -1,6 +1,7 @@ +#include "libjin/jin.h" + #include "common/je_lua_proxy.h" #include "common/je_lua_common.h" -#include "libjin/jin.h" #include "je_lua_sprite.h" #include "je_lua_canvas.h" -- cgit v1.1-26-g67d0