diff options
Diffstat (limited to 'pacman-c++')
| -rw-r--r-- | pacman-c++/actor.cpp | 8 | ||||
| -rw-r--r-- | pacman-c++/block.cpp | 4 | ||||
| -rw-r--r-- | pacman-c++/bonuspoint.cpp | 4 | ||||
| -rw-r--r-- | pacman-c++/client.cpp | 2 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 18 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.h | 3 | ||||
| -rw-r--r-- | pacman-c++/pixmapitem.h | 3 | ||||
| -rw-r--r-- | pacman-c++/point.cpp | 4 | ||||
| -rw-r--r-- | pacman-c++/sceneholder.cpp | 28 | ||||
| -rw-r--r-- | pacman-c++/sceneholder.h | 14 | ||||
| -rw-r--r-- | pacman-c++/server.cpp | 2 | ||||
| -rw-r--r-- | pacman-c++/server.h | 3 |
12 files changed, 60 insertions, 33 deletions
diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index bb062ea..baf6dca 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp | |||
| @@ -13,7 +13,6 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) | |||
| 13 | : PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local), | 13 | : PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local), |
| 14 | m_player(NULL), m_roundPoints(0), m_gamePoints(0) | 14 | m_player(NULL), m_roundPoints(0), m_gamePoints(0) |
| 15 | { | 15 | { |
| 16 | m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); | ||
| 17 | /* DON'T set any pixmap here. we've a pixmap in the animation | 16 | /* DON'T set any pixmap here. we've a pixmap in the animation |
| 18 | * but we need a sprite for the collision detection | 17 | * but we need a sprite for the collision detection |
| 19 | */ | 18 | */ |
| @@ -21,6 +20,13 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) | |||
| 21 | /* higher player "over" lower player */ | 20 | /* higher player "over" lower player */ |
| 22 | setZValue(m_color * 10); | 21 | setZValue(m_color * 10); |
| 23 | 22 | ||
| 23 | /* rest of the ctor is only for clients */ | ||
| 24 | if (Constants::server) | ||
| 25 | return; | ||
| 26 | |||
| 27 | /* our actor pixmap. created after server part */ | ||
| 28 | m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); | ||
| 29 | |||
| 24 | /* setup icon for player */ | 30 | /* setup icon for player */ |
| 25 | m_icon.setPixmap(m_pix); | 31 | m_icon.setPixmap(m_pix); |
| 26 | m_icon.setSprite(Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); | 32 | m_icon.setSprite(Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); |
diff --git a/pacman-c++/block.cpp b/pacman-c++/block.cpp index c168c00..b1ce0e0 100644 --- a/pacman-c++/block.cpp +++ b/pacman-c++/block.cpp | |||
| @@ -7,6 +7,10 @@ QMap<Color::Color, QPixmap> Block::m_pixmaps; | |||
| 7 | Block::Block(Color::Color color, unsigned int neighbours, QGraphicsItem *parent) | 7 | Block::Block(Color::Color color, unsigned int neighbours, QGraphicsItem *parent) |
| 8 | : PixmapItem(parent) | 8 | : PixmapItem(parent) |
| 9 | { | 9 | { |
| 10 | /* empty object for servers */ | ||
| 11 | if (Constants::server) | ||
| 12 | return; | ||
| 13 | |||
| 10 | if (m_pixmaps.find(color) == m_pixmaps.end()) | 14 | if (m_pixmaps.find(color) == m_pixmaps.end()) |
| 11 | { | 15 | { |
| 12 | unsigned int colid = (color == Color::none) ? 0 : (color >> 1) + 1; | 16 | unsigned int colid = (color == Color::none) ? 0 : (color >> 1) + 1; |
diff --git a/pacman-c++/bonuspoint.cpp b/pacman-c++/bonuspoint.cpp index a705c09..bbb26b7 100644 --- a/pacman-c++/bonuspoint.cpp +++ b/pacman-c++/bonuspoint.cpp | |||
| @@ -12,6 +12,10 @@ namespace | |||
| 12 | BonusPoint::BonusPoint(QGraphicsItem *parent) | 12 | BonusPoint::BonusPoint(QGraphicsItem *parent) |
| 13 | : PixmapItem(parent) | 13 | : PixmapItem(parent) |
| 14 | { | 14 | { |
| 15 | /* empty object for servers */ | ||
| 16 | if (Constants::server) | ||
| 17 | return; | ||
| 18 | |||
| 15 | if (pixmap == NULL) | 19 | if (pixmap == NULL) |
| 16 | pixmap = new QPixmap(":/bonuspoints"); | 20 | pixmap = new QPixmap(":/bonuspoints"); |
| 17 | setPixmap(*pixmap); | 21 | setPixmap(*pixmap); |
diff --git a/pacman-c++/client.cpp b/pacman-c++/client.cpp index 3d45ebd..4843d95 100644 --- a/pacman-c++/client.cpp +++ b/pacman-c++/client.cpp | |||
| @@ -69,7 +69,7 @@ int main(int argc, char ** argv) | |||
| 69 | { | 69 | { |
| 70 | GOOGLE_PROTOBUF_VERIFY_VERSION; | 70 | GOOGLE_PROTOBUF_VERIFY_VERSION; |
| 71 | 71 | ||
| 72 | QApplication app(argc, argv); | 72 | QApplication app(argc, argv, true); |
| 73 | app.setOrganizationName("TU Wien FOOP"); | 73 | app.setOrganizationName("TU Wien FOOP"); |
| 74 | app.setApplicationName("Pacman Client"); | 74 | app.setApplicationName("Pacman Client"); |
| 75 | app.setWindowIcon(QIcon(":/appicon")); | 75 | app.setWindowIcon(QIcon(":/appicon")); |
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 518692c..028bfad 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -7,19 +7,23 @@ | |||
| 7 | #include "pacman.pb.h" | 7 | #include "pacman.pb.h" |
| 8 | 8 | ||
| 9 | MainWidget::MainWidget(QWidget *parent) | 9 | MainWidget::MainWidget(QWidget *parent) |
| 10 | : SceneHolder(parent), m_currentKey(Transmission::none), m_running(false) | 10 | : QWidget(parent), m_currentKey(Transmission::none), m_running(false) |
| 11 | { | 11 | { |
| 12 | m_color = connectToServer(); | 12 | Color::Color color = connectToServer(); |
| 13 | if (m_color == Color::none) | 13 | if (color == Color::none) |
| 14 | { | 14 | { |
| 15 | QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); | 15 | QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); |
| 16 | // TODO: quit application here or sth | 16 | // TODO: quit application here or sth |
| 17 | return; | 17 | return; |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | /* create our scene */ | ||
| 21 | m_scene = new SceneHolder(this); | ||
| 22 | m_scene->setColor(color); | ||
| 23 | |||
| 20 | /* call updateMap after m_color ist set! */ | 24 | /* call updateMap after m_color ist set! */ |
| 21 | createGui(); | 25 | createGui(); |
| 22 | updateMap(Util::createDemoMap()); | 26 | m_scene->updateMap(Util::createDemoMap()); |
| 23 | 27 | ||
| 24 | connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); | 28 | connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); |
| 25 | 29 | ||
| @@ -27,7 +31,7 @@ MainWidget::MainWidget(QWidget *parent) | |||
| 27 | connect(sendTimer, SIGNAL(timeout()), this, SLOT(sendKeyUpdate())); | 31 | connect(sendTimer, SIGNAL(timeout()), this, SLOT(sendKeyUpdate())); |
| 28 | sendTimer->start(Constants::tick); | 32 | sendTimer->start(Constants::tick); |
| 29 | 33 | ||
| 30 | qDebug() << "mycolor=" << m_color; | 34 | qDebug() << "mycolor=" << m_scene->color(); |
| 31 | 35 | ||
| 32 | //TODO: play intro as soon as there are enough players | 36 | //TODO: play intro as soon as there are enough players |
| 33 | //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); | 37 | //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); |
| @@ -62,7 +66,7 @@ void MainWidget::createGui() | |||
| 62 | playerLayout->addWidget(new QLabel("", this), 0, 1); | 66 | playerLayout->addWidget(new QLabel("", this), 0, 1); |
| 63 | playerLayout->addWidget(new QLabel("", this), 1, 1); | 67 | playerLayout->addWidget(new QLabel("", this), 1, 1); |
| 64 | 68 | ||
| 65 | if (Color::order[i] == m_color) | 69 | if (Color::order[i] == m_scene->color()) |
| 66 | scoreLayout->insertWidget(0, scoreBox); | 70 | scoreLayout->insertWidget(0, scoreBox); |
| 67 | else | 71 | else |
| 68 | scoreLayout->addWidget(scoreBox); | 72 | scoreLayout->addWidget(scoreBox); |
| @@ -145,7 +149,7 @@ void MainWidget::tick() | |||
| 145 | ++i; | 149 | ++i; |
| 146 | } | 150 | } |
| 147 | } | 151 | } |
| 148 | updateMap(map); | 152 | m_scene->updateMap(map); |
| 149 | updateScore(packet); | 153 | updateScore(packet); |
| 150 | } | 154 | } |
| 151 | 155 | ||
diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index ec053a0..589377b 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h | |||
| @@ -12,7 +12,7 @@ | |||
| 12 | class Actor; | 12 | class Actor; |
| 13 | 13 | ||
| 14 | class MainWidget | 14 | class MainWidget |
| 15 | : public SceneHolder | 15 | : public QWidget |
| 16 | { | 16 | { |
| 17 | Q_OBJECT | 17 | Q_OBJECT |
| 18 | 18 | ||
| @@ -51,6 +51,7 @@ private: | |||
| 51 | bool m_running; | 51 | bool m_running; |
| 52 | 52 | ||
| 53 | QTcpSocket *m_socket; | 53 | QTcpSocket *m_socket; |
| 54 | SceneHolder *m_scene; | ||
| 54 | }; | 55 | }; |
| 55 | 56 | ||
| 56 | #endif // MAINWIDGET_H | 57 | #endif // MAINWIDGET_H |
diff --git a/pacman-c++/pixmapitem.h b/pacman-c++/pixmapitem.h index 4e58a52..7560556 100644 --- a/pacman-c++/pixmapitem.h +++ b/pacman-c++/pixmapitem.h | |||
| @@ -1,11 +1,10 @@ | |||
| 1 | #ifndef PIXMAPITEM__H | 1 | #ifndef PIXMAPITEM__H |
| 2 | #define PIXMAPITEM__H | 2 | #define PIXMAPITEM__H |
| 3 | 3 | ||
| 4 | #include "gameentity.h" | ||
| 4 | #include <QtGui/QGraphicsObject> | 5 | #include <QtGui/QGraphicsObject> |
| 5 | #include <QtGui/QGraphicsScene> | 6 | #include <QtGui/QGraphicsScene> |
| 6 | 7 | ||
| 7 | #include "gameentity.h" | ||
| 8 | |||
| 9 | class PixmapItem | 8 | class PixmapItem |
| 10 | : public QGraphicsObject, public GameEntity | 9 | : public QGraphicsObject, public GameEntity |
| 11 | { | 10 | { |
diff --git a/pacman-c++/point.cpp b/pacman-c++/point.cpp index 4f76007..35266f6 100644 --- a/pacman-c++/point.cpp +++ b/pacman-c++/point.cpp | |||
| @@ -10,6 +10,10 @@ namespace | |||
| 10 | Point::Point(QGraphicsItem *parent) | 10 | Point::Point(QGraphicsItem *parent) |
| 11 | : PixmapItem(parent) | 11 | : PixmapItem(parent) |
| 12 | { | 12 | { |
| 13 | /* empty object for servers */ | ||
| 14 | if (Constants::server) | ||
| 15 | return; | ||
| 16 | |||
| 13 | if (pixmap == NULL) | 17 | if (pixmap == NULL) |
| 14 | pixmap = new QPixmap(":/points"); | 18 | pixmap = new QPixmap(":/points"); |
| 15 | setPixmap(*pixmap); | 19 | setPixmap(*pixmap); |
diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index b3ff588..b788a49 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp | |||
| @@ -1,5 +1,4 @@ | |||
| 1 | #include "sceneholder.h" | 1 | #include "sceneholder.h" |
| 2 | |||
| 3 | #include "constants.h" | 2 | #include "constants.h" |
| 4 | #include "pixmapitem.h" | 3 | #include "pixmapitem.h" |
| 5 | #include "block.h" | 4 | #include "block.h" |
| @@ -8,11 +7,11 @@ | |||
| 8 | #include "point.h" | 7 | #include "point.h" |
| 9 | #include "util.h" | 8 | #include "util.h" |
| 10 | 9 | ||
| 11 | SceneHolder::SceneHolder(QWidget* parent) | 10 | SceneHolder::SceneHolder(QObject *parent) |
| 12 | : QWidget(parent), m_color(Color::none), m_pointsLeft(0) | 11 | : QGraphicsScene(parent), m_color(Color::none), m_pointsLeft(0) |
| 13 | { | 12 | { |
| 14 | m_scene = new QGraphicsScene(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height, this); | 13 | setSceneRect(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height); |
| 15 | m_scene->setBackgroundBrush(Qt::black); | 14 | setBackgroundBrush(Qt::black); |
| 16 | 15 | ||
| 17 | visualMap.resize(Constants::map_size.width); | 16 | visualMap.resize(Constants::map_size.width); |
| 18 | for (int i = 0; i < visualMap.size(); ++i) | 17 | for (int i = 0; i < visualMap.size(); ++i) |
| @@ -39,7 +38,7 @@ void SceneHolder::updateMap(const Transmission::map_t& map) | |||
| 39 | /* remove elements (in case it's not an actor) */ | 38 | /* remove elements (in case it's not an actor) */ |
| 40 | if (oldItem != NULL && dynamic_cast<Actor *>(item) == NULL) | 39 | if (oldItem != NULL && dynamic_cast<Actor *>(item) == NULL) |
| 41 | { | 40 | { |
| 42 | m_scene->removeItem(oldItem); | 41 | removeItem(oldItem); |
| 43 | visualMap[x][y] = NULL; | 42 | visualMap[x][y] = NULL; |
| 44 | Actor *actor = NULL; | 43 | Actor *actor = NULL; |
| 45 | foreach (Actor *i, m_actors) | 44 | foreach (Actor *i, m_actors) |
| @@ -81,7 +80,6 @@ void SceneHolder::updateMap(const Transmission::map_t& map) | |||
| 81 | item = new BonusPoint(); | 80 | item = new BonusPoint(); |
| 82 | else if (cur & Transmission::point) | 81 | else if (cur & Transmission::point) |
| 83 | { | 82 | { |
| 84 | qDebug() << "new point"; | ||
| 85 | item = new Point(); | 83 | item = new Point(); |
| 86 | connect(item, SIGNAL(destroyed()), this, SLOT(decrementPoints())); | 84 | connect(item, SIGNAL(destroyed()), this, SLOT(decrementPoints())); |
| 87 | ++m_pointsLeft; | 85 | ++m_pointsLeft; |
| @@ -93,7 +91,7 @@ void SceneHolder::updateMap(const Transmission::map_t& map) | |||
| 93 | { | 91 | { |
| 94 | actor = new Actor(color, (color == m_color)); | 92 | actor = new Actor(color, (color == m_color)); |
| 95 | m_actors[color] = actor; | 93 | m_actors[color] = actor; |
| 96 | m_scene->addItem(actor); | 94 | addItem(actor); |
| 97 | actor->setPos(mapPositionToCoord(x, y)); | 95 | actor->setPos(mapPositionToCoord(x, y)); |
| 98 | } | 96 | } |
| 99 | else | 97 | else |
| @@ -116,13 +114,13 @@ void SceneHolder::updateMap(const Transmission::map_t& map) | |||
| 116 | 114 | ||
| 117 | if (item != NULL) | 115 | if (item != NULL) |
| 118 | { | 116 | { |
| 119 | m_scene->addItem(item); | 117 | addItem(item); |
| 120 | item->setPos(mapPositionToCoord(x, y)); | 118 | item->setPos(mapPositionToCoord(x, y)); |
| 121 | PixmapItem* oldItem = visualMap[x][y]; | 119 | PixmapItem* oldItem = visualMap[x][y]; |
| 122 | visualMap[x][y] = item; | 120 | visualMap[x][y] = item; |
| 123 | if (oldItem != NULL) | 121 | if (oldItem != NULL) |
| 124 | { | 122 | { |
| 125 | m_scene->removeItem(item); | 123 | removeItem(item); |
| 126 | delete oldItem; | 124 | delete oldItem; |
| 127 | } | 125 | } |
| 128 | } | 126 | } |
| @@ -131,6 +129,16 @@ void SceneHolder::updateMap(const Transmission::map_t& map) | |||
| 131 | 129 | ||
| 132 | } | 130 | } |
| 133 | 131 | ||
| 132 | void SceneHolder::setColor(Color::Color color) | ||
| 133 | { | ||
| 134 | m_color = color; | ||
| 135 | } | ||
| 136 | |||
| 137 | Color::Color SceneHolder::color() | ||
| 138 | { | ||
| 139 | return m_color; | ||
| 140 | } | ||
| 141 | |||
| 134 | unsigned int SceneHolder::pointsLeft() | 142 | unsigned int SceneHolder::pointsLeft() |
| 135 | { | 143 | { |
| 136 | return m_pointsLeft; | 144 | return m_pointsLeft; |
diff --git a/pacman-c++/sceneholder.h b/pacman-c++/sceneholder.h index ccb2a42..61cff3e 100644 --- a/pacman-c++/sceneholder.h +++ b/pacman-c++/sceneholder.h | |||
| @@ -1,30 +1,30 @@ | |||
| 1 | #ifndef SCENEHOLDER_H | 1 | #ifndef SCENEHOLDER_H |
| 2 | #define SCENEHOLDER_H | 2 | #define SCENEHOLDER_H |
| 3 | 3 | ||
| 4 | #include <QtGui> | ||
| 5 | |||
| 6 | #include "constants.h" | 4 | #include "constants.h" |
| 5 | #include <QtGui> | ||
| 7 | 6 | ||
| 8 | class PixmapItem; | 7 | class PixmapItem; |
| 9 | class Actor; | 8 | class Actor; |
| 10 | 9 | ||
| 11 | class SceneHolder | 10 | class SceneHolder |
| 12 | : public QWidget | 11 | : public QGraphicsScene |
| 13 | { | 12 | { |
| 14 | Q_OBJECT | 13 | Q_OBJECT |
| 15 | 14 | ||
| 16 | public: | 15 | public: |
| 17 | SceneHolder(QWidget* parent = 0); | 16 | SceneHolder(QObject *parent = 0); |
| 18 | virtual ~SceneHolder() | 17 | virtual ~SceneHolder() |
| 19 | {}; | 18 | {}; |
| 20 | unsigned int pointsLeft(); | 19 | unsigned int pointsLeft(); |
| 20 | void updateMap(const Transmission::map_t& map); | ||
| 21 | void setColor(Color::Color color = Color::none); | ||
| 22 | Color::Color color(); | ||
| 21 | 23 | ||
| 22 | private slots: | 24 | private slots: |
| 23 | void decrementPoints(); | 25 | void decrementPoints(); |
| 24 | 26 | ||
| 25 | protected: | 27 | protected: |
| 26 | virtual void updateMap(const Transmission::map_t& map); | ||
| 27 | |||
| 28 | /* data conversion */ | 28 | /* data conversion */ |
| 29 | QPoint mapPositionToCoord(unsigned int x, unsigned int y); | 29 | QPoint mapPositionToCoord(unsigned int x, unsigned int y); |
| 30 | QPoint mapPositionToCoord(QPoint point); | 30 | QPoint mapPositionToCoord(QPoint point); |
| @@ -42,8 +42,6 @@ protected: | |||
| 42 | 42 | ||
| 43 | /* points left before round ends */ | 43 | /* points left before round ends */ |
| 44 | unsigned int m_pointsLeft; | 44 | unsigned int m_pointsLeft; |
| 45 | |||
| 46 | QGraphicsScene *m_scene; | ||
| 47 | }; | 45 | }; |
| 48 | 46 | ||
| 49 | #endif // SCENEHOLDER_H | 47 | #endif // SCENEHOLDER_H |
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index c33c559..9a7b188 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp | |||
| @@ -233,7 +233,7 @@ int main(int argc, char ** argv) | |||
| 233 | { | 233 | { |
| 234 | GOOGLE_PROTOBUF_VERIFY_VERSION; | 234 | GOOGLE_PROTOBUF_VERIFY_VERSION; |
| 235 | 235 | ||
| 236 | QApplication app(argc, argv); | 236 | QApplication app(argc, argv, false); |
| 237 | app.setApplicationName("Pacman Server"); | 237 | app.setApplicationName("Pacman Server"); |
| 238 | app.setWindowIcon(QIcon(":/appicon")); | 238 | app.setWindowIcon(QIcon(":/appicon")); |
| 239 | 239 | ||
diff --git a/pacman-c++/server.h b/pacman-c++/server.h index d791c15..32bdf15 100644 --- a/pacman-c++/server.h +++ b/pacman-c++/server.h | |||
| @@ -2,10 +2,9 @@ | |||
| 2 | #define SERVER_H | 2 | #define SERVER_H |
| 3 | 3 | ||
| 4 | #include "sceneholder.h" | 4 | #include "sceneholder.h" |
| 5 | |||
| 6 | #include <QtGui> | ||
| 7 | #include "actor.h" | 5 | #include "actor.h" |
| 8 | #include "pacman.pb.h" | 6 | #include "pacman.pb.h" |
| 7 | #include <QtGui> | ||
| 9 | 8 | ||
| 10 | class QTcpSocket; | 9 | class QTcpSocket; |
| 11 | 10 | ||
