commit ae5d6381
Started moving stage clear handeling out
Changed files
diff --git a/source/code/BlockGame.cpp b/source/code/BlockGame.cpp
index b0e5920..bfb90e5 100644
--- a/source/code/BlockGame.cpp
+++ b/source/code/BlockGame.cpp
@@ -33,6 +33,7 @@ http://blockattack.sf.net
#include "BlockGame.hpp"
#include "puzzlehandler.hpp"
+#include "stageclearhandler.hpp"
#include<boost/lexical_cast.hpp>
@@ -1738,39 +1739,12 @@ void BlockGame::AI_Move() {
//Updates evrything, if not called nothing happends
void BlockGame::Update() {
- Uint32 tempUInt32;
Uint32 nowTime = ticks; //We remember the time, so it doesn't change during this call
{
FindTowerHeight();
if ((linesCleared-TowerHeight>stageClearLimit) && (stageClear) && (!bGameOver)) {
- stageCleared[Level] = true;
- if (stageScores[Level]<score) {
- gameEndedAfter = nowTime-gameStartedAt;
- stageScores[Level] = score;
- stageTimes[Level] = gameEndedAfter;
- }
-
- ofstream outfile;
- outfile.open(stageClearSavePath.c_str(), ios::binary |ios::trunc);
- if (!outfile) {
- cerr << "Error writing to file: " << stageClearSavePath << endl;
- }
- else {
- for (int i=0; i<nrOfStageLevels; i++) {
- bool tempBool = stageCleared[i];
- outfile.write(reinterpret_cast<char*>(&tempBool), sizeof(bool));
- }
- for (int i=0; i<nrOfStageLevels; i++) {
- tempUInt32 = stageScores[i];
- outfile.write(reinterpret_cast<char*>(&tempUInt32), sizeof(Uint32));
- }
- for (int i=0; i<nrOfStageLevels; i++) {
- tempUInt32 = stageTimes[i];
- outfile.write(reinterpret_cast<char*>(&tempUInt32), sizeof(Uint32));
- }
- outfile.close();
- }
+ StageClearSetClear(Level, score, nowTime-gameStartedAt);
setPlayerWon();
stageButtonStatus = SBstageClear;
}
diff --git a/source/code/Makefile b/source/code/Makefile
index 99e464c..8abfe69 100644
--- a/source/code/Makefile
+++ b/source/code/Makefile
@@ -50,7 +50,8 @@ endif
BASE_LIBS += -lphysfs
-OFILES=main.o highscore.o ReadKeyboard.o joypad.o listFiles.o common.o stats.o Libs/NFont.o Libs/SDL_FontCache.o MenuSystem.o menudef.o puzzlehandler.o ${SAGO_O_FILES}
+OFILES=main.o highscore.o ReadKeyboard.o joypad.o listFiles.o common.o stats.o Libs/NFont.o Libs/SDL_FontCache.o MenuSystem.o menudef.o \
+ puzzlehandler.o stageclearhandler.o ${SAGO_O_FILES}
ifeq ($(CROSS),i686-w64-mingw32.static-)
OFILES += winicon.res
diff --git a/source/code/main.cpp b/source/code/main.cpp
index 6a65aa0..f05c060 100644
--- a/source/code/main.cpp
+++ b/source/code/main.cpp
@@ -56,6 +56,7 @@ http://blockattack.sf.net
#include <vector>
#include "MenuSystem.h"
#include "puzzlehandler.hpp"
+#include "stageclearhandler.hpp"
#include <memory>
#include <SDL/SDL_video.h>
@@ -1536,9 +1537,7 @@ int PuzzleLevelSelect(int Type) {
int oldmousex = 0;
int oldmousey = 0;
bool levelSelected = false;
- bool tempBool;
int nrOfLevels = 0;
- Uint32 tempUInt32;
Uint32 totalScore = 0;
Uint32 totalTime = 0;
int selected = 0;
@@ -1555,46 +1554,10 @@ int PuzzleLevelSelect(int Type) {
nrOfLevels = PuzzleGetNumberOfPuzzles();
}
if (Type == 1) {
- ifstream stageFile(stageClearSavePath.c_str(),ios::binary);
- if (stageFile) {
- for (int i = 0; i<nrOfStageLevels; i++) {
- stageFile.read(reinterpret_cast<char*>(&tempBool),sizeof(bool));
- stageCleared[i]=tempBool;
- }
- if (!stageFile.eof()) {
- for (int i=0; i<nrOfStageLevels; i++) {
- tempUInt32 = 0;
- if (!stageFile.eof()) {
- stageFile.read(reinterpret_cast<char*>(&tempUInt32),sizeof(Uint32));
- }
- stageScores[i]=tempUInt32;
- totalScore+=tempUInt32;
- }
- for (int i=0; i<nrOfStageLevels; i++) {
- tempUInt32 = 0;
- if (!stageFile.eof()) {
- stageFile.read(reinterpret_cast<char*>(&tempUInt32),sizeof(Uint32));
- }
- stageTimes[i]=tempUInt32;
- totalTime += tempUInt32;
- }
- }
- else {
- for (int i=0; i<nrOfStageLevels; i++) {
- stageScores[i]=0;
- stageTimes[i]=0;
- }
- }
- stageFile.close();
- }
- else {
- for (int i=0; i<nrOfStageLevels; i++) {
- stageCleared[i]= false;
- stageScores[i]=0;
- stageTimes[i]=0;
- }
- }
- nrOfLevels = nrOfStageLevels;
+ LoadStageClearStages();
+ totalScore = GetTotalScore();
+ totalTime = GetTotalTime();
+ nrOfLevels = GetNrOfLevels();
}
while (!levelSelected) {
@@ -1618,7 +1581,7 @@ int PuzzleLevelSelect(int Type) {
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) {
+ if (Type == 1 && IsStageCleared(i)) {
DrawIMG(iLevelCheck,screen,xplace+10+(i%10)*50, yplace+60+(i/10)*50);
}
}
@@ -1708,11 +1671,11 @@ int PuzzleLevelSelect(int Type) {
string scoreString = _("Best score: 0");
string timeString = _("Time used: -- : --");
- if (stageScores.at(selected)>0) {
- scoreString = _("Best score: ")+itoa(stageScores.at(selected));
+ if (GetStageScores(selected)>0) {
+ scoreString = _("Best score: ")+itoa(GetStageScores(selected));
}
- if (stageTimes.at(selected)>0) {
- timeString = _("Time used: ")+itoa(stageTimes.at(selected)/1000/60)+" : "+itoa2((stageTimes.at(selected)/1000)%60);
+ if (GetStageTime(selected)>0) {
+ timeString = _("Time used: ")+itoa(GetStageTime(selected)/1000/60)+" : "+itoa2((GetStageTime(selected)/1000)%60);
}
NFont_Write(screen, 200,200,scoreString.c_str());
diff --git a/source/code/mainVars.hpp b/source/code/mainVars.hpp
index 9e28c16..007a0ec 100644
--- a/source/code/mainVars.hpp
+++ b/source/code/mainVars.hpp
@@ -195,13 +195,6 @@ static const int bsize = 50;
std::string player1name;
std::string player2name;
-//paths
-static std::string stageClearSavePath;
-
-static const int nrOfStageLevels = 50; //number of stages in stage Clear
-std::vector<bool> stageCleared(nrOfStageLevels); //vector that tells if a stage is cleared
-std::vector<Uint32> stageTimes(nrOfStageLevels); //For statistical puposes
-std::vector<Uint32> stageScores(nrOfStageLevels); //--||--
bool twoPlayers; //True if two players are playing
//Old mouse position:
diff --git a/source/code/stageclearhandler.cpp b/source/code/stageclearhandler.cpp
new file mode 100644
index 0000000..d57d656
--- /dev/null
+++ b/source/code/stageclearhandler.cpp
@@ -0,0 +1,121 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+#include "stageclearhandler.hpp"
+#include "SDL.h"
+#include <fstream>
+#include <vector>
+#include <iostream>
+
+//paths
+std::string stageClearSavePath;
+
+
+std::vector<bool> stageCleared(nrOfStageLevels); //vector that tells if a stage is cleared
+std::vector<Uint32> stageTimes(nrOfStageLevels); //For statistical puposes
+std::vector<Uint32> stageScores(nrOfStageLevels); //--||--
+Uint32 totalScore = 0;
+Uint32 totalTime = 0;
+
+using namespace std;
+
+void StageClearSetClear(int Level, int score, int time) {
+ stageCleared[Level] = true;
+ int gameEndedAfter = time;
+ if (stageScores[Level]<score) {
+ stageScores[Level] = score;
+ stageTimes[Level] = gameEndedAfter;
+ }
+
+ ofstream outfile;
+ outfile.open(stageClearSavePath.c_str(), ios::binary |ios::trunc);
+ if (!outfile) {
+ cerr << "Error writing to file: " << stageClearSavePath << endl;
+ }
+ else {
+ for (int i=0; i<nrOfStageLevels; i++) {
+ bool tempBool = stageCleared[i];
+ outfile.write(reinterpret_cast<char*>(&tempBool), sizeof(bool));
+ }
+ for (int i=0; i<nrOfStageLevels; i++) {
+ Uint32 tempUInt32 = stageScores[i];
+ outfile.write(reinterpret_cast<char*>(&tempUInt32), sizeof(Uint32));
+ }
+ for (int i=0; i<nrOfStageLevels; i++) {
+ Uint32 tempUInt32 = stageTimes[i];
+ outfile.write(reinterpret_cast<char*>(&tempUInt32), sizeof(Uint32));
+ }
+ outfile.close();
+ }
+}
+
+void LoadStageClearStages() {
+ bool tempBool;
+ Uint32 tempUInt32;
+ ifstream stageFile(stageClearSavePath.c_str(),ios::binary);
+ if (stageFile) {
+ for (int i = 0; i<nrOfStageLevels; i++) {
+ stageFile.read(reinterpret_cast<char*>(&tempBool),sizeof(bool));
+ stageCleared[i]=tempBool;
+ }
+ if (!stageFile.eof()) {
+ for (int i=0; i<nrOfStageLevels; i++) {
+ tempUInt32 = 0;
+ if (!stageFile.eof()) {
+ stageFile.read(reinterpret_cast<char*>(&tempUInt32),sizeof(Uint32));
+ }
+ stageScores[i]=tempUInt32;
+ totalScore+=tempUInt32;
+ }
+ for (int i=0; i<nrOfStageLevels; i++) {
+ tempUInt32 = 0;
+ if (!stageFile.eof()) {
+ stageFile.read(reinterpret_cast<char*>(&tempUInt32),sizeof(Uint32));
+ }
+ stageTimes[i]=tempUInt32;
+ totalTime += tempUInt32;
+ }
+ }
+ else {
+ for (int i=0; i<nrOfStageLevels; i++) {
+ stageScores[i]=0;
+ stageTimes[i]=0;
+ }
+ }
+ stageFile.close();
+ }
+ else {
+ for (int i=0; i<nrOfStageLevels; i++) {
+ stageCleared[i]= false;
+ stageScores[i]=0;
+ stageTimes[i]=0;
+ }
+ }
+}
+
+int GetTotalScore() {
+ return totalScore;
+}
+int GetTotalTime() {
+ return totalTime;
+}
+
+int GetNrOfLevels() {
+ return nrOfStageLevels;
+}
+
+bool IsStageCleared(int level) {
+ return stageCleared.at(level);
+}
+
+
+int GetStageScores(int level) {
+ return stageScores.at(level);
+}
+
+int GetStageTime(int level) {
+ return stageTimes.at(level);
+}
\ No newline at end of file
diff --git a/source/code/stageclearhandler.hpp b/source/code/stageclearhandler.hpp
new file mode 100644
index 0000000..8d77bd5
--- /dev/null
+++ b/source/code/stageclearhandler.hpp
@@ -0,0 +1,33 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/*
+ * File: stageclearhandler.hpp
+ * Author: poul
+ *
+ * Created on 29. december 2015, 12:56
+ */
+
+#ifndef STAGECLEARHANDLER_HPP
+#define STAGECLEARHANDLER_HPP
+
+#include <string>
+
+extern std::string stageClearSavePath;
+
+void StageClearSetClear(int level, int score, int time);
+void LoadStageClearStages();
+int GetTotalScore();
+int GetTotalTime();
+int GetNrOfLevels();
+bool IsStageCleared(int level);
+int GetStageScores(int level);
+int GetStageTime(int level);
+
+const int nrOfStageLevels = 50; //number of stages in stage Clear
+
+#endif /* STAGECLEARHANDLER_HPP */
+