Advertisement
Sand25

ResourceManager.h

Apr 8th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #ifndef RESOURCEMANAGER_H
  2. #define RESOURCEMANAGER_H
  3. #include <SFML/Graphics.hpp>
  4. #include <SFML/Audio.hpp>
  5. #include <map>
  6. #include <list>
  7.  
  8. #define rm sfge::ResourceManager::instance()
  9.  
  10. namespace sfge
  11. {
  12.     /// \brief A resource manager that guarantees no resource is loaded twice.
  13.     ///
  14.     /// This class manages the SFML resources and ensures that every resource is loaded into memory only once.\n
  15.     /// This class is a singleton, to guarantee only one instance is loaded into memory at any given time.\n
  16.     /// In order not to let calling the class get verbose, a MACRO is defined.\n
  17.     /// @code #define rm sfge::ResourceManager::instance() @endcode
  18.     /// This makes calling the resourcemanager as this:
  19.     /// @code
  20.     /// #include <SFGE/ResourceManager.h>
  21.     /// rm->getTexture("example.png");
  22.     /// @endcode
  23.     /// When requesting a resource, it will first check if is already loaded. If it is, a reference is returned.
  24.     /// If not, it will load the resource and subsequently return a reference.
  25.     class ResourceManager
  26.     {
  27.         public:
  28.             ~ResourceManager();
  29.  
  30.             /// \brief Returns the singleton instance
  31.             static ResourceManager* instance();
  32.  
  33.             /// \brief Releases all the stored resources
  34.             void releaseAllResources();
  35.  
  36.             /// \brief Releases a specific resource
  37.             /// @param name The filename of the resource to be released
  38.             void releaseResource(const std::string& name);
  39.  
  40.             /// \brief Get a texture
  41.             /// @param name The filename of the texture
  42.             sf::Texture& getTexture(const std::string& name);
  43.            
  44.             /// \brief Get a soundbuffer
  45.             /// @param name The filename of the sound note: Don't buffer the music!
  46.             sf::SoundBuffer& getSoundBuffer(const std::string& name);
  47.  
  48.             /// \brief Get a font
  49.             /// @param name The filename of the font
  50.             sf::Font& getFont(const std::string& name);
  51.  
  52.             /// \brief Sets the directory where the textures are located
  53.             void setTextureDir(const std::string& dir);
  54.  
  55.             /// \brief Sets the directory where audio is located
  56.             void setAudioDir(const std::string& dir);
  57.  
  58.             /// \brief Sets the directory where the fonts are located
  59.             void setFontDir(const std::string& dir);
  60.  
  61.         protected:
  62.             ResourceManager();
  63.  
  64.         private:
  65.             static ResourceManager* mgr;
  66.  
  67.             std::string textureDir;
  68.             std::string audioDir;
  69.             std::string fontDir;
  70.  
  71.             std::map<std::string, sf::Texture*> textures;
  72.             std::map<std::string, sf::SoundBuffer*> soundBuffers;
  73.             std::map<std::string, sf::Font*> fonts;
  74.     };
  75. }
  76. #endif // RESOURCEMANAGER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement