Dragonfly  4.4
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 // Comment out to disable sound (i.e., for Windows WSL).
22 #define DF_SOUND
23 
24 namespace df {
25 
26 // Delimiters used to parse Sprite files -
27 // the ResourceManager "knows" file format.
28 const std::string FRAMES_TOKEN = "frames";
29 const std::string HEIGHT_TOKEN = "height";
30 const std::string WIDTH_TOKEN = "width";
31 const std::string COLOR_TOKEN = "color";
32 const std::string END_FRAME_TOKEN = "end";
33 const std::string END_SPRITE_TOKEN = "eof";
34 
35 // Maximum number of unique assets in game.
36 const int MAX_SPRITES = 1000;
37 const int MAX_SOUNDS = 128;
38 const int MAX_MUSICS = 128;
39 
40 class ResourceManager : public Manager {
41 
42  private:
43  ResourceManager(ResourceManager const&); ///< Don't allow copy.
44  void operator=(ResourceManager const&); ///< Don't allow assignment.
45  ResourceManager(); ///< Private since a singleton.
46  Sprite *p_sprite[MAX_SPRITES]; ///< Array of (pointers to) Sprites.
47  int m_sprite_count; ///< Count of number of loaded sprites.
48 #ifdef DF_SOUND
49  Sound m_sound[MAX_SOUNDS]; ///< Array of sound buffers.
50  int m_sound_count; ///< Count of number of loaded sounds.
51  Music m_music[MAX_MUSICS]; ///< Array of music buffers.
52  int m_music_count; ///< Count of number of loaded musics.
53 #endif
54 
55  public:
56  /// Get the one and only instance of the ResourceManager.
57  static ResourceManager &getInstance();
58 
59  /// Get ResourceManager ready to manage resources.
60  int startUp();
61 
62  /// Shut down manager, freeing up any allocated Sprites, Music and Sounds.
63  void shutDown();
64 
65  /// Load Sprite from file.
66  /// Assign indicated label to Sprite.
67  /// Return 0 if ok, else -1.
68  int loadSprite(std::string filename, std::string label);
69 
70  /// Unload Sprite with indicated label.
71  /// Return 0 if ok, else -1.
72  int unloadSprite(std::string label);
73 
74  /// Find Sprite with indicated label.
75  /// Return pointer to it if found, else NULL.
76  Sprite *getSprite(std::string label) const;
77 
78  /// Load sound from file.
79  /// Return 0 if ok, else -1.
80  int loadSound(std::string filename, std::string label);
81 
82  /// Remove Sound with indicated label.
83  /// Return 0 if ok, else -1.
84  int unloadSound(std::string label);
85 
86  /// Find Sound with indicated label.
87  /// Return pointer to it if found, else NULL.
88  Sound *getSound(std::string label);
89 
90  /// Associate file with Music.
91  /// Return 0 if ok, else -1.
92  int loadMusic(std::string filename, std::string label);
93 
94  /// Remove label for Music with indicated label.
95  /// Return 0 if ok, else -1.
96  int unloadMusic(std::string label);
97 
98  /// Find Music with indicated label.
99  /// Return pointer to it if found, else NULL.
100  Music *getMusic(std::string label);
101 };
102 
103 } // end of namespace df
104 #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:49
int unloadSprite(std::string label)
Unload Sprite with indicated label.
Sprite * p_sprite[MAX_SPRITES]
Array of (pointers to) Sprites.
Definition: ResourceManager.h:46
int m_sound_count
Count of number of loaded sounds.
Definition: ResourceManager.h:50
Music m_music[MAX_MUSICS]
Array of music buffers.
Definition: ResourceManager.h:51
Definition: Sprite.h:17
int m_sprite_count
Count of number of loaded sprites.
Definition: ResourceManager.h:47
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:40
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:52