Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
Classes | Public Types | Public Member Functions | List of all members
HFCore::ResourceManager Class Reference

Interface for ResourceManager which handles all Resource loading etc. More...

#include <ResourceManager.h>

Classes

struct  ShaderCompileInfo
 Shader compile info struct (used for Windows Direct3D). More...
 

Public Types

enum  {
  FONT_TYPE_FONTBUILDER = 0, FONT_TYPE_BMFONT, FONT_TYPE_HFBMFONT_XML, FONT_TYPE_HFBMFONT_BIN,
  FONT_TYPE_HFBMFONT_TXT, FONT_TYPE_UNKNOWN
}
 Font type enum. More...
 
typedef std::map< std::string, Resource * > ResourceMap
 typedef for map holding std::string / Resource pointer pairs.
 
typedef std::map< std::string, UStringResourceStringMap
 typedef for map holding std::string / UString pairs.
 
typedef std::map< std::string, ResourceGroup * > ResourceGroupMap
 typedef for map holding std::string / ResourceGroup pointer pairs.
 

Public Member Functions

 ResourceManager (ResourceLoader *loader, const std::string &language)
 Constructor. More...
 
virtual ~ResourceManager ()
 Destructor.
 
virtual ResourceLoadergetResourceLoader ()
 Get a pointer to ResourceLoader interface. More...
 
virtual bool parseResourceFile (const std::string &fileName)
 Parse resource file and all of its ResourceGroup's. More...
 
virtual bool loadResourceGroup (const std::string &groupName)
 Load ResourceGroup named groupName and call Resource::load() for each item in group. More...
 
virtual ResourceGroupgetResourceGroup (const std::string &groupName)
 Get a pointer to ResourceGroup for given groupName. More...
 
virtual void unloadResourceGroup (const std::string &groupName)
 Unload ResourceGroup named groupName and call Resource::release() for each item in group. More...
 
virtual void reloadResources ()
 Iterate through all Resource's in ResourceManager::ResourceMap and call Resource::reload().
 
virtual void destroyResources (bool includeSounds)
 Iterate through all Resource's in ResourceManager::ResourceMap and call Resource::destroy(). More...
 
virtual void initResources (bool includeSounds)
 Iterate through all Resource's in ResourceManager::ResourceMap and call Resource::init(). More...
 
virtual ResourceMap getResourcesByPathMap ()
 Get internal ResourceManager::ResourceMap containing all Resource's. More...
 
virtual ImagegetImage (const std::string &id)
 Get Image by it's id. More...
 
virtual ImageAtlasgetImageAtlas (const std::string &id)
 Get ImageAtlas by it's id. More...
 
virtual bool hasString (const std::string &id)
 Determines whether string with id exists. More...
 
virtual UString getString (const std::string &id)
 Get UString by it's id. More...
 
virtual SoundgetSound (const std::string &id)
 Get Sound by it's id. More...
 
virtual FontgetFont (const std::string &id)
 Get Font by it's id. More...
 
virtual HFModule::TmxMap * getTmxTileMap (const std::string &id)
 Get HFModule::TmxMap by it's id. More...
 
virtual ShadergetShader (const std::string &id)
 Get Shader by it's id. More...
 
virtual WavefrontOBJgetWavefrontOBJ (const std::string &id)
 Get WavefrontOBJ by it's id. More...
 
virtual ImagegetImageFromPath (const std::string &path)
 Load Image from given path. More...
 
virtual ImageAtlasgetImageAtlasFromPath (const std::string &path)
 Load ImageAtlas from given path. More...
 
virtual FontgetFontFromPath (const std::string &path)
 Load Font from given path. More...
 
virtual HFModule::TmxMap * getTmxTileMapFromPath (const std::string &path)
 Load HFModule::TmxMap from given path. More...
 
virtual ShadergetShaderFromPath (const std::string &path, Shader::Type type, const std::string &entryFunctionVS="main", const std::string &compileVersionVS="vs_3_0", const std::string &entryFunctionFS="main", const std::string &compileVersionFS="ps_3_0")
 Get Shader from given path. More...
 
virtual ShadergetShaderFromSource (const std::string &vertexShaderSrc, const std::string &fragmentShaderSrc, const std::string &entryFunctionVS="main", const std::string &compileVersionVS="vs_3_0", const std::string &entryFunctionFS="main", const std::string &compileVersionFS="ps_3_0")
 Get Shader from given source code. More...
 
virtual WavefrontOBJgetWavefrontOBJFromPath (const std::string &path)
 Load WavefrontOBJ from given path. More...
 
