From 6f13016fe0742df2abfbf4ee08b3a0f27211a993 Mon Sep 17 00:00:00 2001 From: Shamus Hammons Date: Tue, 5 Aug 2014 10:17:56 -0500 Subject: [PATCH] Moved level storage to global location. --- src/boards.cpp | 7 +++++++ src/boards.h | 20 ++++++++++++++++++++ src/editorwidget.h | 22 +--------------------- src/editorwindow.cpp | 14 +++++++------- src/gamewidget.cpp | 9 ++++++--- src/mainwin.cpp | 19 +++++++++++++------ src/mainwin.h | 1 + 7 files changed, 55 insertions(+), 37 deletions(-) diff --git a/src/boards.cpp b/src/boards.cpp index 8cf5ed7..8a2fbbe 100644 --- a/src/boards.cpp +++ b/src/boards.cpp @@ -11,6 +11,13 @@ // JLH 03/03/2014 Created this file // +#include "boards.h" + + +// Level storage... +std::vector levelStorage; + + // // We use anonymous structs to create these, to keep things simple. :-) // diff --git a/src/boards.h b/src/boards.h index e14b564..4c01320 100644 --- a/src/boards.h +++ b/src/boards.h @@ -1,6 +1,9 @@ #ifndef __BOARDS_H__ #define __BOARDS_H__ +#include +#include + // Okay, this is ugly but works and I can't think of any better way to handle // this. So what we do when we pass the anonymous structs into a function is // pass them as a (void *) and then cast them as type (Board *) in order to @@ -12,8 +15,25 @@ struct Board { unsigned char state[]; // Board data }; +#define BOARDSIZE 128 + +struct Point +{ + Point(int xx = 0, int yy = 0) { x = xx; y = yy; } + int x, y; +}; + +struct Level +{ + uint8_t board[BOARDSIZE][BOARDSIZE]; + char name[41]; + Point corner; // Corner dimensions on screen + Point cursor; // Cursor location +}; + #define NUMBER_OF_BOARDS 42 extern const void * boards[]; +extern std::vector levelStorage; #endif // __BOARDS_H__ diff --git a/src/editorwidget.h b/src/editorwidget.h index 87ab02f..01781fa 100644 --- a/src/editorwidget.h +++ b/src/editorwidget.h @@ -3,26 +3,7 @@ #include #include -#include - -#define BOARDSIZE 128 - -struct Point -{ - Point(int xx = 0, int yy = 0) { x = xx; y = yy; } - int x, y; -}; - -struct Level -{ - uint8_t board[BOARDSIZE][BOARDSIZE]; - char name[41]; - Point corner; // Corner dimensions on screen - Point cursor; // Cursor location -// This stuff will go into the save file, but we don't need it here... -// Point size; // Actual dimensions of the level -// Point boardCorner; // Where on the gird the UL corner of the level is -}; +#include "boards.h" class EditorWidget: public QWidget { @@ -62,7 +43,6 @@ class EditorWidget: public QWidget void Pause(int); public: - std::vector levelStorage; uint32_t currentLevel; private: diff --git a/src/editorwindow.cpp b/src/editorwindow.cpp index df77436..ab49a8c 100644 --- a/src/editorwindow.cpp +++ b/src/editorwindow.cpp @@ -36,9 +36,9 @@ EditorWindow::EditorWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Window) // editor->setFocus(); //DISNOWOK // editor->Load(); - for(unsigned int i=0; ilevelStorage.size(); i++) + for(unsigned int i=0; iaddItem(QString(editor->levelStorage[i].name)); + list->addItem(QString(levelStorage[i].name)); QListWidgetItem * item = list->item(i); item->setFlags(item->flags() | Qt::ItemIsEditable); } @@ -93,7 +93,7 @@ void EditorWindow::SetEditorLevel(int level) if (level == 0) up->setDisabled(true); - else if ((unsigned int)level == (editor->levelStorage.size() - 1)) + else if ((unsigned int)level == (levelStorage.size() - 1)) down->setDisabled(true); else { @@ -114,7 +114,7 @@ void EditorWindow::MoveLevelUp(void) { // Swap in the vector... int n = editor->currentLevel; - std::swap(editor->levelStorage[n], editor->levelStorage[n - 1]); + std::swap(levelStorage[n], levelStorage[n - 1]); // Swap in the QListWidget too... int currentRow = list->currentRow(); @@ -127,7 +127,7 @@ void EditorWindow::MoveLevelUp(void) void EditorWindow::MoveLevelDown(void) { int n = editor->currentLevel; - std::swap(editor->levelStorage[n], editor->levelStorage[n + 1]); + std::swap(levelStorage[n], levelStorage[n + 1]); int currentRow = list->currentRow(); QListWidgetItem * currentItem = list->takeItem(currentRow); @@ -152,7 +152,7 @@ void EditorWindow::AddLevel(void) void EditorWindow::DeleteLevel(void) { //printf("DeleteLevel: Erasing level from levelStorage...\n"); - editor->levelStorage.erase(editor->levelStorage.begin() + editor->currentLevel); + levelStorage.erase(levelStorage.begin() + editor->currentLevel); //printf("DeleteLevel: Erasing level from list...\n"); int currentRow = list->currentRow(); @@ -172,6 +172,6 @@ void EditorWindow::DeleteLevel(void) void EditorWindow::PlayLevel(void) { - emit SetupLevel(&(editor->levelStorage[editor->currentLevel])); + emit SetupLevel(&(levelStorage[editor->currentLevel])); } diff --git a/src/gamewidget.cpp b/src/gamewidget.cpp index f66cd58..21dda8b 100644 --- a/src/gamewidget.cpp +++ b/src/gamewidget.cpp @@ -13,11 +13,13 @@ #include "gamewidget.h" #include // for usleep() +#include "boards.h" #include "gameboard.h" GameWidget::GameWidget(QWidget * parent/*= 0*/): QWidget(parent), - level(1), gameBoard(new GameBoard(level)), +// level(1), gameBoard(new GameBoard(level)), + level(1), gameBoard(new GameBoard(&(levelStorage[level - 1]))), animating(false), boxMoving(false) // score(new QLabel) { @@ -61,7 +63,7 @@ void GameWidget::paintEvent(QPaintEvent * /*event*/) else if ((tileType == GTBox) && (!(boxMoving && (movingBoxPositionX == x) && (movingBoxPositionY == y)))) { - painter.setBrush(QBrush(tile & GTBoxSpot ? Qt::green : Qt::red)); + painter.setBrush(QBrush(tile & GTBoxSpot ? Qt::darkRed : Qt::red)); painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength); } else if ((tileType == GTSpace) @@ -300,7 +302,8 @@ void GameWidget::NextLevel(void) { level++; delete gameBoard; - gameBoard = new GameBoard(level); +// gameBoard = new GameBoard(level); + gameBoard = new GameBoard(&(levelStorage[level - 1])); ResizeGrid(); update(); } diff --git a/src/mainwin.cpp b/src/mainwin.cpp index 3c37a7b..c71e897 100644 --- a/src/mainwin.cpp +++ b/src/mainwin.cpp @@ -25,20 +25,19 @@ MainWin::MainWin() { + // We create the EditorWindow 1st because the GameWidget will crash if there + // is no level data loaded... + editorWindow = new EditorWindow(this); gameWidget = new GameWidget(this); setCentralWidget(gameWidget); gameWidget->setFocusPolicy(Qt::StrongFocus); // Without this, it gets no keys setWindowTitle("Warehouse Man Deluxe"); setWindowIcon(QIcon(":/wmd-icon.png")); -// editor = new QWindow(this); - editorWindow = new EditorWindow(this); -// editor->setCentralWidget(editorWidget); -// editorWidget->setFocusPolicy(Qt::StrongFocus); // Without this, it gets no keys editorWindow->show(); newGame = CreateAction(tr("&New"), tr("New Game"), tr("Start a new game of Warehouse Man Deluxe"), QIcon(), QKeySequence(tr("ctrl+n"))); -// connect(newGame, SIGNAL(triggered()), this, SLOT(OnNewGame())); + connect(newGame, SIGNAL(triggered()), this, SLOT(HandleNewGame())); undoAction = CreateAction(tr("&Undo"), tr(""), tr(""), QIcon(), QKeySequence(tr("ctrl+z"))); connect(undoAction, SIGNAL(triggered()), this, SLOT(HandleUndo())); @@ -283,9 +282,17 @@ void MainWin::ResetGame(void) } +void MainWin::HandleNewGame(void) +{ +// ResetGame(); + gameWidget->level = 0; + gameWidget->NextLevel(); +} + + void MainWin::AboutGame(void) { - QMessageBox::about(this, tr("About Warehouse Man Deluxe"), tr("Warehouse Man Deluxe Version 1.0\n\nCopyright (C) 2014 Underground Software\n\nWritten by James L. Hammons")); + QMessageBox::about(this, tr("About Warehouse Man Deluxe"), tr("Warehouse Man Deluxe Version 1.0\n\nCopyright (C) 2014 Underground Software\n\nWritten by James Hammons")); } diff --git a/src/mainwin.h b/src/mainwin.h index 51b4548..2deb6e6 100644 --- a/src/mainwin.h +++ b/src/mainwin.h @@ -26,6 +26,7 @@ class MainWin: public QMainWindow protected slots: void closeEvent(QCloseEvent * event); + void HandleNewGame(void); void AboutGame(void); void WeHaveAWinner(void); void HandleReset(void); -- 2.37.2