diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libjin/graphics/animations/je_animation.cpp | 95 | ||||
-rw-r--r-- | src/libjin/graphics/animations/je_animation.h | 41 | ||||
-rw-r--r-- | src/libjin/graphics/je_graphics.h | 2 | ||||
-rw-r--r-- | src/libjin/graphics/je_sprite.cpp | 4 | ||||
-rw-r--r-- | src/libjin/graphics/je_sprite_sheet.cpp | 61 | ||||
-rw-r--r-- | src/libjin/graphics/je_sprite_sheet.h | 14 | ||||
-rw-r--r-- | src/libjin/math/je_quad.h | 4 | ||||
-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 |
12 files changed, 396 insertions, 219 deletions
diff --git a/src/libjin/graphics/animations/je_animation.cpp b/src/libjin/graphics/animations/je_animation.cpp index 4fe673a..028ef56 100644 --- a/src/libjin/graphics/animations/je_animation.cpp +++ b/src/libjin/graphics/animations/je_animation.cpp @@ -1,5 +1,8 @@ +#include "../../math/je_vector2.hpp" #include "je_animation.h" +using namespace JinEngine::Math; + namespace JinEngine { namespace Graphics @@ -7,7 +10,99 @@ namespace JinEngine namespace Animations { + Animation::Animation() + : mIndex(0) + , mActive(true) + , mLoop(true) + , mTick(0) + { + } + + void Animation::addFrame(const Sprite* frame) + { + if(frame != nullptr) + mFrames.push_back(frame); + } + + void Animation::addFrames(const std::vector<Sprite*>& frames) + { + mFrames.insert(mFrames.end(), frames.begin(), frames.end()); + } + + void Animation::update(float dt) + { + if (!mActive) + return; + mTick += dt; + if (mTick >= mSpeed) + { + mTick -= mSpeed; + next(); + } + } + + void Animation::next() + { + int count = mFrames.size(); + ++mIndex; + if (mLoop) + mIndex %= count; + mIndex = clamp<uint>(mIndex, 0, count - 1); + } + + void Animation::pause() + { + mActive = false; + } + + void Animation::resume() + { + mActive = true; + } + + void Animation::rewind() + { + mIndex = 0; + mTick = 0; + } + + void Animation::setSpeed(float speed) + { + mSpeed = speed; + } + + void Animation::setLoop(bool isLoop) + { + mLoop = isLoop; + } + + uint Animation::getCurrentFrameIndex() + { + return mIndex; + } + + const Sprite* Animation::getCurrentFrame() + { + if (mIndex >= mFrames.size()) + return nullptr; + return mFrames[mIndex]; + } + + void Animation::setCurrentFrame(uint frame) + { + mIndex = frame; + } + void Animation::render(float x, float y, float sx, float sy, float r) + { + if (mFrames.size() == 0) + return; + if (without<uint>(mIndex, 0, mFrames.size() - 1)) + return; + const Sprite* spr = getCurrentFrame(); + if(spr) + spr->render(x, y, sx, sy, r); + } } } diff --git a/src/libjin/graphics/animations/je_animation.h b/src/libjin/graphics/animations/je_animation.h index a13a83b..4c2fd64 100644 --- a/src/libjin/graphics/animations/je_animation.h +++ b/src/libjin/graphics/animations/je_animation.h @@ -19,15 +19,22 @@ namespace JinEngine class Animation { public: + Animation(); + /// /// /// - void update(float dt); + void addFrame(const Sprite* frame); + + /// + /// + /// + void addFrames(const std::vector<Sprite*>& frames); /// /// /// - void start(); + void update(float dt); /// /// @@ -37,7 +44,7 @@ namespace JinEngine /// /// /// - void stop(); + void resume(); /// /// Force rewind. @@ -50,6 +57,11 @@ namespace JinEngine void setSpeed(float speed); /// + /// + /// + void setLoop(bool isLoop); + + /// /// Get current frame index. /// uint getCurrentFrameIndex(); @@ -57,7 +69,7 @@ namespace JinEngine /// /// /// - Sprite* getCurrentFrame(); + const Sprite* getCurrentFrame(); /// /// Set current frame index. @@ -71,17 +83,36 @@ namespace JinEngine /// void render(float x, float y, float sx, float sy, float r); + /// + /// + /// + Animation clone(); + private: + + void next(); + /// /// Key frames. /// - std::vector<Sprite*> mFrames; + std::vector<const Sprite*> mFrames; /// /// Animation playing speed. /// float mSpeed; + /// + /// + /// + float mTick; + + uint mIndex; + + float mLoop; + + bool mActive; + }; } // namespace Animations diff --git a/src/libjin/graphics/je_graphics.h b/src/libjin/graphics/je_graphics.h index 979d8f4..d7824c5 100644 --- a/src/libjin/graphics/je_graphics.h +++ b/src/libjin/graphics/je_graphics.h @@ -21,6 +21,8 @@ #include "particles/je_particle_system.h" +#include "animations/je_animation.h" + //struct Stats //{ // int drawCalls; diff --git a/src/libjin/graphics/je_sprite.cpp b/src/libjin/graphics/je_sprite.cpp index b45fdf3..a82be79 100644 --- a/src/libjin/graphics/je_sprite.cpp +++ b/src/libjin/graphics/je_sprite.cpp @@ -62,7 +62,7 @@ namespace JinEngine switch (origin) { case TopLeft: - org->set(1, t); + org->set(l, t); break; case TopCenter: org->set(r / 2.f, t); @@ -71,7 +71,7 @@ namespace JinEngine org->set(r, t); break; case MiddleLeft: - org->set(1, b / 2.f); + org->set(l, b / 2.f); break; case MiddleCenter: org->set(r / 2.f, b / 2.f); diff --git a/src/libjin/graphics/je_sprite_sheet.cpp b/src/libjin/graphics/je_sprite_sheet.cpp index 936eeab..73d3e81 100644 --- a/src/libjin/graphics/je_sprite_sheet.cpp +++ b/src/libjin/graphics/je_sprite_sheet.cpp @@ -1,5 +1,13 @@ +#include <vector> + +#include "../math/je_quad.h" + #include "je_sprite_sheet.h" +using namespace std; + +using namespace JinEngine::Math; + namespace JinEngine { namespace Graphics @@ -10,13 +18,54 @@ namespace JinEngine { } - Sprite* SpriteSheet::createSprite(const Math::Quad& quad) + Sprite* SpriteSheet::createSprite(const Math::Quad& quad, Sprite::Origin origin) + { + Sprite* spr = new Sprite(mGraphic, quad, origin); + return spr; + } + + Sprite* SpriteSheet::createSprite(const Math::Quad& quad, float ox, float oy) + { + Sprite* spr = new Sprite(mGraphic, quad, ox, oy); + return spr; + } + + std::vector<Sprite*> SpriteSheet::createSprites(uint row, uint colum, uint w, uint h, Sprite::Origin origin) + { + vector<Sprite*> sprites; + for (int r = 0; r < row; ++r) + { + for (int c = 0; c < colum; ++c) + { + Quad quad; + quad.x = (r * colum + c) * w; + quad.y = r * h; + quad.w = w; + quad.h = h; + Sprite* spr = new Sprite(mGraphic, quad, origin); + sprites.push_back(spr); + } + } + return sprites; + } + + vector<Sprite*> SpriteSheet::createSprites(uint row, uint colum, uint w, uint h, float ox, float oy) { - //Sprite* spr = new Sprite(); - //spr->setGraphic(mGraphic); - //spr->setQuad(quad.x, quad.y, quad.w, quad.h); - //return spr; - return nullptr; + vector<Sprite*> sprites; + for (int r = 0; r < row; ++r) + { + for (int c = 0; c < colum; ++c) + { + Quad quad; + quad.x = (r * colum + c) * w; + quad.y = r * h; + quad.w = w; + quad.h = h; + Sprite* spr = new Sprite(mGraphic, quad, ox, oy); + sprites.push_back(spr); + } + } + return sprites; } } diff --git a/src/libjin/graphics/je_sprite_sheet.h b/src/libjin/graphics/je_sprite_sheet.h index 8c56c51..a3db946 100644 --- a/src/libjin/graphics/je_sprite_sheet.h +++ b/src/libjin/graphics/je_sprite_sheet.h @@ -18,7 +18,19 @@ namespace JinEngine /// /// Create a new sprite in sheet. /// - Sprite* createSprite(const Math::Quad& quad); + Sprite* createSprite(const Math::Quad& quad, Sprite::Origin origin); + + /// + /// Create a new sprite in sheet. + /// + Sprite* createSprite(const Math::Quad& quad, float ox, float oy); + + /// + /// + /// + std::vector<Sprite*> createSprites(uint row, uint colum, uint w, uint h, Sprite::Origin origin); + + std::vector<Sprite*> createSprites(uint row, uint colum, uint w, uint h, float ox, float oy); SpriteSheet(const Graphic* graphic); diff --git a/src/libjin/math/je_quad.h b/src/libjin/math/je_quad.h index 74cd294..fd5e7a1 100644 --- a/src/libjin/math/je_quad.h +++ b/src/libjin/math/je_quad.h @@ -11,6 +11,10 @@ namespace JinEngine /// struct Quad { + Quad() + : x(0), y(0), w(0), h(0) + { + } Quad(float _x, float _y, float _w, float _h) : x(_x), y(_y), w(_w), h(_h) { 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); |