From d547dec802f76c346538144f4eacf6d8ca6310c4 Mon Sep 17 00:00:00 2001 From: totycro Date: Sat, 9 Apr 2011 13:54:18 +0200 Subject: Pull up methods from mainwidget to sceneholder for sharing in both client and server --- pacman-c++/mainwidget.cpp | 179 +------------------------------------------ pacman-c++/mainwidget.h | 17 ++-- pacman-c++/pacman.pro | 8 +- pacman-c++/pacman.server.pro | 35 +++++++++ pacman-c++/rebuild-client.sh | 4 + pacman-c++/rebuild-server.sh | 4 + pacman-c++/sceneholder.cpp | 115 +++++++++++++++++++++++++++ pacman-c++/sceneholder.h | 37 +++++++++ pacman-c++/server.cpp | 22 ++++++ pacman-c++/server.h | 15 ++++ pacman-c++/util.cpp | 76 ++++++++++++++++++ pacman-c++/util.h | 8 ++ 12 files changed, 332 insertions(+), 188 deletions(-) create mode 100644 pacman-c++/pacman.server.pro create mode 100755 pacman-c++/rebuild-client.sh create mode 100755 pacman-c++/rebuild-server.sh create mode 100644 pacman-c++/sceneholder.cpp create mode 100644 pacman-c++/sceneholder.h create mode 100644 pacman-c++/server.cpp create mode 100644 pacman-c++/server.h create mode 100644 pacman-c++/util.cpp create mode 100644 pacman-c++/util.h diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 63280f2..34ff63e 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -6,89 +6,11 @@ #include "constants.h" #include "audioplayer.h" -// temporary -Transmission::map_t createDummyMap() -{ - Transmission::map_t map; - map = new Transmission::field_t*[Constants::map_size.width]; - for (unsigned int i = 0; i < Constants::map_size.width; ++i) - map[i] = new Transmission::field_t[Constants::map_size.height]; - - for (unsigned int x = 0; x < Constants::map_size.width; ++x) - { - for (unsigned int y = 0; y < Constants::map_size.height; ++y) - { - Transmission::field_t &cur = map[x][y]; - cur = Transmission::none; - } - } - - - const char *tmpl[] = { - " # # ", - " #### ###### # #### # # ###### ### ", - " # # ", - " # ##### # # # # # ### # # # ", - " # # # # # # # # # # ## # # ", - " # # # # # # # # ### # # # # ", - " # # # # # # # # # # # # ## # ", - " # # ### ##### # ### # # # ", - " ### # ", - " # # ### #### #### #### ##### ", - " #### # #..# #..# #..# # # ", - " # # ### #..# #..# #### # # # # ", - " # # # #..# #..# # # ", - " # #### # #### #### # # ##### # ", - " # # ", - " #### ###### # ##### # ####### ### ", - " # # " - }; - - for (unsigned int x = 0; x < Constants::map_size.width; ++x) - { - for (unsigned int y = 0; y < Constants::map_size.height; ++y) - { - Transmission::field_t &cur = map[x][y]; - cur = Transmission::none; - if (tmpl[y][x] == '#') - cur |= Color::none | Transmission::block; - /* this is a simple hack to create areas where no - * autoplaced points will be placed (see below) - */ - else if (tmpl[y][x] == '.') - cur |= Transmission::point; - } - } +#include "util.h" - map[0][0] |= Transmission::bonuspoint; - map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right; - //map[2][0] |= Color::blue | Transmission::pacman | Transmission::direction_up; - //map[3][0] |= Color::green | Transmission::pacman | Transmission::direction_down; - - /* auto place normal points*/ - for (unsigned int x = 0; x < Constants::map_size.width; ++x) - { - for (unsigned int y = 0; y < Constants::map_size.height; ++y) - { - Transmission::field_t &cur = map[x][y]; - if (cur == Transmission::none) - cur |= Transmission::point; - else if (cur == Transmission::point) - cur = Transmission::none; - } - } - - return map; -} - -MainWidget::MainWidget() - : m_currentKey(0), m_running(false) +MainWidget::MainWidget(QWidget *parent) + : SceneHolder(parent), m_currentKey(0), m_running(false) { - visualMap.resize(Constants::map_size.width); - for (int i=0; isetBackgroundBrush(Qt::black); - QGraphicsView *window = new QGraphicsView(m_scene, this); window->setFrameStyle(0); window->setAlignment(Qt::AlignLeft | Qt::AlignTop); @@ -172,100 +91,10 @@ void MainWidget::updateScore() void MainWidget::updateMap(const Transmission::map_t& map) { - for (unsigned int x = 0; x < Constants::map_size.width; ++x) - { - for (unsigned int y = 0; y < Constants::map_size.height; ++y) - { - const Transmission::field_t &cur = map[x][y]; - if (cur == Transmission::none) - continue; - //qDebug() << "not 0 at x=" << x << ", y=" << y << ", val=" << cur; - - Color::Color color = static_cast(cur & Transmission::color_mask); - //qDebug() << "col=" << color; - - PixmapItem* item = NULL; - if (cur & Transmission::block) - { - unsigned int neighbours = Block::None; - // check left side - if (x > 0 && map[x - 1][y] & Transmission::block) - neighbours |= Block::Left; - // check right side - if (x < Constants::map_size.width && map[x + 1][y] & Transmission::block) - neighbours |= Block::Right; - // check upside - if (y > 0 && map[x][y - 1] & Transmission::block) - neighbours |= Block::Up; - // check down side - if (y < Constants::map_size.height && map[x][y + 1] & Transmission::block) - neighbours |= Block::Down; - item = new Block(color, neighbours); - } - else if (cur & Transmission::bonuspoint) - item = new BonusPoint(); - else if (cur & Transmission::point) - item = new Point(); - else if (cur & Transmission::pacman) - { - Actor *actor = m_actors.value(color, NULL); - if (actor == NULL) - { - //qDebug() << "new actor of col" << color; - actor = new Actor(color, (color == Color::red)); //TODO: red = local for testing - m_actors[color] = actor; - m_scene->addItem(actor); - actor->setPos(mapPositionToCoord(x, y)); - } - - Actor::Movement direction = Actor::None; - switch (cur & Transmission::direction_mask) - { - case Transmission::direction_none: - direction = Actor::None; - break; - case Transmission::direction_left: - direction = Actor::Left; - break; - case Transmission::direction_right: - direction = Actor::Right; - break; - case Transmission::direction_up: - direction = Actor::Up; - break; - case Transmission::direction_down: - direction = Actor::Down; - break; - default: - Q_ASSERT(false); - } - //actor->move(direction, mapPositionToCoord(x, y)); - } - else - Q_ASSERT(false); - - if (item != NULL) - { - m_scene->addItem(item); - item->setPos(mapPositionToCoord(x, y)); - PixmapItem* oldItem = visualMap[x][y]; - visualMap[x][y] = item; - if (oldItem != NULL) - { - m_scene->removeItem(item); - delete oldItem; - } - } - } - } - + SceneHolder::updateMap(map); updateScore(); } -QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y) -{ - return QPoint(x * Constants::field_size.width, y * Constants::field_size.height); -} Transmission::field_t MainWidget::translateKey(int key) { diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index e7d146f..27e901f 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h @@ -1,6 +1,8 @@ #ifndef MAINWIDGET_H #define MAINWIDGET_H +#include "sceneholder.h" + #include "constants.h" #include "pixmapitem.h" #include @@ -10,21 +12,22 @@ class Actor; class MainWidget - : public QWidget + : public SceneHolder { Q_OBJECT public: - MainWidget(); + MainWidget(QWidget *parent = 0); protected: // handling of current key virtual void keyPressEvent(QKeyEvent* ); virtual void keyReleaseEvent(QKeyEvent* ); + virtual void updateMap(const Transmission::map_t& map); + private: void createGui(); - void updateMap(const Transmission::map_t& map); void updateScore(); bool isRunning(); @@ -34,17 +37,9 @@ private slots: void tick(); private: - QVector< QVector > visualMap; - - // data conversion - QPoint mapPositionToCoord(unsigned int x, unsigned int y); // GUI elements needed in the progress of the game QList m_playerScoreLayouts; - QGraphicsScene *m_scene; - - // map of actors in order to keep track of those instances - QMap m_actors; // key currently pressed by user Transmission::field_t m_currentKey; diff --git a/pacman-c++/pacman.pro b/pacman-c++/pacman.pro index f95625b..a1dddf6 100644 --- a/pacman-c++/pacman.pro +++ b/pacman-c++/pacman.pro @@ -7,7 +7,9 @@ SOURCES += pixmapitem.cpp \ bonuspoint.cpp \ mainwidget.cpp \ point.cpp \ - audioplayer.cpp + audioplayer.cpp \ + sceneholder.cpp \ + util.cpp HEADERS += pixmapitem.h \ actor.h \ animationmanager.h \ @@ -17,7 +19,9 @@ HEADERS += pixmapitem.h \ mainwidget.h \ constants.h \ point.h \ - audioplayer.h + audioplayer.h \ + sceneholder.h \ + util.h RESOURCES += pacman.qrc OBJECTS_DIR = .obj diff --git a/pacman-c++/pacman.server.pro b/pacman-c++/pacman.server.pro new file mode 100644 index 0000000..ae9dd2e --- /dev/null +++ b/pacman-c++/pacman.server.pro @@ -0,0 +1,35 @@ +QT += phonon +SOURCES += pixmapitem.cpp \ + actor.cpp \ + animationmanager.cpp \ + block.cpp \ + server.cpp \ + bonuspoint.cpp \ + mainwidget.cpp \ + point.cpp \ + audioplayer.cpp \ + sceneholder.cpp \ + util.cpp +HEADERS += pixmapitem.h \ + actor.h \ + animationmanager.h \ + block.h \ + server.h \ + bonuspoint.h \ + mainwidget.h \ + constants.h \ + point.h \ + audioplayer.h \ + sceneholder.h \ + util.h +RESOURCES += pacman.qrc + +OBJECTS_DIR = .obj +MOC_DIR = .moc +RC_FILE = pacman.rc + +OTHER_FILES += \ + style.qss \ + pacman.rc + +DEFINES += SERVER diff --git a/pacman-c++/rebuild-client.sh b/pacman-c++/rebuild-client.sh new file mode 100755 index 0000000..6cbcaed --- /dev/null +++ b/pacman-c++/rebuild-client.sh @@ -0,0 +1,4 @@ +#!/bin/sh +make distclean +qmake CONFIG+=DEBUG pacman.pro +make diff --git a/pacman-c++/rebuild-server.sh b/pacman-c++/rebuild-server.sh new file mode 100755 index 0000000..b573a2e --- /dev/null +++ b/pacman-c++/rebuild-server.sh @@ -0,0 +1,4 @@ +#!/bin/sh +make distclean +qmake CONFIG+=DEBUG pacman.server.pro +make diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp new file mode 100644 index 0000000..83025d5 --- /dev/null +++ b/pacman-c++/sceneholder.cpp @@ -0,0 +1,115 @@ +#include "sceneholder.h" + +#include "constants.h" +#include "pixmapitem.h" +#include "block.h" +#include "actor.h" +#include "bonuspoint.h" +#include "point.h" + +SceneHolder::SceneHolder(QWidget* parent): QWidget(parent) +{ + m_scene = new QGraphicsScene(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height, this); + m_scene->setBackgroundBrush(Qt::black); + + visualMap.resize(Constants::map_size.width); + for (int i=0; iaddItem(actor); + actor->setPos(mapPositionToCoord(x, y)); + } + + Actor::Movement direction = Actor::None; + switch (cur & Transmission::direction_mask) + { + case Transmission::direction_none: + direction = Actor::None; + break; + case Transmission::direction_left: + direction = Actor::Left; + break; + case Transmission::direction_right: + direction = Actor::Right; + break; + case Transmission::direction_up: + direction = Actor::Up; + break; + case Transmission::direction_down: + direction = Actor::Down; + break; + default: + Q_ASSERT(false); + } + //actor->move(direction, mapPositionToCoord(x, y)); + } + else + Q_ASSERT(false); + + if (item != NULL) + { + m_scene->addItem(item); + item->setPos(mapPositionToCoord(x, y)); + PixmapItem* oldItem = visualMap[x][y]; + visualMap[x][y] = item; + if (oldItem != NULL) + { + m_scene->removeItem(item); + delete oldItem; + } + } + } + } + +} + +QPoint SceneHolder::mapPositionToCoord(unsigned int x, unsigned int y) +{ + return QPoint(x * Constants::field_size.width, y * Constants::field_size.height); +} \ No newline at end of file diff --git a/pacman-c++/sceneholder.h b/pacman-c++/sceneholder.h new file mode 100644 index 0000000..d69e07e --- /dev/null +++ b/pacman-c++/sceneholder.h @@ -0,0 +1,37 @@ +#ifndef SCENEHOLDER_H +#define SCENEHOLDER_H + +#include + +#include "constants.h" + +class PixmapItem; +class Actor; + +class SceneHolder + : public QWidget +{ + Q_OBJECT + +public: + SceneHolder(QWidget* parent = 0); + virtual ~SceneHolder() {}; + +protected: + + virtual void updateMap(const Transmission::map_t& map); + + // data conversion + QPoint mapPositionToCoord(unsigned int x, unsigned int y); + + // map of all pixmap instances + QVector< QVector > visualMap; + + // map of actors in order to keep track of those instances + QMap m_actors; + + QGraphicsScene *m_scene; + +}; + +#endif // SCENEHOLDER_H \ No newline at end of file diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp new file mode 100644 index 0000000..89cf356 --- /dev/null +++ b/pacman-c++/server.cpp @@ -0,0 +1,22 @@ +#include "server.h" + +#include "util.h" + +Server::Server(QWidget *parent) + : SceneHolder(parent) +{ + updateMap(createDummyMap()); +} + +int main(int argc, char ** argv) +{ + QApplication app(argc, argv); + app.setApplicationName("Pacman Server"); + app.setWindowIcon(QIcon(":/appicon")); + + qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); + + Server Server; + + return app.exec(); +} diff --git a/pacman-c++/server.h b/pacman-c++/server.h new file mode 100644 index 0000000..4be0cd8 --- /dev/null +++ b/pacman-c++/server.h @@ -0,0 +1,15 @@ +#ifndef SERVER_H +#define SERVER_H + +#include "sceneholder.h" +#include + +class Server + : public SceneHolder +{ + Q_OBJECT +public: + Server(QWidget *parent = 0); +}; + +#endif // SERVER_H diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp new file mode 100644 index 0000000..185b0a6 --- /dev/null +++ b/pacman-c++/util.cpp @@ -0,0 +1,76 @@ +#include "util.h" + +// temporary +Transmission::map_t createDummyMap() +{ + Transmission::map_t map; + map = new Transmission::field_t*[Constants::map_size.width]; + for (unsigned int i = 0; i < Constants::map_size.width; ++i) + map[i] = new Transmission::field_t[Constants::map_size.height]; + + for (unsigned int x = 0; x < Constants::map_size.width; ++x) + { + for (unsigned int y = 0; y < Constants::map_size.height; ++y) + { + Transmission::field_t &cur = map[x][y]; + cur = Transmission::none; + } + } + + + const char *tmpl[] = { + " # # ", + " #### ###### # #### # # ###### ### ", + " # # ", + " # ##### # # # # # ### # # # ", + " # # # # # # # # # # ## # # ", + " # # # # # # # # ### # # # # ", + " # # # # # # # # # # # # ## # ", + " # # ### ##### # ### # # # ", + " ### # ", + " # # ### #### #### #### ##### ", + " #### # #..# #..# #..# # # ", + " # # ### #..# #..# #### # # # # ", + " # # # #..# #..# # # ", + " # #### # #### #### # # ##### # ", + " # # ", + " #### ###### # ##### # ####### ### ", + " # # " + }; + + for (unsigned int x = 0; x < Constants::map_size.width; ++x) + { + for (unsigned int y = 0; y < Constants::map_size.height; ++y) + { + Transmission::field_t &cur = map[x][y]; + cur = Transmission::none; + if (tmpl[y][x] == '#') + cur |= Color::none | Transmission::block; + /* this is a simple hack to create areas where no + * autoplaced points will be placed (see below) + */ + else if (tmpl[y][x] == '.') + cur |= Transmission::point; + } + } + + map[0][0] |= Transmission::bonuspoint; + map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right; + //map[2][0] |= Color::blue | Transmission::pacman | Transmission::direction_up; + //map[3][0] |= Color::green | Transmission::pacman | Transmission::direction_down; + + /* auto place normal points*/ + for (unsigned int x = 0; x < Constants::map_size.width; ++x) + { + for (unsigned int y = 0; y < Constants::map_size.height; ++y) + { + Transmission::field_t &cur = map[x][y]; + if (cur == Transmission::none) + cur |= Transmission::point; + else if (cur == Transmission::point) + cur = Transmission::none; + } + } + + return map; +} diff --git a/pacman-c++/util.h b/pacman-c++/util.h new file mode 100644 index 0000000..34ddd31 --- /dev/null +++ b/pacman-c++/util.h @@ -0,0 +1,8 @@ +#ifndef UTIL_H +#define UTIL_H + +#include "constants.h" +// temporary +Transmission::map_t createDummyMap(); + +#endif // UTIL_H \ No newline at end of file -- cgit v1.2.3