From ad19e537066742e1a5f9bbb8c3549a47480a33c1 Mon Sep 17 00:00:00 2001 From: Samu Laaksonen Date: Mon, 10 Dec 2012 23:47:03 +0200 Subject: Box2d integration Very crude and buggy Needs to be much better --- src/Sprite.cpp | 216 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 108 insertions(+), 108 deletions(-) (limited to 'src/Sprite.cpp') diff --git a/src/Sprite.cpp b/src/Sprite.cpp index b921394..41a87db 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -1,108 +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(); - } -} +#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(); + } +} -- cgit v1.2.3