git repos / blockattack-game

commit 549ecfae

sago007 · 2016-10-09 08:37
549ecfaea17ecd79082bdd93bb91b4a46afb30db patch · browse files
parent 29edda2da6075134cf638f118cceacef0250471b

Replaced my old custom safe but slow itoa with std::to_string.
Also added some missing curly brackets.

Changed files

M source/code/BlockGame.cpp before
M source/code/BlockGameSdl.inc before
M source/code/ScoresDisplay.cpp before
M source/code/common.cpp before
M source/code/common.h before
M source/code/main.cpp before
M source/code/sago/SagoSpriteHolder.cpp before
diff --git a/source/code/BlockGame.cpp b/source/code/BlockGame.cpp index e8aebfe..6479159 100644 --- a/source/code/BlockGame.cpp +++ b/source/code/BlockGame.cpp
@@ -262,10 +262,11 @@ void BlockGame::NewGame(const BlockGameStartInfo& s) {
puzzleMode = true;
Level = s.level;
MovesLeft = PuzzleNumberOfMovesAllowed(Level);
- for (int i=0; i<6; i++)
+ for (int i=0; i<6; i++) {
for (int j=0; j<12; j++) {
board[i][j+1] = PuzzleGetBrick(Level,i,j);
}
+ }
baseSpeed = 100000;
speed = 100000;
@@ -338,10 +339,11 @@ void BlockGame::NewGame( unsigned int ticks) {
pushedPixelAt = gameStartedAt;
nextGarbageNumber = 10;
handicap=0;
- for (int i=0; i<7; i++)
+ for (int i=0; i<7; i++) {
for (int j=0; j<30; j++) {
board[i][j] = -1;
}
+ }
for (int i=0; i<NUMBEROFCHAINS; i++) {
chainUsed[i]=false;
chainSize[i] = 0;
@@ -368,12 +370,12 @@ void BlockGame::setPlayerWon() {
Stats::getInstance()->addOne("totalWins");
if (vsAI) {
//We have defeated an AI
- Stats::getInstance()->addOne("defeatedAI"+itoa(getAIlevel()));
+ Stats::getInstance()->addOne("defeatedAI"+std::to_string(getAIlevel()));
}
}
if (AI_Enabled && vsAI) {
//The AI have defeated a human player
- Stats::getInstance()->addOne("defeatedByAI"+itoa(getAIlevel()));
+ Stats::getInstance()->addOne("defeatedByAI"+std::to_string(getAIlevel()));
}
FinalizeBlockGameInfo();
}
@@ -399,31 +401,36 @@ void BlockGame::setDraw() {
//Test if LineNr is an empty line, returns false otherwise.
bool BlockGame::LineEmpty(int lineNr) const {
bool empty = true;
- for (int i = 0; i <7; i++)
+ for (int i = 0; i <7; i++) {
if (board[i][lineNr] != -1) {
empty = false;
}
+ }
return empty;
}
//Test if the entire board is empty (used for Puzzles)
bool BlockGame::BoardEmpty() const {
bool empty = true;
- for (int i=0; i<6; i++)
- for (int j=1; j<13; j++)
+ for (int i=0; i<6; i++) {
+ for (int j=1; j<13; j++) {
if (board[i][j] != -1) {
empty = false;
}
+ }
+ }
return empty;
}
//Anything that the user can't move? In that case Game Over cannot occur
bool BlockGame::hasStaticContent() const {
- for (int i=0; i<6; i++)
- for (int j=1; j<13; j++)
+ for (int i=0; i<6; i++) {
+ for (int j=1; j<13; j++) {
if (board[i][j] >= 10000000) { //Higher than this means combos (garbage is static, but the stack is static but nothing to do about it)
return true; //They are static
}
+ }
+ }
return false; //Return false if no static object found
}
@@ -554,7 +561,7 @@ void BlockGame::putStartBlocks(int n) {
void BlockGame::ReduceStuff() {
int howMuchHang = (ticks - FRAMELENGTH*hangTicks)/FRAMELENGTH;
if (howMuchHang>0) {
- for (int i=0; i<7; i++)
+ for (int i=0; i<7; i++) {
for (int j=0; j<30; j++) {
if ((board[i][j]/BLOCKHANG)%10==1) {
int hangNumber = (board[i][j]/10)%100;
@@ -577,6 +584,7 @@ void BlockGame::ReduceStuff() {
}
}
}
+ }
}
hangTicks+=howMuchHang;
}
@@ -743,19 +751,21 @@ void BlockGame::ClearBlocks() {
combo++;
}
else {
- if (combo>2)
+ if (combo>2) {
for (int k = i-combo; k<i; k++) {
toBeCleared[j][k] = true;
}
+ }
combo=1;
previus = board[j][i]%10000000;
}
} //if board
else {
- if (combo>2)
+ if (combo>2) {
for (int k = i-combo; k<i; k++) {
toBeCleared[j][k] = true;
}
+ }
combo = 0;
previus = -1;
}
@@ -764,11 +774,11 @@ void BlockGame::ClearBlocks() {
} //for j
chain = 0;
- for (int i=0; i<6; i++)
+ for (int i=0; i<6; i++) {
for (int j=0; j<30; j++) {
//Clears blocks marked for clearing
int temp=board[i][j];
- if (1==((temp/BLOCKWAIT)%10))
+ if (1==((temp/BLOCKWAIT)%10)) {
if (((temp/10)%100)==0) {
if (chainSize[chain]<chainSize[board[i][j]/10000000]) {
chain = board[i][j]/10000000;
@@ -779,7 +789,9 @@ void BlockGame::ClearBlocks() {
AddExplosion(i, j);
board[i][j]=-2;
}
+ }
}
+ }
for (int i=0; i<7; i++) {
bool setChain=false;
for (int j=0; j<30; j++) {
@@ -791,12 +803,12 @@ void BlockGame::ClearBlocks() {
setChain=true;
BlockPopEvent();
}
- if (board[i][j]!=-1)
+ if (board[i][j]!=-1) {
if ((setChain)&&((board[i][j]/GARBAGE)%10!=1)&&((board[i][j]/GARBAGE)%10!=2)) {
board[i][j]=((board[i][j]%CHAINPLACE)+CHAINPLACE*chain);
//somethingsGottaFall = true;
}
-
+ }
}
}
int startvalue;
@@ -897,7 +909,7 @@ void BlockGame::ClearBlocks() {
if (toBeCleared[j][i]) {
if (!dead) {
dead=true;
- string tempS = itoa(chainSize[chain]);
+ string tempS = std::to_string(chainSize[chain]);
if (chainSize[chain]>1) {
AddText(j, i, tempS, 1000);
}
@@ -1020,7 +1032,7 @@ void BlockGame::ClearBlocks() {
LongChainDoneEvent();
}
if (chainSize[i]>1 && !puzzleMode && recordStats) {
- Stats::getInstance()->addOne((string)"chainX"+itoa(chainSize[i]));
+ Stats::getInstance()->addOne((string)"chainX"+std::to_string(chainSize[i]));
}
chainUsed[i]=false;
}
@@ -1382,10 +1394,11 @@ double BlockGame::firstInLine(int line, int type) {
cerr << "Warning: first in Line: " << line << "\n";
return 3.0;
}
- for (int i=0; i<6; i++)
+ for (int i=0; i<6; i++) {
if (board[i][line]==type) {
return (double)i;
}
+ }
return 3.0;
}
@@ -1467,11 +1480,12 @@ void BlockGame::AI_ClearHori() {
if (left<right) {
// cout << ", right>left";
int count=0;
- for (int i=0; (i<4)&&(count<1); i++)
+ for (int i=0; (i<4)&&(count<1); i++) {
if ((board[i][lowestLine]==AIcolorToClear)&&((i==0)||(board[i+1][lowestLine]!=AIcolorToClear))) {
count++;
xplace = i;
}
+ }
}
else {
// cout << ", left>=right";
@@ -1517,7 +1531,6 @@ bool BlockGame::veriClearPossible() {
AIlineToClear = i;
found=true;
}
-
}
}
return found;
diff --git a/source/code/BlockGameSdl.inc b/source/code/BlockGameSdl.inc index 4865972..a3c108a 100644 --- a/source/code/BlockGameSdl.inc +++ b/source/code/BlockGameSdl.inc
@@ -301,7 +301,7 @@ public:
}
if (puzzleMode&&(!bGameOver)) {
//We need to write nr. of moves left!
- strHolder = _("Moves left: ") + itoa(MovesLeft);
+ strHolder = _("Moves left: ") + std::to_string(MovesLeft);
nf_standard_blue_font.draw(screen, topx+5, topy+5, "%s",strHolder.c_str());
}
@@ -347,7 +347,7 @@ public:
#if DEBUG
if (AI_Enabled&&(!bGameOver)) {
- strHolder = "AI_status: " + itoa(AIstatus)+ ", "+ itoa(AIlineToClear);
+ strHolder = "AI_status: " + std::to_string(AIstatus)+ ", "+ std::to_string(AIlineToClear);
//NFont_Write( 5, 5, strHolder.c_str());
nf_standard_blue_font.draw(screen, topx+5, topy+5, "%s",strHolder.c_str());
}
diff --git a/source/code/ScoresDisplay.cpp b/source/code/ScoresDisplay.cpp index 602bc25..6e42914 100644 --- a/source/code/ScoresDisplay.cpp +++ b/source/code/ScoresDisplay.cpp
@@ -79,18 +79,18 @@ void ScoresDisplay::DrawStats() {
NFont_Write(screen, 10,y,_("Chains") );
for (int i=2; i<13; i++) {
y+=y_spacing;
- NFont_Write(screen, 10,y,(itoa(i)+"X").c_str());
- string numberAsString = itoa(Stats::getInstance()->getNumberOf("chainX"+itoa(i)));
+ NFont_Write(screen, 10,y,(std::to_string(i)+"X").c_str());
+ string numberAsString = std::to_string(Stats::getInstance()->getNumberOf("chainX"+std::to_string(i)));
NFont_Write(screen, 300,y,numberAsString.c_str());
}
y+=y_spacing*2;
NFont_Write(screen, 10,y,_("Lines Pushed: ") );
- string numberAsString = itoa(Stats::getInstance()->getNumberOf("linesPushed"));
+ string numberAsString = std::to_string(Stats::getInstance()->getNumberOf("linesPushed"));
NFont_Write(screen, 300,y,numberAsString.c_str());
y+=y_spacing;
NFont_Write(screen, 10,y, _("Puzzles solved: ") );
- numberAsString = itoa(Stats::getInstance()->getNumberOf("puzzlesSolved"));
+ numberAsString = std::to_string(Stats::getInstance()->getNumberOf("puzzlesSolved"));
NFont_Write(screen, 300,y,numberAsString.c_str());
y+=y_spacing*2;
@@ -123,9 +123,9 @@ void ScoresDisplay::DrawStats() {
NFont_Write(screen, x_offset,y, _("VS CPU (win/loss)") );
for (int i=0; i<7; i++) {
y += y_spacing;
- NFont_Write(screen, x_offset,y,string("AI "+itoa(i+1)).c_str());
- numberAsString = itoa(Stats::getInstance()->getNumberOf("defeatedAI"+itoa(i)));
- string numberAsString2 = itoa(Stats::getInstance()->getNumberOf("defeatedByAI"+itoa(i)));
+ NFont_Write(screen, x_offset,y,string("AI "+std::to_string(i+1)).c_str());
+ numberAsString = std::to_string(Stats::getInstance()->getNumberOf("defeatedAI"+std::to_string(i)));
+ string numberAsString2 = std::to_string(Stats::getInstance()->getNumberOf("defeatedByAI"+std::to_string(i)));
string toPrint = numberAsString + "/" + numberAsString2;
NFont_Write(screen, x_offset+230,y,toPrint.c_str());
}
diff --git a/source/code/common.cpp b/source/code/common.cpp index e1dc6cb..a8aae2f 100644 --- a/source/code/common.cpp +++ b/source/code/common.cpp
@@ -38,21 +38,6 @@ using boost::format;
static stringstream converter;
-//Function to convert numbers to string
-string itoa(int num) {
- converter.str(std::string());
- converter.clear();
- converter << num;
- return converter.str();
-}
-
-string double2str(double num) {
- converter.str(std::string());
- converter.clear();
- converter << num;
- return converter.str();
-}
-
bool strequals(const char* a, const char* b) {
return strcmp(a,b) == 0;
}
@@ -245,11 +230,11 @@ void Config::setString(const string& varName, const string& content) {
}
void Config::setInt(const string& varName, int content) {
- configMap[varName] = itoa(content);
+ configMap[varName] = std::to_string(content);
}
void Config::setValue(const string& varName,double content) {
- configMap[varName] = double2str(content);
+ configMap[varName] = std::to_string(content);
}
string Config::getString(const string& varName) {
diff --git a/source/code/common.h b/source/code/common.h index 330d858..327d396 100644 --- a/source/code/common.h +++ b/source/code/common.h
@@ -48,7 +48,7 @@ struct commonTime
unsigned int seconds = 0;
};
-std::string itoa(int num) __attribute__((const));
+//std::string itoa(int num) __attribute__((const));
bool strequals(const char* a, const char* b);
@@ -59,8 +59,6 @@ bool strequals(const char* a, const char* b);
*/
int str2int(const std::string &str2parse) __attribute__((const));
-std::string double2str(double num) __attribute__((const));
-
void dieOnNullptr(bool, const char* msg);
/**
diff --git a/source/code/main.cpp b/source/code/main.cpp index ad19831..51aaf0b 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp
@@ -37,7 +37,7 @@ http://www.blockattack.net
#include <iostream>
#include <stdlib.h>
#include <time.h> //Used for srand()
-#include <sstream> //Still used by itoa2
+#include <sstream> //Still used by std::to_string2
#include <string>
#include "SDL.h" //The SDL libary, used for most things
#include <SDL_mixer.h> //Used for sound & music
@@ -570,7 +570,7 @@ void writeScreenShot() {
cout << "Saving screenshot" << "\n";
}
int rightNow = (int)time(nullptr);
- string buf = getPathToSaveFiles() + "/screenshots/screenshot"+itoa(rightNow)+".bmp";
+ string buf = getPathToSaveFiles() + "/screenshots/screenshot"+std::to_string(rightNow)+".bmp";
SDL_Surface* sreenshotSurface = SDL_CreateRGBSurface(0, 1024, 768, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
SDL_RenderReadPixels(screen, NULL, SDL_PIXELFORMAT_ARGB8888, sreenshotSurface->pixels, sreenshotSurface->pitch);
SDL_SaveBMP(sreenshotSurface, buf.c_str());
@@ -667,7 +667,7 @@ void DrawEverything(int xsize, int ysize,BlockGameSdl* theGame, BlockGameSdl* th
theGame->DoPaintJob();
theGame2->DoPaintJob();
string strHolder;
- strHolder = itoa(theGame->GetScore()+theGame->GetHandicap());
+ strHolder = std::to_string(theGame->GetScore()+theGame->GetHandicap());
NFont_Write(screen, theGame->GetTopX()+310,theGame->GetTopY()+100,strHolder.c_str());
if (theGame->GetAIenabled()) {
NFont_Write(screen, theGame->GetTopX()+10,theGame->GetTopY()-34,_("AI") );
@@ -697,10 +697,10 @@ void DrawEverything(int xsize, int ysize,BlockGameSdl* theGame, BlockGameSdl* th
seconds=0;
}
if (seconds>9) {
- strHolder = itoa(minutes)+":"+itoa(seconds);
+ strHolder = std::to_string(minutes)+":"+std::to_string(seconds);
}
else {
- strHolder = itoa(minutes)+":0"+itoa(seconds);
+ strHolder = std::to_string(minutes)+":0"+std::to_string(seconds);
}
//if ((SoundEnabled)&&(!NoSound)&&(tid>0)&&(seconds<5)&&(minutes == 0)&&(seconds>1)&&(!(Mix_Playing(6)))) Mix_PlayChannel(6,heartBeat,0);
NFont_Write(screen, theGame->GetTopX()+310,theGame->GetTopY()+150,strHolder.c_str());
@@ -715,17 +715,17 @@ void DrawEverything(int xsize, int ysize,BlockGameSdl* theGame, BlockGameSdl* th
seconds=(theGame->GetGameEndedAt()/1000)%60;
}
if (seconds>9) {
- strHolder = itoa(minutes)+":"+itoa(seconds);
+ strHolder = std::to_string(minutes)+":"+std::to_string(seconds);
}
else {
- strHolder = itoa(minutes)+":0"+itoa(seconds);
+ strHolder = std::to_string(minutes)+":0"+std::to_string(seconds);
}
NFont_Write(screen, theGame->GetTopX()+310,theGame->GetTopY()+150,strHolder.c_str());
}
- strHolder = itoa(theGame->GetChains());
+ strHolder = std::to_string(theGame->GetChains());
NFont_Write(screen, theGame->GetTopX()+310,theGame->GetTopY()+200,strHolder.c_str());
//drawspeedLevel:
- strHolder = itoa(theGame->GetSpeedLevel());
+ strHolder = std::to_string(theGame->GetSpeedLevel());
NFont_Write(screen, theGame->GetTopX()+310,theGame->GetTopY()+250,strHolder.c_str());
if ((theGame->isStageClear()) &&(theGame->GetTopY()+700+50*(theGame->GetStageClearLimit()-theGame->GetLinesCleared())-theGame->GetPixels()-1<600+theGame->GetTopY())) {
oldBubleX = theGame->GetTopX()+280;
@@ -781,7 +781,7 @@ void DrawEverything(int xsize, int ysize,BlockGameSdl* theGame, BlockGameSdl* th
}
}
- strHolder = itoa(theGame2->GetScore()+theGame2->GetHandicap());
+ strHolder = std::to_string(theGame2->GetScore()+theGame2->GetHandicap());
NFont_Write(screen, theGame2->GetTopX()+310,theGame2->GetTopY()+100,strHolder.c_str());
if (theGame2->GetAIenabled()) {
NFont_Write(screen, theGame2->GetTopX()+10,theGame2->GetTopY()-34,_("AI") );
@@ -808,10 +808,10 @@ void DrawEverything(int xsize, int ysize,BlockGameSdl* theGame, BlockGameSdl* th
seconds=0;
}
if (seconds>9) {
- strHolder = itoa(minutes)+":"+itoa(seconds);
+ strHolder = std::to_string(minutes)+":"+std::to_string(seconds);
}
else {
- strHolder = itoa(minutes)+":0"+itoa(seconds);
+ strHolder = std::to_string(minutes)+":0"+std::to_string(seconds);
}
//if ((SoundEnabled)&&(!NoSound)&&(tid>0)&&(seconds<5)&&(minutes == 0)&&(seconds>1)&&(!(Mix_Playing(6)))) Mix_PlayChannel(6,heartBeat,0);
NFont_Write(screen, theGame2->GetTopX()+310,theGame2->GetTopY()+150,strHolder.c_str());
@@ -826,16 +826,16 @@ void DrawEverything(int xsize, int ysize,BlockGameSdl* theGame, BlockGameSdl* th
seconds=(theGame2->GetGameEndedAt()/1000)%60;
}
if (seconds>9) {
- strHolder = itoa(minutes)+":"+itoa(seconds);
+ strHolder = std::to_string(minutes)+":"+std::to_string(seconds);
}
else {
- strHolder = itoa(minutes)+":0"+itoa(seconds);
+ strHolder = std::to_string(minutes)+":0"+std::to_string(seconds);
}
NFont_Write(screen, theGame2->GetTopX()+310,theGame2->GetTopY()+150,strHolder.c_str());
}
- strHolder = itoa(theGame2->GetChains());
+ strHolder = std::to_string(theGame2->GetChains());
NFont_Write(screen, theGame2->GetTopX()+310,theGame2->GetTopY()+200,strHolder.c_str());
- strHolder = itoa(theGame2->GetSpeedLevel());
+ strHolder = std::to_string(theGame2->GetSpeedLevel());
NFont_Write(screen, theGame2->GetTopX()+310,theGame2->GetTopY()+250,strHolder.c_str());
}
//player2 finnish
diff --git a/source/code/sago/SagoSpriteHolder.cpp b/source/code/sago/SagoSpriteHolder.cpp index d3383f4..0773f44 100644 --- a/source/code/sago/SagoSpriteHolder.cpp +++ b/source/code/sago/SagoSpriteHolder.cpp
@@ -35,10 +35,6 @@
#include <string.h>
#include <boost/algorithm/string/predicate.hpp>
-
-//I truely hate the C way of checking for equal. Usually read: "if not X compares to Y then they must be equal"
-#define strequal(X,Y) strcmp(X,Y)==0
-
using std::string;
using std::cerr;
using std::cout;