aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/modules/graphics/l_animation.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin-lua/modules/graphics/l_animation.cpp')
-rw-r--r--src/libjin-lua/modules/graphics/l_animation.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/libjin-lua/modules/graphics/l_animation.cpp b/src/libjin-lua/modules/graphics/l_animation.cpp
new file mode 100644
index 0000000..0bf133f
--- /dev/null
+++ b/src/libjin-lua/modules/graphics/l_animation.cpp
@@ -0,0 +1,122 @@
+#include "libjin/jin.h"
+
+#include "common/l_object.h"
+#include "common/l_common.h"
+
+#include "l_sprite.h"
+#include "l_canvas.h"
+#include "l_texture.h"
+#include "l_shader.h"
+#include "l_animation.h"
+
+using namespace JinEngine::Math;
+using namespace JinEngine::Graphics;
+using namespace JinEngine::Graphics::Shaders;
+using namespace JinEngine::Graphics::Animations;
+
+namespace JinEngine
+{
+ namespace Lua
+ {
+ const char* Jin_Lua_Animation = "Animation";
+
+ LUA_IMPLEMENT inline Animation* checkAnimation(lua_State* L)
+ {
+ LuaObject* luaObj = luax_checkobject(L, 1, Jin_Lua_Animation);
+ return luaObj->getObject<Animation>();
+ }
+
+ LUA_IMPLEMENT int l_gc(lua_State* L)
+ {
+ LuaObject* p = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Animation);
+ p->release();
+ return 0;
+ }
+
+ // addFrame(frame)
+ LUA_IMPLEMENT int l_addFrame(lua_State* L)
+ {
+ LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Animation);
+ Animation* animation = luaObj->getObject<Animation>();
+ LuaObject* luaSprite = (LuaObject*)luax_checktype(L, 2, Jin_Lua_Sprite);
+ Sprite* sprite = luaSprite->getObject<Sprite>();
+ animation->addFrame(sprite);
+ int i = animation->getFrameCount() - 1;
+ luaObj->setDependency((int)AnimationDependency::DEP_SPRITES + i, luaSprite);
+ return 0;
+ }
+
+ // addFrames(frames table)
+ LUA_IMPLEMENT int l_addFrames(lua_State* L)
+ {
+ LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Animation);
+ Animation* shrAnimation = luaObj->getObject<Animation>();
+ if (!luax_istable(L, 2))
+ {
+ luax_typerror(L, 2, "sprites table");
+ return 1;
+ }
+ int n = luax_tableidxlen(L, 2);
+ for (int i = 1; i <= n; ++i)
+ {
+ luax_rawgeti(L, 2, i);
+ LuaObject* luaSprite = (LuaObject*)luax_checktype(L, -1, Jin_Lua_Sprite);
+ Sprite* sprite = luaSprite->getObject<Sprite>();
+ shrAnimation->addFrame(sprite);
+ int index = shrAnimation->getFrameCount() - 1;
+ luaObj->setDependency((int)AnimationDependency::DEP_SPRITES + index, luaSprite);
+ }
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_isLoop(lua_State* L)
+ {
+ Animation* shrAnimation = checkAnimation(L);
+ bool loop = shrAnimation->isLoop();
+ luax_pushboolean(L, loop);
+ return 1;
+ }
+
+ LUA_IMPLEMENT int l_getSpeed(lua_State* L)
+ {
+ Animation* shrAnimation = checkAnimation(L);
+ float speed = shrAnimation->getSpeed();
+ luax_pushnumber(L, speed);
+ return 1;
+ }
+
+ LUA_IMPLEMENT int l_getFrame(lua_State* L)
+ {
+ LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Animation);
+ Animation* shrAnimation = luaObj->getObject<Animation>();
+ int i = luax_checkinteger(L, 2);
+ LuaObject* frame = luaObj->getDependency((int)AnimationDependency::DEP_SPRITES + i);
+ luax_getobject(L, frame);
+ return 1;
+ }
+
+ LUA_IMPLEMENT int getFrameCount(lua_State* L)
+ {
+ Animation* shrAnimation = checkAnimation(L);
+ int n = shrAnimation->getFrameCount();
+ luax_pushinteger(L, n);
+ return 1;
+ }
+
+ LUA_EXPORT void luaopen_Animation(lua_State* L)
+ {
+ luaL_Reg methods[] = {
+ { "__gc", l_gc },
+ { "addFrame", l_addFrame },
+ { "addFrames", l_addFrames },
+ { "isLoop", l_isLoop },
+ { "getSpeed", l_getSpeed },
+ { "getFrameCount", getFrameCount },
+ { "getFrame", l_getFrame },
+ { 0, 0 }
+ };
+ luax_newtype(L, Jin_Lua_Animation, methods);
+ }
+
+ } // namespace Lua
+} // namespace JinEngine \ No newline at end of file