forked from pioneerspacesim/pioneer
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate config parser from game config init. now we can make other k…
…inds of config file if we like
- Loading branch information
Showing
7 changed files
with
85 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "GameConfig.h" | ||
#include "KeyBindings.h" | ||
|
||
GameConfig::GameConfig(const std::string &filename) : IniConfig(filename) | ||
{ | ||
// set defaults | ||
(*this)["EnableHDR"] = "0"; | ||
(*this)["DisableShaders"] = "0"; | ||
(*this)["DisableSound"] = "0"; | ||
(*this)["StartFullscreen"] = "0"; | ||
(*this)["ScrWidth"] = "800"; | ||
(*this)["ScrHeight"] = "600"; | ||
(*this)["DetailCities"] = "1"; | ||
(*this)["DetailPlanets"] = "1"; | ||
(*this)["SfxVolume"] = "0.8"; | ||
(*this)["EnableJoystick"] = "1"; | ||
(*this)["InvertMouseY"] = "0"; | ||
|
||
KeyBindings::SetDefaults(); | ||
|
||
Load(); | ||
|
||
KeyBindings::OnKeyBindingsChanged(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef _GAMECONFIG_H | ||
#define _GAMECONFIG_H | ||
|
||
#include "IniConfig.h" | ||
|
||
class GameConfig : public IniConfig { | ||
public: | ||
GameConfig(const std::string &filename); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,44 @@ | ||
#include "libs.h" | ||
#include "IniConfig.h" | ||
#include "KeyBindings.h" | ||
|
||
void IniConfig::Load(const std::string &filename_) | ||
void IniConfig::Load() | ||
{ | ||
this->clear(); | ||
this->filename = filename_; | ||
FILE *f = fopen(filename_.c_str(), "r"); | ||
|
||
// set defaults | ||
(*this)["EnableHDR"] = "0"; | ||
(*this)["DisableShaders"] = "0"; | ||
(*this)["DisableSound"] = "0"; | ||
(*this)["StartFullscreen"] = "0"; | ||
(*this)["ScrWidth"] = "800"; | ||
(*this)["ScrHeight"] = "600"; | ||
(*this)["DetailCities"] = "1"; | ||
(*this)["DetailPlanets"] = "1"; | ||
(*this)["SfxVolume"] = "0.8"; | ||
(*this)["EnableJoystick"] = "1"; | ||
(*this)["InvertMouseY"] = "0"; | ||
|
||
KeyBindings::SetDefaults(); | ||
FILE *f = fopen(m_filename.c_str(), "r"); | ||
if (!f) return; | ||
|
||
if (f) { | ||
char buf[1024]; | ||
while (fgets(buf, sizeof(buf), f)) { | ||
if (buf[0] == '#') continue; | ||
char *sep = strchr(buf, '='); | ||
char *kend = sep; | ||
if (!sep) continue; | ||
*sep = 0; | ||
// strip whitespace | ||
while (isspace(*(--kend))) *kend = 0; | ||
while (isspace(*(++sep))) *sep = 0; | ||
// snip \r, \n | ||
char *vend = sep; | ||
while (*(++vend)) if ((*vend == '\r') || (*vend == '\n')) { *vend = 0; break; } | ||
std::string key = std::string(buf); | ||
std::string val = std::string(sep); | ||
(*this)[key] = val; | ||
} | ||
fclose(f); | ||
char buf[1024]; | ||
while (fgets(buf, sizeof(buf), f)) { | ||
if (buf[0] == '#') continue; | ||
char *sep = strchr(buf, '='); | ||
char *kend = sep; | ||
if (!sep) continue; | ||
*sep = 0; | ||
// strip whitespace | ||
while (isspace(*(--kend))) *kend = 0; | ||
while (isspace(*(++sep))) *sep = 0; | ||
// snip \r, \n | ||
char *vend = sep; | ||
while (*(++vend)) if ((*vend == '\r') || (*vend == '\n')) { *vend = 0; break; } | ||
std::string key = std::string(buf); | ||
std::string val = std::string(sep); | ||
(*this)[key] = val; | ||
} | ||
|
||
KeyBindings::OnKeyBindingsChanged(); | ||
fclose(f); | ||
} | ||
|
||
bool IniConfig::Save() | ||
{ | ||
FILE *f = fopen(filename.c_str(), "w"); | ||
if (!f) { | ||
FILE *f = fopen(m_filename.c_str(), "w"); | ||
if (!f) | ||
// XXX do something useful here | ||
return false; | ||
} else { | ||
for (std::map<std::string, std::string>::const_iterator i = begin(); i!=end(); ++i) { | ||
if ((*i).second != "") fprintf(f, "%s=%s\n", (*i).first.c_str(), (*i).second.c_str()); | ||
} | ||
fclose(f); | ||
return true; | ||
|
||
for (std::map<std::string, std::string>::const_iterator i = begin(); i!=end(); ++i) { | ||
if ((*i).second != "") fprintf(f, "%s=%s\n", (*i).first.c_str(), (*i).second.c_str()); | ||
} | ||
|
||
fclose(f); | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters