From 8b00d67febf133e89f6a0bfabc41feed555dc4a9 Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 12 Jan 2019 21:48:33 +0800 Subject: =?UTF-8?q?*=E5=8E=BB=E6=8E=89=E6=96=87=E4=BB=B6=E5=89=8D=E7=BC=80?= =?UTF-8?q?je=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/graphics/animations/animation.cpp | 65 ++++++++++++ src/libjin/graphics/animations/animation.h | 59 +++++++++++ src/libjin/graphics/animations/animator.cpp | 128 ++++++++++++++++++++++++ src/libjin/graphics/animations/animator.h | 70 +++++++++++++ src/libjin/graphics/animations/je_animation.cpp | 65 ------------ src/libjin/graphics/animations/je_animation.h | 59 ----------- src/libjin/graphics/animations/je_animator.cpp | 128 ------------------------ src/libjin/graphics/animations/je_animator.h | 70 ------------- 8 files changed, 322 insertions(+), 322 deletions(-) create mode 100644 src/libjin/graphics/animations/animation.cpp create mode 100644 src/libjin/graphics/animations/animation.h create mode 100644 src/libjin/graphics/animations/animator.cpp create mode 100644 src/libjin/graphics/animations/animator.h delete mode 100644 src/libjin/graphics/animations/je_animation.cpp delete mode 100644 src/libjin/graphics/animations/je_animation.h delete mode 100644 src/libjin/graphics/animations/je_animator.cpp delete mode 100644 src/libjin/graphics/animations/je_animator.h (limited to 'src/libjin/graphics/animations') diff --git a/src/libjin/graphics/animations/animation.cpp b/src/libjin/graphics/animations/animation.cpp new file mode 100644 index 0000000..168c3e3 --- /dev/null +++ b/src/libjin/graphics/animations/animation.cpp @@ -0,0 +1,65 @@ +#include "../../math/vector2.hpp" +#include "animation.h" + +using namespace JinEngine::Math; + +namespace JinEngine +{ + namespace Graphics + { + namespace Animations + { + + Animation::Animation() + : mLoop(true) + { + } + + 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::setSpeed(float speed) + { + mSpeed = speed; + } + + void Animation::setLoop(bool loop) + { + mLoop = loop; + } + + const Sprite* Animation::getFrame(uint index) const + { + if (mFrames.size() == 0) + return nullptr; + if (without(index, 0, mFrames.size() - 1)) + return nullptr; + return mFrames[index]; + } + + uint Animation::getFrameCount() const + { + return mFrames.size(); + } + + bool Animation::isLoop() const + { + return mLoop; + } + + float Animation::getSpeed() const + { + return mSpeed; + } + + } + } +} \ No newline at end of file diff --git a/src/libjin/graphics/animations/animation.h b/src/libjin/graphics/animations/animation.h new file mode 100644 index 0000000..72565fc --- /dev/null +++ b/src/libjin/graphics/animations/animation.h @@ -0,0 +1,59 @@ +#ifndef __JE_ANIMATION_H__ +#define __JE_ANIMATION_H__ + +#include +#include + +#include "../../common/object.h" + +#include "../sprite.h" + +namespace JinEngine +{ + namespace Graphics + { + namespace Animations + { + + /// + /// Animation clip with key. + /// + class Animation : public Object + { + public: + Animation(); + + void addFrame(const Sprite* frame); + + void addFrames(const std::vector& frames); + + void setLoop(bool loop); + + void setSpeed(float speed); + + bool isLoop() const; + + float getSpeed() const; + + uint getFrameCount() const; + + const Sprite* getFrame(uint index) const; + + private: + + std::vector mFrames; + + /// + /// Frame per second. + /// + float mSpeed; + + float mLoop; + + }; + + } // namespace Animations + } // namespace Graphics +} // namespace JinEngine + +#endif \ No newline at end of file diff --git a/src/libjin/graphics/animations/animator.cpp b/src/libjin/graphics/animations/animator.cpp new file mode 100644 index 0000000..8db3bc2 --- /dev/null +++ b/src/libjin/graphics/animations/animator.cpp @@ -0,0 +1,128 @@ +#include "../../math/vector2.hpp" +#include "animator.h" + +using namespace JinEngine::Math; + +namespace JinEngine +{ + namespace Graphics + { + namespace Animations + { + + Animator::Animator() + : mIndex(0) + , mTick(0) + , mIsActive(true) + { + } + + void Animator::setAnimation(const Animation* anim) + { + mAnimation = anim; + if (mAnimation) + { + mSpeed = mAnimation->getSpeed(); + mLoop = mAnimation->isLoop(); + } + } + + void Animator::play() + { + mIndex = 0; + mIsActive = true; + mTick = 0; + } + + void Animator::pause() + { + mIsActive = false; + } + + void Animator::resume() + { + mIsActive = true; + } + + void Animator::update(float dt) + { + if (!mIsActive || !mAnimation) + return; + float interval = 1 / mSpeed; + mTick += dt; + uint fc = mAnimation->getFrameCount(); + if (mTick >= interval) + { + mIndex += int(mTick / interval); + mTick = fmod(mTick, interval); + if (mLoop) + mIndex %= fc; + mIndex = clamp(mIndex, 0, fc - 1); + } + } + + void Animator::rewind() + { + mIndex = 0; + } + + void Animator::render(float x, float y, float sx, float sy, float r) const + { + if (!mAnimation) + return; + const Sprite* spr = mAnimation->getFrame(mIndex); + if (spr) + spr->render(x, y, sx, sy, r); + } + + void Animator::forceToFrame(uint index) + { + mIndex = index; + + } + + 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; + } + } + + float Animator::getSpeed() + { + return mSpeed; + } + + uint Animator::getFrameCount() + { + return mAnimation->getFrameCount(); + } + + } + } +} \ No newline at end of file diff --git a/src/libjin/graphics/animations/animator.h b/src/libjin/graphics/animations/animator.h new file mode 100644 index 0000000..263c7cb --- /dev/null +++ b/src/libjin/graphics/animations/animator.h @@ -0,0 +1,70 @@ +#ifndef __JE_ANIMATOR_H__ +#define __JE_ANIMATOR_H__ + +#include + +#include "../../common/object.h" +#include "../../utils/log.h" + +#include "animation.h" + +namespace JinEngine +{ + namespace Graphics + { + namespace Animations + { + + class Animator : public Object, public Renderable + { + public: + Animator(); + + void play(); + + void pause(); + + void resume(); + + void update(float dt); + + void rewind(); + + void render(float x, float y, float sx, float sy, float r) const override; + + void setAnimation(const Animation* anim); + + void forceToFrame(uint index); + + void setSpeed(float speed); + + void setDefaultSpeed(); + + void setLoop(bool loop); + + void setDefaultLoop(); + + float getSpeed(); + + uint getFrameCount(); + + private: + const Animation* mAnimation; + + uint mIndex; + + float mTick; + + bool mIsActive; + + float mSpeed; + + bool mLoop; + + }; + + } + } +} + +#endif \ No newline at end of file diff --git a/src/libjin/graphics/animations/je_animation.cpp b/src/libjin/graphics/animations/je_animation.cpp deleted file mode 100644 index 3168026..0000000 --- a/src/libjin/graphics/animations/je_animation.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "../../math/je_vector2.hpp" -#include "je_animation.h" - -using namespace JinEngine::Math; - -namespace JinEngine -{ - namespace Graphics - { - namespace Animations - { - - Animation::Animation() - : mLoop(true) - { - } - - 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::setSpeed(float speed) - { - mSpeed = speed; - } - - void Animation::setLoop(bool loop) - { - mLoop = loop; - } - - const Sprite* Animation::getFrame(uint index) const - { - if (mFrames.size() == 0) - return nullptr; - if (without(index, 0, mFrames.size() - 1)) - return nullptr; - return mFrames[index]; - } - - uint Animation::getFrameCount() const - { - return mFrames.size(); - } - - bool Animation::isLoop() const - { - return mLoop; - } - - float Animation::getSpeed() const - { - return mSpeed; - } - - } - } -} \ No newline at end of file diff --git a/src/libjin/graphics/animations/je_animation.h b/src/libjin/graphics/animations/je_animation.h deleted file mode 100644 index fd22049..0000000 --- a/src/libjin/graphics/animations/je_animation.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef __JE_ANIMATION_H__ -#define __JE_ANIMATION_H__ - -#include -#include - -#include "../../common/je_object.h" - -#include "../je_sprite.h" - -namespace JinEngine -{ - namespace Graphics - { - namespace Animations - { - - /// - /// Animation clip with key. - /// - class Animation : public Object - { - public: - Animation(); - - void addFrame(const Sprite* frame); - - void addFrames(const std::vector& frames); - - void setLoop(bool loop); - - void setSpeed(float speed); - - bool isLoop() const; - - float getSpeed() const; - - uint getFrameCount() const; - - const Sprite* getFrame(uint index) const; - - private: - - std::vector mFrames; - - /// - /// Frame per second. - /// - float mSpeed; - - float mLoop; - - }; - - } // namespace Animations - } // namespace Graphics -} // namespace JinEngine - -#endif \ No newline at end of file diff --git a/src/libjin/graphics/animations/je_animator.cpp b/src/libjin/graphics/animations/je_animator.cpp deleted file mode 100644 index 1e25639..0000000 --- a/src/libjin/graphics/animations/je_animator.cpp +++ /dev/null @@ -1,128 +0,0 @@ -#include "../../math/je_vector2.hpp" -#include "je_animator.h" - -using namespace JinEngine::Math; - -namespace JinEngine -{ - namespace Graphics - { - namespace Animations - { - - Animator::Animator() - : mIndex(0) - , mTick(0) - , mIsActive(true) - { - } - - void Animator::setAnimation(const Animation* anim) - { - mAnimation = anim; - if (mAnimation) - { - mSpeed = mAnimation->getSpeed(); - mLoop = mAnimation->isLoop(); - } - } - - void Animator::play() - { - mIndex = 0; - mIsActive = true; - mTick = 0; - } - - void Animator::pause() - { - mIsActive = false; - } - - void Animator::resume() - { - mIsActive = true; - } - - void Animator::update(float dt) - { - if (!mIsActive || !mAnimation) - return; - float interval = 1 / mSpeed; - mTick += dt; - uint fc = mAnimation->getFrameCount(); - if (mTick >= interval) - { - mIndex += int(mTick / interval); - mTick = fmod(mTick, interval); - if (mLoop) - mIndex %= fc; - mIndex = clamp(mIndex, 0, fc - 1); - } - } - - void Animator::rewind() - { - mIndex = 0; - } - - void Animator::render(float x, float y, float sx, float sy, float r) const - { - if (!mAnimation) - return; - const Sprite* spr = mAnimation->getFrame(mIndex); - if (spr) - spr->render(x, y, sx, sy, r); - } - - void Animator::forceToFrame(uint index) - { - mIndex = index; - - } - - 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; - } - } - - float Animator::getSpeed() - { - return mSpeed; - } - - uint Animator::getFrameCount() - { - return mAnimation->getFrameCount(); - } - - } - } -} \ No newline at end of file diff --git a/src/libjin/graphics/animations/je_animator.h b/src/libjin/graphics/animations/je_animator.h deleted file mode 100644 index e2369cf..0000000 --- a/src/libjin/graphics/animations/je_animator.h +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef __JE_ANIMATOR_H__ -#define __JE_ANIMATOR_H__ - -#include - -#include "../../common/je_object.h" -#include "../../utils/je_log.h" - -#include "je_animation.h" - -namespace JinEngine -{ - namespace Graphics - { - namespace Animations - { - - class Animator : public Object, public Renderable - { - public: - Animator(); - - void play(); - - void pause(); - - void resume(); - - void update(float dt); - - void rewind(); - - void render(float x, float y, float sx, float sy, float r) const override; - - void setAnimation(const Animation* anim); - - void forceToFrame(uint index); - - void setSpeed(float speed); - - void setDefaultSpeed(); - - void setLoop(bool loop); - - void setDefaultLoop(); - - float getSpeed(); - - uint getFrameCount(); - - private: - const Animation* mAnimation; - - uint mIndex; - - float mTick; - - bool mIsActive; - - float mSpeed; - - bool mLoop; - - }; - - } - } -} - -#endif \ No newline at end of file -- cgit v1.1-26-g67d0