diff options
Diffstat (limited to 'src/libjin/graphics')
-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 |
6 files changed, 203 insertions, 14 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); |