virtual void destroyManuallyLoadedResources ()
 Destroy and remove from map all manually loaded Resource's (those with Resource::getIsManuallyLoaded() flag set). More...
 
virtual void destroyManuallyLoadedResourcesByType (HF_ResourceType type)
 Destroy and remove from map all manually loaded Resource's (those with Resource::getIsManuallyLoaded() flag set and of given HF_ResourceType). More...
 
virtual void destroyResource (Resource *res)
 Destroy a Resource and remove from internal map. More...
 
void getAllSounds (std::vector< Sound *> &sounds)
 Get all Sound's in a convenient vector. More...
 
std::string & getLanguage ()
 Get current language. More...
 
void dump ()
 Dump parsed resource files names, ResourceGroup's names and Resource's string ids (useful for Debug). More...
 

Detailed Description

Interface for ResourceManager which handles all Resource loading etc.

'data/resources.xml' example:

<?xml version="1.0" ?>
<ResourceManifest>
<Resources id="init">
<Image id="IMAGE_TEST" path="data/fx/light_01.png"/>
<ImageAtlas id="IMAGEATLAS_TEST" path="data/atlas/myatlas.xml"/>
<Sound id="SFX_LASER_001" path="data/sfx/laser_001.ogg"/>
<font id="FONT_DEBUG" path="data/fonts/krone_regular_10.xml"/>
<TmxTileMap id="TMXTILEMAP_001" path="data/tmx/level001.tmx"/>
<Shader_VSFS id="SHADER_VSFS" path="data/shader/pixelate.vs,data/shader/pixelate.fs"/>
<WavefrontOBJ id="WAVEFRONTOBJ_CYBORG" path="data/models/cyborg/cyborg.obj"/>
</Resources>
</ResourceManifest>

'data/strings_en.xml" example:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ResourceManifest>
<string id="INIT_HELLO" text="Seeing is Doing"/>
</ResourceManifest>

The file 'data/strings_en.xml' is automatically loaded in ResourceManager::ResourceManager() constructor. '_en' is appended based on language specified. In above example language would be 'en' in config file: 'data/config.xml'.

C++ source:

// parse resource file:
bool result = HFCORE_RESMGR->parseResourceFile("data/resources.xml");
// load resource group needed:
HFCORE_RESMGR->loadResourceGroup("init");
// Get Image from Resource file by it's name:
HFCore::Image *image = HFCORE_RESMGR->getImage("IMAGE_TEST");
// Load Image from given path:
HFCore::Image *image =
HFCORE_RESMGR->getImageFromPath("data/fx/light_01.png");
// Get ImageAtlas from Resource file by it's name:
HFCORE_RESMGR->getImageAtlas("IMAGEATLAS_TEST");
// Load ImageAtlas from given path:
HFCORE_RESMGR->getImageAtlasFromPath("data/atlas/myatlas.xml");
// Get UString by it's name:
HFCore::UString str = HFCORE_RESMGR->getString("INIT_HELLO");
// Get Sound from Resource file by it's name:
HFCore::Sound *snd = HFCORE_RESMGR->getSound("SFX_LASER_001");
// Load Sound from given path:
HFCore::Sound *snd = HFCORE_SOUND->loadSound("data/sfx/laser_001.ogg");
// Get Font from Resource file by it's name:
HFCore::Font *font = HFCORE_RESMGR->getFont("FONT_DEBUG");
// Load Font from given path:
HFCore::Font *font =
HFCORE_RESMGR->getFontFromPath("data/fonts/krone_regular_10.xml");
// Get TmxMap from Resource file by it's name:
HFModule::TmxMap *map = HFCORE_RESMGR->getTmxTileMap("TMXTILEMAP_001");
// Load TmxMap from given path:
HFModule::TmxMap *map =
HFCORE_RESMGR->getTmxTileMapFromPath("data/tmx/level001.tmx");
// Get Shader from Resource file by it's name:
HFCore::Shader *shaderVSFS = HFCORE_RESMGR->getShader("SHADER_VSFS");
// Load Shader from given path (Vertex and Fragment):
std::string path = "data/shader/pixelate.vs,data/shader/pixelate.fs";
HFCore::Shader *shaderVSFS =
HFCORE_RESMGR->getShaderFromPath(path, type);
// Load Shader from given source code:
int32_hf sizeRead = 0;
int8_hf *v =
HFCORE_STORAGE->GetFileContents("data/shader/pixelate.vs",
&sizeRead);
std::string vs(v, sizeRead);
int8_hf *f =
HFCORE_STORAGE->GetFileContents("data/shader/pixelate.fs",
&sizeRead);
std::string fs(f, sizeRead);
// Load Shader from given source code (Vertex and Fragment):
HFCore::Shader *shaderVSFS =
HFCORE_RESMGR->getShaderFromSource(vs, fs);
// Get WavefrontOBJ from Resource file by it's name:
HFCORE_RESMGR->getWavefrontOBJ("WAVEFRONTOBJ_CYBORG");
// Load WavefrontOBJ from given path:
std::string path = "data/models/cyborg/cyborg.obj";
HFCORE_RESMGR->getWavefrontOBJFromPath(path);

