Building Game Engines
ResourceManager Class Reference

Resource manager provides functions to add, get and remove resources. More...

#include <ResourceManager.hpp>

Collaboration diagram for ResourceManager:
Collaboration graph

Public Member Functions

void addMusicResource (std::string key, Mix_Music *music)
 Add music resource into a map. More...
 
Mix_Music * getMusicResource (std::string key)
 Return specified music resource based on its name. More...
 
void removeMusicResource (std::string key)
 Remove specified music resource based on its name. More...
 
void addSoundResource (std::string key, Mix_Chunk *sound)
 Add sound resource into a map. More...
 
Mix_Chunk * getSoundResource (std::string key)
 Return specified sound resource based on its name. More...
 
void addTextDisplayResource (std::string key, TextDisplay *textDisplay)
 Add TextDisplay component into map. More...
 
TextDisplaygetTextDisplayResource (std::string key)
 Return specified TextDisplay class. More...
 
void removeTextDisplayResource (std::string key)
 Remove specified TextDisplay class. More...
 
void addGameComponent (std::string key, Component *component)
 Add classes that inherits Component.h into map. More...
 
ComponentgetComponentResource (std::string key)
 Return specified component resource. More...
 
void removeGameComponent (std::string key, Component *component)
 Remove specified component resource. More...
 
void addFont (std::string key, TTF_Font *font)
 Add font resource into map. More...
 
TTF_Font * getFont (std::string key)
 Return specified font resource. More...
 
void write2Json (const char *const filePath, const char *const data)
 write data into the json file. More...
 
char * readFromJson (const char *const filePath)
 Read data from json file. More...
 
 ~ResourceManager ()=default
 
void destroy ()
 Free the loaded music, free the memory used by font, and free font itself as well, delete text resources. More...
 

Static Public Member Functions

static ResourceManagergetInstance ()
 

Detailed Description

Resource manager provides functions to add, get and remove resources.

Constructor & Destructor Documentation

◆ ~ResourceManager()

ResourceManager::~ResourceManager ( )
default

Destructor.

Member Function Documentation

◆ addFont()

void ResourceManager::addFont ( std::string  key,
TTF_Font *  font 
)
inline

Add font resource into map.

Parameters
keyThe name of the font rosource.
fontThe reference to the TTF_font.

◆ addGameComponent()

void ResourceManager::addGameComponent ( std::string  key,
Component component 
)
inline

Add classes that inherits Component.h into map.

Currently you can add TileMap, TilemapEditor, GameState, Camera, Sprite, Animation as well as TextDisplay by using this function once then inherits Component.h.

Parameters
keyThe name of the component.
componentThe reference of the component.

◆ addMusicResource()

void ResourceManager::addMusicResource ( std::string  key,
Mix_Music *  music 
)
inline

Add music resource into a map.

Parameters
keyThe name of this resource.
musicThe reference to Mix_Music.

◆ addSoundResource()

void ResourceManager::addSoundResource ( std::string  key,
Mix_Chunk *  sound 
)
inline

Add sound resource into a map.

Parameters
keyThe name of this resource.
soundThe reference to Mix_Chunk.

◆ addTextDisplayResource()

void ResourceManager::addTextDisplayResource ( std::string  key,
TextDisplay textDisplay 
)
inline

Add TextDisplay component into map.

Parameters
keyThe name of this component.
textDisplayThe reference of the TextDisplay class.

◆ destroy()

void ResourceManager::destroy ( )
inline

Free the loaded music, free the memory used by font, and free font itself as well, delete text resources.

◆ getComponentResource()

Component* ResourceManager::getComponentResource ( std::string  key)
inline

Return specified component resource.

Parameters
keyThe name of the component you want.

◆ getFont()

TTF_Font* ResourceManager::getFont ( std::string  key)
inline

Return specified font resource.

Parameters
keyThe name of the font resource you want.

◆ getInstance()

static ResourceManager* ResourceManager::getInstance ( )
inlinestatic

Return the reference of the singleton ResourceManager.

◆ getMusicResource()

Mix_Music* ResourceManager::getMusicResource ( std::string  key)
inline

Return specified music resource based on its name.

Parameters
keyThe name of the resource you want.

◆ getSoundResource()

Mix_Chunk* ResourceManager::getSoundResource ( std::string  key)
inline

Return specified sound resource based on its name.

Parameters
keyThe name of the resource you want.

◆ getTextDisplayResource()

TextDisplay* ResourceManager::getTextDisplayResource ( std::string  key)
inline

Return specified TextDisplay class.

Parameters
keyThe name of the TextDisplay class you want.

◆ readFromJson()

char* ResourceManager::readFromJson ( const char *const  filePath)
inline

Read data from json file.

Parameters
filePathThe address of the json file.

◆ removeGameComponent()

void ResourceManager::removeGameComponent ( std::string  key,
Component component 
)
inline

Remove specified component resource.

Parameters
keyThe name of the component you want to remove.
componentThe reference of the component.

◆ removeMusicResource()

void ResourceManager::removeMusicResource ( std::string  key)
inline

Remove specified music resource based on its name.

Parameters
keyThe name of the resource you want to delete.

◆ removeTextDisplayResource()

void ResourceManager::removeTextDisplayResource ( std::string  key)
inline

Remove specified TextDisplay class.

Parameters
keyThe name of the TextDisplay class you want to remove.

◆ write2Json()

void ResourceManager::write2Json ( const char *const  filePath,
const char *const  data 
)
inline

write data into the json file.

Parameters
filePathThe address of the json file.
dataThe data in json file. e.g. "Enemy1":
{ "id": 1, "isActive": true, "posX": 32, "posY": 320 }, "Enemy2":
{ "id": 2, "isActive": true, "posX": 128, "posY": 352 }, ...