From dbeba838ea813b620ec571265c8ea417403fc81c Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 12 Apr 2011 14:45:24 +0200 Subject: make the server a non gui application this required a lot of reorganization: - don't create ANY pixmaps. that requires QtGui - don't create and QtWidgets - thus SceneHolder is now a QGraphisScene itself - and MainWidgets is a QWidget having SceneHolder as member variable --- pacman-c++/actor.cpp | 8 +++++++- pacman-c++/block.cpp | 4 ++++ pacman-c++/bonuspoint.cpp | 4 ++++ pacman-c++/client.cpp | 2 +- pacman-c++/mainwidget.cpp | 18 +++++++++++------- pacman-c++/mainwidget.h | 3 ++- pacman-c++/pixmapitem.h | 3 +-- pacman-c++/point.cpp | 4 ++++ pacman-c++/sceneholder.cpp | 28 ++++++++++++++++++---------- pacman-c++/sceneholder.h | 14 ++++++-------- pacman-c++/server.cpp | 2 +- pacman-c++/server.h | 3 +-- 12 files changed, 60 insertions(+), 33 deletions(-) (limited to 'pacman-c++') 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) : PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local), m_player(NULL), m_roundPoints(0), m_gamePoints(0) { - m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); /* DON'T set any pixmap here. we've a pixmap in the animation * but we need a sprite for the collision detection */ @@ -21,6 +20,13 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) /* higher player "over" lower player */ setZValue(m_color * 10); + /* rest of the ctor is only for clients */ + if (Constants::server) + return; + + /* our actor pixmap. created after server part */ + m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); + /* setup icon for player */ m_icon.setPixmap(m_pix); 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 Block::m_pixmaps; Block::Block(Color::Color color, unsigned int neighbours, QGraphicsItem *parent) : PixmapItem(parent) { + /* empty object for servers */ + if (Constants::server) + return; + if (m_pixmaps.find(color) == m_pixmaps.end()) { 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 BonusPoint::BonusPoint(QGraphicsItem *parent) : PixmapItem(parent) { + /* empty object for servers */ + if (Constants::server) + return; + if (pixmap == NULL) pixmap = new QPixmap(":/bonuspoints"); 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) { GOOGLE_PROTOBUF_VERIFY_VERSION; - QApplication app(argc, argv); + QApplication app(argc, argv, true); app.setOrganizationName("TU Wien FOOP"); app.setApplicationName("Pacman Client"); 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 @@ #include "pacman.pb.h" MainWidget::MainWidget(QWidget *parent) - : SceneHolder(parent), m_currentKey(Transmission::none), m_running(false) + : QWidget(parent), m_currentKey(Transmission::none), m_running(false) { - m_color = connectToServer(); - if (m_color == Color::none) + Color::Color color = connectToServer(); + if (color == Color::none) { QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); // TODO: quit application here or sth return; } + /* create our scene */ + m_scene = new SceneHolder(this); + m_scene->setColor(color); + /* call updateMap after m_color ist set! */ createGui(); - updateMap(Util::createDemoMap()); + m_scene->updateMap(Util::createDemoMap()); connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); @@ -27,7 +31,7 @@ MainWidget::MainWidget(QWidget *parent) connect(sendTimer, SIGNAL(timeout()), this, SLOT(sendKeyUpdate())); sendTimer->start(Constants::tick); - qDebug() << "mycolor=" << m_color; + qDebug() << "mycolor=" << m_scene->color(); //TODO: play intro as soon as there are enough players //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); @@ -62,7 +66,7 @@ void MainWidget::createGui() playerLayout->addWidget(new QLabel("", this), 0, 1); playerLayout->addWidget(new QLabel("", this), 1, 1); - if (Color::order[i] == m_color) + if (Color::order[i] == m_scene->color()) scoreLayout->insertWidget(0, scoreBox); else scoreLayout->addWidget(scoreBox); @@ -145,7 +149,7 @@ void MainWidget::tick() ++i; } } - updateMap(map); + m_scene->updateMap(map); updateScore(packet); } 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 @@ class Actor; class MainWidget - : public SceneHolder + : public QWidget { Q_OBJECT @@ -51,6 +51,7 @@ private: bool m_running; QTcpSocket *m_socket; + SceneHolder *m_scene; }; #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 @@ #ifndef PIXMAPITEM__H #define PIXMAPITEM__H +#include "gameentity.h" #include #include -#include "gameentity.h" - class PixmapItem : public QGraphicsObject, public GameEntity { 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 Point::Point(QGraphicsItem *parent) : PixmapItem(parent) { + /* empty object for servers */ + if (Constants::server) + return; + if (pixmap == NULL) pixmap = new QPixmap(":/points"); 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 @@ #include "sceneholder.h" - #include "constants.h" #include "pixmapitem.h" #include "block.h" @@ -8,11 +7,11 @@ #include "point.h" #include "util.h" -SceneHolder::SceneHolder(QWidget* parent) - : QWidget(parent), m_color(Color::none), m_pointsLeft(0) +SceneHolder::SceneHolder(QObject *parent) + : QGraphicsScene(parent), m_color(Color::none), m_pointsLeft(0) { - m_scene = new QGraphicsScene(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height, this); - m_scene->setBackgroundBrush(Qt::black); + setSceneRect(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height); + setBackgroundBrush(Qt::black); visualMap.resize(Constants::map_size.width); for (int i = 0; i < visualMap.size(); ++i) @@ -39,7 +38,7 @@ void SceneHolder::updateMap(const Transmission::map_t& map) /* remove elements (in case it's not an actor) */ if (oldItem != NULL && dynamic_cast(item) == NULL) { - m_scene->removeItem(oldItem); + removeItem(oldItem); visualMap[x][y] = NULL; Actor *actor = NULL; foreach (Actor *i, m_actors) @@ -81,7 +80,6 @@ void SceneHolder::updateMap(const Transmission::map_t& map) item = new BonusPoint(); else if (cur & Transmission::point) { - qDebug() << "new point"; item = new Point(); connect(item, SIGNAL(destroyed()), this, SLOT(decrementPoints())); ++m_pointsLeft; @@ -93,7 +91,7 @@ void SceneHolder::updateMap(const Transmission::map_t& map) { actor = new Actor(color, (color == m_color)); m_actors[color] = actor; - m_scene->addItem(actor); + addItem(actor); actor->setPos(mapPositionToCoord(x, y)); } else @@ -116,13 +114,13 @@ void SceneHolder::updateMap(const Transmission::map_t& map) if (item != NULL) { - m_scene->addItem(item); + addItem(item); item->setPos(mapPositionToCoord(x, y)); PixmapItem* oldItem = visualMap[x][y]; visualMap[x][y] = item; if (oldItem != NULL) { - m_scene->removeItem(item); + removeItem(item); delete oldItem; } } @@ -131,6 +129,16 @@ void SceneHolder::updateMap(const Transmission::map_t& map) } +void SceneHolder::setColor(Color::Color color) +{ + m_color = color; +} + +Color::Color SceneHolder::color() +{ + return m_color; +} + unsigned int SceneHolder::pointsLeft() { 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 @@ #ifndef SCENEHOLDER_H #define SCENEHOLDER_H -#include - #include "constants.h" +#include class PixmapItem; class Actor; class SceneHolder - : public QWidget + : public QGraphicsScene { Q_OBJECT public: - SceneHolder(QWidget* parent = 0); + SceneHolder(QObject *parent = 0); virtual ~SceneHolder() {}; unsigned int pointsLeft(); + void updateMap(const Transmission::map_t& map); + void setColor(Color::Color color = Color::none); + Color::Color color(); private slots: void decrementPoints(); protected: - virtual void updateMap(const Transmission::map_t& map); - /* data conversion */ QPoint mapPositionToCoord(unsigned int x, unsigned int y); QPoint mapPositionToCoord(QPoint point); @@ -42,8 +42,6 @@ protected: /* points left before round ends */ unsigned int m_pointsLeft; - - QGraphicsScene *m_scene; }; #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) { GOOGLE_PROTOBUF_VERIFY_VERSION; - QApplication app(argc, argv); + QApplication app(argc, argv, false); app.setApplicationName("Pacman Server"); app.setWindowIcon(QIcon(":/appicon")); 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 @@ #define SERVER_H #include "sceneholder.h" - -#include #include "actor.h" #include "pacman.pb.h" +#include class QTcpSocket; -- cgit v1.2.3