Definition at line 162 of file ResourceManager.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum

Font type enum.

Enumerator
FONT_TYPE_FONTBUILDER 

Font type: FontBuilder.

FONT_TYPE_BMFONT 

Font type: BMFont.

FONT_TYPE_HFBMFONT_XML 

Font type: hfbmfont xml descriptor (engine tool 'src/tools/hfbmfont').

FONT_TYPE_HFBMFONT_BIN 

Font type: hfbmfont binary descriptor (engine tool 'src/tools/hfbmfont').

FONT_TYPE_HFBMFONT_TXT 

Font type: hfbmfont text descriptor (engine tool 'src/tools/hfbmfont').

FONT_TYPE_UNKNOWN 

Font type: Unknown.

Definition at line 168 of file ResourceManager.h.

Constructor & Destructor Documentation

◆ ResourceManager()

ResourceManager::ResourceManager ( ResourceLoader loader,
const std::string &  language 
)

Constructor.

Parameters
loaderPointer to ResourceLoader.
languageLanguage to use (example "en").

Definition at line 49 of file ResourceManager.cpp.

Member Function Documentation

◆ destroyManuallyLoadedResources()

void ResourceManager::destroyManuallyLoadedResources ( )
virtual

Destroy and remove from map all manually loaded Resource's (those with Resource::getIsManuallyLoaded() flag set).

Manually loaded Resource's are those loaded outside of resource file such as those methods ending with 'FromPath'.

Definition at line 1142 of file ResourceManager.cpp.

◆ destroyManuallyLoadedResourcesByType()

void ResourceManager::destroyManuallyLoadedResourcesByType ( HF_ResourceType  type)
virtual

Destroy and remove from map all manually loaded Resource's (those with Resource::getIsManuallyLoaded() flag set and of given HF_ResourceType).

Manually loaded Resource's are those loaded outside of resource file such as those methods ending with 'FromPath'.

Parameters
typeHF_ResourceType enum.

Definition at line 1193 of file ResourceManager.cpp.

◆ destroyResource()

void ResourceManager::destroyResource ( Resource res)
virtual

Destroy a Resource and remove from internal map.

Parameters
resResource to destroy.

Definition at line 1258 of file ResourceManager.cpp.

◆ destroyResources()

void ResourceManager::destroyResources ( bool  includeSounds)
virtual

Iterate through all Resource's in ResourceManager::ResourceMap and call Resource::destroy().

Parameters
includeSoundstrue to include Sound's, else false.

Definition at line 587 of file ResourceManager.cpp.

◆ dump()

void ResourceManager::dump ( )

Dump parsed resource files names, ResourceGroup's names and Resource's string ids (useful for Debug).

Information is dumped using envDebugLog() so it will only be visible when DEBUG is enabled.

Definition at line 1315 of file ResourceManager.cpp.

◆ getAllSounds()

void ResourceManager::getAllSounds ( std::vector< Sound *> &  sounds)

Get all Sound's in a convenient vector.

Parameters
[out]soundsVector containing all Sound's.

Definition at line 1296 of file ResourceManager.cpp.

◆ getFont()

Font * ResourceManager::getFont ( const std::string &  id)
virtual

Get Font by it's id.

Parameters
idString ID.
Returns
Font by it's id.
See also
getFontFromPath()

Definition at line 625 of file ResourceManager.cpp.

◆ getFontFromPath()

Font * ResourceManager::getFontFromPath ( const std::string &  path)
virtual

Load Font from given path.

Parameters
pathPath to Font.
Returns
Font loaded from given path.
See also
getFont()

Definition at line 899 of file ResourceManager.cpp.

◆ getImage()

Image * ResourceManager::getImage ( const std::string &  id)
virtual

Get Image by it's id.

Parameters
idString ID.
Returns
Image by it's id.
See also
getImageFromPath()

Definition at line 633 of file ResourceManager.cpp.

