diff options
36 files changed, 557 insertions, 106 deletions
diff --git a/bin/Jin.exe b/bin/Jin.exe Binary files differindex 31add2d..9b923a4 100644 --- a/bin/Jin.exe +++ b/bin/Jin.exe diff --git a/bin/SDL2.dll b/bin/SDL2.dll Binary files differindex 9cc2988..2276718 100644 --- a/bin/SDL2.dll +++ b/bin/SDL2.dll diff --git a/bin/anim2.png b/bin/anim2.png Binary files differnew file mode 100644 index 0000000..63c1f63 --- /dev/null +++ b/bin/anim2.png diff --git a/bin/game/main.lua b/bin/game/main.lua index 324f9dc..b8aa2cd 100644 --- a/bin/game/main.lua +++ b/bin/game/main.lua @@ -1,34 +1,44 @@ io.stdout:setvbuf("no") local shader = [[ #VERTEX_SHADER -Vertex vert(Vertex v) -{ - return v; -} + + Vertex vert(Vertex v) + { + return v; + } + #END_VERTEX_SHADER + #FRAGMENT_SHADER -Color frag(Color col, Texture tex, Vertex v) -{ - return col; -} + + Color frag(Color col, Texture tex, Vertex v) + { + return col; + } + #END_FRAGMENT_SHADER ]] local shader2 = [[ #VERTEX_SHADER -Vertex vert(Vertex v) -{ - return v; -} + + Vertex vert(Vertex v) + { + return v; + } + #END_VERTEX_SHADER + #FRAGMENT_SHADER -Color frag(Color col, Texture tex, Vertex v) -{ - if(v.xy.x > 30) - return Color(1, 0, 0, 1); - Color c = texel(tex, v.uv); - return c; -} + + Color frag(Color col, Texture tex, Vertex v) + { + if(v.xy.x > 30) + return Color(1, 0, 0, 1); + Color c = texel(tex, v.uv); + return c; + } + #END_FRAGMENT_SHADER ]] music = nil @@ -40,6 +50,16 @@ local tb = {x = 1, y = 2} local t = 0 local spr = nil local bitmap = nil +local sprs = {} +local animator = nil + +local function createAnimation(path, count, r, c, w, h, loop, speed) + local tex = jin.graphics.newTexture(path) + local ssheet = jin.graphics.newSpriteSheet(tex) + local sprs = ssheet:newSprites(count, r, c, w, h, jin.graphics.SpriteOrigin.BOTTOMCENTER) + return jin.graphics.newAnimation(sprs, loop, speed) +end + function jin.core.onLoad() bitmap = jin.graphics.newBitmap(200, 200, function(w, h, x, y) return {255*math.sin(x/w),255 - 255,255*math.cos(y/w),255} @@ -48,6 +68,12 @@ function jin.core.onLoad() shader_program2 = jin.graphics.newShader(shader2) --tex = jin.graphics.newTexture("1.png") tex = jin.graphics.newTexture(bitmap) + local tex2 = jin.graphics.newTexture("anim.png") + local ssheet2 = jin.graphics.newSpriteSheet(tex2) + sprs = ssheet2:newSprites(1, 19, 246, 238, jin.graphics.SpriteOrigin.BOTTOMCENTER) + local animation = createAnimation("anim2.png", 27, 3, 10, 200, 200, true, 20) + animator = jin.graphics.newAnimator(animation) + animation = nil local ssheet = jin.graphics.newSpriteSheet(tex) spr = ssheet:newSprite({50, 50, 50, 50}, 20, 20) tex = nil @@ -59,11 +85,12 @@ function jin.core.onLoad() jin.graphics.showWindow() timer = jin.time.newTimer() local h = timer:every(0.5, function(sp) - + end, spr) - timer:after(3, function(p) + timer:after(6, function(p) --timer:cancel(h) - end, h) + --animator:pause() + end, animator) jin.graphics.pushMatrix() jin.graphics.translate(0, 0) --jin.graphics.rotate(0.2) @@ -79,6 +106,7 @@ end function jin.core.onUpdate() tb.x = t t = t + jin.time.getDelta() + animator:update(jin.time.getDelta()) end function jin.core.onDraw() @@ -88,7 +116,10 @@ function jin.core.onDraw() jin.graphics.rect(jin.graphics.RenderMode.FILL, 30, 50, 100, 200) jin.graphics.setColor(255, 255, 255, 255) jin.graphics.unuseShader() - jin.graphics.draw(spr, 0, 0, 1, 1, 0) + --jin.graphics.draw(sprs[2], 150, 150, 1, 1, 0) + local x, y = jin.mouse.getPosition() + animator:render(x, y, 1, 1, 0) + jin.graphics.print(#sprs, 10, 10) --jin.graphics.draw(spr) --jin.graphics.useShader(shader_program2) --jin.graphics.draw(tex, 0, 0,0.2, 0.2) diff --git a/build/vc++/jin.vcxproj b/build/vc++/jin.vcxproj index 1b6325d..c258194 100644 --- a/build/vc++/jin.vcxproj +++ b/build/vc++/jin.vcxproj @@ -158,6 +158,7 @@ <ClCompile Include="..\..\src\lua\modules\event\je_lua_event.cpp" /> <ClCompile Include="..\..\src\lua\modules\filesystem\je_lua_filesystem.cpp" /> <ClCompile Include="..\..\src\lua\modules\graphics\je_lua_animation.cpp" /> + <ClCompile Include="..\..\src\lua\modules\graphics\je_lua_animator.cpp" /> <ClCompile Include="..\..\src\lua\modules\graphics\je_lua_bitmap.cpp" /> <ClCompile Include="..\..\src\lua\modules\graphics\je_lua_canvas.cpp" /> <ClCompile Include="..\..\src\lua\modules\graphics\je_lua_particle_system.cpp" /> @@ -212,6 +213,7 @@ <ClInclude Include="..\..\src\lua\modules\event\je_lua_event.h" /> <ClInclude Include="..\..\src\lua\modules\filesystem\je_lua_filesystem.h" /> <ClInclude Include="..\..\src\lua\modules\graphics\je_lua_animation.h" /> + <ClInclude Include="..\..\src\lua\modules\graphics\je_lua_animator.h" /> <ClInclude Include="..\..\src\lua\modules\graphics\je_lua_bitmap.h" /> <ClInclude Include="..\..\src\lua\modules\graphics\je_lua_canvas.h" /> <ClInclude Include="..\..\src\lua\modules\graphics\je_lua_graphics.h" /> diff --git a/build/vc++/jin.vcxproj.filters b/build/vc++/jin.vcxproj.filters index eb8b588..2c4ff73 100644 --- a/build/vc++/jin.vcxproj.filters +++ b/build/vc++/jin.vcxproj.filters @@ -186,6 +186,9 @@ <ClCompile Include="..\..\src\lua\modules\graphics\je_lua_animation.cpp"> <Filter>source\modules\graphics</Filter> </ClCompile> + <ClCompile Include="..\..\src\lua\modules\graphics\je_lua_animator.cpp"> + <Filter>source\modules\graphics</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="resource.h"> @@ -353,6 +356,9 @@ <ClInclude Include="..\..\src\lua\modules\graphics\je_lua_animation.h"> <Filter>source\modules\graphics</Filter> </ClInclude> + <ClInclude Include="..\..\src\lua\modules\graphics\je_lua_animator.h"> + <Filter>source\modules\graphics</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ResourceCompile Include="jin.rc"> diff --git a/build/vc++/libjin/libjin.vcxproj b/build/vc++/libjin/libjin.vcxproj index 4012fc0..533de49 100644 --- a/build/vc++/libjin/libjin.vcxproj +++ b/build/vc++/libjin/libjin.vcxproj @@ -174,6 +174,7 @@ <ClInclude Include="..\..\..\src\libjin\Graphics\je_image.h" /> <ClInclude Include="..\..\..\src\libjin\Graphics\je_mesh.h" /> <ClInclude Include="..\..\..\src\libjin\Graphics\je_gl.h" /> + <ClInclude Include="..\..\..\src\libjin\graphics\je_renderable.h" /> <ClInclude Include="..\..\..\src\libjin\graphics\je_sprite_batch.h" /> <ClInclude Include="..\..\..\src\libjin\graphics\je_sprite_sheet.h" /> <ClInclude Include="..\..\..\src\libjin\Graphics\je_shapes.h" /> diff --git a/build/vc++/libjin/libjin.vcxproj.filters b/build/vc++/libjin/libjin.vcxproj.filters index a4dc87d..9b9227d 100644 --- a/build/vc++/libjin/libjin.vcxproj.filters +++ b/build/vc++/libjin/libjin.vcxproj.filters @@ -315,6 +315,9 @@ <ClInclude Include="..\..\..\src\libjin\graphics\animations\je_animator.h"> <Filter>source\graphics\animations</Filter> </ClInclude> + <ClInclude Include="..\..\..\src\libjin\graphics\je_renderable.h"> + <Filter>source\graphics</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <None Include="..\..\..\src\libjin\README.md"> diff --git a/examples/animation/main.cpp b/examples/animation/main.cpp index 741385a..7d85c77 100644 --- a/examples/animation/main.cpp +++ b/examples/animation/main.cpp @@ -41,9 +41,9 @@ void onLoad() tex = Texture::createTexture("anim.png"); shader = Shader::createShader(shader_code); SpriteSheet ss = SpriteSheet(tex); - vector<Sprite*> frames = ss.createSprites(1, 19, 246, 238, Sprite::Origin::BottomCenter); + vector<Sprite*> frames = ss.createSprites(1, 19, 246, 238, Origin::BottomCenter); anim.addFrames(frames); - anim.setSpeed(0.03); + anim.setSpeed(10); animator.setAnimation(&anim); } diff --git a/src/3rdparty/buildvm/buildvm.exe b/src/3rdparty/buildvm/buildvm.exe Binary files differindex 0990afe..3d72985 100644 --- a/src/3rdparty/buildvm/buildvm.exe +++ b/src/3rdparty/buildvm/buildvm.exe diff --git a/src/3rdparty/minilua/minilua.exe b/src/3rdparty/minilua/minilua.exe Binary files differindex 9a56818..33e9227 100644 --- a/src/3rdparty/minilua/minilua.exe +++ b/src/3rdparty/minilua/minilua.exe diff --git a/src/libjin/graphics/animations/je_animation.h b/src/libjin/graphics/animations/je_animation.h index 1c8a885..4037721 100644 --- a/src/libjin/graphics/animations/je_animation.h +++ b/src/libjin/graphics/animations/je_animation.h @@ -41,6 +41,9 @@ namespace JinEngine std::vector<const Sprite*> mFrames; + /// + /// Frame per second. + /// float mSpeed; float mLoop; diff --git a/src/libjin/graphics/animations/je_animator.cpp b/src/libjin/graphics/animations/je_animator.cpp index 4528e8c..9550116 100644 --- a/src/libjin/graphics/animations/je_animator.cpp +++ b/src/libjin/graphics/animations/je_animator.cpp @@ -48,11 +48,12 @@ namespace JinEngine { if (!mIsActive || !mAnimation) return; + float interval = 1 / mSpeed; mTick += dt; uint fc = mAnimation->getFrameCount(); - while (mTick >= mSpeed) + while (mTick >= interval) { - mTick -= mSpeed; + mTick -= interval; ++mIndex; if (mLoop) mIndex %= fc; @@ -65,9 +66,9 @@ namespace JinEngine mIndex = 0; } - void Animator::render(float x, float y, float sx, float sy, float r) + void Animator::render(float x, float y, float sx, float sy, float r) const { - if (!mIsActive || !mAnimation) + if (!mAnimation) return; const Sprite* spr = mAnimation->getFrame(mIndex); if (spr) @@ -80,6 +81,38 @@ namespace JinEngine } + void Animator::setSpeed(float speed) + { + mSpeed = speed; + } + + void Animator::setDefaultSpeed() + { + if(mAnimation != nullptr) + mSpeed = mAnimation->getSpeed(); + else + { + jin_log_error("Animation is null."); + return; + } + } + + void Animator::setLoop(bool loop) + { + mLoop = loop; + } + + void Animator::setDefaultLoop() + { + if(mAnimation != nullptr) + mLoop = mAnimation->isLoop(); + else + { + jin_log_error("Animation is null."); + return; + } + } + } } }
\ No newline at end of file diff --git a/src/libjin/graphics/animations/je_animator.h b/src/libjin/graphics/animations/je_animator.h index 72d9021..ad8138d 100644 --- a/src/libjin/graphics/animations/je_animator.h +++ b/src/libjin/graphics/animations/je_animator.h @@ -1,6 +1,10 @@ #ifndef __JE_ANIMATOR_H__ #define __JE_ANIMATOR_H__ +#include <string> + +#include "../../utils/je_log.h" + #include "je_animation.h" namespace JinEngine @@ -10,7 +14,7 @@ namespace JinEngine namespace Animations { - class Animator + class Animator : public IRenderable { public: Animator(); @@ -25,13 +29,21 @@ namespace JinEngine void rewind(); - void render(float x, float y, float sx, float sy, float r); + void render(float x, float y, float sx, float sy, float r) const override; void setAnimation(const Animation* anim); void forceToFrame(uint index); - private: + void setSpeed(float speed); + + void setDefaultSpeed(); + + void setLoop(bool loop); + + void setDefaultLoop(); + + private: const Animation* mAnimation; uint mIndex; diff --git a/src/libjin/graphics/fonts/je_font.h b/src/libjin/graphics/fonts/je_font.h index 9581b9f..e72ef6b 100644 --- a/src/libjin/graphics/fonts/je_font.h +++ b/src/libjin/graphics/fonts/je_font.h @@ -2,6 +2,9 @@ #define __JE_FONT_H__ #include <vector> + +#include "../je_renderable.h" + #include "je_text.h" namespace JinEngine @@ -22,7 +25,7 @@ namespace JinEngine /// /// Base Font class. /// - class Font + class Font : public IRenderable { public: /// diff --git a/src/libjin/graphics/fonts/je_texture_font.h b/src/libjin/graphics/fonts/je_texture_font.h index 8a50699..df8f956 100644 --- a/src/libjin/graphics/fonts/je_texture_font.h +++ b/src/libjin/graphics/fonts/je_texture_font.h @@ -23,9 +23,7 @@ namespace JinEngine /// /// /// - class TextureFont - : public Font - , public Graphic + class TextureFont : public Font, public Graphic { public: diff --git a/src/libjin/graphics/je_canvas.h b/src/libjin/graphics/je_canvas.h index 3964517..a908747 100644 --- a/src/libjin/graphics/je_canvas.h +++ b/src/libjin/graphics/je_canvas.h @@ -14,8 +14,7 @@ namespace JinEngine /// /// A canvas is a rendering target. /// - class Canvas - : public Graphic + class Canvas : public Graphic { public: /// diff --git a/src/libjin/graphics/je_graphic.h b/src/libjin/graphics/je_graphic.h index 636ea60..58de7ec 100644 --- a/src/libjin/graphics/je_graphic.h +++ b/src/libjin/graphics/je_graphic.h @@ -7,6 +7,7 @@ #include "../math/je_vector2.hpp" #include "../math/je_transform.h" +#include "je_renderable.h" #include "je_gl.h" #include "je_bitmap.h" @@ -19,7 +20,7 @@ namespace JinEngine /// Class inherites Graphic doesn't keep any state such as origin, scale and other properties. Very low /// level visualized resources. /// - class Graphic + class Graphic : public IRenderable { public: /// diff --git a/src/libjin/graphics/je_mesh.h b/src/libjin/graphics/je_mesh.h index e0a38f8..27c0cb7 100644 --- a/src/libjin/graphics/je_mesh.h +++ b/src/libjin/graphics/je_mesh.h @@ -14,12 +14,13 @@ namespace JinEngine class Mesh { public: - void setGraphic(); - + void setGraphic(const Graphic* graphic); + void pushVertex(float x, float y, float u, float v); private: const Graphic* mGraphic; + }; } // namespace Graphics diff --git a/src/libjin/graphics/je_renderable.h b/src/libjin/graphics/je_renderable.h new file mode 100644 index 0000000..e1d0ae8 --- /dev/null +++ b/src/libjin/graphics/je_renderable.h @@ -0,0 +1,34 @@ +#ifndef __JE_RENDERABLE_H__ +#define __JE_RENDERABLE_H__ + +namespace JinEngine +{ + namespace Graphics + { + + enum class Origin + { + TopLeft, + TopCenter, + TopRight, + MiddleLeft, + MiddleCenter, + MiddleRight, + BottomLeft, + BottomCenter, + BottomRight + }; + + class IRenderable + { + public: + virtual void render(float x, float y, float sx, float sy, float r) const {}; + virtual void render(float x, float y, float sx, float sy, float r, float ox, float oy) const {}; + virtual void render(float x, float y, float sx, float sy, float r, Origin origin) const {}; + + }; + + } +} + +#endif
\ No newline at end of file diff --git a/src/libjin/graphics/je_sprite.cpp b/src/libjin/graphics/je_sprite.cpp index a82be79..890dc63 100644 --- a/src/libjin/graphics/je_sprite.cpp +++ b/src/libjin/graphics/je_sprite.cpp @@ -61,31 +61,31 @@ namespace JinEngine Vector2<float>* org = const_cast<Vector2<float>*>(&mOrigin); switch (origin) { - case TopLeft: + case Origin::TopLeft: org->set(l, t); break; - case TopCenter: + case Origin::TopCenter: org->set(r / 2.f, t); break; - case TopRight: + case Origin::TopRight: org->set(r, t); break; - case MiddleLeft: + case Origin::MiddleLeft: org->set(l, b / 2.f); break; - case MiddleCenter: + case Origin::MiddleCenter: org->set(r / 2.f, b / 2.f); break; - case MiddleRight: + case Origin::MiddleRight: org->set(r, b / 2.f); break; - case BottomLeft: + case Origin::BottomLeft: org->set(l, b); break; - case BottomCenter: + case Origin::BottomCenter: org->set(r / 2.f, b); break; - case BottomRight: + case Origin::BottomRight: org->set(r, b); break; } diff --git a/src/libjin/graphics/je_sprite.h b/src/libjin/graphics/je_sprite.h index 4050ed8..c7c5a8b 100644 --- a/src/libjin/graphics/je_sprite.h +++ b/src/libjin/graphics/je_sprite.h @@ -6,6 +6,7 @@ #include "je_color.h" #include "je_graphic.h" +#include "je_renderable.h" namespace JinEngine { @@ -15,23 +16,10 @@ namespace JinEngine /// /// A sprite is unit of rendering. Animation is based on sprite, but not texture or other graphic stuff. /// - class Sprite + class Sprite : public IRenderable { public: - enum Origin - { - TopLeft, - TopCenter, - TopRight, - MiddleLeft, - MiddleCenter, - MiddleRight, - BottomLeft, - BottomCenter, - BottomRight - }; - Sprite(const Graphic* graphic, const Math::Quad& quad, Origin origin); Sprite(const Graphic* graphic, const Math::Quad& quad, float ox, float oy); @@ -44,7 +32,7 @@ namespace JinEngine Math::Vector2<int> getSize(); - void render(float x, float y, float sx, float sy, float r) const; + void render(float x, float y, float sx, float sy, float r) const override; private: diff --git a/src/libjin/graphics/je_sprite_sheet.cpp b/src/libjin/graphics/je_sprite_sheet.cpp index 73d3e81..6129da7 100644 --- a/src/libjin/graphics/je_sprite_sheet.cpp +++ b/src/libjin/graphics/je_sprite_sheet.cpp @@ -18,7 +18,7 @@ namespace JinEngine { } - Sprite* SpriteSheet::createSprite(const Math::Quad& quad, Sprite::Origin origin) + Sprite* SpriteSheet::createSprite(const Math::Quad& quad, Origin origin) { Sprite* spr = new Sprite(mGraphic, quad, origin); return spr; @@ -30,41 +30,49 @@ namespace JinEngine return spr; } - std::vector<Sprite*> SpriteSheet::createSprites(uint row, uint colum, uint w, uint h, Sprite::Origin origin) + std::vector<Sprite*> SpriteSheet::createSprites(uint count, uint row, uint colum, uint w, uint h, Origin origin, uint offx, uint offy) { vector<Sprite*> sprites; + int i = 0; 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.x = (r * colum + c) * w + offx; + quad.y = r * h + offy; quad.w = w; quad.h = h; Sprite* spr = new Sprite(mGraphic, quad, origin); sprites.push_back(spr); + if ((++i) == count) + goto done; } } + done: return sprites; } - vector<Sprite*> SpriteSheet::createSprites(uint row, uint colum, uint w, uint h, float ox, float oy) + vector<Sprite*> SpriteSheet::createSprites(uint count, uint row, uint colum, uint w, uint h, float ox, float oy, uint offx, uint offy) { vector<Sprite*> sprites; + int i = 0; 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.x = (r * colum + c) * w + offx; + quad.y = r * h + offy; quad.w = w; quad.h = h; Sprite* spr = new Sprite(mGraphic, quad, ox, oy); sprites.push_back(spr); + if ((++i) == count) + goto done; } } + done: return sprites; } diff --git a/src/libjin/graphics/je_sprite_sheet.h b/src/libjin/graphics/je_sprite_sheet.h index a3db946..57e31f7 100644 --- a/src/libjin/graphics/je_sprite_sheet.h +++ b/src/libjin/graphics/je_sprite_sheet.h @@ -18,7 +18,7 @@ namespace JinEngine /// /// Create a new sprite in sheet. /// - Sprite* createSprite(const Math::Quad& quad, Sprite::Origin origin); + Sprite* createSprite(const Math::Quad& quad, Origin origin); /// /// Create a new sprite in sheet. @@ -28,9 +28,9 @@ namespace JinEngine /// /// /// - std::vector<Sprite*> createSprites(uint row, uint colum, uint w, uint h, Sprite::Origin origin); + std::vector<Sprite*> createSprites(uint count, uint row, uint colum, uint w, uint h, Origin origin = Origin::TopLeft, uint offx = 0, uint offy = 0); - std::vector<Sprite*> createSprites(uint row, uint colum, uint w, uint h, float ox, float oy); + std::vector<Sprite*> createSprites(uint count, uint row, uint colum, uint w, uint h, float ox = 0, float oy = 0, uint offx = 0, uint offy = 0); SpriteSheet(const Graphic* graphic); diff --git a/src/libjin/graphics/je_texture.h b/src/libjin/graphics/je_texture.h index 566ba84..8e667f1 100644 --- a/src/libjin/graphics/je_texture.h +++ b/src/libjin/graphics/je_texture.h @@ -17,8 +17,7 @@ namespace JinEngine /// /// /// - class Texture - : public Graphic + class Texture : public Graphic { public: /// diff --git a/src/libjin/graphics/particles/je_particle.h b/src/libjin/graphics/particles/je_particle.h index c03f4ca..8012024 100644 --- a/src/libjin/graphics/particles/je_particle.h +++ b/src/libjin/graphics/particles/je_particle.h @@ -90,7 +90,7 @@ namespace JinEngine /// A single particle contains various properties of particle, such as position, accelaration, color /// and other attributes changed over time. /// - struct Particle + struct Particle : public IRenderable { /// /// Default constructor. diff --git a/src/libjin/graphics/particles/je_particle_system.h b/src/libjin/graphics/particles/je_particle_system.h index fb4b8c7..614ee7c 100644 --- a/src/libjin/graphics/particles/je_particle_system.h +++ b/src/libjin/graphics/particles/je_particle_system.h @@ -33,7 +33,7 @@ namespace JinEngine /// /// Particle emitter, handle all particles it emitts. /// - class ParticleSystem + class ParticleSystem : public IRenderable { public: /// diff --git a/src/lua/common/je_lua_proxy.h b/src/lua/common/je_lua_proxy.h index ca4a56a..2914533 100644 --- a/src/lua/common/je_lua_proxy.h +++ b/src/lua/common/je_lua_proxy.h @@ -57,6 +57,7 @@ namespace JinEngine return shared->type; } + // Bind shared object. SharedBase* shared; }; diff --git a/src/lua/common/je_lua_shared.hpp b/src/lua/common/je_lua_shared.hpp index 91705d1..7bb3f76 100644 --- a/src/lua/common/je_lua_shared.hpp +++ b/src/lua/common/je_lua_shared.hpp @@ -90,6 +90,11 @@ namespace JinEngine return strcmp(type, t) == 0; } + int getDependencyCount() + { + return mDependencies.size(); + } + protected: using DepMap = std::map<int, SharedBase*>; diff --git a/src/lua/libraries/luax/luax.h b/src/lua/libraries/luax/luax.h index 311bc95..06593e9 100644 --- a/src/lua/libraries/luax/luax.h +++ b/src/lua/libraries/luax/luax.h @@ -200,6 +200,7 @@ inline char luax_getfieldbool(lua_State* L, int I, const char* N) #define luax_optudata(L, i, name, x)\ (!lua_isnoneornil(L, i) ? luaL_checkudata(L, i, name) : (x)) #define luax_optnumber luaL_optnumber +#define luax_optinteger luaL_optinteger inline int luax_newlib(lua_State* L, const luaL_Reg* f) { 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<Sprite>& shrSprite = pxySprite->getShared<Sprite>(); + 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<int> 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<Sprite>& shrSprite = pxySprite->getShared<Sprite>(); + 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<Animator>& SharedAnimator; + + LUA_IMPLEMENT inline SharedAnimator checkAnimator(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Animator); + return proxy->getShared<Animator>(); + } + + 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<Animation>& shrAnimation = proxy->getShared<Animation>(); + 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<Sprite::Origin>(o); + Origin origin = static_cast<Origin>(o); Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); p->bind(new Shared<Sprite>(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<Sprite::Origin>(o); + Origin origin = static_cast<Origin>(o); Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); p->bind(new Shared<Sprite>(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<Animation>* shrAnimation = new Shared<Animation>(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<Sprite>& shrSprite = pxySprite->getShared<Sprite>(); + (*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<Animator>* shrAniamtor = new Shared<Animator>(new Animator(), Jin_Lua_Animator); + if (args >= 1) + { + Proxy* pxyAnimation = (Proxy*)luax_checktype(L, 1, Jin_Lua_Animation); + Shared<Animation>& shrAnimtion = pxyAnimation->getShared<Animation>(); + (*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 <vector> +#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<Sprite::Origin>(o); + Origin origin; + origin = static_cast<Origin>(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<SpriteSheet>& shrSS = pxySS->getShared<SpriteSheet>(); + SpriteSheet* ss = pxySS->getObject<SpriteSheet>(); + 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<Sprite*> 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<Origin>(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<Sprite>* shrSpr = new Shared<Sprite>(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); } |