aboutsummaryrefslogtreecommitdiff
path: root/src/Sprite.cpp
diff options
context:
space:
mode:
authorSamu Laaksonen <laaksonen.sj@gmail.com>2012-09-25 20:00:41 +0300
committerSamu Laaksonen <laaksonen.sj@gmail.com>2012-09-25 20:00:41 +0300
commit685fe05def77b039221edf06c74af74915d536c5 (patch)
tree9c1a14b8f68bc0f801bdec3edc447d04fdbf7a4c /src/Sprite.cpp
parent29eabac0670574efd384182c065f53d08c42a483 (diff)
downloadprism-685fe05def77b039221edf06c74af74915d536c5.tar.gz
prism-685fe05def77b039221edf06c74af74915d536c5.zip
Initial code commit
Added some stuff for project base - tiled sources - few Qt based classes for gfx
Diffstat (limited to 'src/Sprite.cpp')
-rw-r--r--src/Sprite.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/Sprite.cpp b/src/Sprite.cpp
new file mode 100644
index 0000000..b921394
--- /dev/null
+++ b/src/Sprite.cpp
@@ -0,0 +1,108 @@
+#include "Sprite.h"
+
+Sprite::Sprite(QGraphicsItem *parent, QGraphicsScene *scene)
+ : QObject(0),
+ QGraphicsPixmapItem(parent, scene),
+ m_currentFrame(0),
+ m_interval(1000/20)
+{
+ addAnimation("default", FrameList());
+ setAnimation("default");
+
+ m_timer.invalidate();
+}
+
+void Sprite::addFrame(const QString &anim, QPixmap frame)
+{
+ if (!m_animations.contains(anim))
+ {
+ FrameList l;
+ m_animations[anim] = l;
+ }
+
+ m_animations[anim].append(frame);
+}
+
+void Sprite::addAnimation(const QString &anim, const FrameList &frames)
+{
+ if (!m_animations.contains(anim))
+ {
+ m_animations[anim] = frames;
+ }
+ else
+ {
+ m_animations[anim].append(frames);
+ }
+}
+
+void Sprite::setFrame(int frame)
+{
+ if (frame < 0 || frame >= getCurrentAnimation().size())
+ {
+ qWarning("invalid frame number %d", frame);
+ return;
+ }
+
+ m_currentFrame = frame;
+ setPixmap(getFramePixmap());
+}
+
+int Sprite::getFrame() const
+{
+ return m_currentFrame;
+}
+
+QPixmap Sprite::getFramePixmap() const
+{
+ return getCurrentAnimation()[m_currentFrame];
+}
+
+int Sprite::getFrameCount() const
+{
+ return getCurrentAnimation().size();
+}
+
+Sprite::FrameList Sprite::getCurrentAnimation() const
+{
+ return m_animations[m_currentAnimation];
+}
+
+void Sprite::nextFrame()
+{
+ m_currentFrame++;
+
+ if (m_currentFrame >= getCurrentAnimation().size())
+ m_currentFrame = 0;
+
+ setFrame(m_currentFrame);
+}
+
+void Sprite::setAnimation(const QString &anim)
+{
+ if (!m_animations.contains(anim))
+ {
+ qWarning("animation '%s' doesn't exist", anim.toUtf8().data());
+ return;
+ }
+
+ if (m_currentAnimation != anim)
+ m_currentAnimation = anim;
+}
+
+void Sprite::advance(int phase)
+{
+ if (phase == 1)
+ return;
+
+ if (m_timer.isValid() && m_timer.elapsed() >= m_interval)
+ {
+ nextFrame();
+ m_timer.start();
+ }
+
+ // this should be run the first time advance() is called
+ if (!m_timer.isValid())
+ {
+ m_timer.start();
+ }
+}