commit 055218be
Seperated a lot of the puzzle logic out in its own file
Changed files
| M | source/code/BlockGame.cpp before |
| M | source/code/Makefile before |
| M | source/code/main.cpp before |
| M | source/code/mainVars.hpp before |
| A | source/code/puzzlehandler.cpp |
| A | source/code/puzzlehandler.hpp |
diff --git a/source/code/BlockGame.cpp b/source/code/BlockGame.cpp
index e24b28c..dbddcae 100644
--- a/source/code/BlockGame.cpp
+++ b/source/code/BlockGame.cpp
@@ -32,6 +32,7 @@ http://blockattack.sf.net
#define CHAINPLACE 10000000
#include "BlockGame.hpp"
+#include "puzzlehandler.hpp"
#include<boost/lexical_cast.hpp>
@@ -390,11 +391,11 @@ void BlockGame::NewPuzzleGame(int level, unsigned int ticks)
NewGame(ticks);
puzzleMode = true;
Level = level;
- MovesLeft = nrOfMovesAllowed[Level];
+ MovesLeft = PuzzleNumberOfMovesAllowed(Level);
for (int i=0; i<6; i++)
for (int j=0; j<12; j++)
{
- board[i][j+1] = puzzleLevels[Level][i][j];
+ board[i][j+1] = PuzzleGetBrick(Level,i,j);
}
baseSpeed = 100000;
speed = 100000;
@@ -437,7 +438,7 @@ void BlockGame::retryLevel(unsigned int ticks)
void BlockGame::nextLevel(unsigned int ticks)
{
if (puzzleMode) {
- if (Level<nrOfPuzzles-1) {
+ if (Level<PuzzleGetNumberOfPuzzles()-1) {
NewPuzzleGame(Level+1, ticks);
}
}
@@ -1434,11 +1435,11 @@ void BlockGame::PushLineInternal()
if (puzzleMode && !bGameOver)
{
//Reloads level
- MovesLeft = nrOfMovesAllowed[Level];
+ MovesLeft = PuzzleNumberOfMovesAllowed(Level);
for (int i=0; i<6; i++)
for (int j=0; j<12; j++)
{
- board[i][j+1] = puzzleLevels[Level][i][j];
+ board[i][j+1] = PuzzleGetBrick(Level,i,j);
}
score=0;
bGameOver=false;
@@ -1996,31 +1997,13 @@ void BlockGame::Update()
nrStops++;
}
//If we have static content, we don't raise at all!
- if (hasStaticContent())
+ if (hasStaticContent()) {
stop++;
- if ((puzzleMode)&&(!bGameOver)&&BoardEmpty())
- {
- if (!singlePuzzle)
- {
- if(puzzleCleared[Level]==false)
- Stats::getInstance()->addOne("puzzlesSolved");
- puzzleCleared[Level] = true;
- ofstream outfile;
+ }
+ if ((puzzleMode)&&(!bGameOver)&&BoardEmpty()) {
+ if (!singlePuzzle) {
+ PuzzleSetClear(Level);
stageButtonStatus = SBpuzzleMode;
- outfile.open(puzzleSavePath.c_str(), ios::binary |ios::trunc);
- if (!outfile)
- {
- cerr << "Error writing to file: " << puzzleSavePath << endl;
- }
- else
- {
- for (int i=0; i<nrOfPuzzles; i++)
- {
- bool tempBool = puzzleCleared[i];
- outfile.write(reinterpret_cast<char*>(&tempBool), sizeof(bool));
- }
- outfile.close();
- }
}
setPlayerWon();
}
diff --git a/source/code/Makefile b/source/code/Makefile
index c92daee..030061c 100644
--- a/source/code/Makefile
+++ b/source/code/Makefile
@@ -43,7 +43,7 @@ endif
BASE_LIBS += -lphysfs
-OFILES=main.o highscore.o ReadKeyboard.o joypad.o listFiles.o replay.o common.o stats.o CppSdlException.o CppSdlImageHolder.o Libs/NFont.o MenuSystem.o menudef.o
+OFILES=main.o highscore.o ReadKeyboard.o joypad.o listFiles.o replay.o common.o stats.o CppSdlException.o CppSdlImageHolder.o Libs/NFont.o MenuSystem.o menudef.o puzzlehandler.o
ifeq ($(CROSS),i686-pc-mingw32-)
OFILES += winicon.res
diff --git a/source/code/main.cpp b/source/code/main.cpp
index 61af457..14cef66 100644
--- a/source/code/main.cpp
+++ b/source/code/main.cpp
@@ -71,6 +71,7 @@ http://blockattack.sf.net
#include <SDL/SDL_ttf.h>
#include "CppSdlImageHolder.hpp"
#include "MenuSystem.h"
+#include "puzzlehandler.hpp"
//if SHAREDIR is not used we look in current directory
#ifndef SHAREDIR
@@ -813,34 +814,6 @@ static string itoa2(int num)
return converter.str();
}
-/*Loads all the puzzle levels*/
-static int LoadPuzzleStages()
-{
- //if(puzzleLoaded)
- // return 1;
- if (!PHYSFS_exists(((string)("puzzles/"+puzzleName)).c_str()))
- {
- cerr << "Warning: File not in blockattack.data: " << ("puzzles/"+puzzleName) << endl;
- return -1; //file doesn't exist
- }
- PhysFS::ifstream inFile(((string)("puzzles/"+puzzleName)).c_str());
-
- inFile >> nrOfPuzzles;
- if (nrOfPuzzles>maxNrOfPuzzleStages)
- nrOfPuzzles=maxNrOfPuzzleStages;
- for (int k=0; (k<nrOfPuzzles) /*&&(!inFile.eof())*/ ; k++)
- {
- inFile >> nrOfMovesAllowed[k];
- for (int i=11; i>=0; i--)
- for (int j=0; j<6; j++)
- {
- inFile >> puzzleLevels[k][j][i];
- }
- }
- puzzleLoaded = true;
- return 0;
-}
-
/*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/
void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y)
{
@@ -1566,7 +1539,7 @@ public:
if(puzzleMode && stageButtonStatus == SBpuzzleMode)
{
DrawIMG(bRetry,sBoard, cordRetryButton.x, cordRetryButton.y);
- if(Level<nrOfPuzzles-1)
+ if (Level<PuzzleGetNumberOfPuzzles()-1)
{
if(hasWonTheGame)
DrawIMG(bNext,sBoard,cordNextButton.x, cordNextButton.y);
@@ -2657,23 +2630,7 @@ int PuzzleLevelSelect(int Type)
MakeBackground(xsize,ysize);
if(Type == 0)
{
- ifstream puzzleFile(puzzleSavePath.c_str(),ios::binary);
- if (puzzleFile)
- {
- for (int i=0; (i<nrOfPuzzles)&&(!puzzleFile.eof()); i++)
- {
- puzzleFile.read(reinterpret_cast<char*>(&tempBool),sizeof(bool));
- puzzleCleared[i] = tempBool;
- }
- puzzleFile.close();
- }
- else
- {
- tempBool = false;
- for (int i=0; i<nrOfPuzzles; i++)
- puzzleCleared[i] = tempBool;
- }
- nrOfLevels = nrOfPuzzles;
+ nrOfLevels = PuzzleGetNumberOfPuzzles();
}
if(Type == 1)
{
@@ -2742,7 +2699,7 @@ int PuzzleLevelSelect(int Type)
{
DrawIMG(iLevelCheckBox,screen,xplace+10+(i%10)*50, yplace+60+(i/10)*50);
if(i==selected) DrawIMG(iLevelCheckBoxMarked,screen,xplace+10+(i%10)*50, yplace+60+(i/10)*50);
- if (Type == 0 && puzzleCleared.at(i)==true) DrawIMG(iLevelCheck,screen,xplace+10+(i%10)*50, yplace+60+(i/10)*50);
+ if (Type == 0 && PuzzleIsCleared(i)) DrawIMG(iLevelCheck,screen,xplace+10+(i%10)*50, yplace+60+(i/10)*50);
if (Type == 1 && stageCleared.at(i)==true) DrawIMG(iLevelCheck,screen,xplace+10+(i%10)*50, yplace+60+(i/10)*50);
}
@@ -3009,30 +2966,30 @@ void startVsMenu()
void changePuzzleLevels()
{
char theFileName[30];
- strcpy(theFileName,puzzleName.c_str());
- for (int i=puzzleName.length(); i<30; i++)
+ strcpy(theFileName, PuzzleGetName().c_str());
+ for (int i=PuzzleGetName().length(); i<30; i++)
theFileName[i]=' ';
theFileName[29]=0;
if (OpenFileDialogbox(200,100,theFileName))
{
for (int i=28; ((theFileName[i]==' ')&&(i>0)); i--)
theFileName[i]=0;
- puzzleName = theFileName;
+ PuzzleSetName(theFileName);
#if defined(__unix__)
string home = getenv("HOME");
- puzzleSavePath = home+"/.gamesaves/blockattack/"+puzzleName+".save";
+ PuzzleSetSavePath(home+"/.gamesaves/blockattack/"+PuzzleGetName()+".save");
#elif defined(_WIN32)
string home = getMyDocumentsPath();
if (&home!=NULL)
{
- puzzleSavePath = home+"/My Games/blockattack/"+puzzleName+".save";
+ PuzzleSetSavePath(home+"/My Games/blockattack/"+PuzzleGetName()+".save");
}
else
{
- puzzleSavePath = puzzleName+".save";
+ PuzzleSetSavePath(PuzzleGetName()+".save");
}
#else
- puzzleSavePath = puzzleName+".save";
+ PuzzleSetSavePath(PuzzleGetName()+".save");
#endif
}
@@ -3297,23 +3254,23 @@ int main(int argc, char *argv[])
#if defined(__unix__)
stageClearSavePath = home+"/.gamesaves/blockattack/stageClear.SCsave";
- puzzleSavePath = home+"/.gamesaves/blockattack/puzzle.levels.save";
+ PuzzleSetSavePath(home+"/.gamesaves/blockattack/puzzle.levels.save");
#elif defined(_WIN32)
if (&home!=NULL)
{
stageClearSavePath = home+"/My Games/blockattack/stageClear.SCsave";
- puzzleSavePath = home+"/My Games/blockattack/puzzle.levels.save";
+ PuzzleSetSavePath(home+"/My Games/blockattack/puzzle.levels.save");
}
else
{
stageClearSavePath = "stageClear.SCsave";
- puzzleSavePath = "puzzle.levels.save";
+ PuzzleSetSavePath("puzzle.levels.save");
}
#else
stageClearSavePath = "stageClear.SCsave";
- puzzleSavePath = "puzzle.levels.save";
+ PuzzleSetSavePath("puzzle.levels.save");
#endif
- puzzleName="puzzle.levels";
+ PuzzleSetName("puzzle.levels");
//Init SDL
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
diff --git a/source/code/mainVars.hpp b/source/code/mainVars.hpp
index 704756b..b57b0d8 100644
--- a/source/code/mainVars.hpp
+++ b/source/code/mainVars.hpp
@@ -204,18 +204,11 @@ char player2name[30];
//paths
static string stageClearSavePath;
-static string puzzleSavePath;
-static string puzzleName; //The filename of
static const int nrOfStageLevels = 50; //number of stages in stage Clear
-static const int maxNrOfPuzzleStages = 50; //Maximum number of puzzle stages
vector<bool> stageCleared(nrOfStageLevels); //vector that tells if a stage is cleared
vector<Uint32> stageTimes(nrOfStageLevels); //For statistical puposes
vector<Uint32> stageScores(nrOfStageLevels); //--||--
-vector<bool> puzzleCleared(maxNrOfPuzzleStages); //vector that tells if puzzle cleared
-vector<int> nrOfMovesAllowed(maxNrOfPuzzleStages); //Moves to clear
-int puzzleLevels[maxNrOfPuzzleStages][6][12]; //Contains board layout;
-int nrOfPuzzles; //How many are there actually?
bool twoPlayers; //True if two players are playing
//Old mouse position:
diff --git a/source/code/puzzlehandler.cpp b/source/code/puzzlehandler.cpp
new file mode 100644
index 0000000..03250c1
--- /dev/null
+++ b/source/code/puzzlehandler.cpp
@@ -0,0 +1,106 @@
+#include "puzzlehandler.hpp"
+#include <vector>
+#include <fstream>
+#include <iostream>
+#include "stats.h"
+#include <physfs.h> //Abstract file system. To use containers
+#include "physfs_stream.hpp" //To use C++ style file streams
+
+static const int maxNrOfPuzzleStages = 50; //Maximum number of puzzle stages
+static std::string puzzleSavePath;
+static std::string puzzleName; //The filename of
+static std::vector<bool> puzzleCleared(maxNrOfPuzzleStages); //vector that tells if puzzle cleared
+static std::vector<int> nrOfMovesAllowed(maxNrOfPuzzleStages); //Moves to clear
+static int puzzleLevels[maxNrOfPuzzleStages][6][12]; //Contains board layout;
+static int nrOfPuzzles; //How many are there actually?
+
+int PuzzleNumberOfMovesAllowed(int level) {
+ return nrOfMovesAllowed.at(level);
+}
+
+int PuzzleGetBrick(int level, int x, int y) {
+ return puzzleLevels[level][x][y];
+}
+
+int PuzzleGetNumberOfPuzzles() {
+ return nrOfPuzzles;
+}
+
+bool PuzzleIsCleared(int level) {
+ return puzzleCleared.at(level);
+}
+
+const std::string& PuzzleGetName() {
+ return puzzleName;
+}
+
+void PuzzleSetName(const std::string& name) {
+ puzzleName = name;
+}
+
+void PuzzleSetSavePath(const std::string& filepath) {
+ puzzleSavePath = filepath;
+}
+
+void PuzzleSetClear(int Level) {
+ if(puzzleCleared[Level]==false)
+ Stats::getInstance()->addOne("puzzlesSolved");
+ puzzleCleared[Level] = true;
+ std::ofstream outfile;
+ outfile.open(puzzleSavePath.c_str(), ios::binary |ios::trunc);
+ if (!outfile)
+ {
+ std::cerr << "Error writing to file: " << puzzleSavePath << endl;
+ }
+ else
+ {
+ for (int i=0; i<nrOfPuzzles; i++)
+ {
+ bool tempBool = puzzleCleared[i];
+ outfile.write(reinterpret_cast<char*>(&tempBool), sizeof(bool));
+ }
+ outfile.close();
+ }
+}
+
+/*Loads all the puzzle levels*/
+int LoadPuzzleStages( )
+{
+ if (!PHYSFS_exists(((std::string)("puzzles/"+puzzleName)).c_str()))
+ {
+ std::cerr << "Warning: File not in blockattack.data: " << ("puzzles/"+puzzleName) << endl;
+ return -1; //file doesn't exist
+ }
+ PhysFS::ifstream inFile(((std::string)("puzzles/"+puzzleName)).c_str());
+
+ inFile >> nrOfPuzzles;
+ if (nrOfPuzzles>maxNrOfPuzzleStages)
+ nrOfPuzzles=maxNrOfPuzzleStages;
+ for (int k=0; (k<nrOfPuzzles) /*&&(!inFile.eof())*/ ; k++)
+ {
+ inFile >> nrOfMovesAllowed[k];
+ for (int i=11; i>=0; i--)
+ for (int j=0; j<6; j++)
+ {
+ inFile >> puzzleLevels[k][j][i];
+ }
+ }
+ bool tempBool;
+ std::ifstream puzzleFile(puzzleSavePath.c_str(), std::ios::binary);
+ if (puzzleFile)
+ {
+ for (int i=0; (i<nrOfPuzzles)&&(!puzzleFile.eof()); i++)
+ {
+ puzzleFile.read(reinterpret_cast<char*>(&tempBool),sizeof(bool));
+ puzzleCleared[i] = tempBool;
+ }
+ puzzleFile.close();
+ }
+ else
+ {
+ tempBool = false;
+ for (int i=0; i<nrOfPuzzles; i++)
+ puzzleCleared[i] = tempBool;
+ }
+ return 0;
+}
\ No newline at end of file
diff --git a/source/code/puzzlehandler.hpp b/source/code/puzzlehandler.hpp
new file mode 100644
index 0000000..d40c659
--- /dev/null
+++ b/source/code/puzzlehandler.hpp
@@ -0,0 +1,25 @@
+/*
+ * File: puzzlehandler.hpp
+ * Author: poul
+ *
+ * Created on 22. august 2015, 20:09
+ */
+
+#ifndef PUZZLEHANDLER_HPP
+#define PUZZLEHANDLER_HPP
+
+
+#include <string>
+
+int PuzzleNumberOfMovesAllowed(int level);
+int PuzzleGetBrick(int level, int x, int y);
+bool PuzzleIsCleared(int level);
+int LoadPuzzleStages();
+int PuzzleGetNumberOfPuzzles();
+void PuzzleSetClear(int level);
+const std::string& PuzzleGetName();
+void PuzzleSetName(const std::string& name);
+void PuzzleSetSavePath(const std::string& filepath);
+
+#endif /* PUZZLEHANDLER_HPP */
+