◆ getImageAtlas()

ImageAtlas * ResourceManager::getImageAtlas ( const std::string &  id)
virtual

Get ImageAtlas by it's id.

Parameters
idString ID.
Returns
ImageAtlas by it's id.
See also
getImageAtlasFromPath()

Definition at line 647 of file ResourceManager.cpp.

◆ getImageAtlasFromPath()

ImageAtlas * ResourceManager::getImageAtlasFromPath ( const std::string &  path)
virtual

Load ImageAtlas from given path.

Parameters
pathPath to ImageAtlas.
Returns
ImageAtlas loaded from given path.
See also
getImageAtlas()

Definition at line 785 of file ResourceManager.cpp.

◆ getImageFromPath()

Image * ResourceManager::getImageFromPath ( const std::string &  path)
virtual

Load Image from given path.

Parameters
pathPath to Image.
Returns
Image loaded from given path.
See also
getImage()

Definition at line 729 of file ResourceManager.cpp.

◆ getLanguage()

std::string& HFCore::ResourceManager::getLanguage ( )
inline

Get current language.

Returns
Current language.

Definition at line 564 of file ResourceManager.h.

◆ getResourceGroup()

ResourceGroup * ResourceManager::getResourceGroup ( const std::string &  groupName)
virtual

Get a pointer to ResourceGroup for given groupName.

Parameters
groupNameName of ResourceGroup.
Returns
Pointer to ResourceGroup for given groupName.

Definition at line 535 of file ResourceManager.cpp.

◆ getResourceLoader()

virtual ResourceLoader* HFCore::ResourceManager::getResourceLoader ( )
inlinevirtual

Get a pointer to ResourceLoader interface.

Returns
Pointer to ResourceLoader interface.

Definition at line 222 of file ResourceManager.h.

◆ getResourcesByPathMap()

virtual ResourceMap HFCore::ResourceManager::getResourcesByPathMap ( )
inlinevirtual

Get internal ResourceManager::ResourceMap containing all Resource's.

Returns
Internal ResourceManager::ResourceMap.

Definition at line 292 of file ResourceManager.h.

◆ getShader()

Shader * ResourceManager::getShader ( const std::string &  id)
virtual

Get Shader by it's id.

Parameters
idString ID.
Returns
Shader by it's id.
See also
getShaderFromPath()
getShaderFromSource()

Definition at line 701 of file ResourceManager.cpp.

◆ getShaderFromPath()

Shader * ResourceManager::getShaderFromPath ( const std::string &  path,
Shader::Type  type,
const std::string &  entryFunctionVS = "main",
const std::string &  compileVersionVS = "vs_3_0",
const std::string &  entryFunctionFS = "main",
const std::string &  compileVersionFS = "ps_3_0" 
)
virtual

Get Shader from given path.

Parameters
pathPath to Shader.
typeShader::Type.
entryFunctionVSEntry point function in Vertex shader to use. Only used for Direct3D HLSL as in OpenGL GLSL "main" is used.
compileVersionVSVersion to compile Vertex Shader for. Only used for Direct3D HLSL as compiler expects this to be set. Example version strings: "vs_2_0" and "vs_3_0". See MSDN for more information about version strings.
entryFunctionFSEntry point function in Fragment (pixel) shader to use. Only used for Direct3D HLSL as in OpenGL GLSL "main" is used.
compileVersionFSVersion to compile Fragment (pixel) Shader for. Only used for Direct3D HLSL as compiler expects this to be set. Example version strings: "ps_2_0" and "ps_3_0". See MSDN for more information about version strings.
Returns
Shader loaded from given path.
See also
getShader()
getShaderFromSource()

Definition at line 955 of file ResourceManager.cpp.

◆ getShaderFromSource()

Shader * ResourceManager::getShaderFromSource ( const std::string &  vertexShaderSrc,
const std::string &  fragmentShaderSrc,
const std::string &  entryFunctionVS = "main",
const std::string &  compileVersionVS = "vs_3_0",
const std::string &  entryFunctionFS = "main",
const std::string &  compileVersionFS = "ps_3_0" 
)
virtual

Get Shader from given source code.

