Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // GameSettings.h
- #pragma once
- #include "tinyxml2.h"
- #include <map>
- #include <string>
- #include <sstream>
- /*
- Provides access to the settings of the game. The settings are loaded once
- as the game launches, and then every aspect that needs the settings
- (read or write) can access one singleton (AGAIN, DONT JUDGE ME) of the settings class.
- */
- class GameSettings
- {
- public:
- static void LoadSettings(std::string settingsFileName);
- static GameSettings* GetSettings();
- template <typename T> T GetValue(std::string settingsName) {}
- template <> int GetValue<int>(std::string settingsName);
- template <> std::string GetValue<std::string>(std::string settingsName);
- protected:
- private:
- GameSettings(std::string settingsFileName);
- ~GameSettings();
- static GameSettings* m_SettingsInstance;
- tinyxml2::XMLElement* m_SettingsElement;
- tinyxml2::XMLElement* GetXMLElementWithTagName(std::string tagName);
- };
- // GameSettings.cpp
- #include "GameSettings.h"
- GameSettings* GameSettings::m_SettingsInstance;
- GameSettings::GameSettings(std::string settingsFileName)
- {
- tinyxml2::XMLDocument settingsFile;
- settingsFile.LoadFile(settingsFileName.c_str());
- // Gets the settings element.
- m_SettingsElement = settingsFile.FirstChildElement("Settings");
- }
- GameSettings::~GameSettings()
- {
- }
- /*
- Loads the settings file as a singleton.
- */
- void GameSettings::LoadSettings(std::string settingsFileName)
- {
- m_SettingsInstance = new GameSettings(settingsFileName);
- }
- /*
- Returns the settings singleton.
- */
- GameSettings* GameSettings::GetSettings()
- {
- return m_SettingsInstance;
- }
- /*
- Returns the xml element with the given tag name.
- */
- tinyxml2::XMLElement* GameSettings::GetXMLElementWithTagName(std::string tagName)
- {
- // Loops over the elements in the file.
- for (tinyxml2::XMLElement* valueElement = m_SettingsElement->FirstChildElement(); valueElement != NULL; valueElement = valueElement->NextSiblingElement())
- {
- if (strcmp(valueElement->Value(), tagName.c_str()) == 0)
- {
- return valueElement;
- }
- }
- return NULL;
- }
- /*
- Returns an integer value.
- */
- template <> int GameSettings::GetValue<int>(std::string settingsName)
- {
- tinyxml2::XMLElement* valueElement = GetXMLElementWithTagName(settingsName);
- if (!valueElement)
- {
- throw valueElement;
- }
- std::stringstream strValue;
- strValue << valueElement->GetText();
- int intValue; strValue >> intValue;
- return intValue;
- }
- /*
- Returns a std::string value.
- */
- template <> std::string GameSettings::GetValue<std::string>(std::string settingsName)
- {
- tinyxml2::XMLElement* valueElement = GetXMLElementWithTagName(settingsName);
- if (!valueElement)
- {
- throw valueElement;
- }
- return std::string(valueElement->GetText());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement