aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/modules/graphics/je_lua_animation.cpp
blob: 5cd3b1ff3155ff3bb8a3d41494d125b84ad8278e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "libjin/jin.h"

#include "common/je_lua_object.h"
#include "common/je_lua_common.h"

#include "je_lua_sprite.h"
#include "je_lua_canvas.h"
#include "je_lua_texture.h"
#include "je_lua_shader.h"
#include "je_lua_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