diff options
| author | totycro <totycro@unknown-horizons.org> | 2011-04-09 13:54:18 +0200 |
|---|---|---|
| committer | totycro <totycro@unknown-horizons.org> | 2011-04-09 13:54:18 +0200 |
| commit | d547dec802f76c346538144f4eacf6d8ca6310c4 (patch) | |
| tree | 50613653be09988bfd1a122496faed93c960de45 | |
| parent | 7e4bc8ece49543533c4e27f5d6bc867c1bada601 (diff) | |
| download | foop-d547dec802f76c346538144f4eacf6d8ca6310c4.tar.gz foop-d547dec802f76c346538144f4eacf6d8ca6310c4.tar.bz2 foop-d547dec802f76c346538144f4eacf6d8ca6310c4.zip | |
Pull up methods from mainwidget to sceneholder for sharing in both client and server
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 179 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.h | 17 | ||||
| -rw-r--r-- | pacman-c++/pacman.pro | 8 | ||||
| -rw-r--r-- | pacman-c++/pacman.server.pro | 35 | ||||
| -rwxr-xr-x | pacman-c++/rebuild-client.sh | 4 | ||||
| -rwxr-xr-x | pacman-c++/rebuild-server.sh | 4 | ||||
| -rw-r--r-- | pacman-c++/sceneholder.cpp | 115 | ||||
| -rw-r--r-- | pacman-c++/sceneholder.h | 37 | ||||
| -rw-r--r-- | pacman-c++/server.cpp | 22 | ||||
| -rw-r--r-- | pacman-c++/server.h | 15 | ||||
| -rw-r--r-- | pacman-c++/util.cpp | 76 | ||||
| -rw-r--r-- | pacman-c++/util.h | 8 |
12 files changed, 332 insertions, 188 deletions
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 @@ | |||
| 6 | #include "constants.h" | 6 | #include "constants.h" |
| 7 | #include "audioplayer.h" | 7 | #include "audioplayer.h" |
| 8 | 8 | ||
| 9 | // temporary | 9 | #include "util.h" |
| 10 | Transmission::map_t createDummyMap() | ||
| 11 | { | ||
| 12 | Transmission::map_t map; | ||
| 13 | map = new Transmission::field_t*[Constants::map_size.width]; | ||
| 14 | for (unsigned int i = 0; i < Constants::map_size.width; ++i) | ||
| 15 | map[i] = new Transmission::field_t[Constants::map_size.height]; | ||
| 16 | |||
| 17 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 18 | { | ||
| 19 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 20 | { | ||
| 21 | Transmission::field_t &cur = map[x][y]; | ||
| 22 | cur = Transmission::none; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | const char *tmpl[] = { | ||
| 28 | " # # ", | ||
| 29 | " #### ###### # #### # # ###### ### ", | ||
| 30 | " # # ", | ||
| 31 | " # ##### # # # # # ### # # # ", | ||
| 32 | " # # # # # # # # # # ## # # ", | ||
| 33 | " # # # # # # # # ### # # # # ", | ||
| 34 | " # # # # # # # # # # # # ## # ", | ||
| 35 | " # # ### ##### # ### # # # ", | ||
| 36 | " ### # ", | ||
| 37 | " # # ### #### #### #### ##### ", | ||
| 38 | " #### # #..# #..# #..# # # ", | ||
| 39 | " # # ### #..# #..# #### # # # # ", | ||
| 40 | " # # # #..# #..# # # ", | ||
| 41 | " # #### # #### #### # # ##### # ", | ||
| 42 | " # # ", | ||
| 43 | " #### ###### # ##### # ####### ### ", | ||
| 44 | " # # " | ||
| 45 | }; | ||
| 46 | |||
| 47 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 48 | { | ||
| 49 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 50 | { | ||
| 51 | Transmission::field_t &cur = map[x][y]; | ||
| 52 | cur = Transmission::none; | ||
| 53 | if (tmpl[y][x] == '#') | ||
| 54 | cur |= Color::none | Transmission::block; | ||
| 55 | /* this is a simple hack to create areas where no | ||
| 56 | * autoplaced points will be placed (see below) | ||
| 57 | */ | ||
| 58 | else if (tmpl[y][x] == '.') | ||
| 59 | cur |= Transmission::point; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | 10 | ||
| 63 | map[0][0] |= Transmission::bonuspoint; | 11 | MainWidget::MainWidget(QWidget *parent) |
| 64 | map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right; | 12 | : SceneHolder(parent), m_currentKey(0), m_running(false) |
| 65 | //map[2][0] |= Color::blue | Transmission::pacman | Transmission::direction_up; | ||
| 66 | //map[3][0] |= Color::green | Transmission::pacman | Transmission::direction_down; | ||
| 67 | |||
| 68 | /* auto place normal points*/ | ||
| 69 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 70 | { | ||
| 71 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 72 | { | ||
| 73 | Transmission::field_t &cur = map[x][y]; | ||
| 74 | if (cur == Transmission::none) | ||
| 75 | cur |= Transmission::point; | ||
| 76 | else if (cur == Transmission::point) | ||
| 77 | cur = Transmission::none; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | return map; | ||
| 82 | } | ||
| 83 | |||
| 84 | MainWidget::MainWidget() | ||
| 85 | : m_currentKey(0), m_running(false) | ||
| 86 | { | 13 | { |
| 87 | visualMap.resize(Constants::map_size.width); | ||
| 88 | for (int i=0; i<visualMap.size(); ++i) { | ||
| 89 | visualMap[i].resize(Constants::map_size.height); | ||
| 90 | } | ||
| 91 | |||
| 92 | createGui(); | 14 | createGui(); |
| 93 | updateMap(createDummyMap()); | 15 | updateMap(createDummyMap()); |
| 94 | 16 | ||
| @@ -129,9 +51,6 @@ void MainWidget::createGui() | |||
| 129 | m_playerScoreLayouts.append(playerLayout); | 51 | m_playerScoreLayouts.append(playerLayout); |
| 130 | } | 52 | } |
| 131 | 53 | ||
| 132 | m_scene = new QGraphicsScene(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height, this); | ||
| 133 | m_scene->setBackgroundBrush(Qt::black); | ||
| 134 | |||
| 135 | QGraphicsView *window = new QGraphicsView(m_scene, this); | 54 | QGraphicsView *window = new QGraphicsView(m_scene, this); |
| 136 | window->setFrameStyle(0); | 55 | window->setFrameStyle(0); |
| 137 | window->setAlignment(Qt::AlignLeft | Qt::AlignTop); | 56 | window->setAlignment(Qt::AlignLeft | Qt::AlignTop); |
| @@ -172,100 +91,10 @@ void MainWidget::updateScore() | |||
| 172 | 91 | ||
| 173 | void MainWidget::updateMap(const Transmission::map_t& map) | 92 | void MainWidget::updateMap(const Transmission::map_t& map) |
| 174 | { | 93 | { |
| 175 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | 94 | SceneHolder::updateMap(map); |
| 176 | { | ||
| 177 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 178 | { | ||
| 179 | const Transmission::field_t &cur = map[x][y]; | ||
| 180 | if (cur == Transmission::none) | ||
| 181 | continue; | ||
| 182 | //qDebug() << "not 0 at x=" << x << ", y=" << y << ", val=" << cur; | ||
| 183 | |||
| 184 | Color::Color color = static_cast<Color::Color>(cur & Transmission::color_mask); | ||
| 185 | //qDebug() << "col=" << color; | ||
| 186 | |||
| 187 | PixmapItem* item = NULL; | ||
| 188 | if (cur & Transmission::block) | ||
| 189 | { | ||
| 190 | unsigned int neighbours = Block::None; | ||
| 191 | // check left side | ||
| 192 | if (x > 0 && map[x - 1][y] & Transmission::block) | ||
| 193 | neighbours |= Block::Left; | ||
| 194 | // check right side | ||
| 195 | if (x < Constants::map_size.width && map[x + 1][y] & Transmission::block) | ||
| 196 | neighbours |= Block::Right; | ||
| 197 | // check upside | ||
| 198 | if (y > 0 && map[x][y - 1] & Transmission::block) | ||
| 199 | neighbours |= Block::Up; | ||
| 200 | // check down side | ||
| 201 | if (y < Constants::map_size.height && map[x][y + 1] & Transmission::block) | ||
| 202 | neighbours |= Block::Down; | ||
| 203 | item = new Block(color, neighbours); | ||
| 204 | } | ||
| 205 | else if (cur & Transmission::bonuspoint) | ||
| 206 | item = new BonusPoint(); | ||
| 207 | else if (cur & Transmission::point) | ||
| 208 | item = new Point(); | ||
| 209 | else if (cur & Transmission::pacman) | ||
| 210 | { | ||
| 211 | Actor *actor = m_actors.value(color, NULL); | ||
| 212 | if (actor == NULL) | ||
| 213 | { | ||
| 214 | //qDebug() << "new actor of col" << color; | ||
| 215 | actor = new Actor(color, (color == Color::red)); //TODO: red = local for testing | ||
| 216 | m_actors[color] = actor; | ||
| 217 | m_scene->addItem(actor); | ||
| 218 | actor->setPos(mapPositionToCoord(x, y)); | ||
| 219 | } | ||
| 220 | |||
| 221 | Actor::Movement direction = Actor::None; | ||
| 222 | switch (cur & Transmission::direction_mask) | ||
| 223 | { | ||
| 224 | case Transmission::direction_none: | ||
| 225 | direction = Actor::None; | ||
| 226 | break; | ||
| 227 | case Transmission::direction_left: | ||
| 228 | direction = Actor::Left; | ||
| 229 | break; | ||
| 230 | case Transmission::direction_right: | ||
| 231 | direction = Actor::Right; | ||
| 232 | break; | ||
| 233 | case Transmission::direction_up: | ||
| 234 | direction = Actor::Up; | ||
| 235 | break; | ||
| 236 | case Transmission::direction_down: | ||
| 237 | direction = Actor::Down; | ||
| 238 | break; | ||
| 239 | default: | ||
| 240 | Q_ASSERT(false); | ||
| 241 | } | ||
| 242 | //actor->move(direction, mapPositionToCoord(x, y)); | ||
| 243 | } | ||
| 244 | else | ||
| 245 | Q_ASSERT(false); | ||
| 246 | |||
| 247 | if (item != NULL) | ||
| 248 | { | ||
| 249 | m_scene->addItem(item); | ||
| 250 | item->setPos(mapPositionToCoord(x, y)); | ||
| 251 | PixmapItem* oldItem = visualMap[x][y]; | ||
| 252 | visualMap[x][y] = item; | ||
| 253 | if (oldItem != NULL) | ||
| 254 | { | ||
| 255 | m_scene->removeItem(item); | ||
| 256 | delete oldItem; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | updateScore(); | 95 | updateScore(); |
| 263 | } | 96 | } |
| 264 | 97 | ||
| 265 | QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y) | ||
| 266 | { | ||
| 267 | return QPoint(x * Constants::field_size.width, y * Constants::field_size.height); | ||
| 268 | } | ||
| 269 | 98 | ||
| 270 | Transmission::field_t MainWidget::translateKey(int key) | 99 | Transmission::field_t MainWidget::translateKey(int key) |
| 271 | { | 100 | { |
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 @@ | |||
| 1 | #ifndef MAINWIDGET_H | 1 | #ifndef MAINWIDGET_H |
| 2 | #define MAINWIDGET_H | 2 | #define MAINWIDGET_H |
| 3 | 3 | ||
| 4 | #include "sceneholder.h" | ||
| 5 | |||
| 4 | #include "constants.h" | 6 | #include "constants.h" |
| 5 | #include "pixmapitem.h" | 7 | #include "pixmapitem.h" |
| 6 | #include <QtGui> | 8 | #include <QtGui> |
| @@ -10,21 +12,22 @@ | |||
| 10 | class Actor; | 12 | class Actor; |
| 11 | 13 | ||
| 12 | class MainWidget | 14 | class MainWidget |
| 13 | : public QWidget | 15 | : public SceneHolder |
| 14 | { | 16 | { |
| 15 | Q_OBJECT | 17 | Q_OBJECT |
| 16 | 18 | ||
| 17 | public: | 19 | public: |
| 18 | MainWidget(); | 20 | MainWidget(QWidget *parent = 0); |
| 19 | 21 | ||
| 20 | protected: | 22 | protected: |
| 21 | // handling of current key | 23 | // handling of current key |
| 22 | virtual void keyPressEvent(QKeyEvent* ); | 24 | virtual void keyPressEvent(QKeyEvent* ); |
| 23 | virtual void keyReleaseEvent(QKeyEvent* ); | 25 | virtual void keyReleaseEvent(QKeyEvent* ); |
| 24 | 26 | ||
| 27 | virtual void updateMap(const Transmission::map_t& map); | ||
| 28 | |||
| 25 | private: | 29 | private: |
| 26 | void createGui(); | 30 | void createGui(); |
| 27 | void updateMap(const Transmission::map_t& map); | ||
| 28 | void updateScore(); | 31 | void updateScore(); |
| 29 | bool isRunning(); | 32 | bool isRunning(); |
| 30 | 33 | ||
| @@ -34,17 +37,9 @@ private slots: | |||
| 34 | void tick(); | 37 | void tick(); |
| 35 | 38 | ||
| 36 | private: | 39 | private: |
| 37 | QVector< QVector<PixmapItem *> > visualMap; | ||
| 38 | |||
| 39 | // data conversion | ||
| 40 | QPoint mapPositionToCoord(unsigned int x, unsigned int y); | ||
| 41 | 40 | ||
| 42 | // GUI elements needed in the progress of the game | 41 | // GUI elements needed in the progress of the game |
| 43 | QList<QGridLayout*> m_playerScoreLayouts; | 42 | QList<QGridLayout*> m_playerScoreLayouts; |
| 44 | QGraphicsScene *m_scene; | ||
| 45 | |||
| 46 | // map of actors in order to keep track of those instances | ||
| 47 | QMap<Color::Color, Actor*> m_actors; | ||
| 48 | 43 | ||
| 49 | // key currently pressed by user | 44 | // key currently pressed by user |
| 50 | Transmission::field_t m_currentKey; | 45 | 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 \ | |||
| 7 | bonuspoint.cpp \ | 7 | bonuspoint.cpp \ |
| 8 | mainwidget.cpp \ | 8 | mainwidget.cpp \ |
| 9 | point.cpp \ | 9 | point.cpp \ |
| 10 | audioplayer.cpp | 10 | audioplayer.cpp \ |
| 11 | sceneholder.cpp \ | ||
| 12 | util.cpp | ||
| 11 | HEADERS += pixmapitem.h \ | 13 | HEADERS += pixmapitem.h \ |
| 12 | actor.h \ | 14 | actor.h \ |
| 13 | animationmanager.h \ | 15 | animationmanager.h \ |
| @@ -17,7 +19,9 @@ HEADERS += pixmapitem.h \ | |||
| 17 | mainwidget.h \ | 19 | mainwidget.h \ |
| 18 | constants.h \ | 20 | constants.h \ |
| 19 | point.h \ | 21 | point.h \ |
| 20 | audioplayer.h | 22 | audioplayer.h \ |
| 23 | sceneholder.h \ | ||
| 24 | util.h | ||
| 21 | RESOURCES += pacman.qrc | 25 | RESOURCES += pacman.qrc |
| 22 | 26 | ||
| 23 | OBJECTS_DIR = .obj | 27 | 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 @@ | |||
| 1 | QT += phonon | ||
| 2 | SOURCES += pixmapitem.cpp \ | ||
| 3 | actor.cpp \ | ||
| 4 | animationmanager.cpp \ | ||
| 5 | block.cpp \ | ||
| 6 | server.cpp \ | ||
| 7 | bonuspoint.cpp \ | ||
| 8 | mainwidget.cpp \ | ||
| 9 | point.cpp \ | ||
| 10 | audioplayer.cpp \ | ||
| 11 | sceneholder.cpp \ | ||
| 12 | util.cpp | ||
| 13 | HEADERS += pixmapitem.h \ | ||
| 14 | actor.h \ | ||
| 15 | animationmanager.h \ | ||
| 16 | block.h \ | ||
| 17 | server.h \ | ||
| 18 | bonuspoint.h \ | ||
| 19 | mainwidget.h \ | ||
| 20 | constants.h \ | ||
| 21 | point.h \ | ||
| 22 | audioplayer.h \ | ||
| 23 | sceneholder.h \ | ||
| 24 | util.h | ||
| 25 | RESOURCES += pacman.qrc | ||
| 26 | |||
| 27 | OBJECTS_DIR = .obj | ||
| 28 | MOC_DIR = .moc | ||
| 29 | RC_FILE = pacman.rc | ||
| 30 | |||
| 31 | OTHER_FILES += \ | ||
| 32 | style.qss \ | ||
| 33 | pacman.rc | ||
| 34 | |||
| 35 | 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 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | make distclean | ||
| 3 | qmake CONFIG+=DEBUG pacman.pro | ||
| 4 | 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 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | make distclean | ||
| 3 | qmake CONFIG+=DEBUG pacman.server.pro | ||
| 4 | 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 @@ | |||
| 1 | #include "sceneholder.h" | ||
| 2 | |||
| 3 | #include "constants.h" | ||
| 4 | #include "pixmapitem.h" | ||
| 5 | #include "block.h" | ||
| 6 | #include "actor.h" | ||
| 7 | #include "bonuspoint.h" | ||
| 8 | #include "point.h" | ||
| 9 | |||
| 10 | SceneHolder::SceneHolder(QWidget* parent): QWidget(parent) | ||
| 11 | { | ||
| 12 | m_scene = new QGraphicsScene(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height, this); | ||
| 13 | m_scene->setBackgroundBrush(Qt::black); | ||
| 14 | |||
| 15 | visualMap.resize(Constants::map_size.width); | ||
| 16 | for (int i=0; i<visualMap.size(); ++i) { | ||
| 17 | visualMap[i].resize(Constants::map_size.height); | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | void SceneHolder::updateMap(const Transmission::map_t& map) | ||
| 22 | { | ||
| 23 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 24 | { | ||
| 25 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 26 | { | ||
| 27 | const Transmission::field_t &cur = map[x][y]; | ||
| 28 | if (cur == Transmission::none) | ||
| 29 | continue; | ||
| 30 | //qDebug() << "not 0 at x=" << x << ", y=" << y << ", val=" << cur; | ||
| 31 | |||
| 32 | Color::Color color = static_cast<Color::Color>(cur & Transmission::color_mask); | ||
| 33 | //qDebug() << "col=" << color; | ||
| 34 | |||
| 35 | PixmapItem* item = NULL; | ||
| 36 | if (cur & Transmission::block) | ||
| 37 | { | ||
| 38 | unsigned int neighbours = Block::None; | ||
| 39 | // check left side | ||
| 40 | if (x > 0 && map[x - 1][y] & Transmission::block) | ||
| 41 | neighbours |= Block::Left; | ||
| 42 | // check right side | ||
| 43 | if (x < Constants::map_size.width && map[x + 1][y] & Transmission::block) | ||
| 44 | neighbours |= Block::Right; | ||
| 45 | // check upside | ||
| 46 | if (y > 0 && map[x][y - 1] & Transmission::block) | ||
| 47 | neighbours |= Block::Up; | ||
| 48 | // check down side | ||
| 49 | if (y < Constants::map_size.height && map[x][y + 1] & Transmission::block) | ||
| 50 | neighbours |= Block::Down; | ||
| 51 | item = new Block(color, neighbours); | ||
| 52 | } | ||
| 53 | else if (cur & Transmission::bonuspoint) | ||
| 54 | item = new BonusPoint(); | ||
| 55 | else if (cur & Transmission::point) | ||
| 56 | item = new Point(); | ||
| 57 | else if (cur & Transmission::pacman) | ||
| 58 | { | ||
| 59 | Actor *actor = m_actors.value(color, NULL); | ||
| 60 | if (actor == NULL) | ||
| 61 | { | ||
| 62 | //qDebug() << "new actor of col" << color; | ||
| 63 | actor = new Actor(color, (color == Color::red)); //TODO: red = local for testing | ||
| 64 | m_actors[color] = actor; | ||
| 65 | m_scene->addItem(actor); | ||
| 66 | actor->setPos(mapPositionToCoord(x, y)); | ||
| 67 | } | ||
| 68 | |||
| 69 | Actor::Movement direction = Actor::None; | ||
| 70 | switch (cur & Transmission::direction_mask) | ||
| 71 | { | ||
| 72 | case Transmission::direction_none: | ||
| 73 | direction = Actor::None; | ||
| 74 | break; | ||
| 75 | case Transmission::direction_left: | ||
| 76 | direction = Actor::Left; | ||
| 77 | break; | ||
| 78 | case Transmission::direction_right: | ||
| 79 | direction = Actor::Right; | ||
| 80 | break; | ||
| 81 | case Transmission::direction_up: | ||
| 82 | direction = Actor::Up; | ||
| 83 | break; | ||
| 84 | case Transmission::direction_down: | ||
| 85 | direction = Actor::Down; | ||
| 86 | break; | ||
| 87 | default: | ||
| 88 | Q_ASSERT(false); | ||
| 89 | } | ||
| 90 | //actor->move(direction, mapPositionToCoord(x, y)); | ||
| 91 | } | ||
| 92 | else | ||
| 93 | Q_ASSERT(false); | ||
| 94 | |||
| 95 | if (item != NULL) | ||
| 96 | { | ||
| 97 | m_scene->addItem(item); | ||
| 98 | item->setPos(mapPositionToCoord(x, y)); | ||
| 99 | PixmapItem* oldItem = visualMap[x][y]; | ||
| 100 | visualMap[x][y] = item; | ||
| 101 | if (oldItem != NULL) | ||
| 102 | { | ||
| 103 | m_scene->removeItem(item); | ||
| 104 | delete oldItem; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | QPoint SceneHolder::mapPositionToCoord(unsigned int x, unsigned int y) | ||
| 113 | { | ||
| 114 | return QPoint(x * Constants::field_size.width, y * Constants::field_size.height); | ||
| 115 | } \ 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 @@ | |||
| 1 | #ifndef SCENEHOLDER_H | ||
| 2 | #define SCENEHOLDER_H | ||
| 3 | |||
| 4 | #include <QtGui> | ||
| 5 | |||
| 6 | #include "constants.h" | ||
| 7 | |||
| 8 | class PixmapItem; | ||
| 9 | class Actor; | ||
| 10 | |||
| 11 | class SceneHolder | ||
| 12 | : public QWidget | ||
| 13 | { | ||
| 14 | Q_OBJECT | ||
| 15 | |||
| 16 | public: | ||
| 17 | SceneHolder(QWidget* parent = 0); | ||
| 18 | virtual ~SceneHolder() {}; | ||
| 19 | |||
| 20 | protected: | ||
| 21 | |||
| 22 | virtual void updateMap(const Transmission::map_t& map); | ||
| 23 | |||
| 24 | // data conversion | ||
| 25 | QPoint mapPositionToCoord(unsigned int x, unsigned int y); | ||
| 26 | |||
| 27 | // map of all pixmap instances | ||
| 28 | QVector< QVector<PixmapItem *> > visualMap; | ||
| 29 | |||
| 30 | // map of actors in order to keep track of those instances | ||
| 31 | QMap<Color::Color, Actor*> m_actors; | ||
| 32 | |||
| 33 | QGraphicsScene *m_scene; | ||
| 34 | |||
| 35 | }; | ||
| 36 | |||
| 37 | #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 @@ | |||
| 1 | #include "server.h" | ||
| 2 | |||
| 3 | #include "util.h" | ||
| 4 | |||
| 5 | Server::Server(QWidget *parent) | ||
| 6 | : SceneHolder(parent) | ||
| 7 | { | ||
| 8 | updateMap(createDummyMap()); | ||
| 9 | } | ||
| 10 | |||
| 11 | int main(int argc, char ** argv) | ||
| 12 | { | ||
| 13 | QApplication app(argc, argv); | ||
| 14 | app.setApplicationName("Pacman Server"); | ||
| 15 | app.setWindowIcon(QIcon(":/appicon")); | ||
| 16 | |||
| 17 | qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); | ||
| 18 | |||
| 19 | Server Server; | ||
| 20 | |||
| 21 | return app.exec(); | ||
| 22 | } | ||
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 @@ | |||
| 1 | #ifndef SERVER_H | ||
| 2 | #define SERVER_H | ||
| 3 | |||
| 4 | #include "sceneholder.h" | ||
| 5 | #include <QtGui> | ||
| 6 | |||
| 7 | class Server | ||
| 8 | : public SceneHolder | ||
| 9 | { | ||
| 10 | Q_OBJECT | ||
| 11 | public: | ||
| 12 | Server(QWidget *parent = 0); | ||
| 13 | }; | ||
| 14 | |||
| 15 | #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 @@ | |||
| 1 | #include "util.h" | ||
| 2 | |||
| 3 | // temporary | ||
| 4 | Transmission::map_t createDummyMap() | ||
| 5 | { | ||
| 6 | Transmission::map_t map; | ||
| 7 | map = new Transmission::field_t*[Constants::map_size.width]; | ||
| 8 | for (unsigned int i = 0; i < Constants::map_size.width; ++i) | ||
| 9 | map[i] = new Transmission::field_t[Constants::map_size.height]; | ||
| 10 | |||
| 11 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 12 | { | ||
| 13 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 14 | { | ||
| 15 | Transmission::field_t &cur = map[x][y]; | ||
| 16 | cur = Transmission::none; | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | |||
| 21 | const char *tmpl[] = { | ||
| 22 | " # # ", | ||
| 23 | " #### ###### # #### # # ###### ### ", | ||
| 24 | " # # ", | ||
| 25 | " # ##### # # # # # ### # # # ", | ||
| 26 | " # # # # # # # # # # ## # # ", | ||
| 27 | " # # # # # # # # ### # # # # ", | ||
| 28 | " # # # # # # # # # # # # ## # ", | ||
| 29 | " # # ### ##### # ### # # # ", | ||
| 30 | " ### # ", | ||
| 31 | " # # ### #### #### #### ##### ", | ||
| 32 | " #### # #..# #..# #..# # # ", | ||
| 33 | " # # ### #..# #..# #### # # # # ", | ||
| 34 | " # # # #..# #..# # # ", | ||
| 35 | " # #### # #### #### # # ##### # ", | ||
| 36 | " # # ", | ||
| 37 | " #### ###### # ##### # ####### ### ", | ||
| 38 | " # # " | ||
| 39 | }; | ||
| 40 | |||
| 41 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 42 | { | ||
| 43 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 44 | { | ||
| 45 | Transmission::field_t &cur = map[x][y]; | ||
| 46 | cur = Transmission::none; | ||
| 47 | if (tmpl[y][x] == '#') | ||
| 48 | cur |= Color::none | Transmission::block; | ||
| 49 | /* this is a simple hack to create areas where no | ||
| 50 | * autoplaced points will be placed (see below) | ||
| 51 | */ | ||
| 52 | else if (tmpl[y][x] == '.') | ||
| 53 | cur |= Transmission::point; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | map[0][0] |= Transmission::bonuspoint; | ||
| 58 | map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right; | ||
| 59 | //map[2][0] |= Color::blue | Transmission::pacman | Transmission::direction_up; | ||
| 60 | //map[3][0] |= Color::green | Transmission::pacman | Transmission::direction_down; | ||
| 61 | |||
| 62 | /* auto place normal points*/ | ||
| 63 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 64 | { | ||
| 65 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 66 | { | ||
| 67 | Transmission::field_t &cur = map[x][y]; | ||
| 68 | if (cur == Transmission::none) | ||
| 69 | cur |= Transmission::point; | ||
| 70 | else if (cur == Transmission::point) | ||
| 71 | cur = Transmission::none; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | return map; | ||
| 76 | } | ||
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 @@ | |||
| 1 | #ifndef UTIL_H | ||
| 2 | #define UTIL_H | ||
| 3 | |||
| 4 | #include "constants.h" | ||
| 5 | // temporary | ||
| 6 | Transmission::map_t createDummyMap(); | ||
| 7 | |||
| 8 | #endif // UTIL_H \ No newline at end of file | ||
