From 9474767c68c48eff2618df80433b0b81ebf529c6 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 20 Nov 2018 21:43:01 +0800 Subject: =?UTF-8?q?*=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/Jin.exe | Bin 2502656 -> 2267136 bytes bin/anim.png | Bin 0 -> 44218 bytes bin/game/anim.png | Bin 0 -> 44218 bytes bin/game/main.lua | 13 +- bin/splash.png | Bin 0 -> 392 bytes bin/texture.png | Bin 0 -> 1050 bytes build/vc++/examples/Animation/Animation.vcxproj | 137 ++++++++++++++ .../examples/Animation/Animation.vcxproj.filters | 6 + .../vc++/examples/Animation/Animation.vcxproj.user | 8 + build/vc++/jin.sln | 13 +- build/vc++/jin.vcxproj | 2 + build/vc++/jin.vcxproj.filters | 6 + examples/animation/main.cpp | 102 ++++++++++ examples/particle_system/main.cpp | 20 +- src/libjin/graphics/animations/je_animation.cpp | 95 ++++++++++ src/libjin/graphics/animations/je_animation.h | 41 +++- src/libjin/graphics/je_graphics.h | 2 + src/libjin/graphics/je_sprite.cpp | 4 +- src/libjin/graphics/je_sprite_sheet.cpp | 61 +++++- src/libjin/graphics/je_sprite_sheet.h | 14 +- src/libjin/math/je_quad.h | 4 + src/lua/modules/graphics/je_lua_animation.cpp | 71 +++++++ src/lua/modules/graphics/je_lua_animation.h | 20 ++ src/lua/modules/graphics/je_lua_graphics.cpp | 70 ++++++- src/lua/modules/graphics/je_lua_sprite.cpp | 210 ++------------------- src/lua/modules/graphics/je_lua_spritesheet.cpp | 23 ++- 26 files changed, 683 insertions(+), 239 deletions(-) create mode 100644 bin/anim.png create mode 100644 bin/game/anim.png create mode 100644 bin/splash.png create mode 100644 bin/texture.png create mode 100644 build/vc++/examples/Animation/Animation.vcxproj create mode 100644 build/vc++/examples/Animation/Animation.vcxproj.filters create mode 100644 build/vc++/examples/Animation/Animation.vcxproj.user create mode 100644 examples/animation/main.cpp create mode 100644 src/lua/modules/graphics/je_lua_animation.cpp create mode 100644 src/lua/modules/graphics/je_lua_animation.h diff --git a/bin/Jin.exe b/bin/Jin.exe index 391db99..31add2d 100644 Binary files a/bin/Jin.exe and b/bin/Jin.exe differ diff --git a/bin/anim.png b/bin/anim.png new file mode 100644 index 0000000..ef0bc81 Binary files /dev/null and b/bin/anim.png differ diff --git a/bin/game/anim.png b/bin/game/anim.png new file mode 100644 index 0000000..ef0bc81 Binary files /dev/null and b/bin/game/anim.png differ diff --git a/bin/game/main.lua b/bin/game/main.lua index a7b9a86..324f9dc 100644 --- a/bin/game/main.lua +++ b/bin/game/main.lua @@ -49,12 +49,8 @@ function jin.core.onLoad() --tex = jin.graphics.newTexture("1.png") tex = jin.graphics.newTexture(bitmap) local ssheet = jin.graphics.newSpriteSheet(tex) - spr = ssheet:newSprite(50, 50, 50, 50) - spr:setPosition(0, 50) + spr = ssheet:newSprite({50, 50, 50, 50}, 20, 20) tex = nil - --spr:setScale(1, 1) - spr:setColor(100, 0, 100, 255) - spr:setOrigin(jin.graphics.SpriteOrigin.BOTTOMCENTER) -- music = jin.audio.newSource("forest.ogg") -- music:setVolume(0.5) -- music:setLoop(true) @@ -63,14 +59,13 @@ function jin.core.onLoad() jin.graphics.showWindow() timer = jin.time.newTimer() local h = timer:every(0.5, function(sp) - local x, y = spr:getPosition() - spr:move(5, 0) + end, spr) timer:after(3, function(p) --timer:cancel(h) end, h) jin.graphics.pushMatrix() - jin.graphics.translate(10, 0) + jin.graphics.translate(0, 0) --jin.graphics.rotate(0.2) end local stop = false @@ -93,7 +88,7 @@ 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() - spr:render() + jin.graphics.draw(spr, 0, 0, 1, 1, 0) --jin.graphics.draw(spr) --jin.graphics.useShader(shader_program2) --jin.graphics.draw(tex, 0, 0,0.2, 0.2) diff --git a/bin/splash.png b/bin/splash.png new file mode 100644 index 0000000..b0c4256 Binary files /dev/null and b/bin/splash.png differ diff --git a/bin/texture.png b/bin/texture.png new file mode 100644 index 0000000..6b1efee Binary files /dev/null and b/bin/texture.png differ diff --git a/build/vc++/examples/Animation/Animation.vcxproj b/build/vc++/examples/Animation/Animation.vcxproj new file mode 100644 index 0000000..f53dff8 --- /dev/null +++ b/build/vc++/examples/Animation/Animation.vcxproj @@ -0,0 +1,137 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {3191C07B-085A-46BC-9436-469B4E2BF5F0} + Animation + 10.0.17134.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + $(SolutionDir)..\..\bin + + + + Level3 + Disabled + true + true + $(SolutionDir)..\..\src\;$(SolutionDir)..\..\src\3rdparty\SDL2\include;$(SolutionDir)..\..\src\3rdparty\LuaJIT-2.0.5\include;$(SolutionDir)..\..\src\3rdparty\ + + + $(SolutionDir)libs\SDL2-2.0.5\lib\x86;%(AdditionalLibraryDirectories) + opengl32.lib;%(AdditionalDependencies) + Console + + + + + Level3 + Disabled + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + $(SolutionDir)..\..\src\;$(SolutionDir)..\..\src\3rdparty\SDL2\include;$(SolutionDir)..\..\src\3rdparty\LuaJIT-2.0.5\include;$(SolutionDir)..\..\src\3rdparty\ + + + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + + + + {9ee02090-c15e-4520-9c05-c435e45ef2fc} + + + + + + \ No newline at end of file diff --git a/build/vc++/examples/Animation/Animation.vcxproj.filters b/build/vc++/examples/Animation/Animation.vcxproj.filters new file mode 100644 index 0000000..57272c2 --- /dev/null +++ b/build/vc++/examples/Animation/Animation.vcxproj.filters @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/build/vc++/examples/Animation/Animation.vcxproj.user b/build/vc++/examples/Animation/Animation.vcxproj.user new file mode 100644 index 0000000..307fc45 --- /dev/null +++ b/build/vc++/examples/Animation/Animation.vcxproj.user @@ -0,0 +1,8 @@ + + + + $(SolutionDir)..\..\bin + false + WindowsLocalDebugger + + \ No newline at end of file diff --git a/build/vc++/jin.sln b/build/vc++/jin.sln index b15527a..0f33f15 100644 --- a/build/vc++/jin.sln +++ b/build/vc++/jin.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.27703.2047 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jin", "jin.vcxproj", "{A3E35ECA-62EB-45CE-8152-674FBC7F7A3B}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libjin-lua", "jin.vcxproj", "{A3E35ECA-62EB-45CE-8152-674FBC7F7A3B}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libjin", "libjin\libjin.vcxproj", "{9EE02090-C15E-4520-9C05-C435E45EF2FC}" EndProject @@ -45,6 +45,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SDL2main", "3rdparty\SDL2ma EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StateMachine", "examples\StateMachine\StateMachine.vcxproj", "{C70BFD61-6462-49D2-9F54-F16D77FF434A}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Animation", "examples\Animation\Animation.vcxproj", "{3191C07B-085A-46BC-9436-469B4E2BF5F0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -173,6 +175,14 @@ Global {C70BFD61-6462-49D2-9F54-F16D77FF434A}.Release|x64.Build.0 = Release|x64 {C70BFD61-6462-49D2-9F54-F16D77FF434A}.Release|x86.ActiveCfg = Release|Win32 {C70BFD61-6462-49D2-9F54-F16D77FF434A}.Release|x86.Build.0 = Release|Win32 + {3191C07B-085A-46BC-9436-469B4E2BF5F0}.Debug|x64.ActiveCfg = Debug|x64 + {3191C07B-085A-46BC-9436-469B4E2BF5F0}.Debug|x64.Build.0 = Debug|x64 + {3191C07B-085A-46BC-9436-469B4E2BF5F0}.Debug|x86.ActiveCfg = Debug|Win32 + {3191C07B-085A-46BC-9436-469B4E2BF5F0}.Debug|x86.Build.0 = Debug|Win32 + {3191C07B-085A-46BC-9436-469B4E2BF5F0}.Release|x64.ActiveCfg = Release|x64 + {3191C07B-085A-46BC-9436-469B4E2BF5F0}.Release|x64.Build.0 = Release|x64 + {3191C07B-085A-46BC-9436-469B4E2BF5F0}.Release|x86.ActiveCfg = Release|Win32 + {3191C07B-085A-46BC-9436-469B4E2BF5F0}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -195,6 +205,7 @@ Global {CD378AC6-A4FE-48F7-984A-EB68C60A211B} = {65DFFFA6-F434-47FB-9DB2-DCF019060B92} {5522BDBA-3F18-4EDD-B6E4-CE6A70D9D794} = {CD378AC6-A4FE-48F7-984A-EB68C60A211B} {C70BFD61-6462-49D2-9F54-F16D77FF434A} = {6FC112F9-B317-4444-B4FB-2E0BF70328F1} + {3191C07B-085A-46BC-9436-469B4E2BF5F0} = {6FC112F9-B317-4444-B4FB-2E0BF70328F1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {633D8D43-3349-43F7-8835-7D7F58BAD2A8} diff --git a/build/vc++/jin.vcxproj b/build/vc++/jin.vcxproj index 02bf7f0..1b6325d 100644 --- a/build/vc++/jin.vcxproj +++ b/build/vc++/jin.vcxproj @@ -157,6 +157,7 @@ + @@ -210,6 +211,7 @@ + diff --git a/build/vc++/jin.vcxproj.filters b/build/vc++/jin.vcxproj.filters index 1dba6ab..eb8b588 100644 --- a/build/vc++/jin.vcxproj.filters +++ b/build/vc++/jin.vcxproj.filters @@ -183,6 +183,9 @@ source\common + + source\modules\graphics + @@ -347,6 +350,9 @@ source\common + + source\modules\graphics + diff --git a/examples/animation/main.cpp b/examples/animation/main.cpp new file mode 100644 index 0000000..033e499 --- /dev/null +++ b/examples/animation/main.cpp @@ -0,0 +1,102 @@ +#include + +#include "libjin/jin.h" + +using namespace std; +using namespace JinEngine::AI; +using namespace JinEngine::Game; +using namespace JinEngine::Math; +using namespace JinEngine::Input; +using namespace JinEngine::Graphics; +using namespace JinEngine::Graphics::Shaders; +using namespace JinEngine::Graphics::Particles; +using namespace JinEngine::Graphics::Animations; +using namespace JinEngine::Filesystem; +using namespace JinEngine::Time; + +Timer timer; +Timer::Handler* hnd; +Texture* tex; +Shader* shader; +Animation anim; +const char* shader_code = R"( +#VERTEX_SHADER +Vertex vert(Vertex v) +{ + return v; +} +#END_VERTEX_SHADER +#FRAGMENT_SHADER +Color frag(Color col, Texture tex, Vertex v) +{ + Color c = texel(tex, v.uv); + return c * col; +} +#END_FRAGMENT_SHADER +)"; +const float Pi = 3.14f; +void onLoad() +{ + tex = Texture::createTexture("anim.png"); + shader = Shader::createShader(shader_code); + SpriteSheet ss = SpriteSheet(tex); + vector frames = ss.createSprites(1, 19, 246, 238, Sprite::Origin::BottomCenter); + anim.addFrames(frames); + anim.setSpeed(0.05); +} + +void onEvent(Event* e) +{ + static Application* Application = Application::get(); + if (e->type == EventType::QUIT) + Application->stop(); +} + +void onUpdate(int ms) +{ + anim.update(ms / 1000.f); + timer.update(ms); +} + +void onDraw() +{ + shader->use(); + Mouse* m = Mouse::get(); + int x, y; + m->getState(&x, &y); + anim.render(x, y, 1, 1, 0); + shader->unuse(); +} + +int main(int argc, char* argv[]) +{ + Application* Application = Application::get(); + Application::Setting setting; + setting.loader = onLoad; + setting.eventHandler = onEvent; + setting.updater = onUpdate; + setting.drawer = onDraw; + Application->init(&setting); + + AssetDatabase::get()->mount("."); + + Window* wnd = Window::get(); + Window::Setting wndSetting; + wndSetting.width = 600; + wndSetting.height = 512; + wndSetting.title = "Jin v0.1.1"; + wndSetting.fps = 60; + wndSetting.vsync = false; + wndSetting.fullscreen = false; + wndSetting.resizable = false; + wndSetting.icon = "."; + wnd->init(&wndSetting); + wnd->show(); + + Application->run(); + + Application->quit(); + wnd->quit(); + + return 0; +} \ No newline at end of file diff --git a/examples/particle_system/main.cpp b/examples/particle_system/main.cpp index a838bed..9251cc4 100644 --- a/examples/particle_system/main.cpp +++ b/examples/particle_system/main.cpp @@ -38,23 +38,23 @@ const float Pi = 3.14f; void onLoad() { tex = Texture::createTexture("texture.png"); - spr = new Sprite(tex, Sprite::Origin::MiddleCenter); + spr = new Sprite(tex, Sprite::Origin::BottomCenter); shader = Shader::createShader(shader_code); ParticleSystemDef def; def.maxParticleCount = 30; def.emitterDef.emitRateDef.rate.rate = 0.01f; - def.emitterDef.forceDef.force.force = 10.f; + def.emitterDef.forceDef.force.force = 50.f; def.emitterDef.positionDef.enableRandom = true; - def.emitterDef.positionDef.position.random.floor = Vector2(0, 0); - def.emitterDef.positionDef.position.random.ceil = Vector2(0, 10); + def.emitterDef.positionDef.position.random.floor = Vector2(-20, 0); + def.emitterDef.positionDef.position.random.ceil = Vector2(20, 0); def.emitterDef.directionDef.enableRandom = true; - def.emitterDef.directionDef.direction.random.floor = 0; - def.emitterDef.directionDef.direction.random.ceil = Pi*2; + def.emitterDef.directionDef.direction.random.floor = -Pi / 2 ; + def.emitterDef.directionDef.direction.random.ceil = -Pi / 2 ; def.particleDef.colorOverTimeDef.enable = true; - def.particleDef.colorOverTimeDef.colorStart = Color(255, 255, 0, 255); + def.particleDef.colorOverTimeDef.colorStart = Color(255, 100, 0, 255); def.particleDef.colorOverTimeDef.colorEnd = Color(255, 0, 0, 0); /* def.particleDef.angularSpeedDef.enableRandom = true; @@ -62,13 +62,13 @@ void onLoad() def.particleDef.angularSpeedDef.angularSpeed.random.ceil = 1; */ - //def.particleDef.linearAccelarationDef.linearAccelaration = Vector2(0, 10); + def.particleDef.linearAccelarationDef.linearAccelaration = Vector2(0, 10); def.particleDef.sizeOverTimeDef.enable = true; def.particleDef.sizeOverTimeDef.start = 1; - def.particleDef.sizeOverTimeDef.end = 0.5; + def.particleDef.sizeOverTimeDef.end = 1; - def.particleDef.lifeTimeDef.life.life = 0.5; + def.particleDef.lifeTimeDef.life.life = 3; p = new ParticleSystem(def); p->setSprite(spr); } 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& 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(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(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& frames); /// /// /// - void start(); + void update(float dt); /// /// @@ -37,7 +44,7 @@ namespace JinEngine /// /// /// - void stop(); + void resume(); /// /// Force rewind. @@ -49,6 +56,11 @@ namespace JinEngine /// void setSpeed(float speed); + /// + /// + /// + void setLoop(bool isLoop); + /// /// Get current frame index. /// @@ -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 mFrames; + std::vector 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 + +#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 SpriteSheet::createSprites(uint row, uint colum, uint w, uint h, Sprite::Origin origin) + { + vector 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 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 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 createSprites(uint row, uint colum, uint w, uint h, Sprite::Origin origin); + + std::vector 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& SharedAnimation; + + LUA_IMPLEMENT inline SharedAnimation checkAnimation(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Animation); + 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_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 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(); - spr->render(); + Sprite* sprite = pxySprite->getObject(); + 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(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(); + 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(o); + Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); + p->bind(new Shared(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(new Sprite(graphic, quad, ox, oy), Jin_Lua_Sprite)); + } + else if (n == 2) + { + int o = luax_checkinteger(L, 2); + Sprite::Origin origin = static_cast(o); + Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); + p->bind(new Shared(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(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(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()); - sprite.setDependency((int)SpriteDependency::DEP_GRAPHIC, &p->getShared()); - } - 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 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& 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& 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 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& shrSprite = pxySprite->getShared(); - 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& shrSprite = pxySprite->getShared(); - 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& shrSSheet = pxySSheet->getShared(); SpriteSheet* sheet = pxySSheet->getObject(); 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(o); + spr = sheet->createSprite(quad, origin); + } Proxy* pxySprite = luax_newinstance(L, Jin_Lua_Sprite); Shared* shrSprite = new Shared(spr, Jin_Lua_Sprite); shrSprite->setDependency((int)SpriteDependency::DEP_SPRITESHEET, &shrSSheet); -- cgit v1.1-26-g67d0