Dragonfly 4.20
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
21namespace df {
22
23// Delimiters used to parse Sprite files -
24// the ResourceManager "knows" file format.
25#define HEADER_TOKEN "HEADER"
26#define BODY_TOKEN "BODY"
27#define FOOTER_TOKEN "FOOTER"
28#define FRAMES_TOKEN "frames"
29#define TRANSPARENCY_TOKEN "transparency"
30#define HEIGHT_TOKEN "height"
31#define WIDTH_TOKEN "width"
32#define COLOR_TOKEN "color"
33#define SLOWDOWN_TOKEN "slowdown"
34#define END_FRAME_TOKEN "end"
35#define VERSION_TOKEN "version"
36
37// Maximum number of unique assets in game.
38const int MAX_SPRITES = 1000;
39const int MAX_SOUNDS = 100;
40const int MAX_MUSICS = 50;
41
42class ResourceManager : public Manager {
43
44 private:
45 ResourceManager(ResourceManager const&); ///< Don't allow copy.
46 void operator=(ResourceManager const&); ///< Don't allow assignment.
47 ResourceManager(); ///< Private since a singleton.
48 Sprite *m_p_sprite[MAX_SPRITES]; ///< Array of (pointers to) Sprites.
49 int m_sprite_count; ///< Count of number of loaded sprites.
50#ifndef NO_SOUND
51 Sound m_sound[MAX_SOUNDS]; ///< Array of sound buffers.
52 int m_sound_count; ///< Count of number of loaded sounds.
53 Music m_music[MAX_MUSICS]; ///< Array of music buffers.
54 int m_music_count; ///< Count of number of loaded musics.
55#endif
56
57 // Load Sprite assuming robust sprite format in file.
58 // Return Sprite if found, else NULL.
59 Sprite *loadRobustSprite(std::ifstream *p_file);
60
61 // Load Sprite assuming simple sprite format in file.
62 // Return Sprite if found, else NULL.
63 Sprite *loadSimpleSprite(std::ifstream *p_file);
64
65 public:
66 /// Get the one and only instance of the ResourceManager.
68
69 /// Get ResourceManager ready to manage resources.
70 int startUp() override;
71
72 /// Shut down manager, freeing up any allocated Sprites, Music and Sounds.
73 void shutDown() override;
74
75 /// Load Sprite from file.
76 /// Assign indicated label to Sprite.
77 /// Return 0 if ok, else -1.
78 int loadSprite(std::string filename, std::string label);
79
80 /// Unload Sprite with indicated label.
81 /// Return 0 if ok, else -1.
82 int unloadSprite(std::string label);
83
84 /// Find Sprite with indicated label.
85 /// Return pointer to it if found, else NULL.
86 Sprite *getSprite(std::string label) const;
87
88 /// Load sound from file.
89 /// Return 0 if ok, else -1.
90 int loadSound(std::string filename, std::string label);
91
92 /// Remove Sound with indicated label.
93 /// Return 0 if ok, else -1.
94 int unloadSound(std::string label);
95
96 /// Find Sound with indicated label.
97 /// Return pointer to it if found, else NULL.
98 Sound *getSound(std::string label);
99
100 /// Associate file with Music.
101 /// Return 0 if ok, else -1.
102 int loadMusic(std::string filename, std::string label);
103
104 /// Remove label for Music with indicated label.
105 /// Return 0 if ok, else -1.
106 int unloadMusic(std::string label);
107
108 /// Find Music with indicated label.
109 /// Return pointer to it if found, else NULL.
110 Music *getMusic(std::string label);
111};
112
113} // end of namespace df
114#endif //__RESOURCE_MANAGER_H__
Definition: Manager.h:24
Definition: Music.h:14
Definition: ResourceManager.h:42
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 startUp() override
Get ResourceManager ready to manage resources.
int m_sound_count
Count of number of loaded sounds.
Definition: ResourceManager.h:52
int m_sprite_count
Count of number of loaded sprites.
Definition: ResourceManager.h:49
int loadMusic(std::string filename, std::string label)
Associate file with Music.
Music * getMusic(std::string label)
Find Music with indicated label.
int loadSound(std::string filename, std::string label)
Load sound from file.
int m_music_count
Count of number of loaded musics.
Definition: ResourceManager.h:54
void shutDown() override
Shut down manager, freeing up any allocated Sprites, Music and Sounds.
Sound m_sound[MAX_SOUNDS]
Array of sound buffers.
Definition: ResourceManager.h:51
Sprite * getSprite(std::string label) const
Find Sprite with indicated label.
int unloadMusic(std::string label)
Remove label for Music with indicated label.
ResourceManager()
Private since a singleton.
Music m_music[MAX_MUSICS]
Array of music buffers.
Definition: ResourceManager.h:53
void operator=(ResourceManager const &)
Don't allow assignment.
Sound * getSound(std::string label)
Find Sound with indicated label.
int unloadSound(std::string label)
Remove Sound with indicated label.
Sprite * m_p_sprite[MAX_SPRITES]
Array of (pointers to) Sprites.
Definition: ResourceManager.h:48
ResourceManager(ResourceManager const &)
Don't allow copy.
int unloadSprite(std::string label)
Unload Sprite with indicated label.
Definition: Sound.h:14
Definition: Sprite.h:17
An animation for a sprite.
Definition: Animation.h:15