Advertisement
Guest User

GameSettings.h

a guest
Aug 20th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "tinyxml2.h"
  4.  
  5. #include <map>
  6. #include <string>
  7. #include <sstream>
  8.  
  9. /*
  10. Provides access to the settings of the game. The settings are loaded once
  11. as the game launches, and then every aspect that needs the settings
  12. (read or write) can access one singleton (DONT JUDGE ME!) of the settings class.
  13. */
  14. class GameSettings
  15. {
  16. public:
  17.     static void LoadSettings(std::string settingsFileName);
  18.     static GameSettings* GetSettings();
  19.  
  20.     template <typename T> inline T GetValue(std::string settingsName);
  21. protected:
  22. private:
  23.     GameSettings(std::string settingsFileName);
  24.     ~GameSettings();
  25.  
  26.     static GameSettings* m_SettingsInstance;
  27.  
  28.     tinyxml2::XMLElement* m_SettingsElement;
  29.     tinyxml2::XMLElement* GetXMLElementWithTagName(std::string tagName);
  30. };
  31.  
  32. /*
  33. Returns an integer value.
  34. */
  35. template <> int GameSettings::GetValue<int>(std::string settingsName)
  36. {
  37.     tinyxml2::XMLElement* valueElement = GetXMLElementWithTagName(settingsName);
  38.     if (!valueElement)
  39.     {
  40.         throw valueElement;
  41.     }
  42.     std::stringstream strValue;
  43.     strValue << valueElement->GetText();
  44.     int intValue; strValue >> intValue;
  45.     return intValue;
  46. }
  47.  
  48. /*
  49. Returns a std::string value.
  50. */
  51. template <> std::string GameSettings::GetValue<std::string>(std::string settingsName)
  52. {
  53.     tinyxml2::XMLElement* valueElement = GetXMLElementWithTagName(settingsName);
  54.     if (!valueElement)
  55.     {
  56.         throw valueElement;
  57.     }
  58.     return std::string(valueElement->GetText());
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement