Skip to content

Commit

Permalink
Alec's Values loader thing extracted into the Monocle core folder
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Jun 9, 2011
1 parent 6eef111 commit 1ce5d33
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Core/ConfigValues.cpp
@@ -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];
}
}
36 changes: 36 additions & 0 deletions Core/ConfigValues.h
@@ -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;
};

}

0 comments on commit 1ce5d33

Please sign in to comment.