diff options
Diffstat (limited to 'src/lua/modules')
-rw-r--r-- | src/lua/modules/graphics/je_lua_animation.cpp | 71 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_animation.h | 20 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_graphics.cpp | 70 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_sprite.cpp | 210 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_spritesheet.cpp | 23 |
5 files changed, 189 insertions, 205 deletions
diff --git a/src/lua/modules/graphics/je_lua_animation.cpp b/src/lua/modules/graphics/je_lua_animation.cpp new file mode 100644 index 0000000..1db66f1 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_animation.cpp @@ -0,0 +1,71 @@ +#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" +#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"; + + typedef Shared<Animation>& SharedAnimation; + + LUA_IMPLEMENT inline SharedAnimation checkAnimation(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Animation); + return proxy->getShared<Animation>(); + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, Jin_Lua_Animation); + p->release(); + return 0; + } + + LUA_IMPLEMENT int l_render(lua_State* L) + { + SharedAnimation sprite = checkAnimation(L); + float x = luax_checknumber(L, 2); + float y = luax_checknumber(L, 3); + float sx = luax_checknumber(L, 4); + float sy = luax_checknumber(L, 5); + float r = luax_checknumber(L, 6); + sprite->render(x, y, sx, sy, r); + return 0; + } + + LUA_IMPLEMENT int l_getSize(lua_State* L) + { + SharedAnimation sprite = checkAnimation(L); + //Vector2<int> size = sprite->getSize(); + //luax_pushinteger(L, size.x); + //luax_pushinteger(L, size.y); + return 1; + } + + LUA_EXPORT void luaopen_Animation(lua_State* L) + { + luaL_Reg methods[] = { + { "__gc", l_gc }, + { "render", l_render }, + { "getSize", l_getSize }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_Animation, methods); + } + + } // namespace Lua +} // namespace JinEngine
\ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_animation.h b/src/lua/modules/graphics/je_lua_animation.h new file mode 100644 index 0000000..d5e2c6a --- /dev/null +++ b/src/lua/modules/graphics/je_lua_animation.h @@ -0,0 +1,20 @@ +#ifndef __JE_LUA_ANIMATION_H__ +#define __JE_LUA_ANIMATION_H__ + +namespace JinEngine +{ + namespace Lua + { + + extern const char* Jin_Lua_Animation; + + enum clsas AnimationDependency + { + }; + + void luaopen_Animation(lua_State* L); + + } +} + +#endif
\ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_graphics.cpp b/src/lua/modules/graphics/je_lua_graphics.cpp index 900b26b..2bc3a78 100644 --- a/src/lua/modules/graphics/je_lua_graphics.cpp +++ b/src/lua/modules/graphics/je_lua_graphics.cpp @@ -21,6 +21,7 @@ using namespace std; using namespace JinEngine; +using namespace JinEngine::Math; using namespace JinEngine::Graphics; using namespace JinEngine::Graphics::Fonts; using namespace JinEngine::Graphics::Shaders; @@ -391,8 +392,13 @@ namespace JinEngine if (!luax_istype(L, 1, Jin_Lua_Sprite)) return; Proxy* pxySprite = (Proxy*)luax_toudata(L, 1); - Sprite* spr = pxySprite->getObject<Sprite>(); - spr->render(); + Sprite* sprite = pxySprite->getObject<Sprite>(); + float x = luax_checknumber(L, 2); + float y = luax_checknumber(L, 3); + float sx = luax_checknumber(L, 4); + float sy = luax_checknumber(L, 5); + float r = luax_checknumber(L, 6); + sprite->render(x, y, sx, sy, r); } LUA_IMPLEMENT int l_draw(lua_State* L) @@ -710,10 +716,66 @@ namespace JinEngine return 1; } + // newSprite(Texture tex, Quad quad, Origin origin) + // newSprite(Texture tex, Quad quad, Number ox, Number oy) + // newSprite(Texture tex, Origin origin) + // newSprite(Texture tex, Number ox, Number oy) LUA_IMPLEMENT int l_newSprite(lua_State* L) { - Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); - p->bind(new Shared<Sprite>(new Sprite(), Jin_Lua_Sprite)); + int n = luax_gettop(L); + Proxy* pxyGraphic = nullptr; + if (luax_istype(L, 1, Jin_Lua_Texture)) + pxyGraphic = (Proxy*)luax_checktype(L, 1, Jin_Lua_Texture); + else if (luax_istype(L, 1, Jin_Lua_Canvas)) + pxyGraphic = (Proxy*)luax_checktype(L, 1, Jin_Lua_Canvas); + Graphic* graphic = pxyGraphic->getObject<Graphic>(); + if (pxyGraphic != nullptr) + { + if (n == 3 && luax_istable(L, 2)) + { + Quad quad; + quad.x = luax_rawgetnumberthenpop(L, 2, 1); + quad.y = luax_rawgetnumberthenpop(L, 2, 2); + quad.w = luax_rawgetnumberthenpop(L, 2, 3); + quad.h = luax_rawgetnumberthenpop(L, 2, 4); + int o = luax_checkinteger(L, 3); + Sprite::Origin origin = static_cast<Sprite::Origin>(o); + Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); + p->bind(new Shared<Sprite>(new Sprite(graphic, quad, origin), Jin_Lua_Sprite)); + } + else if (n == 4) + { + Quad quad; + quad.x = luax_rawgetnumberthenpop(L, 2, 1); + quad.y = luax_rawgetnumberthenpop(L, 2, 2); + quad.w = luax_rawgetnumberthenpop(L, 2, 3); + quad.h = luax_rawgetnumberthenpop(L, 2, 4); + int ox = luax_checkinteger(L, 3); + int oy = luax_checkinteger(L, 4); + Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); + p->bind(new Shared<Sprite>(new Sprite(graphic, quad, ox, oy), Jin_Lua_Sprite)); + } + else if (n == 2) + { + int o = luax_checkinteger(L, 2); + Sprite::Origin origin = static_cast<Sprite::Origin>(o); + Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); + p->bind(new Shared<Sprite>(new Sprite(graphic, origin), Jin_Lua_Sprite)); + } + else if (n == 3) + { + int ox = luax_checkinteger(L, 2); + int oy = luax_checkinteger(L, 3); + Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); + p->bind(new Shared<Sprite>(new Sprite(graphic, ox, oy), Jin_Lua_Sprite)); + } + else + { + luax_error(L, "No matched overloaded functions."); + return 1; + } + } + return 1; } diff --git a/src/lua/modules/graphics/je_lua_sprite.cpp b/src/lua/modules/graphics/je_lua_sprite.cpp index 76d6c1f..0a56de8 100644 --- a/src/lua/modules/graphics/je_lua_sprite.cpp +++ b/src/lua/modules/graphics/je_lua_sprite.cpp @@ -8,6 +8,7 @@ #include "je_lua_texture.h" #include "je_lua_shader.h" +using namespace JinEngine::Math; using namespace JinEngine::Graphics; using namespace JinEngine::Graphics::Shaders; @@ -32,217 +33,34 @@ namespace JinEngine 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) + LUA_IMPLEMENT int l_render(lua_State* L) { SharedSprite sprite = checkSprite(L); float x = luax_checknumber(L, 2); float y = luax_checknumber(L, 3); - sprite->move(x, y); + float sx = luax_checknumber(L, 4); + float sy = luax_checknumber(L, 5); + float r = luax_checknumber(L, 6); + sprite->render(x, y, sx, sy, r); return 0; } - LUA_IMPLEMENT int l_scale(lua_State* L) + LUA_IMPLEMENT int l_getSize(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); + Vector2<int> size = sprite->getSize(); + luax_pushinteger(L, size.x); + luax_pushinteger(L, size.y); 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 } + { "__gc", l_gc }, + { "render", l_render }, + { "getSize", l_getSize }, + { 0, 0 } }; luax_newtype(L, Jin_Lua_Sprite, methods); } diff --git a/src/lua/modules/graphics/je_lua_spritesheet.cpp b/src/lua/modules/graphics/je_lua_spritesheet.cpp index 15469e9..a1652e0 100644 --- a/src/lua/modules/graphics/je_lua_spritesheet.cpp +++ b/src/lua/modules/graphics/je_lua_spritesheet.cpp @@ -28,11 +28,24 @@ namespace JinEngine Shared<SpriteSheet>& shrSSheet = pxySSheet->getShared<SpriteSheet>(); SpriteSheet* sheet = pxySSheet->getObject<SpriteSheet>(); Quad quad; - quad.x = luax_checkinteger(L, 2); - quad.y = luax_checkinteger(L, 3); - quad.w = luax_checkinteger(L, 4); - quad.h = luax_checkinteger(L, 5); - Sprite* spr = sheet->createSprite(quad); + quad.x = luax_rawgetnumberthenpop(L, 2, 1); + quad.y = luax_rawgetnumberthenpop(L, 2, 2); + quad.w = luax_rawgetnumberthenpop(L, 2, 3); + quad.h = luax_rawgetnumberthenpop(L, 2, 4); + Sprite* spr = nullptr; + if (luax_gettop(L) >= 4) + { + float ox = luax_checknumber(L, 3); + float oy = luax_checknumber(L, 4); + spr = sheet->createSprite(quad, ox, oy); + } + else if (luax_gettop(L) == 3) + { + int o = luax_checkinteger(L, 3); + Sprite::Origin origin; + origin = static_cast<Sprite::Origin>(o); + spr = sheet->createSprite(quad, origin); + } Proxy* pxySprite = luax_newinstance(L, Jin_Lua_Sprite); Shared<Sprite>* shrSprite = new Shared<Sprite>(spr, Jin_Lua_Sprite); shrSprite->setDependency((int)SpriteDependency::DEP_SPRITESHEET, &shrSSheet); |