git repos / blockattack-game

blame: source/code/stageclearhandler.cpp

normal view · raw

ae5d6381 sago007 2015-12-29 13:26 1
/*
1899da11 sago007 2015-12-30 19:04 2
===========================================================================
1899da11 sago007 2015-12-30 19:04 3
blockattack - Block Attack - Rise of the Blocks
1899da11 sago007 2015-12-30 19:04 4
Copyright (C) 2005-2015 Poul Sander
1899da11 sago007 2015-12-30 19:04 5
1899da11 sago007 2015-12-30 19:04 6
This program is free software: you can redistribute it and/or modify
1899da11 sago007 2015-12-30 19:04 7
it under the terms of the GNU General Public License as published by
1899da11 sago007 2015-12-30 19:04 8
the Free Software Foundation, either version 2 of the License, or
1899da11 sago007 2015-12-30 19:04 9
(at your option) any later version.
1899da11 sago007 2015-12-30 19:04 10
1899da11 sago007 2015-12-30 19:04 11
This program is distributed in the hope that it will be useful,
1899da11 sago007 2015-12-30 19:04 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
1899da11 sago007 2015-12-30 19:04 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1899da11 sago007 2015-12-30 19:04 14
GNU General Public License for more details.
1899da11 sago007 2015-12-30 19:04 15
1899da11 sago007 2015-12-30 19:04 16
You should have received a copy of the GNU General Public License
1899da11 sago007 2015-12-30 19:04 17
along with this program.  If not, see http://www.gnu.org/licenses/
1899da11 sago007 2015-12-30 19:04 18
1899da11 sago007 2015-12-30 19:04 19
Source information and contacts persons can be found at
1899da11 sago007 2015-12-30 19:04 20
http://blockattack.net
1899da11 sago007 2015-12-30 19:04 21
===========================================================================
1899da11 sago007 2015-12-30 19:04 22
*/
ae5d6381 sago007 2015-12-29 13:26 23
ae5d6381 sago007 2015-12-29 13:26 24
#include "stageclearhandler.hpp"
ae5d6381 sago007 2015-12-29 13:26 25
#include "SDL.h"
ae5d6381 sago007 2015-12-29 13:26 26
#include <fstream>
ae5d6381 sago007 2015-12-29 13:26 27
#include <vector>
ae5d6381 sago007 2015-12-29 13:26 28
#include <iostream>
ae5d6381 sago007 2015-12-29 13:26 29
ae5d6381 sago007 2015-12-29 13:26 30
//paths
ae5d6381 sago007 2015-12-29 13:26 31
std::string stageClearSavePath;
ae5d6381 sago007 2015-12-29 13:26 32
ae5d6381 sago007 2015-12-29 13:26 33
ae5d6381 sago007 2015-12-29 13:26 34
std::vector<bool> stageCleared(nrOfStageLevels);		//vector that tells if a stage is cleared
daa8f1cf sago007 2016-01-23 10:57 35
std::vector<Sint32> stageTimes(nrOfStageLevels);             //For statistical puposes
daa8f1cf sago007 2016-01-23 10:57 36
std::vector<Sint32> stageScores(nrOfStageLevels);            //--||--
daa8f1cf sago007 2016-01-23 10:57 37
Sint32 totalScore = 0;
daa8f1cf sago007 2016-01-23 10:57 38
Sint32 totalTime = 0;
ae5d6381 sago007 2015-12-29 13:26 39
ae5d6381 sago007 2015-12-29 13:26 40
using namespace std;
ae5d6381 sago007 2015-12-29 13:26 41
ae5d6381 sago007 2015-12-29 13:26 42
void StageClearSetClear(int Level, int score, int time) {
ae5d6381 sago007 2015-12-29 13:26 43
	stageCleared[Level] = true;
ae5d6381 sago007 2015-12-29 13:26 44
	int gameEndedAfter = time;
ae5d6381 sago007 2015-12-29 13:26 45
	if (stageScores[Level]<score) {
ae5d6381 sago007 2015-12-29 13:26 46
		stageScores[Level] = score;
ae5d6381 sago007 2015-12-29 13:26 47
		stageTimes[Level] = gameEndedAfter;
ae5d6381 sago007 2015-12-29 13:26 48
	}
ae5d6381 sago007 2015-12-29 13:26 49
ae5d6381 sago007 2015-12-29 13:26 50
	ofstream outfile;
ae5d6381 sago007 2015-12-29 13:26 51
	outfile.open(stageClearSavePath.c_str(), ios::binary |ios::trunc);
ae5d6381 sago007 2015-12-29 13:26 52
	if (!outfile) {
ae5d6381 sago007 2015-12-29 13:26 53
		cerr << "Error writing to file: " << stageClearSavePath << endl;
ae5d6381 sago007 2015-12-29 13:26 54
	}
ae5d6381 sago007 2015-12-29 13:26 55
	else {
ae5d6381 sago007 2015-12-29 13:26 56
		for (int i=0; i<nrOfStageLevels; i++) {
ae5d6381 sago007 2015-12-29 13:26 57
			bool tempBool = stageCleared[i];
ae5d6381 sago007 2015-12-29 13:26 58
			outfile.write(reinterpret_cast<char*>(&tempBool), sizeof(bool));
ae5d6381 sago007 2015-12-29 13:26 59
		}
ae5d6381 sago007 2015-12-29 13:26 60
		for (int i=0; i<nrOfStageLevels; i++) {
ae5d6381 sago007 2015-12-29 13:26 61
			Uint32 tempUInt32 = stageScores[i];
ae5d6381 sago007 2015-12-29 13:26 62
			outfile.write(reinterpret_cast<char*>(&tempUInt32), sizeof(Uint32));
ae5d6381 sago007 2015-12-29 13:26 63
		}
ae5d6381 sago007 2015-12-29 13:26 64
		for (int i=0; i<nrOfStageLevels; i++) {
ae5d6381 sago007 2015-12-29 13:26 65
			Uint32 tempUInt32 = stageTimes[i];
ae5d6381 sago007 2015-12-29 13:26 66
			outfile.write(reinterpret_cast<char*>(&tempUInt32), sizeof(Uint32));
ae5d6381 sago007 2015-12-29 13:26 67
		}
ae5d6381 sago007 2015-12-29 13:26 68
		outfile.close();
ae5d6381 sago007 2015-12-29 13:26 69
	}
ae5d6381 sago007 2015-12-29 13:26 70
}
ae5d6381 sago007 2015-12-29 13:26 71
ae5d6381 sago007 2015-12-29 13:26 72
void LoadStageClearStages() {
ae5d6381 sago007 2015-12-29 13:26 73
	bool tempBool;
ae5d6381 sago007 2015-12-29 13:26 74
	Uint32 tempUInt32;
ae5d6381 sago007 2015-12-29 13:26 75
	ifstream stageFile(stageClearSavePath.c_str(),ios::binary);
ae5d6381 sago007 2015-12-29 13:26 76
	if (stageFile) {
ae5d6381 sago007 2015-12-29 13:26 77
		for (int i = 0; i<nrOfStageLevels; i++) {
ae5d6381 sago007 2015-12-29 13:26 78
			stageFile.read(reinterpret_cast<char*>(&tempBool),sizeof(bool));
ae5d6381 sago007 2015-12-29 13:26 79
			stageCleared[i]=tempBool;
ae5d6381 sago007 2015-12-29 13:26 80
		}
ae5d6381 sago007 2015-12-29 13:26 81
		if (!stageFile.eof()) {
ae5d6381 sago007 2015-12-29 13:26 82
			for (int i=0; i<nrOfStageLevels; i++) {
ae5d6381 sago007 2015-12-29 13:26 83
				tempUInt32 = 0;
ae5d6381 sago007 2015-12-29 13:26 84
				if (!stageFile.eof()) {
ae5d6381 sago007 2015-12-29 13:26 85
					stageFile.read(reinterpret_cast<char*>(&tempUInt32),sizeof(Uint32));
ae5d6381 sago007 2015-12-29 13:26 86
				}
ae5d6381 sago007 2015-12-29 13:26 87
				stageScores[i]=tempUInt32;
ae5d6381 sago007 2015-12-29 13:26 88
				totalScore+=tempUInt32;
ae5d6381 sago007 2015-12-29 13:26 89
			}
ae5d6381 sago007 2015-12-29 13:26 90
			for (int i=0; i<nrOfStageLevels; i++) {
ae5d6381 sago007 2015-12-29 13:26 91
				tempUInt32 = 0;
ae5d6381 sago007 2015-12-29 13:26 92
				if (!stageFile.eof()) {
ae5d6381 sago007 2015-12-29 13:26 93
					stageFile.read(reinterpret_cast<char*>(&tempUInt32),sizeof(Uint32));
ae5d6381 sago007 2015-12-29 13:26 94
				}
ae5d6381 sago007 2015-12-29 13:26 95
				stageTimes[i]=tempUInt32;
ae5d6381 sago007 2015-12-29 13:26 96
				totalTime += tempUInt32;
ae5d6381 sago007 2015-12-29 13:26 97
			}
ae5d6381 sago007 2015-12-29 13:26 98
		}
ae5d6381 sago007 2015-12-29 13:26 99
		else {
ae5d6381 sago007 2015-12-29 13:26 100
			for (int i=0; i<nrOfStageLevels; i++) {
ae5d6381 sago007 2015-12-29 13:26 101
				stageScores[i]=0;
ae5d6381 sago007 2015-12-29 13:26 102
				stageTimes[i]=0;
ae5d6381 sago007 2015-12-29 13:26 103
			}
ae5d6381 sago007 2015-12-29 13:26 104
		}
ae5d6381 sago007 2015-12-29 13:26 105
		stageFile.close();
ae5d6381 sago007 2015-12-29 13:26 106
	}
ae5d6381 sago007 2015-12-29 13:26 107
	else {
ae5d6381 sago007 2015-12-29 13:26 108
		for (int i=0; i<nrOfStageLevels; i++) {
ae5d6381 sago007 2015-12-29 13:26 109
			stageCleared[i]= false;
ae5d6381 sago007 2015-12-29 13:26 110
			stageScores[i]=0;
ae5d6381 sago007 2015-12-29 13:26 111
			stageTimes[i]=0;
ae5d6381 sago007 2015-12-29 13:26 112
		}
ae5d6381 sago007 2015-12-29 13:26 113
	}
ae5d6381 sago007 2015-12-29 13:26 114
}
ae5d6381 sago007 2015-12-29 13:26 115
ae5d6381 sago007 2015-12-29 13:26 116
int GetTotalScore() {
ae5d6381 sago007 2015-12-29 13:26 117
	return totalScore;
ae5d6381 sago007 2015-12-29 13:26 118
}
ae5d6381 sago007 2015-12-29 13:26 119
int GetTotalTime() {
ae5d6381 sago007 2015-12-29 13:26 120
	return totalTime;
ae5d6381 sago007 2015-12-29 13:26 121
}
ae5d6381 sago007 2015-12-29 13:26 122
ae5d6381 sago007 2015-12-29 13:26 123
int GetNrOfLevels() {
ae5d6381 sago007 2015-12-29 13:26 124
	return nrOfStageLevels;
ae5d6381 sago007 2015-12-29 13:26 125
}
ae5d6381 sago007 2015-12-29 13:26 126
ae5d6381 sago007 2015-12-29 13:26 127
bool IsStageCleared(int level) {
ae5d6381 sago007 2015-12-29 13:26 128
	return stageCleared.at(level);
ae5d6381 sago007 2015-12-29 13:26 129
}
ae5d6381 sago007 2015-12-29 13:26 130
ae5d6381 sago007 2015-12-29 13:26 131
ae5d6381 sago007 2015-12-29 13:26 132
int GetStageScores(int level) {
ae5d6381 sago007 2015-12-29 13:26 133
	return stageScores.at(level);
ae5d6381 sago007 2015-12-29 13:26 134
}
ae5d6381 sago007 2015-12-29 13:26 135
ae5d6381 sago007 2015-12-29 13:26 136
int GetStageTime(int level) {
ae5d6381 sago007 2015-12-29 13:26 137
	return stageTimes.at(level);
ae5d6381 sago007 2015-12-29 13:26 138
}