aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libjin/graphics/animations/je_animation.cpp74
-rw-r--r--src/libjin/graphics/animations/je_animation.h86
-rw-r--r--src/libjin/graphics/animations/je_animator.cpp85
-rw-r--r--src/libjin/graphics/animations/je_animator.h53
-rw-r--r--src/libjin/graphics/je_graphics.h1
-rw-r--r--src/lua/modules/graphics/je_lua_spritesheet.cpp6
6 files changed, 169 insertions, 136 deletions
diff --git a/src/libjin/graphics/animations/je_animation.cpp b/src/libjin/graphics/animations/je_animation.cpp
index 028ef56..3168026 100644
--- a/src/libjin/graphics/animations/je_animation.cpp
+++ b/src/libjin/graphics/animations/je_animation.cpp
@@ -11,10 +11,7 @@ namespace JinEngine
{
Animation::Animation()
- : mIndex(0)
- , mActive(true)
- , mLoop(true)
- , mTick(0)
+ : mLoop(true)
{
}
@@ -29,79 +26,38 @@ namespace JinEngine
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)
+ void Animation::setLoop(bool loop)
{
- mLoop = isLoop;
+ mLoop = loop;
}
- uint Animation::getCurrentFrameIndex()
+ const Sprite* Animation::getFrame(uint index) const
{
- return mIndex;
+ if (mFrames.size() == 0)
+ return nullptr;
+ if (without<uint>(index, 0, mFrames.size() - 1))
+ return nullptr;
+ return mFrames[index];
}
- const Sprite* Animation::getCurrentFrame()
+ uint Animation::getFrameCount() const
{
- if (mIndex >= mFrames.size())
- return nullptr;
- return mFrames[mIndex];
+ return mFrames.size();
}
- void Animation::setCurrentFrame(uint frame)
+ bool Animation::isLoop() const
{
- mIndex = frame;
+ return mLoop;
}
- void Animation::render(float x, float y, float sx, float sy, float r)
+ float Animation::getSpeed() const
{
- 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);
+ return mSpeed;
}
}
diff --git a/src/libjin/graphics/animations/je_animation.h b/src/libjin/graphics/animations/je_animation.h
index 4c2fd64..1c8a885 100644
--- a/src/libjin/graphics/animations/je_animation.h
+++ b/src/libjin/graphics/animations/je_animation.h
@@ -21,98 +21,30 @@ namespace JinEngine
public:
Animation();
- ///
- ///
- ///
void addFrame(const Sprite* frame);
- ///
- ///
- ///
void addFrames(const std::vector<Sprite*>& frames);
- ///
- ///
- ///
- void update(float dt);
-
- ///
- ///
- ///
- void pause();
-
- ///
- ///
- ///
- void resume();
-
- ///
- /// Force rewind.
- ///
- void rewind();
-
- ///
- ///
- ///
+ void setLoop(bool loop);
+
void setSpeed(float speed);
- ///
- ///
- ///
- void setLoop(bool isLoop);
-
- ///
- /// Get current frame index.
- ///
- uint getCurrentFrameIndex();
-
- ///
- ///
- ///
- const Sprite* getCurrentFrame();
-
- ///
- /// Set current frame index.
- ///
- /// @param frame Current frame to play.
- ///
- void setCurrentFrame(uint frame);
-
- ///
- ///
- ///
- void render(float x, float y, float sx, float sy, float r);
-
- ///
- ///
- ///
- Animation clone();
+ bool isLoop() const;
- private:
+ float getSpeed() const;
- void next();
+ uint getFrameCount() const;
- ///
- /// Key frames.
- ///
- std::vector<const Sprite*> mFrames;
+ const Sprite* getFrame(uint index) const;
- ///
- /// Animation playing speed.
- ///
- float mSpeed;
+ private:
- ///
- ///
- ///
- float mTick;
+ std::vector<const Sprite*> mFrames;
- uint mIndex;
+ float mSpeed;
float mLoop;
- bool mActive;
-
};
} // namespace Animations
diff --git a/src/libjin/graphics/animations/je_animator.cpp b/src/libjin/graphics/animations/je_animator.cpp
new file mode 100644
index 0000000..4528e8c
--- /dev/null
+++ b/src/libjin/graphics/animations/je_animator.cpp
@@ -0,0 +1,85 @@
+#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;
+ mTick += dt;
+ uint fc = mAnimation->getFrameCount();
+ while (mTick >= mSpeed)
+ {
+ mTick -= mSpeed;
+ ++mIndex;
+ if (mLoop)
+ mIndex %= fc;
+ mIndex = clamp<uint>(mIndex, 0, fc - 1);
+ }
+ }
+
+ void Animator::rewind()
+ {
+ mIndex = 0;
+ }
+
+ void Animator::render(float x, float y, float sx, float sy, float r)
+ {
+ if (!mIsActive || !mAnimation)
+ return;
+ const Sprite* spr = mAnimation->getFrame(mIndex);
+ if (spr)
+ spr->render(x, y, sx, sy, r);
+ }
+
+ void Animator::forceToFrame(uint index)
+ {
+ mIndex = index;
+
+ }
+
+ }
+ }
+} \ No newline at end of file
diff --git a/src/libjin/graphics/animations/je_animator.h b/src/libjin/graphics/animations/je_animator.h
new file mode 100644
index 0000000..72d9021
--- /dev/null
+++ b/src/libjin/graphics/animations/je_animator.h
@@ -0,0 +1,53 @@
+#ifndef __JE_ANIMATOR_H__
+#define __JE_ANIMATOR_H__
+
+#include "je_animation.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Animations
+ {
+
+ class Animator
+ {
+ 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);
+
+ void setAnimation(const Animation* anim);
+
+ void forceToFrame(uint index);
+
+ 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/je_graphics.h b/src/libjin/graphics/je_graphics.h
index d7824c5..64f92a4 100644
--- a/src/libjin/graphics/je_graphics.h
+++ b/src/libjin/graphics/je_graphics.h
@@ -22,6 +22,7 @@
#include "particles/je_particle_system.h"
#include "animations/je_animation.h"
+#include "animations/je_animator.h"
//struct Stats
//{
diff --git a/src/lua/modules/graphics/je_lua_spritesheet.cpp b/src/lua/modules/graphics/je_lua_spritesheet.cpp
index a1652e0..8454321 100644
--- a/src/lua/modules/graphics/je_lua_spritesheet.cpp
+++ b/src/lua/modules/graphics/je_lua_spritesheet.cpp
@@ -53,6 +53,12 @@ namespace JinEngine
return 1;
}
+ // {} = newSprites
+ LUA_IMPLEMENT int l_newSprites(lua_State* L)
+ {
+
+ }
+
LUA_EXPORT void luaopen_SpriteSheet(lua_State* L)
{
luaL_Reg methods[] = {