Advertisement
Guest User

GameSettings .cpp & .h

a guest
Aug 20th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. // GameSettings.h
  2. #pragma once
  3.  
  4. #include "tinyxml2.h"
  5.  
  6. #include <map>
  7. #include <string>
  8. #include <sstream>
  9.  
  10. /*
  11. Provides access to the settings of the game. The settings are loaded once
  12. as the game launches, and then every aspect that needs the settings
  13. (read or write) can access one singleton (AGAIN, DONT JUDGE ME) of the settings class.
  14. */
  15. class GameSettings
  16. {
  17. public:
  18.     static void LoadSettings(std::string settingsFileName);
  19.     static GameSettings* GetSettings();
  20.  
  21.     template <typename T> T GetValue(std::string settingsName) {}
  22.     template <> int GetValue<int>(std::string settingsName);
  23.     template <> std::string GetValue<std::string>(std::string settingsName);
  24. protected:
  25. private:
  26.     GameSettings(std::string settingsFileName);
  27.     ~GameSettings();
  28.  
  29.     static GameSettings* m_SettingsInstance;
  30.  
  31.     tinyxml2::XMLElement* m_SettingsElement;
  32.     tinyxml2::XMLElement* GetXMLElementWithTagName(std::string tagName);
  33. };
  34.  
  35. // GameSettings.cpp
  36. #include "GameSettings.h"
  37.  
  38. GameSettings* GameSettings::m_SettingsInstance;
  39.  
  40. GameSettings::GameSettings(std::string settingsFileName)
  41. {
  42.     tinyxml2::XMLDocument settingsFile;
  43.     settingsFile.LoadFile(settingsFileName.c_str());
  44.  
  45.     // Gets the settings element.
  46.     m_SettingsElement = settingsFile.FirstChildElement("Settings");
  47. }
  48.  
  49. GameSettings::~GameSettings()
  50. {
  51. }
  52.  
  53. /*
  54. Loads the settings file as a singleton.
  55. */
  56. void GameSettings::LoadSettings(std::string settingsFileName)
  57. {
  58.     m_SettingsInstance = new GameSettings(settingsFileName);
  59. }
  60.  
  61. /*
  62. Returns the settings singleton.
  63. */
  64. GameSettings* GameSettings::GetSettings()
  65. {
  66.     return m_SettingsInstance;
  67. }
  68.  
  69. /*
  70. Returns the xml element with the given tag name.
  71. */
  72. tinyxml2::XMLElement* GameSettings::GetXMLElementWithTagName(std::string tagName)
  73. {
  74.     // Loops over the elements in the file.
  75.     for (tinyxml2::XMLElement* valueElement = m_SettingsElement->FirstChildElement(); valueElement != NULL; valueElement = valueElement->NextSiblingElement())
  76.     {
  77.         if (strcmp(valueElement->Value(), tagName.c_str()) == 0)
  78.         {
  79.             return valueElement;
  80.         }
  81.     }
  82.     return NULL;
  83. }
  84.  
  85. /*
  86. Returns an integer value.
  87. */
  88. template <> int GameSettings::GetValue<int>(std::string settingsName)
  89. {
  90.     tinyxml2::XMLElement* valueElement = GetXMLElementWithTagName(settingsName);
  91.     if (!valueElement)
  92.     {
  93.         throw valueElement;
  94.     }
  95.     std::stringstream strValue;
  96.     strValue << valueElement->GetText();
  97.     int intValue; strValue >> intValue;
  98.     return intValue;
  99. }
  100.  
  101. /*
  102. Returns a std::string value.
  103. */
  104. template <> std::string GameSettings::GetValue<std::string>(std::string settingsName)
  105. {
  106.     tinyxml2::XMLElement* valueElement = GetXMLElementWithTagName(settingsName);
  107.     if (!valueElement)
  108.     {
  109.         throw valueElement;
  110.     }
  111.     return std::string(valueElement->GetText());
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement