git repos / blockattack-game

commit 769a41a0

sago007 · 2008-09-24 12:54
769a41a0683b31b191d1da6e80f7cddf9a20dd76 patch · browse files
parent 281354a75842d43eb59c16f2625ccfc10ad05514

A new config file format. Old configs will be automatically converted


git-svn-id: https://blockattack.svn.sourceforge.net/svnroot/blockattack/trunk@46 9d7177f8-192b-0410-8f35-a16a89829b06

Changed files

M source/code/block.make before
M source/code/common.cc before
M source/code/common.h before
M source/code/main.cpp before
diff --git a/source/code/block.make b/source/code/block.make index 0afa978..98a8bb9 100644 --- a/source/code/block.make +++ b/source/code/block.make
@@ -14,7 +14,7 @@ NETWORK=1
endif
ifndef USE_ABSTRACT_FS
-USE_ABSTRACT_FS=1
+USE_ABSTRACT_FS=0
endif
ifeq ($(DEBUG),1)
diff --git a/source/code/common.cc b/source/code/common.cc index dca8c3c..2dd3a3d 100644 --- a/source/code/common.cc +++ b/source/code/common.cc
@@ -25,6 +25,48 @@ Copyright (C) 2008 Poul Sander
*/
#include "common.h"
+#include <sstream>
+
+//Function to convert numbers to string
+string itoa(int num)
+{
+ stringstream converter;
+ converter << num;
+ return converter.str();
+}
+
+string double2str(double num)
+{
+ stringstream converter;
+ converter << num;
+ return converter.str();
+}
+
+double str2double(string str2parse)
+{
+ try{
+ stringstream converter(str2parse);
+ double val = 0.0;
+ converter >> val;
+ return val;
+ }catch(ios_base::failure f)
+ {
+ return 0.0;
+ }
+}
+
+int str2int(string str2parse)
+{
+ try{
+ stringstream converter(str2parse);
+ int val = 0;
+ converter >> val;
+ return val;
+ }catch(ios_base::failure f)
+ {
+ return 0;
+ }
+}
#ifdef WIN32
//Returns path to "my Documents" in windows:
@@ -117,3 +159,115 @@ commonTime addTotalTime(commonTime toAdd)
cout << "Error writing total time to: " << filename << endl;
return ct;
}
+
+Config* Config::instance = 0;
+
+Config::Config()
+{
+ configMap.clear();
+ load();
+}
+
+void Config::load()
+{
+ string filename = getPathToSaveFiles()+"/configFile";
+ ifstream inFile(filename.c_str());
+ string key;
+ string previuskey;
+ char value[MAX_VAR_LENGTH];
+ if(inFile)
+ {
+ while(!inFile.eof())
+ {
+ inFile >> key;
+ if(key==previuskey)
+ continue;
+ previuskey = key;
+ inFile.get(); //Read the space between the key and the content
+ inFile.getline(value,MAX_VAR_LENGTH);
+ #if DEBUG
+ cout << "Config "<< "read: " << key << " with:\"" << value << "\"" << endl;
+ #endif
+ configMap[key] = (string)value;
+ }
+ inFile.close();
+ }
+}
+
+Config* Config::getInstance()
+{
+ if(Config::instance==0)
+ {
+ Config::instance = new Config();
+
+ }
+ return Config::instance;
+}
+
+void Config::save()
+{
+ string filename = getPathToSaveFiles()+"/configFile";
+ ofstream outFile(filename.c_str(),ios::trunc);
+
+ if(outFile)
+ {
+ //outFile << statMap.size() << endl;
+ map<string,string>::iterator iter;
+ for(iter = configMap.begin(); iter != configMap.end(); iter++)
+ {
+ outFile << iter->first << " " << iter->second << endl;
+ }
+ outFile << "\n";
+ }
+}
+
+bool Config::exists(string varName)
+{
+ //Using that find returns an iterator to the end of the map if not found
+ return configMap.find(varName) != configMap.end();
+}
+
+void Config::setString(string varName, string content)
+{
+ configMap[varName] = content;
+}
+
+void Config::setInt(string varName, int content)
+{
+ configMap[varName] = itoa(content);
+}
+
+void Config::setValue(string varName,double content)
+{
+ configMap[varName] = double2str(content);
+}
+
+string Config::getString(string varName)
+{
+ if(exists(varName))
+ {
+ return configMap[varName];
+ }
+ else
+ return "";
+}
+
+int Config::getInt(string varName)
+{
+ if(exists(varName))
+ {
+ return str2int(configMap[varName]);
+ }
+ else
+ return 0;
+}
+
+double Config::getValue(string varName)
+{
+ if(exists(varName))
+ {
+ return str2double(configMap[varName]);
+ }
+ else
+ return 0.0;
+}
diff --git a/source/code/common.h b/source/code/common.h index 74bf6fc..5c1aa05 100644 --- a/source/code/common.h +++ b/source/code/common.h
@@ -32,6 +32,7 @@ Copyright (C) 2008 Poul Sander
#include <string>
#include <iostream>
#include <fstream>
+#include <map>
#include <stdlib.h>
using namespace std;
@@ -43,11 +44,49 @@ struct commonTime{
unsigned int seconds;
};
+string itoa(int num);
+
string getPathToSaveFiles();
commonTime getTotalTime();
commonTime addTotalTime(commonTime toAdd);
+#define MAX_VAR_LENGTH 1024
+
+class Config
+{
+private:
+ map<string,string> configMap;
+
+ static Config *instance;
+
+ void load();
+protected:
+
+ Config();
+
+
+public:
+
+ static Config* getInstance();
+
+ void save();
+
+ string getString(string varName);
+
+ int getInt(string varName);
+
+ double getValue(string varName);
+
+ void setString(string varName,string content);
+
+ void setInt(string varName,int content);
+
+ void setValue(string varName,double content);
+
+ bool exists(string varName);
+};
+
#endif /* _COMMON_H */
diff --git a/source/code/main.cpp b/source/code/main.cpp index e505621..7d218d9 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp
@@ -41,8 +41,9 @@ Copyright (C) 2008 Poul Sander
#define NETWORK 1
#endif
+//Abstract layer is experimental and appears to cause trouble in some cercumstances. And it is not implemented
#ifndef USE_ABSTRACT_FS
- #define USE_ABSTRACT_FS 1
+ #define USE_ABSTRACT_FS 0
#endif
//Build-in level editor is still experimental!
@@ -57,7 +58,7 @@ Copyright (C) 2008 Poul Sander
#include <iostream>
#include <stdlib.h>
#include <time.h> //Used for srand()
-#include <sstream>
+#include <sstream> //Still used by itoa2
#include <string>
#include "SDL.h" //The SDL libary, used for most things
#include <SDL_mixer.h> //Used for sound & music
@@ -804,12 +805,12 @@ void UnloadImages()
}
//Function to convert numbers to string
-string itoa(int num)
+/*string itoa(int num)
{
stringstream converter;
converter << num;
return converter.str();
-}
+}*/
//Function to convert numbers to string (2 diget)
string itoa2(int num)
@@ -3277,44 +3278,79 @@ int main(int argc, char *argv[])
strcpy(serverAddress, "192.168.0.2 \0");
#endif
- //Reads options from file:
- ifstream optionsFile(optionsPath.c_str(), ios::binary);
- if (optionsFile)
+ Config *configSettings = Config::getInstance();
+ //configSettings->setString("aNumber"," A string");
+ //configSettings->save();
+ if(configSettings->exists("fullscreen")) //Test if an configFile exists
{
- //reads data: xsize,ysize,fullescreen, player1keys, player2keys, MusicEnabled, SoundEnabled,player1name,player2name
- optionsFile.read(reinterpret_cast<char*>(&xsize), sizeof(int));
- optionsFile.read(reinterpret_cast<char*>(&ysize), sizeof(int));
- optionsFile.read(reinterpret_cast<char*>(&bFullscreen), sizeof(bool));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].up), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].down), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].left), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].right), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].change), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].push), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].up), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].down), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].left), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].right), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].change), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].push), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&MusicEnabled), sizeof(bool));
- optionsFile.read(reinterpret_cast<char*>(&SoundEnabled), sizeof(bool));
- optionsFile.read(player1name, 30*sizeof(char));
- optionsFile.read(player2name, 30*sizeof(char));
- //mouseplay?
- if (!optionsFile.eof())
- {
- optionsFile.read(reinterpret_cast<char*>(&mouseplay1), sizeof(bool));
- optionsFile.read(reinterpret_cast<char*>(&mouseplay2), sizeof(bool));
- optionsFile.read(reinterpret_cast<char*>(&joyplay1),sizeof(bool));
- optionsFile.read(reinterpret_cast<char*>(&joyplay2),sizeof(bool));
- }
- optionsFile.close();
- cout << "Data loaded from options file" << endl;
+ bFullscreen = (bool)configSettings->getInt("fullscreen");
+ MusicEnabled = (bool)configSettings->getInt("musicenabled");
+ SoundEnabled = (bool)configSettings->getInt("soundenabled");
+ mouseplay1 = (bool)configSettings->getInt("mouseplay1");
+ mouseplay2 = (bool)configSettings->getInt("mouseplay2");
+ joyplay1 = (bool)configSettings->getInt("joypad1");
+ joyplay2 = (bool)configSettings->getInt("joypad2");
+
+ if(configSettings->exists("player1keyup")) keySettings[0].up = (SDLKey)configSettings->getInt("player1keyup");
+ if(configSettings->exists("player1keydown")) keySettings[0].down = (SDLKey)configSettings->getInt("player1keydown");
+ if(configSettings->exists("player1keyleft")) keySettings[0].left = (SDLKey)configSettings->getInt("player1keyleft");
+ if(configSettings->exists("player1keyright")) keySettings[0].right = (SDLKey)configSettings->getInt("player1keyright");
+ if(configSettings->exists("player1keychange")) keySettings[0].change = (SDLKey)configSettings->getInt("player1keychange");
+ if(configSettings->exists("player1keypush")) keySettings[0].push = (SDLKey)configSettings->getInt("player1keypush");
+
+ if(configSettings->exists("player2keyup")) keySettings[2].up = (SDLKey)configSettings->getInt("player2keyup");
+ if(configSettings->exists("player2keydown")) keySettings[2].down = (SDLKey)configSettings->getInt("player2keydown");
+ if(configSettings->exists("player2keyleft")) keySettings[2].left = (SDLKey)configSettings->getInt("player2keyleft");
+ if(configSettings->exists("player2keyright")) keySettings[2].right = (SDLKey)configSettings->getInt("player2keyright");
+ if(configSettings->exists("player2keychange")) keySettings[2].change = (SDLKey)configSettings->getInt("player2keychange");
+ if(configSettings->exists("player2keypush")) keySettings[2].push = (SDLKey)configSettings->getInt("player2keypush");
+ if(configSettings->exists("player1name"))
+ strncpy(player1name,(configSettings->getString("player1name")).c_str(),28);
+ if(configSettings->exists("player2name"))
+ strncpy(player2name,(configSettings->getString("player2name")).c_str(),28);
+ cout << "Data loaded from config file" << endl;
}
else
{
- cout << "Unable to load options file, using default values" << endl;
+ //Reads options from file:
+ ifstream optionsFile(optionsPath.c_str(), ios::binary);
+ if (optionsFile)
+ {
+ //reads data: xsize,ysize,fullescreen, player1keys, player2keys, MusicEnabled, SoundEnabled,player1name,player2name
+ optionsFile.read(reinterpret_cast<char*>(&xsize), sizeof(int));
+ optionsFile.read(reinterpret_cast<char*>(&ysize), sizeof(int));
+ optionsFile.read(reinterpret_cast<char*>(&bFullscreen), sizeof(bool));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[0].up), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[0].down), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[0].left), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[0].right), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[0].change), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[0].push), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[2].up), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[2].down), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[2].left), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[2].right), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[2].change), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&keySettings[2].push), sizeof(SDLKey));
+ optionsFile.read(reinterpret_cast<char*>(&MusicEnabled), sizeof(bool));
+ optionsFile.read(reinterpret_cast<char*>(&SoundEnabled), sizeof(bool));
+ optionsFile.read(player1name, 30*sizeof(char));
+ optionsFile.read(player2name, 30*sizeof(char));
+ //mouseplay?
+ if (!optionsFile.eof())
+ {
+ optionsFile.read(reinterpret_cast<char*>(&mouseplay1), sizeof(bool));
+ optionsFile.read(reinterpret_cast<char*>(&mouseplay2), sizeof(bool));
+ optionsFile.read(reinterpret_cast<char*>(&joyplay1),sizeof(bool));
+ optionsFile.read(reinterpret_cast<char*>(&joyplay2),sizeof(bool));
+ }
+ optionsFile.close();
+ cout << "Data loaded from oldstyle options file" << endl;
+ }
+ else
+ {
+ cout << "Unable to load options file, using default values" << endl;
+ }
}
xsize = 1024;
@@ -4572,41 +4608,31 @@ int main(int argc, char *argv[])
//Saves options
if (!editorMode)
{
- ofstream optionsFileOut;
- optionsFileOut.open(optionsPath.c_str(),ios::binary|ios::trunc);
- if (optionsFileOut)
- {
- //writes data: xsize,ysize,fullescreen, player1keys, player2keys, MusicEnabled, SoundEnabled,player1name,player2name
- optionsFileOut.write(reinterpret_cast<char*>(&xsize),sizeof(int));
- optionsFileOut.write(reinterpret_cast<char*>(&ysize),sizeof(int));
- optionsFileOut.write(reinterpret_cast<char*>(&bFullscreen),sizeof(bool));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[0].up), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[0].down), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[0].left), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[0].right), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[0].change), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[0].push), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[2].up), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[2].down), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[2].left), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[2].right), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[2].change), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&keySettings[2].push), sizeof(SDLKey));
- optionsFileOut.write(reinterpret_cast<char*>(&MusicEnabled),sizeof(bool));
- optionsFileOut.write(reinterpret_cast<char*>(&SoundEnabled),sizeof(bool));
- optionsFileOut.write(player1name,30*sizeof(char));
- optionsFileOut.write(player2name,30*sizeof(char));
- optionsFileOut.write(reinterpret_cast<char*>(&mouseplay1),sizeof(bool));
- optionsFileOut.write(reinterpret_cast<char*>(&mouseplay2),sizeof(bool));
- optionsFileOut.write(reinterpret_cast<char*>(&joyplay1),sizeof(bool));
- optionsFileOut.write(reinterpret_cast<char*>(&joyplay2),sizeof(bool));
- optionsFileOut.close();
- cout << "options written to file" << endl;
- }
- else
- {
- cout << "Failed to write options" << endl;
- }
+ configSettings->setInt("fullscreen",(int)bFullscreen);
+ configSettings->setInt("musicenabled",(int)MusicEnabled);
+ configSettings->setInt("soundenabled",(int)SoundEnabled);
+ configSettings->setInt("mouseplay1",(int)mouseplay1);
+ configSettings->setInt("mouseplay2",(int)mouseplay2);
+ configSettings->setInt("joypad1",(int)joyplay1);
+ configSettings->setInt("joypad2",(int)joyplay2);
+
+ configSettings->setInt("player1keyup",(int)keySettings[0].up);
+ configSettings->setInt("player1keydown",(int)keySettings[0].down);
+ configSettings->setInt("player1keyleft",(int)keySettings[0].left);
+ configSettings->setInt("player1keyright",(int)keySettings[0].right);
+ configSettings->setInt("player1keychange",(int)keySettings[0].change);
+ configSettings->setInt("player1keypush",(int)keySettings[0].push);
+
+ configSettings->setInt("player2keyup",(int)keySettings[2].up);
+ configSettings->setInt("player2keydown",(int)keySettings[2].down);
+ configSettings->setInt("player2keyleft",(int)keySettings[2].left);
+ configSettings->setInt("player2keyright",(int)keySettings[2].right);
+ configSettings->setInt("player2keychange",(int)keySettings[2].change);
+ configSettings->setInt("player2keypush",(int)keySettings[2].push);
+
+ configSettings->setString("player1name",player1name);
+ configSettings->setString("player2name",player2name);
+ configSettings->save();
}
Stats::getInstance()->save();