From 07022c42a925d4d0c23ab31f0e75883766ce773a Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 21 Nov 2018 21:12:42 +0800 Subject: =?UTF-8?q?*=E5=8A=A8=E7=94=BB=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/modules/graphics/je_lua_animation.cpp | 67 +++++++--- src/lua/modules/graphics/je_lua_animation.h | 6 +- src/lua/modules/graphics/je_lua_animator.cpp | 157 ++++++++++++++++++++++++ src/lua/modules/graphics/je_lua_animator.h | 23 ++++ src/lua/modules/graphics/je_lua_graphics.cpp | 60 ++++++++- src/lua/modules/graphics/je_lua_spritesheet.cpp | 63 ++++++++-- 6 files changed, 349 insertions(+), 27 deletions(-) create mode 100644 src/lua/modules/graphics/je_lua_animator.cpp create mode 100644 src/lua/modules/graphics/je_lua_animator.h (limited to 'src/lua/modules/graphics') diff --git a/src/lua/modules/graphics/je_lua_animation.cpp b/src/lua/modules/graphics/je_lua_animation.cpp index 1db66f1..cc82685 100644 --- a/src/lua/modules/graphics/je_lua_animation.cpp +++ b/src/lua/modules/graphics/je_lua_animation.cpp @@ -35,34 +35,67 @@ namespace JinEngine return 0; } - LUA_IMPLEMENT int l_render(lua_State* L) + // addFrame(frame) + LUA_IMPLEMENT int l_addFrame(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); + SharedAnimation shrAnimation = checkAnimation(L); + Proxy* pxySprite = (Proxy*)luax_checktype(L, 2, Jin_Lua_Sprite); + Shared& shrSprite = pxySprite->getShared(); + shrAnimation->addFrame(shrSprite.getObject()); + int i = shrAnimation->getFrameCount() - 1; + shrAnimation.setDependency((int)AnimationDependency::DEP_SPRITES + i, &shrSprite); return 0; } - LUA_IMPLEMENT int l_getSize(lua_State* L) + // addFrames(frames table) + LUA_IMPLEMENT int l_addFrames(lua_State* L) { - SharedAnimation sprite = checkAnimation(L); - //Vector2 size = sprite->getSize(); - //luax_pushinteger(L, size.x); - //luax_pushinteger(L, size.y); + SharedAnimation shrAnimation = checkAnimation(L); + 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); + Proxy* pxySprite = (Proxy*)luax_checktype(L, -1, Jin_Lua_Sprite); + Shared& shrSprite = pxySprite->getShared(); + shrAnimation->addFrame(shrSprite.getObject()); + int index = shrAnimation->getFrameCount() - 1; + shrAnimation.setDependency((int)AnimationDependency::DEP_SPRITES + index, &shrSprite); + } + return 0; + } + + LUA_IMPLEMENT int l_isLoop(lua_State* L) + { + SharedAnimation shrAnimation = checkAnimation(L); + bool loop = shrAnimation->isLoop(); + luax_pushboolean(L, loop); + return 1; + } + + LUA_IMPLEMENT int l_getSpeed(lua_State* L) + { + SharedAnimation shrAnimation = checkAnimation(L); + float speed = shrAnimation->getSpeed(); + luax_pushnumber(L, speed); return 1; } LUA_EXPORT void luaopen_Animation(lua_State* L) { luaL_Reg methods[] = { - { "__gc", l_gc }, - { "render", l_render }, - { "getSize", l_getSize }, - { 0, 0 } + { "__gc", l_gc }, + { "addFrame", l_addFrame }, + { "addFrames", l_addFrames }, + { "isLoop", l_isLoop }, + { "getSpeed", l_getSpeed }, + { "getFrameCount", l_getSpeed }, + //{ "getFrame", l_getFrame }, + { 0, 0 } }; luax_newtype(L, Jin_Lua_Animation, methods); } diff --git a/src/lua/modules/graphics/je_lua_animation.h b/src/lua/modules/graphics/je_lua_animation.h index d5e2c6a..1b32ec3 100644 --- a/src/lua/modules/graphics/je_lua_animation.h +++ b/src/lua/modules/graphics/je_lua_animation.h @@ -8,8 +8,12 @@ namespace JinEngine extern const char* Jin_Lua_Animation; - enum clsas AnimationDependency + /// + /// + /// + enum class AnimationDependency { + DEP_SPRITES = 1 ///< Index from 1 }; void luaopen_Animation(lua_State* L); diff --git a/src/lua/modules/graphics/je_lua_animator.cpp b/src/lua/modules/graphics/je_lua_animator.cpp new file mode 100644 index 0000000..2c835be --- /dev/null +++ b/src/lua/modules/graphics/je_lua_animator.cpp @@ -0,0 +1,157 @@ +#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_animator.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_Animator = "Animator"; + + typedef Shared& SharedAnimator; + + LUA_IMPLEMENT inline SharedAnimator checkAnimator(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Animator); + return proxy->getShared(); + } + + 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_update(lua_State* L) + { + SharedAnimator animator = checkAnimator(L); + float dt = luax_checknumber(L, 2); + animator->update(dt); + return 0; + } + + LUA_IMPLEMENT int l_play(lua_State* L) + { + SharedAnimator animator = checkAnimator(L); + animator->play(); + return 0; + } + + LUA_IMPLEMENT int l_pause(lua_State* L) + { + SharedAnimator animator = checkAnimator(L); + animator->pause(); + return 0; + } + + LUA_IMPLEMENT int l_resume(lua_State* L) + { + SharedAnimator animator = checkAnimator(L); + animator->resume(); + return 0; + } + + LUA_IMPLEMENT int l_rewind(lua_State* L) + { + SharedAnimator animator = checkAnimator(L); + animator->rewind(); + return 0; + } + + LUA_IMPLEMENT int l_render(lua_State* L) + { + SharedAnimator animator = checkAnimator(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); + animator->render(x, y, sx, sy, r); + return 0; + } + + LUA_IMPLEMENT int l_setAnimation(lua_State* L) + { + SharedAnimator shrAnimator = checkAnimator(L); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Animation); + Shared& shrAnimation = proxy->getShared(); + shrAnimator.setDependency((int)AnimatorDependency::DEP_ANIMATION, &shrAnimation); + shrAnimator->setAnimation(shrAnimation.getObject()); + return 0; + } + + LUA_IMPLEMENT int l_forceToFrame(lua_State* L) + { + SharedAnimator shrAnimator = checkAnimator(L); + int index = luax_checkinteger(L, 2); + shrAnimator->forceToFrame(index); + return 0; + } + + LUA_IMPLEMENT int l_setSpeed(lua_State* L) + { + SharedAnimator shrAnimator = checkAnimator(L); + float fps = luax_checknumber(L, 2); + shrAnimator->setSpeed(fps); + return 0; + } + + LUA_IMPLEMENT int l_setDefaultSpeed(lua_State* L) + { + SharedAnimator shrAnimator = checkAnimator(L); + shrAnimator->setDefaultSpeed(); + return 0; + } + + LUA_IMPLEMENT int l_setLoop(lua_State* L) + { + SharedAnimator shrAnimator = checkAnimator(L); + bool loop = luax_checkbool(L, 2); + shrAnimator->setLoop(loop); + return 0; + } + + LUA_IMPLEMENT int l_setDefaultLoop(lua_State* L) + { + SharedAnimator shrAnimator = checkAnimator(L); + shrAnimator->setDefaultLoop(); + return 0; + } + + LUA_EXPORT void luaopen_Animator(lua_State* L) + { + luaL_Reg methods[] = { + { "__gc", l_gc }, + { "update", l_update }, + { "play", l_play }, + { "pause", l_pause }, + { "resume", l_resume }, + { "rewind", l_rewind }, + { "render", l_render }, + { "setAnimation", l_setAnimation }, + { "forceToFrame", l_forceToFrame }, + { "setSpeed", l_setSpeed }, + { "setDefaultSpeed", l_setDefaultSpeed }, + { "setLoop", l_setLoop }, + { "setDefaultLoop", l_setDefaultLoop }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_Animator, methods); + } + + } // namespace Lua +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_animator.h b/src/lua/modules/graphics/je_lua_animator.h new file mode 100644 index 0000000..0292a77 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_animator.h @@ -0,0 +1,23 @@ +#ifndef __JE_LUA_ANIMATOR_H__ +#define __JE_LUA_ANIMATOR_H__ + +#include "libjin/jin.h" + +namespace JinEngine +{ + namespace Lua + { + + extern const char* Jin_Lua_Animator; + + enum class AnimatorDependency + { + DEP_ANIMATION = 1 + }; + + void luaopen_Animator(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 2bc3a78..4c95fa6 100644 --- a/src/lua/modules/graphics/je_lua_graphics.cpp +++ b/src/lua/modules/graphics/je_lua_graphics.cpp @@ -18,6 +18,8 @@ #include "je_lua_texture_font.h" #include "je_lua_page.h" #include "je_lua_sprite.h" +#include "je_lua_animation.h" +#include "je_lua_animator.h" using namespace std; using namespace JinEngine; @@ -25,6 +27,7 @@ using namespace JinEngine::Math; using namespace JinEngine::Graphics; using namespace JinEngine::Graphics::Fonts; using namespace JinEngine::Graphics::Shaders; +using namespace JinEngine::Graphics::Animations; using namespace JinEngine::Filesystem; namespace JinEngine @@ -739,7 +742,7 @@ namespace JinEngine 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(o); + Origin origin = static_cast(o); Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); p->bind(new Shared(new Sprite(graphic, quad, origin), Jin_Lua_Sprite)); } @@ -758,7 +761,7 @@ namespace JinEngine else if (n == 2) { int o = luax_checkinteger(L, 2); - Sprite::Origin origin = static_cast(o); + Origin origin = static_cast(o); Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); p->bind(new Shared(new Sprite(graphic, origin), Jin_Lua_Sprite)); } @@ -800,6 +803,55 @@ namespace JinEngine return 0; } + // newAnimation([frames table, loop, speed]) + LUA_IMPLEMENT int l_newAnimation(lua_State* L) + { + int args = luax_gettop(L); + Shared* shrAnimation = new Shared(new Animation(), Jin_Lua_Animation); + if (args >= 3) + { + if (!luax_istable(L, 1)) + { + luax_typerror(L, 1, "frames table"); + return 1; + } + bool loop = luax_checkbool(L, 2); + float speed = luax_checknumber(L, 3); + int n = luax_tableidxlen(L, 1); + for (int i = 1; i <= n; ++i) + { + luax_rawgeti(L, 1, i); + Proxy* pxySprite = (Proxy*)luax_checktype(L, -1, Jin_Lua_Sprite); + Shared& shrSprite = pxySprite->getShared(); + (*shrAnimation)->addFrame(shrSprite.getObject()); + int index = (*shrAnimation)->getFrameCount() - 1; + (*shrAnimation).setDependency((int)AnimationDependency::DEP_SPRITES + index, &shrSprite); + } + (*shrAnimation)->setLoop(loop); + (*shrAnimation)->setSpeed(speed); + } + Proxy* pxyAnimation = luax_newinstance(L, Jin_Lua_Animation); + pxyAnimation->bind(shrAnimation); + return 1; + } + + // newAnimator([animation]) + LUA_IMPLEMENT int l_newAnimator(lua_State* L) + { + int args = luax_gettop(L); + Shared* shrAniamtor = new Shared(new Animator(), Jin_Lua_Animator); + if (args >= 1) + { + Proxy* pxyAnimation = (Proxy*)luax_checktype(L, 1, Jin_Lua_Animation); + Shared& shrAnimtion = pxyAnimation->getShared(); + (*shrAniamtor)->setAnimation(shrAnimtion.getObject()); + (*shrAniamtor).setDependency((int)AnimatorDependency::DEP_ANIMATION, &shrAnimtion); + } + Proxy* pxyAnimator = luax_newinstance(L, Jin_Lua_Animator); + pxyAnimator->bind(shrAniamtor); + return 1; + } + /* newTextureFont(bitmap, text, color | cellw, cellh) */ LUA_IMPLEMENT int l_newTextureFont(lua_State* L) { @@ -930,6 +982,8 @@ namespace JinEngine luaopen_Shader(L); luaopen_Sprite(L); luaopen_SpriteSheet(L); + luaopen_Animation(L); + luaopen_Animator(L); luaL_Reg f[] = { /* window */ @@ -952,6 +1006,8 @@ namespace JinEngine { "newTextureFont", l_newTextureFont }, { "newSprite", l_newSprite }, { "newSpriteSheet", l_newSpriteSheet }, + { "newAnimation", l_newAnimation }, + { "newAnimator", l_newAnimator }, /* render */ { "setClearColor", l_setClearColor }, { "clear", l_clear }, diff --git a/src/lua/modules/graphics/je_lua_spritesheet.cpp b/src/lua/modules/graphics/je_lua_spritesheet.cpp index 8454321..504e022 100644 --- a/src/lua/modules/graphics/je_lua_spritesheet.cpp +++ b/src/lua/modules/graphics/je_lua_spritesheet.cpp @@ -1,10 +1,12 @@ -#include "lua/modules/luax.h" +#include +#include "lua/modules/luax.h" #include "lua/common/je_lua_common.h" #include "libjin/jin.h" #include "je_lua_sprite.h" #include "je_lua_spritesheet.h" +using namespace std; using namespace JinEngine::Math; using namespace JinEngine::Graphics; @@ -42,8 +44,8 @@ namespace JinEngine else if (luax_gettop(L) == 3) { int o = luax_checkinteger(L, 3); - Sprite::Origin origin; - origin = static_cast(o); + Origin origin; + origin = static_cast(o); spr = sheet->createSprite(quad, origin); } Proxy* pxySprite = luax_newinstance(L, Jin_Lua_Sprite); @@ -56,15 +58,62 @@ namespace JinEngine // {} = newSprites LUA_IMPLEMENT int l_newSprites(lua_State* L) { - + Proxy* pxySS = (Proxy*)luax_checktype(L, 1, Jin_Lua_SpriteSheet); + Shared& shrSS = pxySS->getShared(); + SpriteSheet* ss = pxySS->getObject(); + int count = luax_checkinteger(L, 2); + int r = luax_checkinteger(L, 3); + int c = luax_checkinteger(L, 4); + int w = luax_checkinteger(L, 5); + int h = luax_checkinteger(L, 6); + vector sprs; + int args = luax_gettop(L); + if (args == 6) + { + sprs = ss->createSprites(count, r, c, w, h, Origin::TopLeft); + } + else if (args >= 8) + { + int ox = luax_checkinteger(L, 7); + int oy = luax_checkinteger(L, 8); + int offx = luax_optinteger(L, 9, 0); + int offy = luax_optinteger(L, 10, 0); + sprs = ss->createSprites(count, r, c, w, h, ox, oy, offx, offy); + } + else if (args >= 7) + { + int o = luax_checkinteger(L, 7); + Origin origin = static_cast(o); + int offx = luax_optinteger(L, 8, 0); + int offy = luax_optinteger(L, 9, 0); + sprs = ss->createSprites(count, r, c, w, h, origin, offx, offy); + } + else + { + luax_error(L, "No matched override function."); + return 1; + } + luax_newtable(L); + SharedBase* shrGraphic = shrSS.getDependency((int)SpriteSheetDependency::DEP_GRAPHIC); + for (int i = 0; i < sprs.size(); ++i) + { + Sprite* spr = sprs[i]; + Proxy* pxys = (Proxy*)luax_newinstance(L, Jin_Lua_Sprite); + Shared* shrSpr = new Shared(spr, Jin_Lua_Sprite); + shrSpr->setDependency((int)SpriteDependency::DEP_GRAPHIC, shrGraphic); + pxys->bind(shrSpr); + luax_rawseti(L, -2, i + 1); + } + return 1; } LUA_EXPORT void luaopen_SpriteSheet(lua_State* L) { luaL_Reg methods[] = { - { "__gc", l_gc }, - { "newSprite", l_newSprite }, - { 0, 0 } + { "__gc", l_gc }, + { "newSprite", l_newSprite }, + { "newSprites", l_newSprites }, + { 0, 0 } }; luax_newtype(L, Jin_Lua_SpriteSheet, methods); } -- cgit v1.1-26-g67d0