Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alec's Values loader thing extracted into the Monocle core folder
- Loading branch information
unknown
authored and
unknown
committed
Jun 9, 2011
1 parent
6eef111
commit 1ce5d33
Showing
2 changed files
with
104 additions
and
0 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,68 @@ | ||
#include "ConfigValues.h" | ||
|
||
namespace Monocle | ||
{ | ||
ConfigValues *ConfigValues::instance = NULL; | ||
|
||
ConfigValues::ConfigValues() | ||
{ | ||
instance = this; | ||
} | ||
|
||
void ConfigValues::Load(const std::string &filename) | ||
{ | ||
TiXmlDocument doc; | ||
doc.LoadFile(Assets::GetContentPath() + filename); | ||
TiXmlElement *xmlValues = doc.FirstChildElement("config"); | ||
if (xmlValues) | ||
{ | ||
TiXmlElement *xmlValue = xmlValues->FirstChildElement("value"); | ||
while (xmlValue) | ||
{ | ||
std::string name = XMLReadString(xmlValue, "name"); | ||
|
||
if (xmlValue->Attribute("float")) | ||
instance->floats[name] = XMLReadFloat(xmlValue, "float"); | ||
|
||
if (xmlValue->Attribute("string")) | ||
instance->strings[name] = XMLReadString(xmlValue, "string"); | ||
|
||
if (xmlValue->Attribute("int")) | ||
instance->ints[name] = XMLReadInt(xmlValue, "int"); | ||
|
||
if (xmlValue->Attribute("bool")) | ||
instance->bools[name] = XMLReadBool(xmlValue, "bool"); | ||
|
||
if (xmlValue->Attribute("vector2")) | ||
instance->vector2s[name] = XMLReadVector2(xmlValue, "vector2"); | ||
|
||
xmlValue = xmlValue->NextSiblingElement("value"); | ||
} | ||
} | ||
} | ||
|
||
std::string ConfigValues::GetString(const std::string &valueName) | ||
{ | ||
return instance->strings[valueName]; | ||
} | ||
|
||
int ConfigValues::GetInt(const std::string &valueName) | ||
{ | ||
return instance->ints[valueName]; | ||
} | ||
|
||
float ConfigValues::GetFloat(const std::string &valueName) | ||
{ | ||
return instance->floats[valueName]; | ||
} | ||
|
||
bool ConfigValues::GetBool(const std::string &valueName) | ||
{ | ||
return instance->bools[valueName]; | ||
} | ||
|
||
Vector2 ConfigValues::GetVector2(const std::string &valueName) | ||
{ | ||
return instance->vector2s[valueName]; | ||
} | ||
} |
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,36 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include <string> | ||
#include "Vector2.h" | ||
#include "Monocle.h" | ||
|
||
namespace Monocle | ||
{ | ||
//! \brief A quick way to load in various datatypes from an XML config file. | ||
//! Config file should be XML of format: | ||
//! <config> <value name="variable_name" float="10.5"/> </config> | ||
//! Supports: int/float/string/bool/vector2 | ||
//! Stolen from Alec Holowka's code | ||
class ConfigValues | ||
{ | ||
public: | ||
ConfigValues(); | ||
static void Load(const std::string &filename); | ||
static std::string GetString(const std::string &valueName); | ||
static int GetInt(const std::string &valueName); | ||
static float GetFloat(const std::string &valueName); | ||
static bool GetBool(const std::string &valueName); | ||
static Vector2 GetVector2(const std::string &valueName); | ||
|
||
private: | ||
static ConfigValues *instance; | ||
|
||
std::map<std::string, int> ints; | ||
std::map<std::string, float> floats; | ||
std::map<std::string, std::string> strings; | ||
std::map<std::string, bool> bools; | ||
std::map<std::string, Vector2> vector2s; | ||
}; | ||
|
||
} |