diff options
| author | manuel <manuel@mausz.at> | 2011-04-04 11:27:54 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2011-04-04 11:27:54 +0200 |
| commit | a35824a3319ae9c304263185c4efc449e2c2cf73 (patch) | |
| tree | f769dc8e8e06265594b2f0e5bda47163428c47be /pacman-c++ | |
| parent | 2a2c8d3b30e99e2bd636891a2eef27c0f35dcbc3 (diff) | |
| download | foop-a35824a3319ae9c304263185c4efc449e2c2cf73.tar.gz foop-a35824a3319ae9c304263185c4efc449e2c2cf73.tar.bz2 foop-a35824a3319ae9c304263185c4efc449e2c2cf73.zip | |
added simple pacman animation
Diffstat (limited to 'pacman-c++')
| -rw-r--r-- | pacman-c++/actor.cpp | 47 | ||||
| -rw-r--r-- | pacman-c++/actor.h | 2 | ||||
| -rw-r--r-- | pacman-c++/animationmanager.cpp | 56 | ||||
| -rw-r--r-- | pacman-c++/animationmanager.h | 33 | ||||
| -rw-r--r-- | pacman-c++/pacman.pro | 8 | ||||
| -rw-r--r-- | pacman-c++/pacman.qrc | 1 | ||||
| -rw-r--r-- | pacman-c++/pics/actor1.png | bin | 0 -> 485 bytes | |||
| -rw-r--r-- | pacman-c++/pixmapitem.cpp | 44 | ||||
| -rw-r--r-- | pacman-c++/pixmapitem.h | 12 |
9 files changed, 195 insertions, 8 deletions
diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index 860f490..5080497 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp | |||
| @@ -1,7 +1,50 @@ | |||
| 1 | #include "actor.h" | 1 | #include "actor.h" |
| 2 | #include "animationmanager.h" | ||
| 3 | #include <QtCore/QPropertyAnimation> | ||
| 4 | #include <QtCore/QSequentialAnimationGroup> | ||
| 5 | #include <QtCore/QVariantAnimation> | ||
| 6 | |||
| 7 | #define SPRITE_MARGIN 2 | ||
| 8 | #define SPRITE_PLAYER_WIDTH 16 | ||
| 9 | #define SPRITE_PLAYER_HEIGHT 16 | ||
| 10 | |||
| 11 | static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal progress) | ||
| 12 | { | ||
| 13 | return (progress == 1.0) ? end : start; | ||
| 14 | } | ||
| 2 | 15 | ||
| 3 | Actor::Actor(Type type) | 16 | Actor::Actor(Type type) |
| 4 | : PixmapItem("google-pacman-sprite"), m_type(type), m_direction(Actor::None) | 17 | : m_type(type), m_direction(Actor::None) |
| 5 | { | 18 | { |
| 6 | setSprite(2, 2, 16, 16); | 19 | m_pix = ":/" + QString("actor%1").arg(type); |
| 20 | setPixmap(m_pix); | ||
| 21 | setSprite(82, 82, 16, 16); | ||
| 22 | |||
| 23 | m_direction = Actor::Left; | ||
| 24 | |||
| 25 | qRegisterAnimationInterpolator<bool>(myBooleanInterpolator); | ||
| 26 | QSequentialAnimationGroup *group = new QSequentialAnimationGroup(this); | ||
| 27 | group->setLoopCount(-1); | ||
| 28 | for (int i = 0; i < 4; i++) | ||
| 29 | { | ||
| 30 | PixmapItem *step = new PixmapItem(m_pix, this); | ||
| 31 | int x = i * 20 + SPRITE_MARGIN; | ||
| 32 | int y = m_direction * 20 + SPRITE_MARGIN; | ||
| 33 | step->setSprite(x, y, SPRITE_PLAYER_WIDTH, SPRITE_PLAYER_HEIGHT); | ||
| 34 | step->setVisible(false); | ||
| 35 | |||
| 36 | |||
| 37 | QPropertyAnimation *anim = new QPropertyAnimation(step, "visible", group); | ||
| 38 | anim->setDuration(0); | ||
| 39 | anim->setEndValue(true); | ||
| 40 | |||
| 41 | group->addPause(100); | ||
| 42 | |||
| 43 | QPropertyAnimation *anim2 = new QPropertyAnimation(step, "visible", group); | ||
| 44 | anim2->setDuration(0); | ||
| 45 | anim2->setEndValue(false); | ||
| 46 | } | ||
| 47 | |||
| 48 | AnimationManager::self()->registerAnimation(group); | ||
| 49 | group->start(); | ||
| 7 | } | 50 | } |
diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h index 5f68f8f..43314a9 100644 --- a/pacman-c++/actor.h +++ b/pacman-c++/actor.h | |||
| @@ -22,7 +22,9 @@ public: | |||
| 22 | }; | 22 | }; |
| 23 | 23 | ||
| 24 | Actor(Type type); | 24 | Actor(Type type); |
| 25 | |||
| 25 | private: | 26 | private: |
| 27 | QPixmap m_pix; | ||
| 26 | Type m_type; | 28 | Type m_type; |
| 27 | Movement m_direction; | 29 | Movement m_direction; |
| 28 | }; | 30 | }; |
diff --git a/pacman-c++/animationmanager.cpp b/pacman-c++/animationmanager.cpp new file mode 100644 index 0000000..69bec53 --- /dev/null +++ b/pacman-c++/animationmanager.cpp | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | #include "animationmanager.h" | ||
| 2 | #include <QtCore/QAbstractAnimation> | ||
| 3 | #include <QtCore/QDebug> | ||
| 4 | |||
| 5 | // the universe's only animation manager | ||
| 6 | AnimationManager *AnimationManager::instance = 0; | ||
| 7 | |||
| 8 | AnimationManager::AnimationManager() | ||
| 9 | { | ||
| 10 | } | ||
| 11 | |||
| 12 | AnimationManager *AnimationManager::self() | ||
| 13 | { | ||
| 14 | if (!instance) | ||
| 15 | instance = new AnimationManager; | ||
| 16 | return instance; | ||
| 17 | } | ||
| 18 | |||
| 19 | void AnimationManager::registerAnimation(QAbstractAnimation *anim) | ||
| 20 | { | ||
| 21 | QObject::connect(anim, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterAnimation_helper(QObject*))); | ||
| 22 | animations.append(anim); | ||
| 23 | } | ||
| 24 | |||
| 25 | void AnimationManager::unregisterAnimation_helper(QObject *obj) | ||
| 26 | { | ||
| 27 | unregisterAnimation(static_cast<QAbstractAnimation*>(obj)); | ||
| 28 | } | ||
| 29 | |||
| 30 | void AnimationManager::unregisterAnimation(QAbstractAnimation *anim) | ||
| 31 | { | ||
| 32 | QObject::disconnect(anim, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterAnimation_helper(QObject*))); | ||
| 33 | animations.removeAll(anim); | ||
| 34 | } | ||
| 35 | |||
| 36 | void AnimationManager::unregisterAllAnimations() | ||
| 37 | { | ||
| 38 | animations.clear(); | ||
| 39 | } | ||
| 40 | |||
| 41 | void AnimationManager::pauseAll() | ||
| 42 | { | ||
| 43 | foreach (QAbstractAnimation* animation, animations) | ||
| 44 | { | ||
| 45 | if (animation->state() == QAbstractAnimation::Running) | ||
| 46 | animation->pause(); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | void AnimationManager::resumeAll() | ||
| 50 | { | ||
| 51 | foreach (QAbstractAnimation* animation, animations) | ||
| 52 | { | ||
| 53 | if (animation->state() == QAbstractAnimation::Paused) | ||
| 54 | animation->resume(); | ||
| 55 | } | ||
| 56 | } | ||
diff --git a/pacman-c++/animationmanager.h b/pacman-c++/animationmanager.h new file mode 100644 index 0000000..eb5b7d0 --- /dev/null +++ b/pacman-c++/animationmanager.h | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | #ifndef ANIMATIONMANAGER_H | ||
| 2 | #define ANIMATIONMANAGER_H | ||
| 3 | |||
| 4 | #include <QtCore/QObject> | ||
| 5 | |||
| 6 | QT_BEGIN_NAMESPACE | ||
| 7 | class QAbstractAnimation; | ||
| 8 | QT_END_NAMESPACE | ||
| 9 | |||
| 10 | class AnimationManager | ||
| 11 | : public QObject | ||
| 12 | { | ||
| 13 | Q_OBJECT | ||
| 14 | public: | ||
| 15 | AnimationManager(); | ||
| 16 | void registerAnimation(QAbstractAnimation *anim); | ||
| 17 | void unregisterAnimation(QAbstractAnimation *anim); | ||
| 18 | void unregisterAllAnimations(); | ||
| 19 | static AnimationManager *self(); | ||
| 20 | |||
| 21 | public slots: | ||
| 22 | void pauseAll(); | ||
| 23 | void resumeAll(); | ||
| 24 | |||
| 25 | private slots: | ||
| 26 | void unregisterAnimation_helper(QObject *obj); | ||
| 27 | |||
| 28 | private: | ||
| 29 | static AnimationManager *instance; | ||
| 30 | QList<QAbstractAnimation *> animations; | ||
| 31 | }; | ||
| 32 | |||
| 33 | #endif // ANIMATIONMANAGER_H | ||
diff --git a/pacman-c++/pacman.pro b/pacman-c++/pacman.pro index 6b705a8..3ce84da 100644 --- a/pacman-c++/pacman.pro +++ b/pacman-c++/pacman.pro | |||
| @@ -1,12 +1,12 @@ | |||
| 1 | SOURCES += main.cpp \ | 1 | SOURCES += main.cpp \ |
| 2 | pixmapitem.cpp \ | 2 | pixmapitem.cpp \ |
| 3 | actor.cpp | 3 | actor.cpp \ |
| 4 | animationmanager.cpp | ||
| 4 | HEADERS += pixmapitem.h \ | 5 | HEADERS += pixmapitem.h \ |
| 5 | actor.h | 6 | actor.h \ |
| 7 | animationmanager.h | ||
| 6 | RESOURCES += pacman.qrc | 8 | RESOURCES += pacman.qrc |
| 7 | 9 | ||
| 8 | 10 | ||
| 9 | OBJECTS_DIR = .obj | 11 | OBJECTS_DIR = .obj |
| 10 | MOC_DIR = .moc | 12 | MOC_DIR = .moc |
| 11 | |||
| 12 | |||
diff --git a/pacman-c++/pacman.qrc b/pacman-c++/pacman.qrc index c209878..a643886 100644 --- a/pacman-c++/pacman.qrc +++ b/pacman-c++/pacman.qrc | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | <RCC> | 1 | <RCC> |
| 2 | <qresource prefix="/"> | 2 | <qresource prefix="/"> |
| 3 | <file alias="google-pacman-sprite">pics/pacman10-hp-sprite-2.png</file> | 3 | <file alias="google-pacman-sprite">pics/pacman10-hp-sprite-2.png</file> |
| 4 | <file alias="actor1">pics/actor1.png</file> | ||
| 4 | </qresource> | 5 | </qresource> |
| 5 | </RCC> | 6 | </RCC> |
diff --git a/pacman-c++/pics/actor1.png b/pacman-c++/pics/actor1.png new file mode 100644 index 0000000..2b4a32a --- /dev/null +++ b/pacman-c++/pics/actor1.png | |||
| Binary files differ | |||
diff --git a/pacman-c++/pixmapitem.cpp b/pacman-c++/pixmapitem.cpp index 9c2f941..a977a9e 100644 --- a/pacman-c++/pixmapitem.cpp +++ b/pacman-c++/pixmapitem.cpp | |||
| @@ -1,10 +1,16 @@ | |||
| 1 | #include "pixmapitem.h" | 1 | #include "pixmapitem.h" |
| 2 | #include <QPainter> | 2 | #include <QPainter> |
| 3 | #include <QDebug> | ||
| 4 | |||
| 5 | PixmapItem::PixmapItem(QGraphicsItem *parent) | ||
| 6 | : QGraphicsObject(parent), m_x(0), m_y(0) | ||
| 7 | { | ||
| 8 | } | ||
| 3 | 9 | ||
| 4 | PixmapItem::PixmapItem(const QString &fileName, QGraphicsItem *parent) | 10 | PixmapItem::PixmapItem(const QString &fileName, QGraphicsItem *parent) |
| 5 | : QGraphicsObject(parent), m_x(0), m_y(0) | 11 | : QGraphicsObject(parent), m_x(0), m_y(0) |
| 6 | { | 12 | { |
| 7 | m_pix = ":/" + fileName; | 13 | m_pix = ":/" + fileName; |
| 8 | m_width = m_pix.width(); | 14 | m_width = m_pix.width(); |
| 9 | m_height = m_pix.height(); | 15 | m_height = m_pix.height(); |
| 10 | } | 16 | } |
| @@ -12,12 +18,34 @@ PixmapItem::PixmapItem(const QString &fileName, QGraphicsItem *parent) | |||
| 12 | PixmapItem::PixmapItem(const QString &fileName, QGraphicsScene *scene) | 18 | PixmapItem::PixmapItem(const QString &fileName, QGraphicsScene *scene) |
| 13 | : QGraphicsObject(), m_x(0), m_y(0) | 19 | : QGraphicsObject(), m_x(0), m_y(0) |
| 14 | { | 20 | { |
| 15 | m_pix = ":/" + fileName; | 21 | m_pix = ":/" + fileName; |
| 16 | m_width = m_pix.width(); | 22 | m_width = m_pix.width(); |
| 17 | m_height = m_pix.height(); | 23 | m_height = m_pix.height(); |
| 18 | scene->addItem(this); | 24 | scene->addItem(this); |
| 19 | } | 25 | } |
| 20 | 26 | ||
| 27 | PixmapItem::PixmapItem(const QPixmap &pix, QGraphicsItem *parent) | ||
| 28 | : QGraphicsObject(parent), m_pix(pix), m_x(0), m_y(0) | ||
| 29 | { | ||
| 30 | m_width = m_pix.width(); | ||
| 31 | m_height = m_pix.height(); | ||
| 32 | } | ||
| 33 | |||
| 34 | PixmapItem::PixmapItem(const QPixmap &pix, QGraphicsScene *scene) | ||
| 35 | : QGraphicsObject(), m_pix(pix), m_x(0), m_y(0) | ||
| 36 | { | ||
| 37 | m_width = m_pix.width(); | ||
| 38 | m_height = m_pix.height(); | ||
| 39 | scene->addItem(this); | ||
| 40 | } | ||
| 41 | |||
| 42 | void PixmapItem::setPixmap(const QPixmap &pix) | ||
| 43 | { | ||
| 44 | m_pix = pix; | ||
| 45 | m_width = m_pix.width(); | ||
| 46 | m_height = m_pix.height(); | ||
| 47 | } | ||
| 48 | |||
| 21 | void PixmapItem::setSprite(int x, int y, int width, int height) | 49 | void PixmapItem::setSprite(int x, int y, int width, int height) |
| 22 | { | 50 | { |
| 23 | m_x = x; | 51 | m_x = x; |
| @@ -41,4 +69,16 @@ void PixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWid | |||
| 41 | painter->drawPixmap(0, 0, m_pix, m_x, m_y, m_width, m_height); | 69 | painter->drawPixmap(0, 0, m_pix, m_x, m_y, m_width, m_height); |
| 42 | } | 70 | } |
| 43 | 71 | ||
| 72 | #if 0 | ||
| 73 | bool PixmapItem::isVisible() const | ||
| 74 | { | ||
| 75 | qDebug() << "isVisible()"; | ||
| 76 | return true; | ||
| 77 | } | ||
| 78 | |||
| 79 | void PixmapItem::setVisible(bool v) | ||
| 80 | { | ||
| 81 | qDebug() << "setVisible(" << v << ")"; | ||
| 82 | } | ||
| 83 | #endif | ||
| 44 | 84 | ||
diff --git a/pacman-c++/pixmapitem.h b/pacman-c++/pixmapitem.h index 853604d..e6aa79e 100644 --- a/pacman-c++/pixmapitem.h +++ b/pacman-c++/pixmapitem.h | |||
| @@ -7,13 +7,25 @@ | |||
| 7 | class PixmapItem | 7 | class PixmapItem |
| 8 | : public QGraphicsObject | 8 | : public QGraphicsObject |
| 9 | { | 9 | { |
| 10 | Q_PROPERTY(bool visibleX READ isVisible WRITE setVisible) | ||
| 11 | |||
| 10 | public: | 12 | public: |
| 13 | PixmapItem(QGraphicsItem *parent = 0); | ||
| 11 | PixmapItem(const QString &fileName, QGraphicsItem *parent = 0); | 14 | PixmapItem(const QString &fileName, QGraphicsItem *parent = 0); |
| 12 | PixmapItem(const QString &fileName, QGraphicsScene *scene); | 15 | PixmapItem(const QString &fileName, QGraphicsScene *scene); |
| 16 | PixmapItem(const QPixmap &pix, QGraphicsItem *parent = 0); | ||
| 17 | PixmapItem(const QPixmap &pix, QGraphicsScene *scene); | ||
| 18 | void setPixmap(const QPixmap &pix); | ||
| 13 | void setSprite(int x, int y, int width, int height); | 19 | void setSprite(int x, int y, int width, int height); |
| 14 | QSizeF size() const; | 20 | QSizeF size() const; |
| 15 | QRectF boundingRect() const; | 21 | QRectF boundingRect() const; |
| 16 | void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); | 22 | void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); |
| 23 | |||
| 24 | #if 0 | ||
| 25 | bool isVisible() const; | ||
| 26 | void setVisible(bool v); | ||
| 27 | #endif | ||
| 28 | |||
| 17 | private: | 29 | private: |
| 18 | QPixmap m_pix; | 30 | QPixmap m_pix; |
| 19 | int m_x, m_y; | 31 | int m_x, m_y; |
