From eef1d8ea60e3797ba261ebfe61a7d1e165069ed4 Mon Sep 17 00:00:00 2001 From: totycro Date: Mon, 4 Apr 2011 22:02:31 +0200 Subject: Use only 1 color format for everything Simple map parsing for Blocks and Bonus points --- pacman-c++/actor.cpp | 8 +++--- pacman-c++/actor.h | 13 +++------- pacman-c++/block.cpp | 17 ++++++++----- pacman-c++/block.h | 10 +++++--- pacman-c++/bonuspoint.cpp | 13 ++++++++++ pacman-c++/bonuspoint.h | 15 +++++++++++ pacman-c++/constants.h | 20 +++++++++------ pacman-c++/mainwidget.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++ pacman-c++/mainwidget.h | 5 ++++ pacman-c++/pacman.pro | 2 ++ pacman-c++/pacman.qrc | 6 ++++- pacman-c++/pics/actor3.png | Bin 703 -> 0 bytes pacman-c++/pics/actor4.png | Bin 706 -> 703 bytes pacman-c++/pics/actor8.png | Bin 0 -> 706 bytes pacman-c++/pics/block0.png | Bin 0 -> 181 bytes pacman-c++/pics/block2.png | Bin 0 -> 195 bytes pacman-c++/pics/block4.png | Bin 0 -> 180 bytes pacman-c++/pics/cherry.png | Bin 0 -> 214 bytes 18 files changed, 139 insertions(+), 31 deletions(-) create mode 100644 pacman-c++/bonuspoint.cpp create mode 100644 pacman-c++/bonuspoint.h delete mode 100644 pacman-c++/pics/actor3.png create mode 100644 pacman-c++/pics/actor8.png create mode 100644 pacman-c++/pics/block0.png create mode 100644 pacman-c++/pics/block2.png create mode 100644 pacman-c++/pics/block4.png create mode 100644 pacman-c++/pics/cherry.png (limited to 'pacman-c++') diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index 5f668fc..2df238b 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp @@ -15,14 +15,14 @@ static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal return (progress == 1.0) ? end : start; } -Actor::Actor(Type type, QGraphicsItem *parent) - : PixmapItem(parent), m_type(type), m_direction(Actor::None) +Actor::Actor(Color color, QGraphicsItem *parent) + : PixmapItem(parent), m_color(color), m_direction(Actor::None) { - m_pix = ":/" + QString("actor%1").arg(m_type); + m_pix = ":/" + QString("actor%1").arg(m_color); // DON'T set any pixmap here. we've a pixmap in the animation //setPixmap(m_pix); // higher player "over" lower player - setZValue(m_type * 10); + setZValue(m_color * 10); m_direction = Actor::Left; diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h index 3cee699..d2d7875 100644 --- a/pacman-c++/actor.h +++ b/pacman-c++/actor.h @@ -3,6 +3,8 @@ #include "pixmapitem.h" +#include "constants.h" + class Actor : public PixmapItem { @@ -15,18 +17,11 @@ public: Down }; - enum Type { - Player1 = 1, // red - Player2, // blue - Player3, // green - Player4, // yellow - }; - - Actor(Type type, QGraphicsItem *parent = 0); + Actor(Color color, QGraphicsItem *parent = 0); private: QPixmap m_pix; - Type m_type; + Color m_color; Movement m_direction; }; diff --git a/pacman-c++/block.cpp b/pacman-c++/block.cpp index d5435f3..4240dd4 100644 --- a/pacman-c++/block.cpp +++ b/pacman-c++/block.cpp @@ -1,12 +1,17 @@ #include "block.h" -#include +#include -Block::Block(Actor::Type type) +#include "constants.h" + +std::map Block::m_pixmaps; + +Block::Block(Color color) { - if (m_pixmaps.find(type) == m_pixmaps.end()) { - QString pixmapName = ":/" + QString("block%1").arg(type); - m_pixmaps[type] = QPixmap( pixmapName ); + if (m_pixmaps.find(color) == m_pixmaps.end()) { + QString pixmapName = ":/" + QString("block%1").arg(color); + m_pixmaps[color] = QPixmap( pixmapName ); } - setPixmap( m_pixmaps.find(type)->second ); + setPixmap( m_pixmaps.find(color)->second ); + qDebug() << "loading block w color: " << color; } diff --git a/pacman-c++/block.h b/pacman-c++/block.h index 1e76aa9..f19b590 100644 --- a/pacman-c++/block.h +++ b/pacman-c++/block.h @@ -1,20 +1,22 @@ #ifndef BLOCK_H #define BLOCK_H -#include #include "pixmapitem.h" -#include "actor.h" + +#include + +#include "constants.h" class Block : public PixmapItem { public: - Block(Actor::Type type); + Block(Color color); private: // map for saving QPixmaps for reuse - std::map m_pixmaps; + static std::map m_pixmaps; }; diff --git a/pacman-c++/bonuspoint.cpp b/pacman-c++/bonuspoint.cpp new file mode 100644 index 0000000..67c8250 --- /dev/null +++ b/pacman-c++/bonuspoint.cpp @@ -0,0 +1,13 @@ +#include "bonuspoint.h" + +namespace { + QPixmap *pixmap = 0; +} + +BonusPoint::BonusPoint() +{ + if (pixmap == 0) { + pixmap = new QPixmap(":/cherry"); + } + setPixmap(*pixmap); +} diff --git a/pacman-c++/bonuspoint.h b/pacman-c++/bonuspoint.h new file mode 100644 index 0000000..cfae4b4 --- /dev/null +++ b/pacman-c++/bonuspoint.h @@ -0,0 +1,15 @@ +#ifndef BONUSPOINT_H +#define BONUSPOINT_H + +#include "pixmapitem.h" + +class BonusPoint + : public PixmapItem +{ +public: + BonusPoint(); +private: + +}; + +#endif // BONUSPOINT_H \ No newline at end of file diff --git a/pacman-c++/constants.h b/pacman-c++/constants.h index 278f2b3..a8907b1 100644 --- a/pacman-c++/constants.h +++ b/pacman-c++/constants.h @@ -4,20 +4,26 @@ const unsigned int map_size[2] = { 20, 20 }; const unsigned int field_size[2] = { 16, 16 }; +enum Color { + noColor = 0, + red = (1 << 0), + blue = (1 << 1), + green = (1 << 2), +}; + // constants for data transmission to client namespace transmission { typedef unsigned int field_t; - const field_t red = (1 << 0); - const field_t blue = (1 << 1); - const field_t green = (1 << 2); + typedef unsigned int mask_t; - const field_t box = (1 << 3); - const field_t foo = (1 << 4); + const field_t block = (1 << 3); + const field_t bonuspoint = (1 << 4); + + const mask_t color_mask = noColor | red | blue | green; + const mask_t type_mask = block | bonuspoint; typedef field_t** map_t; } - - #endif // CONSTANTS_H \ No newline at end of file diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 982c809..72d9c8a 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -2,6 +2,8 @@ #include "actor.h" #include "block.h" +#include "bonuspoint.h" +#include "constants.h" MainWidget::MainWidget() { @@ -26,8 +28,62 @@ MainWidget::MainWidget() { loadDummyMap(); } +// temporary +transmission::map_t createDummyMap() { + transmission::map_t map; + map = new transmission::field_t*[map_size[0]]; + for (unsigned int i=0; i(cur & transmission::color_mask); + qDebug() << "col " << color; + + PixmapItem *item = 0; + if (cur & transmission::block) { + item = new Block(color); + } else if (cur & transmission::bonuspoint) { + item = new BonusPoint(); + } + Q_ASSERT(item != 0); + scene->addItem(item); + item->setPos( mapPositionToCoord(x, y) ); + } + } + + /* Actor *actor3 = new Actor(Actor::Player3); scene->addItem(actor3); actor3->setPos(140, 100); @@ -37,5 +93,10 @@ void MainWidget::loadDummyMap() scene->addItem(b); b->setPos( 100 + i*16, 200); } + */ +} +QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y) +{ + return QPoint(x * field_size[0], y * field_size[1]); } diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index d1ec761..4cdb3b8 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h @@ -3,6 +3,8 @@ #include +#include "constants.h" + class MainWidget : public QWidget { @@ -14,6 +16,9 @@ public: private: void loadDummyMap(); + // data conversion + QPoint mapPositionToCoord(unsigned int x, unsigned int y); + QGraphicsScene *scene; }; diff --git a/pacman-c++/pacman.pro b/pacman-c++/pacman.pro index 1add92e..954ff77 100644 --- a/pacman-c++/pacman.pro +++ b/pacman-c++/pacman.pro @@ -4,12 +4,14 @@ SOURCES += pixmapitem.cpp \ animationmanager.cpp \ block.cpp \ client.cpp \ + bonuspoint.cpp \ mainwidget.cpp HEADERS += pixmapitem.h \ actor.h \ animationmanager.h \ block.h \ client.h \ + bonuspoint.h \ mainwidget.h RESOURCES += pacman.qrc diff --git a/pacman-c++/pacman.qrc b/pacman-c++/pacman.qrc index 6a7fefb..1d82831 100644 --- a/pacman-c++/pacman.qrc +++ b/pacman-c++/pacman.qrc @@ -2,8 +2,12 @@ pics/actor1.png pics/actor2.png - pics/actor3.png pics/actor4.png + pics/actor8.png + pics/block0.png pics/block1.png + pics/block2.png + pics/block4.png + pics/cherry.png diff --git a/pacman-c++/pics/actor3.png b/pacman-c++/pics/actor3.png deleted file mode 100644 index 99e025c..0000000 Binary files a/pacman-c++/pics/actor3.png and /dev/null differ diff --git a/pacman-c++/pics/actor4.png b/pacman-c++/pics/actor4.png index 9947f9b..99e025c 100644 Binary files a/pacman-c++/pics/actor4.png and b/pacman-c++/pics/actor4.png differ diff --git a/pacman-c++/pics/actor8.png b/pacman-c++/pics/actor8.png new file mode 100644 index 0000000..9947f9b Binary files /dev/null and b/pacman-c++/pics/actor8.png differ diff --git a/pacman-c++/pics/block0.png b/pacman-c++/pics/block0.png new file mode 100644 index 0000000..7f93087 Binary files /dev/null and b/pacman-c++/pics/block0.png differ diff --git a/pacman-c++/pics/block2.png b/pacman-c++/pics/block2.png new file mode 100644 index 0000000..cf1a043 Binary files /dev/null and b/pacman-c++/pics/block2.png differ diff --git a/pacman-c++/pics/block4.png b/pacman-c++/pics/block4.png new file mode 100644 index 0000000..13eb22d Binary files /dev/null and b/pacman-c++/pics/block4.png differ diff --git a/pacman-c++/pics/cherry.png b/pacman-c++/pics/cherry.png new file mode 100644 index 0000000..6aff6de Binary files /dev/null and b/pacman-c++/pics/cherry.png differ -- cgit v1.2.3