commit cd12e0fe
totalTime is now generic - play time added.
The stats page now shows both Play Time and Total Time using the new generic TimeHandler class.
git-svn-id: https://blockattack.svn.sourceforge.net/svnroot/blockattack/trunk@62 9d7177f8-192b-0410-8f35-a16a89829b06
Changed files
| M | source/code/BlockGame.hpp before |
| M | source/code/common.cc before |
| M | source/code/common.h before |
| M | source/code/main.cpp before |
| M | source/code/stats.cc before |
diff --git a/source/code/BlockGame.hpp b/source/code/BlockGame.hpp
index 600731b..c84197e 100644
--- a/source/code/BlockGame.hpp
+++ b/source/code/BlockGame.hpp
@@ -415,7 +415,13 @@ public:
//Prints "winner" and ends game
void setPlayerWon() {
if (!bGameOver)
+ {
gameEndedAfter = SDL_GetTicks()-gameStartedAt; //We game ends now!
+ if(!AI_Enabled && !bReplaying)
+ {
+ TimeHandler::addTime("playTime",TimeHandler::ms2ct(gameEndedAfter));
+ }
+ }
theReplay.setFinalFrame(getPackage(), 1);
bGameOver = true;
hasWonTheGame = true;
@@ -450,6 +456,10 @@ public:
//Prints "draw" and ends the game
void setDraw() {
bGameOver = true;
+ if(!AI_Enabled && !bReplaying)
+ {
+ TimeHandler::addTime("playTime",TimeHandler::ms2ct(gameEndedAfter));
+ }
theReplay.setFinalFrame(getPackage(), 3);
hasWonTheGame = false;
bDraw = true;
@@ -1176,8 +1186,13 @@ public:
//prints "Game Over" and ends game
void SetGameOver() {
- if (!bGameOver)
- gameEndedAfter = SDL_GetTicks()-gameStartedAt; //We game ends now!
+ if (!bGameOver) {
+ gameEndedAfter = SDL_GetTicks()-gameStartedAt; //We game ends now!
+ if(!AI_Enabled && !bReplaying)
+ {
+ TimeHandler::addTime("playTime",TimeHandler::ms2ct(gameEndedAfter));
+ }
+ }
theReplay.setFinalFrame(getPackage(), 0);
bGameOver = true;
showGame = false;
diff --git a/source/code/common.cc b/source/code/common.cc
index d1ada46..b029fba 100644
--- a/source/code/common.cc
+++ b/source/code/common.cc
@@ -121,7 +121,7 @@ string getPathToSaveFiles()
/**
* Takes a number of milliseconds and returns the value in commonTime format.
*/
-commonTime ms2ct(unsigned int milliseconds)
+commonTime TimeHandler::ms2ct(unsigned int milliseconds)
{
commonTime ct;
ct.days = 0;
@@ -134,13 +134,13 @@ commonTime ms2ct(unsigned int milliseconds)
return ct;
}
-commonTime getTotalTime()
+commonTime TimeHandler::getTime(string name)
{
commonTime ct;
- ct.days = Config::getInstance()->getInt("totalTimeDays");
- ct.hours = Config::getInstance()->getInt("totalTimeHours");
- ct.minutes = Config::getInstance()->getInt("totalTimeMinutes");
- ct.seconds = Config::getInstance()->getInt("totalTimeSeconds");
+ ct.days = Config::getInstance()->getInt(name+"Days");
+ ct.hours = Config::getInstance()->getInt(name+"Hours");
+ ct.minutes = Config::getInstance()->getInt(name+"Minutes");
+ ct.seconds = Config::getInstance()->getInt(name+"Seconds");
return ct;
}
@@ -148,9 +148,9 @@ commonTime getTotalTime()
* Returns the total runtime with toAdd added but without writing it to config file.
* Used for stats
*/
-commonTime peekTotalTime(commonTime toAdd)
+commonTime TimeHandler::peekTime(string name, commonTime toAdd)
{
- commonTime ct = getTotalTime();
+ commonTime ct = getTime(name);
ct.seconds +=toAdd.seconds;
ct.minutes +=ct.seconds/60;
@@ -172,14 +172,14 @@ commonTime peekTotalTime(commonTime toAdd)
* Same as peekTotalTime but writes the time to the config file.
* Should only be called only once! when the program shuts down
*/
-commonTime addTotalTime(commonTime toAdd)
+commonTime TimeHandler::addTime(string name, commonTime toAdd)
{
- commonTime ct = peekTotalTime(toAdd);
+ commonTime ct = peekTime(name,toAdd);
- Config::getInstance()->setInt("totalTimeDays",ct.days);
- Config::getInstance()->setInt("totalTimeHours",ct.hours);
- Config::getInstance()->setInt("totalTimeMinutes",ct.minutes);
- Config::getInstance()->setInt("totalTimeSeconds",ct.seconds);
+ Config::getInstance()->setInt(name+"Days",ct.days);
+ Config::getInstance()->setInt(name+"Hours",ct.hours);
+ Config::getInstance()->setInt(name+"Minutes",ct.minutes);
+ Config::getInstance()->setInt(name+"Seconds",ct.seconds);
return ct;
}
diff --git a/source/code/common.h b/source/code/common.h
index d7a498a..e18a377 100644
--- a/source/code/common.h
+++ b/source/code/common.h
@@ -59,13 +59,17 @@ string itoa(int num);
string getPathToSaveFiles();
-commonTime ms2ct(unsigned int milliseconds);
-
-commonTime getTotalTime();
-
-commonTime peekTotalTime(commonTime toAdd);
-
-commonTime addTotalTime(commonTime toAdd);
+class TimeHandler
+{
+public:
+ static commonTime ms2ct(unsigned int milliseconds);
+
+ static commonTime getTime(string name);
+
+ static commonTime peekTime(string name, commonTime toAdd);
+
+ static commonTime addTime(string name, commonTime toAdd);
+};
#define MAX_VAR_LENGTH 1024
diff --git a/source/code/main.cpp b/source/code/main.cpp
index d85ff3c..ee981aa 100644
--- a/source/code/main.cpp
+++ b/source/code/main.cpp
@@ -1941,7 +1941,7 @@ void DrawStats()
y+=y_spacing*2;
SFont_Write(screen,fBlueFont,10,y,"Run time: ");
- commonTime ct = peekTotalTime(ms2ct(SDL_GetTicks()));
+ commonTime ct = TimeHandler::peekTime("totalTime",TimeHandler::ms2ct(SDL_GetTicks()));
y+=y_spacing;
SFont_Write(screen,fBlueFont,10,y,((string)("Days: "+itoa(ct.days))).c_str());
y+=y_spacing;
@@ -1951,6 +1951,19 @@ void DrawStats()
y+=y_spacing;
SFont_Write(screen,fBlueFont,10,y,((string)("Seconds: "+itoa(ct.seconds))).c_str());
+ y-=y_spacing*4; //Four rows back
+ const int x_offset3 = 1024/3+10; //Ofset for three rows
+ SFont_Write(screen,fBlueFont,x_offset3,y,"Play time: ");
+ ct = TimeHandler::getTime("playTime");
+ y+=y_spacing;
+ SFont_Write(screen,fBlueFont,x_offset3,y,((string)("Days: "+itoa(ct.days))).c_str());
+ y+=y_spacing;
+ SFont_Write(screen,fBlueFont,x_offset3,y,((string)("Hours: "+itoa(ct.hours))).c_str());
+ y+=y_spacing;
+ SFont_Write(screen,fBlueFont,x_offset3,y,((string)("Minutes: "+itoa(ct.minutes))).c_str());
+ y+=y_spacing;
+ SFont_Write(screen,fBlueFont,x_offset3,y,((string)("Seconds: "+itoa(ct.seconds))).c_str());
+
const int x_offset = 1024/2+10;
y = 5+y_spacing*2;
SFont_Write(screen,fBlueFont,x_offset,y,"VS CPU (win/loss)");
@@ -4798,11 +4811,11 @@ int main(int argc, char *argv[])
//calculate uptime:
//int hours, mins, secs,
- commonTime ct = ms2ct(SDL_GetTicks());
+ commonTime ct = TimeHandler::ms2ct(SDL_GetTicks());
cout << "Block Attack - Rise of the Blocks ran for: " << ct.hours << " hours " << ct.minutes << " mins and " << ct.seconds << " secs" << endl;
- ct = addTotalTime(ct);
+ ct = TimeHandler::addTime("totalTime",ct);
cout << "Total run time is now: " << ct.days << " days " << ct.hours << " hours " << ct.minutes << " mins and " << ct.seconds << " secs" << endl;
diff --git a/source/code/stats.cc b/source/code/stats.cc
index 40baa8c..7364e08 100644
--- a/source/code/stats.cc
+++ b/source/code/stats.cc
@@ -107,6 +107,6 @@ void Stats::addOne(string statName)
bool Stats::exists(string statName)
{
- //Using that find returns an iterator to the end of the map if not found
+ //Using that 'find' returns an iterator to the end of the map if not found
return statMap.find(statName) != statMap.end();
}