Dragonfly  4.3
A text-based game engine
ResourceManager.h
1 ///
2 /// The resource manager
3 ///
4 
5 #ifndef __RESOURCE_MANAGER_H__
6 #define __RESOURCE_MANAGER_H__
7 
8 // System includes.
9 #include <SFML/Audio.hpp>
10 #include <string>
11 
12 // Engine includes.
13 #include "Manager.h"
14 #include "Music.h"
15 #include "Sound.h"
16 #include "Sprite.h"
17 
18 // Two-letter acronym for easier access to manager.
19 #define RM df::ResourceManager::getInstance()
20 
21 namespace df {
22 
23 // Delimiters used to parse Sprite files -
24 // the ResourceManager "knows" file format.
25 const std::string FRAMES_TOKEN = "frames";
26 const std::string HEIGHT_TOKEN = "height";
27 const std::string WIDTH_TOKEN = "width";
28 const std::string COLOR_TOKEN = "color";
29 const std::string END_FRAME_TOKEN = "end";
30 const std::string END_SPRITE_TOKEN = "eof";
31 
32 // Maximum number of unique assets in game.
33 const int MAX_SPRITES = 1000;
34 const int MAX_SOUNDS = 128;
35 const int MAX_MUSICS = 128;
36 
37 class ResourceManager : public Manager {
38 
39  private:
40  ResourceManager(ResourceManager const&); ///< Don't allow copy.
41  void operator=(ResourceManager const&); ///< Don't allow assignment.
42  ResourceManager(); ///< Private since a singleton.
43  Sprite *p_sprite[MAX_SPRITES]; ///< Array of (pointers to) Sprites.
44  int m_sprite_count; ///< Count of number of loaded sprites.
45  Sound m_sound[MAX_SOUNDS]; ///< Array of sound buffers.
46  int m_sound_count; ///< Count of number of loaded sounds.
47  Music m_music[MAX_MUSICS]; ///< Array of music buffers.
48  int m_music_count; ///< Count of number of loaded musics.
49 
50  public:
51  /// Get the one and only instance of the ResourceManager.
52  static ResourceManager &getInstance();
53 
54  /// Get ResourceManager ready to manage resources.
55  int startUp();
56 
57  /// Shut down manager, freeing up any allocated Sprites, Music and Sounds.
58  void shutDown();
59 
60  /// Load Sprite from file.
61  /// Assign indicated label to Sprite.
62  /// Return 0 if ok, else -1.
63  int loadSprite(std::string filename, std::string label);
64 
65  /// Unload Sprite with indicated label.
66  /// Return 0 if ok, else -1.
67  int unloadSprite(std::string label);
68 
69  /// Find Sprite with indicated label.
70  /// Return pointer to it if found, else NULL.
71  Sprite *getSprite(std::string label) const;
72 
73  /// Load sound from file.
74  /// Return 0 if ok, else -1.
75  int loadSound(std::string filename, std::string label);
76 
77  /// Remove Sound with indicated label.
78  /// Return 0 if ok, else -1.
79  int unloadSound(std::string label);
80 
81  /// Find Sound with indicated label.
82  /// Return pointer to it if found, else NULL.
83  Sound *getSound(std::string label);
84 
85  /// Associate file with Music.
86  /// Return 0 if ok, else -1.
87  int loadMusic(std::string filename, std::string label);
88 
89  /// Remove label for Music with indicated label.
90  /// Return 0 if ok, else -1.
91  int unloadMusic(std::string label);
92 
93  /// Find Music with indicated label.
94  /// Return pointer to it if found, else NULL.
95  Music *getMusic(std::string label);
96 };
97 
98 } // end of namespace df
99 #endif //__RESOURCE_MANAGER_H__
int loadSound(std::string filename, std::string label)
Load sound from file.
Music * getMusic(std::string label)
Find Music with indicated label.
int startUp()
Get ResourceManager ready to manage resources.
Definition: Sound.h:14
Definition: Music.h:14
void operator=(ResourceManager const &)
Don&#39;t allow assignment.
Sound * getSound(std::string label)
Find Sound with indicated label.
A 2-d bounding box.
Definition: Box.h:10
int unloadMusic(std::string label)
Remove label for Music with indicated label.
ResourceManager()
Private since a singleton.
Sound m_sound[MAX_SOUNDS]
Array of sound buffers.
Definition: ResourceManager.h:45
int unloadSprite(std::string label)
Unload Sprite with indicated label.
Sprite * p_sprite[MAX_SPRITES]
Array of (pointers to) Sprites.
Definition: ResourceManager.h:43
int m_sound_count
Count of number of loaded sounds.
Definition: ResourceManager.h:46
Music m_music[MAX_MUSICS]
Array of music buffers.
Definition: ResourceManager.h:47
Definition: Sprite.h:17
int m_sprite_count
Count of number of loaded sprites.
Definition: ResourceManager.h:44
int loadMusic(std::string filename, std::string label)
Associate file with Music.
Sprite * getSprite(std::string label) const
Find Sprite with indicated label.
Definition: Manager.h:20
Definition: ResourceManager.h:37
int unloadSound(std::string label)
Remove Sound with indicated label.
void shutDown()
Shut down manager, freeing up any allocated Sprites, Music and Sounds.
int loadSprite(std::string filename, std::string label)
Load Sprite from file.
static ResourceManager & getInstance()
Get the one and only instance of the ResourceManager.
int m_music_count
Count of number of loaded musics.
Definition: ResourceManager.h:48