Parameters
vertexShaderSrcVertex shader source code.
fragmentShaderSrcFragment (pixel) shader source code.
entryFunctionVSEntry point function in Vertex shader to use. Only used for Direct3D HLSL as in OpenGL GLSL "main" is used.
compileVersionVSVersion to compile Vertex Shader for. Only used for Direct3D HLSL as compiler expects this to be set. Example version strings: "vs_2_0" and "vs_3_0". See MSDN for more information about version strings.
entryFunctionFSEntry point function in Fragment (pixel) shader to use. Only used for Direct3D HLSL as in OpenGL GLSL "main" is used.
compileVersionFSVersion to compile Fragment (pixel) Shader for. Only used for Direct3D HLSL as compiler expects this to be set. Example version strings: "ps_2_0" and "ps_3_0". See MSDN for more information about version strings.
Returns
Shader for given source code.
See also
getShader()
getShaderFromPath()

Definition at line 1019 of file ResourceManager.cpp.

◆ getSound()

Sound * ResourceManager::getSound ( const std::string &  id)
virtual

Get Sound by it's id.

Parameters
idString ID.
Returns
Sound by it's id.

Definition at line 672 of file ResourceManager.cpp.

◆ getString()

UString ResourceManager::getString ( const std::string &  id)
virtual

Get UString by it's id.

Parameters
idString ID.
Returns
UString by it's id.
See also
hasString()

Definition at line 661 of file ResourceManager.cpp.

◆ getTmxTileMap()

HFModule::TmxMap * ResourceManager::getTmxTileMap ( const std::string &  id)
virtual

Get HFModule::TmxMap by it's id.

Parameters
idString ID.
Returns
HFModule::TmxMap by it's id.
Note
Engine must be compiled with pre-processor HF_MODULE_TMXPARSER set for this method to be available along with compiling all sources in 'src/engine/modules/tmxparser'.
See also
getTmxTileMapFromPath()

Definition at line 686 of file ResourceManager.cpp.

◆ getTmxTileMapFromPath()

HFModule::TmxMap * ResourceManager::getTmxTileMapFromPath ( const std::string &  path)
virtual

Load HFModule::TmxMap from given path.

Parameters
pathPath to HFModule::TmxMap.
Returns
HFModule::TmxMap loaded from given path.
Note
Engine must be compiled with pre-processor HF_MODULE_TMXPARSER set for this method to be available along with compiling all sources in 'src/engine/modules/tmxparser'.
See also
getTmxTileMap()

Definition at line 842 of file ResourceManager.cpp.

◆ getWavefrontOBJ()

WavefrontOBJ * ResourceManager::getWavefrontOBJ ( const std::string &  id)
virtual

Get WavefrontOBJ by it's id.

Parameters
idString ID.
Returns
WavefrontOBJ by it's id.
See also
getWavefrontOBJFromPath()

Definition at line 715 of file ResourceManager.cpp.

◆ getWavefrontOBJFromPath()

WavefrontOBJ * ResourceManager::getWavefrontOBJFromPath ( const std::string &  path)
virtual

Load WavefrontOBJ from given path.

Parameters
pathPath to WavefrontOBJ.
Returns
WavefrontOBJ loaded from given path.
See also
getWavefrontOBJ()

Definition at line 1086 of file ResourceManager.cpp.

◆ hasString()

bool ResourceManager::hasString ( const std::string &  id)
virtual

Determines whether string with id exists.

Parameters
idString ID.
Returns
true if string id exists, else false.
See also
getString()

Definition at line 667 of file ResourceManager.cpp.

◆ initResources()

void ResourceManager::initResources ( bool  includeSounds)
virtual

Iterate through all Resource's in ResourceManager::ResourceMap and call Resource::init().

Parameters
includeSoundstrue to include Sound's, else false.

Definition at line 606 of file ResourceManager.cpp.

◆ loadResourceGroup()

bool ResourceManager::loadResourceGroup ( const std::string &  groupName)
virtual

Load ResourceGroup named groupName and call Resource::load() for each item in group.

Parameters
groupNameName of ResourceGroup.
Returns
true if successful, else false.

Definition at line 498 of file ResourceManager.cpp.

◆ parseResourceFile()

bool ResourceManager::parseResourceFile ( const std::string &  fileName)
virtual

Parse resource file and all of its ResourceGroup's.

Besides parsing, map / create the Resource internally so that it can be loaded / unloaded easily using loadResourceGroup() and unloadResourceGroup().

Parameters
fileNameFile to parse.
Returns
true if successful, else false.

Definition at line 175 of file ResourceManager.cpp.

◆ unloadResourceGroup()

void ResourceManager::unloadResourceGroup ( const std::string &  groupName)
virtual

Unload ResourceGroup named groupName and call Resource::release() for each item in group.

Parameters
groupNameName of ResourceGroup.

Definition at line 545 of file ResourceManager.cpp.


The documentation for this class was generated from the following files: