aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/graphics/je_lua_sprite.cpp
blob: 76d6c1f4808decec6064476d79d448c2a6e46d12 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "lua/modules/luax.h"

#include "lua/common/je_lua_common.h"
#include "libjin/jin.h"

#include "je_lua_sprite.h"
#include "je_lua_canvas.h"
#include "je_lua_texture.h"
#include "je_lua_shader.h"

using namespace JinEngine::Graphics;
using namespace JinEngine::Graphics::Shaders;

namespace JinEngine
{
    namespace Lua
    {
        const char* Jin_Lua_Sprite = "Sprite";

        typedef Shared<Sprite>& SharedSprite;

        LUA_IMPLEMENT inline SharedSprite checkSprite(lua_State* L)
        {
            Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Sprite);
            return proxy->getShared<Sprite>();
        }

        LUA_IMPLEMENT int l_gc(lua_State* L)
        {
            Proxy* p = (Proxy*)luax_checktype(L, 1, Jin_Lua_Sprite);
            p->release();
            return 0;
        }

        LUA_IMPLEMENT int l_setRotation(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            float r = luax_checknumber(L, 2);
            sprite->setRotation(r);
            return 0;
        }

        LUA_IMPLEMENT int l_setOrigin(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            switch (luax_gettop(L))
            {
            case 2:
            {
                int origin = luax_checkinteger(L, 2);
                sprite->setOrigin(static_cast<Sprite::Origin>(origin));
            }
                break;
            case 3:
            {
                int x = luax_checkinteger(L, 2);
                int y = luax_checkinteger(L, 3);
                sprite->setOrigin(x, y);
            }
                break;
            }
            return 0;
        }

        LUA_IMPLEMENT int l_setPosition(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            float x = luax_checknumber(L, 2);
            float y = luax_checknumber(L, 3);
            sprite->setPosition(x, y);
            return 0;
        }

        LUA_IMPLEMENT int l_setScale(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            float sx = luax_checknumber(L, 2);
            float sy = luax_checknumber(L, 3);
            sprite->setScale(sx, sy);
            return 0;
        }

        LUA_IMPLEMENT int l_setColor(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            Channel r = luax_checkinteger(L, 2);
            Channel g = luax_checkinteger(L, 3);
            Channel b = luax_checkinteger(L, 4);
            Channel a = luax_checkinteger(L, 5);
            sprite->setColor(Color(r, g, b, a));
            return 0;
        }

        LUA_IMPLEMENT int l_setGraphic(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            Graphic* graphic = nullptr;
            Proxy* p = nullptr;
            if (luax_istype(L, 2, Jin_Lua_Texture))
                p = (Proxy*)luax_checktype(L, 2, Jin_Lua_Texture);
            else if (luax_istype(L, 2, Jin_Lua_Canvas))
                p = (Proxy*)luax_checktype(L, 2, Jin_Lua_Canvas);
            if (p != nullptr)
            {
                sprite->setGraphic(p->getObject<Graphic>());
                sprite.setDependency((int)SpriteDependency::DEP_GRAPHIC, &p->getShared<Graphic>());
            }
            return 0;
        }

        LUA_IMPLEMENT int l_move(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            float x = luax_checknumber(L, 2);
            float y = luax_checknumber(L, 3);
            sprite->move(x, y);
            return 0;
        }

        LUA_IMPLEMENT int l_scale(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            float sx = luax_checknumber(L, 2);
            float sy = luax_checknumber(L, 3);
            sprite->scale(sx, sy);
            return 0;
        }

        LUA_IMPLEMENT int l_rotate(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            float r = luax_checknumber(L, 2);
            sprite->rotate(r);
            return 0;
        }

        LUA_IMPLEMENT int l_getRotation(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            float r = sprite->getRotation();
            luax_pushnumber(L, r);
            return 1;
        }

        LUA_IMPLEMENT int l_getPosition(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            const Math::Vector2<float>& pos = sprite->getPosition();
            luax_pushnumber(L, pos.x);
            luax_pushnumber(L, pos.y);
            return 2;
        }

        LUA_IMPLEMENT int l_getOrigin(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            const Math::Vector2<float>& origin = sprite->getOrigin();
            luax_pushinteger(L, origin.x);
            luax_pushinteger(L, origin.y);
            return 2;
        }

        LUA_IMPLEMENT int l_getScale(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            const Math::Vector2<float> scale = sprite->getScale();
            luax_pushnumber(L, scale.x);
            luax_pushnumber(L, scale.y);
            return 2;
        }

        LUA_IMPLEMENT int l_getColor(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            const Color& c = sprite->getColor();
            luax_pushinteger(L, c.r);
            luax_pushinteger(L, c.g);
            luax_pushinteger(L, c.b);
            luax_pushinteger(L, c.a);
            return 4;
        }

        LUA_IMPLEMENT int l_render(lua_State* L)
        {
            SharedSprite sprite = checkSprite(L);
            sprite->render();
            return 0;
        }

        LUA_IMPLEMENT int l_getGraphic(lua_State* L)
        {
            Proxy* pxySprite = (Proxy*)luax_checktype(L, 1, Jin_Lua_Sprite);
            Shared<Sprite>& shrSprite = pxySprite->getShared<Sprite>();
            SharedBase* shrGraphic = shrSprite.getDependency((int)SpriteDependency::DEP_GRAPHIC);
            if (shrGraphic->isType(Jin_Lua_Canvas))
            {
                Proxy* pxyCanvas = luax_newinstance(L, Jin_Lua_Canvas);
                pxyCanvas->bind(shrGraphic);
                return 1;
            }
            else if (shrGraphic->isType(Jin_Lua_Texture))
            {
                Proxy* pxyTexture = luax_newinstance(L, Jin_Lua_Texture);
                pxyTexture->bind(shrGraphic);
                return 1;
            }
            return 0;
        }

        LUA_IMPLEMENT int l_getShader(lua_State* L)
        {
            Proxy* pxySprite = (Proxy*)luax_checktype(L, 1, Jin_Lua_Sprite);
            Shared<Sprite>& shrSprite = pxySprite->getShared<Sprite>();
            SharedBase* shrShader = shrSprite.getDependency((int)SpriteDependency::DEP_SHADER);
            if (shrShader != nullptr && shrShader->isType(Jin_Lua_Shader))
            {
                Proxy* pxyShader = luax_newinstance(L, Jin_Lua_Shader);
                pxyShader->bind(shrShader);
                return 1;
            }
            return 0;
        }

        LUA_EXPORT void luaopen_Sprite(lua_State* L)
        {
            luaL_Reg methods[] = {
                { "__gc",        l_gc          },
                { "render",      l_render      },
                { "setRotation", l_setRotation },
                { "setOrigin",   l_setOrigin   },
                { "setPosition", l_setPosition },
                { "setScale",    l_setScale    },
                { "setColor",    l_setColor    },
                { "setGraphic",  l_setGraphic  },
                { "move",        l_move        },
                { "scale",       l_scale       },
                { "rotate",      l_rotate      },
                { "getRotation", l_getRotation },
                { "getPosition", l_getPosition },
                { "getOrigin",   l_getOrigin   },
                { "getScale",    l_getScale    },
                { "getColor",    l_getColor    },
                { "getShader",   l_getShader   },
                { "getGraphic",  l_getGraphic  },
                { 0,             0             }
            };
            luax_newtype(L, Jin_Lua_Sprite, methods);
        }

    } // namespace Lua
} // namespace JinEngine