aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/graphics/je_lua_sprite.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/modules/graphics/je_lua_sprite.cpp')
-rw-r--r--src/lua/modules/graphics/je_lua_sprite.cpp220
1 files changed, 218 insertions, 2 deletions
diff --git a/src/lua/modules/graphics/je_lua_sprite.cpp b/src/lua/modules/graphics/je_lua_sprite.cpp
index d87f23b..f02d1cc 100644
--- a/src/lua/modules/graphics/je_lua_sprite.cpp
+++ b/src/lua/modules/graphics/je_lua_sprite.cpp
@@ -1,11 +1,227 @@
+#include "lua/modules/luax.h"
+#include "lua/modules/types.h"
+#include "lua/common/je_lua_common.h"
#include "libjin/jin.h"
+#include "je_lua_sprite.h"
+
+using namespace JinEngine::Graphics;
+using namespace JinEngine::Graphics::Shaders;
namespace JinEngine
{
namespace Lua
{
+ //////////////////////////////////////////////////////////////////////////////////////////////////////////
+ // Sprite class.
+ //////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ Lua::Sprite::~Sprite()
+ {
+ if (mGraphicShared != nullptr)
+ {
+ mGraphicShared->release();
+ mGraphicShared = nullptr;
+ }
+ if (mShaderShared != nullptr)
+ {
+ mShaderShared->release();
+ mShaderShared = nullptr;
+ }
+ }
+
+ void Lua::Sprite::setShader(Shared<Shader>* shader)
+ {
+ if (mShaderShared != nullptr)
+ mShaderShared->release();
+ mShaderShared = shader;
+ mShaderShared->retain();
+ Parent::setShader(mShaderShared->getObject());
+ }
+
+ void Lua::Sprite::setGraphic(Shared<Graphic>* graphic)
+ {
+ if (mGraphicShared != nullptr)
+ mGraphicShared = graphic;
+ mGraphicShared = graphic;
+ mGraphicShared->retain();
+ Parent::setGraphic(mGraphicShared->getObject());
+ }
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////////////
+ // Register Sprite class.
+ //////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ typedef Shared<Lua::Sprite>& SharedSprite;
+
+ LUA_IMPLEMENT inline SharedSprite checkSprite(lua_State* L)
+ {
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SPRITE);
+ return proxy->getShared<Lua::Sprite>();
+ }
+
+ LUA_IMPLEMENT int l_gc(lua_State* L)
+ {
+ Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_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<Lua::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);
+ int x = luax_checkinteger(L, 2);
+ int y = luax_checkinteger(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_setShader(lua_State* L)
+ {
+ SharedSprite sprite = checkSprite(L);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_SHADER);
+ sprite->setShader(&proxy->getShared<Shader>());
+ 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_GRAPHICS_TEXTURE))
+ p = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXTURE);
+ else if (luax_istype(L, 2, JIN_GRAPHICS_CANVAS))
+ p = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_CANVAS);
+ if(p != nullptr)
+ sprite->setGraphic(&p->getShared<Graphic>());
+ 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<int>& 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_EXPORT int 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 },
+ { "setShader", l_setShader },
+ { "setGraphic", l_setGraphic },
+ { "getRotation", l_getRotation },
+ { "getPosition", l_getPosition },
+ { "getOrigin", l_getOrigin },
+ { "getScale", l_getScale },
+ { "getColor", l_getColor },
+ { 0, 0 }
+ };
+ luax_newtype(L, JIN_GRAPHICS_SPRITE, methods);
+ return 0;
+ }
- } // namespace JinEngine
-} // namespace Lua \ No newline at end of file
+ } // namespace Lua
+} // namespace JinEngine \ No newline at end of file