1. dbacchet
  2. dbsdev

Commits

dbacchet  committed 26e5d8a

resource manager refactoring

  • Participants
  • Parent commits a38311e
  • Branches default

Comments (0)

Files changed (18)

File temp/test_hordefiles/Horde3D.cpp

View file
 #include "egModules.h"
 #include "egCom.h"
 #include "engine_logger.h"
+#include "engine_resource_manager.h"
 // #include "egExtensions.h"
 #include "egRenderer.h"
 #include "egModel.h"
 void h3dShowOverlays( const float *verts, int vertCount, float colR, float colG,
                              float colB, float colA, ResHandle materialRes, int flags )
 {
-	Resource *resObj = Modules::resMan().resolveResHandle( materialRes ); 
+	Resource *resObj = Modules::resMan().get_resource( materialRes ); 
 	APIFUNC_VALIDATE_RES_TYPE( resObj, ResourceTypes::Material, "h3dShowOverlays", APIFUNC_RET_VOID );
 
 	float rgba[4] = { colR, colG, colB, colA };
 
 int h3dGetResType( ResHandle res )
 {
-	Resource *resObj = Modules::resMan().resolveResHandle( res );
+	Resource *resObj = Modules::resMan().get_resource( res );
 	APIFUNC_VALIDATE_RES( resObj, "h3dGetResType", ResourceTypes::Undefined );
 	
 	return resObj->get_type();
 
 const char *h3dGetResName( ResHandle res )
 {
-	Resource *resObj = Modules::resMan().resolveResHandle( res );
+	Resource *resObj = Modules::resMan().get_resource( res );
 	APIFUNC_VALIDATE_RES( resObj, "h3dGetResName", emptyCString );
 	
 	return resObj->get_name().c_str();
 
 ResHandle h3dFindResource( int type, const char *name )
 {
-	Resource *resObj = Modules::resMan().getResource( type, safeStr( name, 0 ) );
+	Resource *resObj = Modules::resMan().get_resource( type, safeStr( name, 0 ) );
 	
 	return resObj != 0x0 ? resObj->get_handle() : 0;
 }
 
 ResHandle h3dAddResource( int type, const char *name, int flags )
 {
-	return Modules::resMan().addResource( type, safeStr( name, 0 ), flags );
+	return Modules::resMan().add_resource( type, safeStr( name, 0 ), flags );
 }
 
 
 // int h3dRemoveResource( ResHandle res )
 // {
-// 	Resource *resObj = Modules::resMan().resolveResHandle( res );
+// 	Resource *resObj = Modules::resMan().get_resource( res );
 // 	APIFUNC_VALIDATE_RES( resObj, "h3dRemoveResource", -1 );
 	
 // 	return Modules::resMan().removeResource( *resObj, true );
 
 bool h3dIsResLoaded( ResHandle res )
 {
-	Resource *resObj = Modules::resMan().resolveResHandle( res );
+	Resource *resObj = Modules::resMan().get_resource( res );
 	APIFUNC_VALIDATE_RES( resObj, "h3dIsResLoaded", false );
 	
 	return resObj->is_loaded();
 
 bool h3dLoadResource( ResHandle res, const char *data, int size )
 {
-	Resource *resObj = Modules::resMan().resolveResHandle( res );
+	Resource *resObj = Modules::resMan().get_resource( res );
 	APIFUNC_VALIDATE_RES( resObj, "h3dLoadResource", false );
 	
 	Modules::log().info( "Loading resource '%s'", resObj->get_name().c_str() );
 
 void h3dUnloadResource( ResHandle res )
 {
-	Resource *resObj = Modules::resMan().resolveResHandle( res );
+	Resource *resObj = Modules::resMan().get_resource( res );
 	APIFUNC_VALIDATE_RES( resObj, "h3dUnloadResource", APIFUNC_RET_VOID );
 
 	resObj->unload();
 
 ResHandle h3dQueryUnloadedResource( int index )
 {
-	return Modules::resMan().queryUnloadedResource( index );
+	return Modules::resMan().query_unloaded_resource( index );
 }
 
 
 void h3dReleaseUnusedResources()
 {
-	Modules::resMan().releaseUnusedResources();
+	Modules::resMan().release_unused_resources();
 }
 
 
 	TextureResource *texRes = new TextureResource( safeStr( name, 0 ), (uint32_t)width,
 		(uint32_t)height, 1, (TextureFormats::List)fmt, flags );
 
-	if (Modules::resMan().getResource(ResourceTypes::Texture, name) == 0)
+	if (Modules::resMan().get_resource(ResourceTypes::Texture, name) == 0)
 	{	
 		Modules::log().debug( "Failed to add resource in h3dCreateTexture; maybe the name is already in use?" );
 		delete texRes;
 	}
 
-	ResHandle res = Modules::resMan().addResource( *texRes );
+	ResHandle res = Modules::resMan().add_resource( *texRes );
 	return res;
 }
 
 
 bool h3dSetMaterialUniform( ResHandle materialRes, const char *name, float a, float b, float c, float d )
 {
-	Resource *resObj = Modules::resMan().resolveResHandle( materialRes );
+	Resource *resObj = Modules::resMan().get_resource( materialRes );
 	APIFUNC_VALIDATE_RES_TYPE( resObj, ResourceTypes::Material, "h3dSetMaterialUniform", false );
 
 	return ((MaterialResource *)resObj)->setUniform( safeStr( name, 0 ), a, b, c, d );
 
 void h3dResizePipelineBuffers( ResHandle pipeRes, int width, int height )
 {
-	Resource *resObj = Modules::resMan().resolveResHandle( pipeRes );
+	Resource *resObj = Modules::resMan().get_resource( pipeRes );
 	APIFUNC_VALIDATE_RES_TYPE( resObj, ResourceTypes::Pipeline, "h3dResizePipelineBuffers", APIFUNC_RET_VOID );
 
 	PipelineResource *pipeResObj = (PipelineResource *)resObj;
 {
 	if( pipelineRes != 0 )
 	{
-		Resource *resObj = Modules::resMan().resolveResHandle( pipelineRes );
+		Resource *resObj = Modules::resMan().get_resource( pipelineRes );
 		APIFUNC_VALIDATE_RES_TYPE( resObj, ResourceTypes::Pipeline, "h3dGetRenderTargetData", false );
 		
 		return ((PipelineResource *)resObj)->getRenderTargetData( safeStr( targetName, 0 ), bufIndex,
 	SceneNode *parentNode = Modules::sceneMan().resolveNodeHandle( parent );
 	APIFUNC_VALIDATE_NODE( parentNode, "h3dAddNodes", 0 );
 	
-	Resource *sgRes = Modules::resMan().resolveResHandle( sceneGraphRes );
+	Resource *sgRes = Modules::resMan().get_resource( sceneGraphRes );
 	APIFUNC_VALIDATE_RES_TYPE( sgRes, ResourceTypes::SceneGraph, "h3dAddNodes", 0 );
 
 	if( !sgRes->is_loaded() )
 {
 	SceneNode *parentNode = Modules::sceneMan().resolveNodeHandle( parent );
 	APIFUNC_VALIDATE_NODE( parentNode, "h3dAddModelNode", 0 );
-	Resource *geoRes = Modules::resMan().resolveResHandle( geometryRes );
+	Resource *geoRes = Modules::resMan().get_resource( geometryRes );
 	APIFUNC_VALIDATE_RES_TYPE( geoRes, ResourceTypes::Geometry, "h3dAddModelNode", 0 );
 
 	//Modules::log().info( "Adding Model node '%s'", safeStr( name ).c_str() );
 	Resource *animRes = 0x0;
 	if( animationRes != 0 )
 	{
-		animRes = Modules::resMan().resolveResHandle( animationRes );
+		animRes = Modules::resMan().get_resource( animationRes );
 		APIFUNC_VALIDATE_RES_TYPE( animRes, ResourceTypes::Animation, "h3dSetupModelAnimStage", APIFUNC_RET_VOID );
 	}
 	
 {
 	SceneNode *parentNode = Modules::sceneMan().resolveNodeHandle( parent );
 	APIFUNC_VALIDATE_NODE( parentNode, "h3dAddMeshNode", 0 );
-	Resource *matRes = Modules::resMan().resolveResHandle( materialRes );
+	Resource *matRes = Modules::resMan().get_resource( materialRes );
 	APIFUNC_VALIDATE_RES_TYPE( matRes, ResourceTypes::Material, "h3dAddMeshNode", 0 );
 
 	//Modules::log().info( "Adding Mesh node '%s'", safeStr( name ).c_str() );
 {
 	SceneNode *parentNode = Modules::sceneMan().resolveNodeHandle( parent );
 	APIFUNC_VALIDATE_NODE( parentNode, "h3dAddLightNode", 0 );
-	Resource *matRes = Modules::resMan().resolveResHandle( materialRes );
+	Resource *matRes = Modules::resMan().get_resource( materialRes );
 	if(matRes != 0x0 )
 	{
 		APIFUNC_VALIDATE_RES_TYPE( matRes, ResourceTypes::Material, "h3dAddLightNode", 0 );
 {
 	SceneNode *parentNode = Modules::sceneMan().resolveNodeHandle( parent );
 	APIFUNC_VALIDATE_NODE( parentNode, "h3dAddCameraNode", 0 );
-	Resource *pipeRes = Modules::resMan().resolveResHandle( pipelineRes );
+	Resource *pipeRes = Modules::resMan().get_resource( pipelineRes );
 	APIFUNC_VALIDATE_RES_TYPE( pipeRes, ResourceTypes::Pipeline, "h3dAddCameraNode", 0 );
 	
 	//Modules::log().info( "Adding Camera node '%s'", safeStr( name ).c_str() );
 {
 	SceneNode *parentNode = Modules::sceneMan().resolveNodeHandle( parent );
 	APIFUNC_VALIDATE_NODE( parentNode, "h3dAddEmitterNode", 0 );
-	Resource *matRes = Modules::resMan().resolveResHandle( materialRes );
+	Resource *matRes = Modules::resMan().get_resource( materialRes );
 	APIFUNC_VALIDATE_RES_TYPE( matRes, ResourceTypes::Material, "h3dAddEmitterNode", 0 );
-	Resource *effRes = Modules::resMan().resolveResHandle( particleEffectRes );
+	Resource *effRes = Modules::resMan().get_resource( particleEffectRes );
 	APIFUNC_VALIDATE_RES_TYPE( effRes, ResourceTypes::ParticleEffect, "h3dAddEmitterNode", 0 );
 	
 	//Modules::log().info( "Adding Emitter node '%s'", safeStr( name ).c_str() );

File temp/test_hordefiles/egAnimatables.cpp

View file
 //
 // *************************************************************************************************
 
+
 #include "egModel.h"
 #include "egMaterial.h"
 #include "egModules.h"
 #include "egRenderer.h"
 
+#include "engine_resource_manager.h"
 
 
 namespace Horde3D {
 	itr = attribs.find( "material" );
 	if( itr != attribs.end() )
 	{
-		uint32_t res = Modules::resMan().addResource( ResourceTypes::Material, itr->second, 0 );
+		uint32_t res = Modules::resMan().add_resource( ResourceTypes::Material, itr->second, 0 );
 		if( res != 0 )
-			meshTpl->matRes = (MaterialResource *)Modules::resMan().resolveResHandle( res );
+			meshTpl->matRes = (MaterialResource *)Modules::resMan().get_resource( res );
 	}
 	else result = false;
 	itr = attribs.find( "batchStart" );
 	switch( param )
 	{
 	case MeshNodeParams::MatResI:
-		res = Modules::resMan().resolveResHandle( value );
+		res = Modules::resMan().get_resource( value );
 		if( res != 0x0 && res->get_type() == ResourceTypes::Material )
 		{
 			_materialRes = (MaterialResource *)res;

File temp/test_hordefiles/egCamera.cpp

View file
 #include "egCamera.h"
 #include "egModules.h"
 #include "egRenderer.h"
+#include "engine_resource_manager.h"
 
 
 
 	itr = attribs.find( "pipeline" );
 	if( itr != attribs.end() )
 	{
-		uint32_t res = Modules::resMan().addResource( ResourceTypes::Pipeline, itr->second, 0 );
-		cameraTpl->pipeRes = (PipelineResource *)Modules::resMan().resolveResHandle( res );
+		uint32_t res = Modules::resMan().add_resource( ResourceTypes::Pipeline, itr->second, 0 );
+		cameraTpl->pipeRes = (PipelineResource *)Modules::resMan().get_resource( res );
 	}
 	else result = false;
 	itr = attribs.find( "outputTex" );
 	if( itr != attribs.end() )
 	{	
-		cameraTpl->outputTex = (TextureResource *)Modules::resMan().getResource(ResourceTypes::Texture, itr->second );
+		cameraTpl->outputTex = (TextureResource *)Modules::resMan().get_resource(ResourceTypes::Texture, itr->second );
 	}
 	itr = attribs.find( "outputBufferIndex" );
 	if( itr != attribs.end() ) cameraTpl->outputBufferIndex = atoi( itr->second.c_str() );
 	switch( param )
 	{
 	case CameraNodeParams::PipeResI:
-		res = Modules::resMan().resolveResHandle( value );
+		res = Modules::resMan().get_resource( value );
 		if( res != 0x0 && res->get_type() == ResourceTypes::Pipeline )
 			_pipelineRes = (PipelineResource *)res;
 		else
 			Modules::setError( "Invalid handle in h3dSetNodeParamI for H3DCamera::PipeResI" );
 		return;
 	case CameraNodeParams::OutTexResI:
-		res = Modules::resMan().resolveResHandle( value );
+		res = Modules::resMan().get_resource( value );
 		if( res == 0x0 || (res->get_type() == ResourceTypes::Texture &&
 		    ((TextureResource *)res)->getTexType() == TextureTypes::Tex2D) )
 			_outputTex = (TextureResource *)res;

File temp/test_hordefiles/egGeometry.cpp

View file
 
 #include "engine_logger.h"
 #include "egGeometry.h"
-#include "egResource.h"
+#include "engine_resource.h"
 // #include "egAnimation.h"
 #include "egModules.h"
 // #include "egCom.h"

File temp/test_hordefiles/egLight.cpp

View file
 #include "egMaterial.h"
 #include "egModules.h"
 #include "egRenderer.h"
-
+#include "engine_resource_manager.h"
 
 
 namespace Horde3D {
 	itr = attribs.find( "material" );
 	if( itr != attribs.end() )
 	{
-		uint32_t res = Modules::resMan().addResource( ResourceTypes::Material, itr->second, 0 );
+		uint32_t res = Modules::resMan().add_resource( ResourceTypes::Material, itr->second, 0 );
 		if( res != 0 )
-			lightTpl->matRes = (MaterialResource *)Modules::resMan().resolveResHandle( res );
+			lightTpl->matRes = (MaterialResource *)Modules::resMan().get_resource( res );
 	}
 	itr = attribs.find( "lightingContext" );
 	if( itr != attribs.end() ) lightTpl->lightingContext = itr->second;
 	switch( param )
 	{
 	case LightNodeParams::MatResI:
-		res = Modules::resMan().resolveResHandle( value );
+		res = Modules::resMan().get_resource( value );
 		if( res == 0x0 || res->get_type() == ResourceTypes::Material )
 			_materialRes = (MaterialResource *)res;
 		else

File temp/test_hordefiles/egMaterial.cpp

View file
 // *************************************************************************************************
 
 #include "engine_logger.h"
+#include "engine_resource_manager.h"
 #include "egMaterial.h"
 #include "egTexture.h"
 #include "egModules.h"
 	// Link
 	if( strcmp( rootNode.getAttribute( "link", "" ), "" ) != 0 )
 	{
-		uint32_t mat = Modules::resMan().addResource(
+		uint32_t mat = Modules::resMan().add_resource(
 			ResourceTypes::Material, rootNode.getAttribute( "link" ), 0 );
-		_matLink = (MaterialResource *)Modules::resMan().resolveResHandle( mat );
+		_matLink = (MaterialResource *)Modules::resMan().get_resource( mat );
 		if( _matLink == this )
 			return raiseError( "Illegal self link in material, causing infinite link loop" );
 	}
 	{
 		if( node1.getAttribute( "source" ) == 0x0 ) return raiseError( "Missing Shader attribute 'source'" );
 			
-		uint32_t shader = Modules::resMan().addResource(
+		uint32_t shader = Modules::resMan().add_resource(
 				ResourceTypes::Shader, node1.getAttribute( "source" ), 0 );
-		_shaderRes = (ShaderResource *)Modules::resMan().resolveResHandle( shader );
+		_shaderRes = (ShaderResource *)Modules::resMan().get_resource( shader );
 
 		_combMask = ShaderResource::calcCombMask( _shaderFlags );
 		_shaderRes->preLoadCombination( _combMask );
 			_stricmp( node1.getAttribute( "mipmaps", "1" ), "0" ) == 0 )
 			flags |= TextureFlags::TexNoMipmaps;
 
-		texMap = Modules::resMan().addResource(
+		texMap = Modules::resMan().add_resource(
 			ResourceTypes::Texture, node1.getAttribute( "map" ), flags );
 
-		sampler.texRes = (TextureResource *)Modules::resMan().resolveResHandle( texMap );
+		sampler.texRes = (TextureResource *)Modules::resMan().get_resource( texMap );
 		
 		_samplers.push_back( sampler );
 		

File temp/test_hordefiles/egMaterial.h

View file
 #define _egMaterial_H_
 
 #include "egPrerequisites.h"
-#include "egResource.h"
+#include "engine_resource.h"
 #include "egShader.h"
 #include "egTexture.h"
 

File temp/test_hordefiles/egModel.cpp

View file
 #include "egModules.h"
 #include "egRenderer.h"
 #include "egCom.h"
+#include "engine_resource_manager.h"
 #include <cstring>
 
 
 	map< string, string >::iterator itr = attribs.find( "geometry" );
 	if( itr != attribs.end() )
 	{
-		uint32_t res = Modules::resMan().addResource( ResourceTypes::Geometry, itr->second, 0 );
+		uint32_t res = Modules::resMan().add_resource( ResourceTypes::Geometry, itr->second, 0 );
 		if( res != 0 )
-			modelTpl->geoRes = (GeometryResource *)Modules::resMan().resolveResHandle( res );
+			modelTpl->geoRes = (GeometryResource *)Modules::resMan().get_resource( res );
 	}
 	else result = false;
 	itr = attribs.find( "softwareSkinning" );
 	switch( param )
 	{
 	case ModelNodeParams::GeoResI:
-		res = Modules::resMan().resolveResHandle( value );
+		res = Modules::resMan().get_resource( value );
 		if( res != 0x0 && res->get_type() == ResourceTypes::Geometry )
 			setGeometryRes( *(GeometryResource *)res );
 		else

File temp/test_hordefiles/egModules.cpp

View file
 // *************************************************************************************************
 
 #include "engine_logger.h"
-#include "egResource.h"
+#include "engine_resource_manager.h"
 #include "egScene.h"
 #include "egSceneGraphRes.h"
 #include "egModules.h"
 #include "egLight.h"
 #include "egCamera.h"
 #include "egParticle.h"
-#include "egResource.h"
 #include "engine_renderdevice_opengl.h"
 #include "egRenderer.h"
 #include "egPipeline.h"
 
 bool Modules::init()
 {
-	// Create modules (order is important because of dependencies)
-	if( _engineLog == 0x0 ) _engineLog = new Logger(true, "Horde3D.log");
-	if( _engineConfig == 0x0 ) _engineConfig = new EngineConfig();
-	if( _sceneManager == 0x0 ) _sceneManager = new SceneManager();
-	if( _resourceManager == 0x0 ) _resourceManager = new ResourceManager();
-	if( _renderDevice == 0x0 ) _renderDevice = new RenderDevice();
-	gRDI = _renderDevice;
-	if( _renderer == 0x0 ) _renderer = new Renderer();
-	if( _statManager == 0x0 ) _statManager = new StatManager();
+    // Create modules (order is important because of dependencies)
+    if( _engineLog == 0x0 ) _engineLog = new Logger(true, "Horde3D.log");
+    if( _engineConfig == 0x0 ) _engineConfig = new EngineConfig();
+    if( _sceneManager == 0x0 ) _sceneManager = new SceneManager();
+    if( _resourceManager == 0x0 ) _resourceManager = new ResourceManager();
+    if( _renderDevice == 0x0 ) _renderDevice = new RenderDevice();
+    gRDI = _renderDevice;
+    if( _renderer == 0x0 ) _renderer = new Renderer();
+    if( _statManager == 0x0 ) _statManager = new StatManager();
 
-	// Init modules
-	if( !renderer().init() ) return false;
+    // Init modules
+    if( !renderer().init() ) return false;
 
-	// Register resource types
-	resMan().registerResType( ResourceTypes::SceneGraph,	"SceneGraph",	SceneGraphResource::factoryFunc );
-	resMan().registerResType( ResourceTypes::Geometry,  	"Geometry", 	GeometryResource::factoryFunc );
-	resMan().registerResType( ResourceTypes::Geometry, 		"Geometry",		GeometryResource::factoryFunc );
-	resMan().registerResType( ResourceTypes::Animation, 	"Animation",	AnimationResource::factoryFunc );
-	resMan().registerResType( ResourceTypes::Material, 		"Material",		MaterialResource::factoryFunc );
-	resMan().registerResType( ResourceTypes::Code, 			"Code",			CodeResource::factoryFunc );
-	resMan().registerResType( ResourceTypes::Shader, 		"Shader",		ShaderResource::factoryFunc );
-	resMan().registerResType( ResourceTypes::Texture, 		"Texture", 		TextureResource::factoryFunc );
-	resMan().registerResType( ResourceTypes::Texture, 		"Texture",		TextureResource::factoryFunc );
-	resMan().registerResType( ResourceTypes::ParticleEffect,"ParticleEffect",ParticleEffectResource::factoryFunc );
-	resMan().registerResType( ResourceTypes::Pipeline, 		"Pipeline",		PipelineResource::factoryFunc );
+    // Register resource types
+    resMan().register_resource_type( ResourceTypes::SceneGraph,     "SceneGraph",   SceneGraphResource::factoryFunc );
+    resMan().register_resource_type( ResourceTypes::Geometry,       "Geometry",     GeometryResource::factoryFunc );
+    resMan().register_resource_type( ResourceTypes::Geometry,       "Geometry",     GeometryResource::factoryFunc );
+    resMan().register_resource_type( ResourceTypes::Animation,      "Animation",    AnimationResource::factoryFunc );
+    resMan().register_resource_type( ResourceTypes::Material,       "Material",     MaterialResource::factoryFunc );
+    resMan().register_resource_type( ResourceTypes::Code,           "Code",         CodeResource::factoryFunc );
+    resMan().register_resource_type( ResourceTypes::Shader,         "Shader",       ShaderResource::factoryFunc );
+    resMan().register_resource_type( ResourceTypes::Texture,        "Texture",      TextureResource::factoryFunc );
+    resMan().register_resource_type( ResourceTypes::Texture,        "Texture",      TextureResource::factoryFunc );
+    resMan().register_resource_type( ResourceTypes::ParticleEffect, "ParticleEffect",ParticleEffectResource::factoryFunc );
+    resMan().register_resource_type( ResourceTypes::Pipeline,       "Pipeline",     PipelineResource::factoryFunc );
 
-	// Register node types
-	sceneMan().registerNodeType( SceneNodeTypes::Group, "Group",
-		GroupNode::parsingFunc, GroupNode::factoryFunc );
-	sceneMan().registerNodeType( SceneNodeTypes::Model, "Model",
-		ModelNode::parsingFunc, ModelNode::factoryFunc);
-	sceneMan().registerNodeType( SceneNodeTypes::Mesh, "Mesh",
-		MeshNode::parsingFunc, MeshNode::factoryFunc );
-	sceneMan().registerNodeType( SceneNodeTypes::Joint, "Joint",
-		JointNode::parsingFunc, JointNode::factoryFunc );
-	sceneMan().registerNodeType( SceneNodeTypes::Light, "Light",
-		LightNode::parsingFunc, LightNode::factoryFunc );
-	sceneMan().registerNodeType( SceneNodeTypes::Camera, "Camera",
-		CameraNode::parsingFunc, CameraNode::factoryFunc );
-	sceneMan().registerNodeType( SceneNodeTypes::Emitter, "Emitter",
-		EmitterNode::parsingFunc, EmitterNode::factoryFunc );
+    // Register node types
+    sceneMan().registerNodeType( SceneNodeTypes::Group, "Group",
+        GroupNode::parsingFunc, GroupNode::factoryFunc );
+    sceneMan().registerNodeType( SceneNodeTypes::Model, "Model",
+        ModelNode::parsingFunc, ModelNode::factoryFunc);
+    sceneMan().registerNodeType( SceneNodeTypes::Mesh, "Mesh",
+        MeshNode::parsingFunc, MeshNode::factoryFunc );
+    sceneMan().registerNodeType( SceneNodeTypes::Joint, "Joint",
+        JointNode::parsingFunc, JointNode::factoryFunc );
+    sceneMan().registerNodeType( SceneNodeTypes::Light, "Light",
+        LightNode::parsingFunc, LightNode::factoryFunc );
+    sceneMan().registerNodeType( SceneNodeTypes::Camera, "Camera",
+        CameraNode::parsingFunc, CameraNode::factoryFunc );
+    sceneMan().registerNodeType( SceneNodeTypes::Emitter, "Emitter",
+        EmitterNode::parsingFunc, EmitterNode::factoryFunc );
 
-	// Register render functions
-	renderer().registerRenderFunc( SceneNodeTypes::Mesh, Renderer::drawMeshes );
-	renderer().registerRenderFunc( SceneNodeTypes::Emitter, Renderer::drawParticles );
-	
-	// Create default resources
-	TextureResource *tex2DRes = new TextureResource(
-		"$Tex2D", 32, 32, 1, TextureFormats::BGRA8, TextureFlags::TexNoMipmaps );
-	void *image = tex2DRes->mapStream( TextureResData::ImageElem, 0, TextureResData::ImgPixelStream, false, true );
-	ASSERT( image != 0x0 );
-	for( uint32_t i = 0; i < 32*32; ++i ) ((uint32_t *)image)[i] = 0xffffffff;
-	tex2DRes->unmapStream();
-	tex2DRes->addRef();
-	resMan().addResource( *tex2DRes );
+    // Register render functions
+    renderer().registerRenderFunc( SceneNodeTypes::Mesh, Renderer::drawMeshes );
+    renderer().registerRenderFunc( SceneNodeTypes::Emitter, Renderer::drawParticles );
+    
+    // Create default resources
+    TextureResource *tex2DRes = new TextureResource(
+        "$Tex2D", 32, 32, 1, TextureFormats::BGRA8, TextureFlags::TexNoMipmaps );
+    void *image = tex2DRes->mapStream( TextureResData::ImageElem, 0, TextureResData::ImgPixelStream, false, true );
+    ASSERT( image != 0x0 );
+    for( uint32_t i = 0; i < 32*32; ++i ) ((uint32_t *)image)[i] = 0xffffffff;
+    tex2DRes->unmapStream();
+    tex2DRes->addRef();
+    resMan().add_resource( *tex2DRes );
 
-	TextureResource *texCubeRes = new TextureResource(
-		"$TexCube", 32, 32, 1, TextureFormats::BGRA8, TextureFlags::TexCubemap | TextureFlags::TexNoMipmaps );
-	for( uint32_t i = 0; i < 6; ++i )
-	{
-		image = texCubeRes->mapStream( TextureResData::ImageElem, i, TextureResData::ImgPixelStream, false, true );
-		ASSERT( image != 0x0 );
-		for( uint32_t j = 0; j < 32*32; ++j ) ((uint32_t *)image)[j] = 0xff000000;
-		texCubeRes->unmapStream();
-	}
-	texCubeRes->addRef();
-	resMan().addResource( *texCubeRes );
+    TextureResource *texCubeRes = new TextureResource(
+        "$TexCube", 32, 32, 1, TextureFormats::BGRA8, TextureFlags::TexCubemap | TextureFlags::TexNoMipmaps );
+    for( uint32_t i = 0; i < 6; ++i )
+    {
+        image = texCubeRes->mapStream( TextureResData::ImageElem, i, TextureResData::ImgPixelStream, false, true );
+        ASSERT( image != 0x0 );
+        for( uint32_t j = 0; j < 32*32; ++j ) ((uint32_t *)image)[j] = 0xff000000;
+        texCubeRes->unmapStream();
+    }
+    texCubeRes->addRef();
+    resMan().add_resource( *texCubeRes );
 
-	TextureResource *tex3DRes = new TextureResource(
-		"$Tex3D", 16, 16, 4, TextureFormats::BGRA8, TextureFlags::TexNoMipmaps );
-	image = tex3DRes->mapStream( TextureResData::ImageElem, 0, TextureResData::ImgPixelStream, false, true );
-	ASSERT( image != 0x0 );
-	for( uint32_t i = 0; i < 16*16*4; ++i ) ((uint32_t *)image)[i] = 0xffffffff;
-	tex3DRes->unmapStream();
-	tex3DRes->addRef();
-	resMan().addResource( *tex3DRes );
-	
-	return true;
+    TextureResource *tex3DRes = new TextureResource(
+        "$Tex3D", 16, 16, 4, TextureFormats::BGRA8, TextureFlags::TexNoMipmaps );
+    image = tex3DRes->mapStream( TextureResData::ImageElem, 0, TextureResData::ImgPixelStream, false, true );
+    ASSERT( image != 0x0 );
+    for( uint32_t i = 0; i < 16*16*4; ++i ) ((uint32_t *)image)[i] = 0xffffffff;
+    tex3DRes->unmapStream();
+    tex3DRes->addRef();
+    resMan().add_resource( *tex3DRes );
+    
+    return true;
 }
 
 
 void Modules::release()
 {
-	// Remove overlays since they reference resources and resource manager is removed before renderer
-	if( _renderer ) _renderer->clearOverlays();
-	
-	// Order of destruction is important
-	delete _sceneManager; _sceneManager = 0x0;
-	delete _resourceManager; _resourceManager = 0x0;
-	delete _renderer; _renderer = 0x0;
-	delete _renderDevice; _renderDevice = 0x0;
-	gRDI = 0x0;
-	delete _statManager; _statManager = 0x0;
-	delete _engineLog; _engineLog = 0x0;
-	delete _engineConfig; _engineConfig = 0x0;
+    // Remove overlays since they reference resources and resource manager is removed before renderer
+    if( _renderer ) _renderer->clearOverlays();
+    
+    // Order of destruction is important
+    delete _sceneManager; _sceneManager = 0x0;
+    delete _resourceManager; _resourceManager = 0x0;
+    delete _renderer; _renderer = 0x0;
+    delete _renderDevice; _renderDevice = 0x0;
+    gRDI = 0x0;
+    delete _statManager; _statManager = 0x0;
+    delete _engineLog; _engineLog = 0x0;
+    delete _engineConfig; _engineConfig = 0x0;
 }
 
 
 void Modules::setError( const char *errorStr1, const char *errorStr2 )
 {
-	static std::string msg;
-	msg.resize( 0 );
+    static std::string msg;
+    msg.resize( 0 );
 
-	if( errorStr1 ) msg.append( errorStr1 );
-	if( errorStr2 ) msg.append( errorStr2 );
-	
-	_errorFlag = true;
-	_engineLog->debug( msg.c_str() );
+    if( errorStr1 ) msg.append( errorStr1 );
+    if( errorStr2 ) msg.append( errorStr2 );
+    
+    _errorFlag = true;
+    _engineLog->debug( msg.c_str() );
 }
 
 
 bool Modules::getError()
 {
-	if( _errorFlag )
-	{
-		_errorFlag = false;
-		return true;
-	}
-	else
-		return false;
+    if( _errorFlag )
+    {
+        _errorFlag = false;
+        return true;
+    }
+    else
+        return false;
 }
 
 }  // namespace

File temp/test_hordefiles/egParticle.cpp

View file
 #include "egModules.h"
 #include "egCom.h"
 #include "egRenderer.h"
+#include "engine_resource_manager.h"
 #include "utXML.h"
 
 
 	itr = attribs.find( "material" );
 	if( itr != attribs.end() )
 	{
-		uint32_t res = Modules::resMan().addResource( ResourceTypes::Material, itr->second, 0 );
+		uint32_t res = Modules::resMan().add_resource( ResourceTypes::Material, itr->second, 0 );
 		if( res != 0 )
-			emitterTpl->matRes = (MaterialResource *)Modules::resMan().resolveResHandle( res );
+			emitterTpl->matRes = (MaterialResource *)Modules::resMan().get_resource( res );
 	}
 	else result = false;
 	itr = attribs.find( "particleEffect" );
 	if( itr != attribs.end() )
 	{
-		uint32_t res = Modules::resMan().addResource( ResourceTypes::ParticleEffect, itr->second, 0 );
+		uint32_t res = Modules::resMan().add_resource( ResourceTypes::ParticleEffect, itr->second, 0 );
 		if( res != 0 )
-			emitterTpl->effectRes = (ParticleEffectResource *)Modules::resMan().resolveResHandle( res );
+			emitterTpl->effectRes = (ParticleEffectResource *)Modules::resMan().get_resource( res );
 	}
 	else result = false;
 	itr = attribs.find( "maxCount" );
 	switch( param )
 	{
 	case EmitterNodeParams::MatResI:
-		res = Modules::resMan().resolveResHandle( value );
+		res = Modules::resMan().get_resource( value );
 		if( res != 0x0 && res->get_type() == ResourceTypes::Material )
 			_materialRes = (MaterialResource *)res;
 		else
 			Modules::setError( "Invalid handle in h3dSetNodeParamI for H3DEmitter::MatResI" );
 		return;
 	case EmitterNodeParams::PartEffResI:
-		res = Modules::resMan().resolveResHandle( value );
+		res = Modules::resMan().get_resource( value );
 		if( res != 0x0 && res->get_type() == ResourceTypes::ParticleEffect )
 			_effectRes = (ParticleEffectResource *)res;
 		else

File temp/test_hordefiles/egPipeline.cpp

View file
 // *************************************************************************************************
 
 #include "engine_logger.h"
+#include "engine_resource_manager.h"
 #include "egPipeline.h"
 #include "egMaterial.h"
 #include "egModules.h"
 
 	if( strcmp( node.getAttribute( "link", "" ), "" ) != 0 )
 	{
-		uint32_t mat = Modules::resMan().addResource(
+		uint32_t mat = Modules::resMan().add_resource(
 			ResourceTypes::Material, node.getAttribute( "link" ), 0 );
-		stage.matLink = (MaterialResource *)Modules::resMan().resolveResHandle( mat );
+		stage.matLink = (MaterialResource *)Modules::resMan().get_resource( mat );
 	}
 	
 	stage.commands.reserve( node.countChildNodes() );
 			if( !node1.getAttribute( "material" ) ) return "Missing DrawQuad attribute 'material'";
 			if( !node1.getAttribute( "context" ) ) return "Missing DrawQuad attribute 'context'";
 			
-			uint32_t matRes = Modules::resMan().addResource(
+			uint32_t matRes = Modules::resMan().add_resource(
 				ResourceTypes::Material, node1.getAttribute( "material" ), 0 );
 			
 			stage.commands.push_back( PipelineCommand( PipelineCommands::DrawQuad ) );
 			vector< PipeCmdParam > &params = stage.commands.back().params;
 			params.resize( 2 );
-			params[0].setResource( Modules::resMan().resolveResHandle( matRes ) );
+			params[0].setResource( Modules::resMan().get_resource( matRes ) );
 			params[1].setString( node1.getAttribute( "context" ) );
 		}
 		else if( strcmp( node1.getName(), "DoForwardLightLoop" ) == 0 )
 			if( !node1.getAttribute( "material" ) ) return "Missing SetUniform attribute 'material'";
 			if( !node1.getAttribute( "uniform" ) ) return "Missing SetUniform attribute 'uniform'";
 			
-			uint32_t matRes = Modules::resMan().addResource(
+			uint32_t matRes = Modules::resMan().add_resource(
 				ResourceTypes::Material, node1.getAttribute( "material" ), 0 );
 			
 			stage.commands.push_back( PipelineCommand( PipelineCommands::SetUniform ) );
 			vector< PipeCmdParam > &params = stage.commands.back().params;
 			params.resize( 6 );
-			params[0].setResource( Modules::resMan().resolveResHandle( matRes ) );
+			params[0].setResource( Modules::resMan().get_resource( matRes ) );
 			params[1].setString( node1.getAttribute( "uniform" ) );
 			params[2].setFloat( (float)atof( node1.getAttribute( "a", "0" ) ) );
 			params[3].setFloat( (float)atof( node1.getAttribute( "b", "0" ) ) );

File temp/test_hordefiles/egResource.cpp

-// *************************************************************************************************
-//
-// Horde3D
-//   Next-Generation Graphics Engine
-// --------------------------------------
-// Copyright (C) 2006-2011 Nicolas Schulz
-//
-// This software is distributed under the terms of the Eclipse Public License v1.0.
-// A copy of the license may be obtained at: http://www.eclipse.org/legal/epl-v10.html
-//
-// *************************************************************************************************
-
-#include "engine_logger.h"
-#include "engine_resource.h"
-#include "egResource.h"
-#include "egModules.h"
-#include "egCom.h"
-#include <sstream>
-#include <cstring>
-using namespace std;
-
-
-
-namespace Horde3D {
-
-
-
-
-// **********************************************************************************
-// Class ResourceManager
-// **********************************************************************************
-
-ResourceManager::ResourceManager()
-{
-	for (unsigned int i=0; i<ResourceTypes::NumResourceTypes; i++)
-		_resourcesDD[i].reserve( 100 );		// preallocate some space to reduce memory juggling
-}
-
-
-ResourceManager::~ResourceManager()
-{
-	clear();
-}
-
-
-bool ResourceManager::registerResType( uint8_t type, const string &typeString, ResTypeFactoryFunc ff )
-{
-	if (type>=ResourceTypes::NumResourceTypes)
-	{
-		Modules::log().error("invalid resource type: %s [%u]\n",typeString.c_str(), type);
-		return false;
-	}
-
-	ResourceRegEntry entry;
-	entry.typeString = typeString;
-	entry.factoryFunc = ff;
-	_registryDD[type] = entry;
-	return true;
-}
-
-
-Resource *ResourceManager::getResource( uint8_t type, const string &name )
-{
-	ASSERT(type<ResourceTypes::NumResourceTypes);
-
-	for( size_t i = 0, s = _resourcesDD[type].size(); i < s; ++i )
-	{
-		if( _resourcesDD[type][i] != NULL && _resourcesDD[type][i]->_name == name )
-		{
-			return _resourcesDD[type][i];
-		}
-	}
-	
-	return NULL;
-}
-
-
-ResHandle ResourceManager::addResource( Resource &resource )
-{
-	uint8_t type = resource.get_type();
-	// Try first to insert resource in a free slot
-	for( uint32_t i = 0; i < _resourcesDD[type].size(); ++i )
-	{
-		if( _resourcesDD[type][i] == NULL )
-		{
-			resource._handle = create_handle(type,i);	// keep the type in the handle and update the id part
-			_resourcesDD[type][i] = &resource;
-			return i + resource._handle;
-		}
-	}
-	
-	// If there is no free slot, add resource to end
-	resource._handle = create_handle(type,_resourcesDD[type].size());
-	_resourcesDD[type].push_back( &resource );
-	return resource._handle;
-}
-
-
-ResHandle ResourceManager::addResource( uint8_t type, const string &name, int flags )
-{
-	if( name == "" )
-	{	
-		Modules::log().debug( "Invalid name for added resource of type %i (name='%s')", type, name.c_str() );
-		return 0;
-	}
-	ASSERT(type<ResourceTypes::NumResourceTypes);
-
-	// Check if resource is already in list and return index
-	for( uint32_t i = 0; i < _resourcesDD[type].size(); ++i )
-	{
-		if( _resourcesDD[type][i] != NULL && _resourcesDD[type][i]->_name == name )
-			return  _resourcesDD[type][i]->_handle;
-	}
-	
-	// Create resource
-	Resource *resource = NULL;
-	ResTypeFactoryFunc factoryFunc= _registryDD[type].factoryFunc;
-	if (factoryFunc!=NULL)
-		resource = factoryFunc( name, flags );
-
-	if( resource == NULL ) return 0;
-	resource->_handle = create_handle(type, RESHANDLE_ID_MASK);	// store only the type; the id part will be updated in addResource
-
-	Modules::log().debug( "Adding %s resource '%s'", _registryDD[type].typeString.c_str(), name.c_str() );
-		
-	return addResource( *resource );
-}
-
-
-void ResourceManager::clear()
-{
-	// Delete resources
-	for( uint8_t t = 0; t < ResourceTypes::NumResourceTypes; t++ )
-	{
-		for( uint32_t i = 0; i < _resourcesDD[t].size(); ++i )
-		{
-			if( _resourcesDD[t][i] != NULL )
-			{
-				_resourcesDD[t][i]->release();
-				delete _resourcesDD[t][i]; 
-				_resourcesDD[t][i] = NULL;
-			}
-		}
-		_resourcesDD[t].clear();
-	}
-}
-
-
-ResHandle ResourceManager::queryUnloadedResource( int index )
-{
-	int j = 0;
-
-	for( uint8_t t = 0; t < ResourceTypes::NumResourceTypes; t++ )
-	{
-		for( uint32_t i = 0; i < _resourcesDD[t].size(); i++ )
-		{
-			if( _resourcesDD[t][i] && !_resourcesDD[t][i]->_loaded )
-			{	
-				if( j == index ) return _resourcesDD[t][i]->_handle;
-				else ++j;
-			}
-		}
-	}
-	return 0; // return invalid handle: equivalent to create_handle(ResourceTypes::Undefined,0);
-}
-
-
-struct Entry
-{
-	uint8_t type;
-	uint32_t idx;
-	Entry(uint8_t _type, uint32_t _idx)
-	: type(_type), idx(_idx)
-	{}
-};
-void ResourceManager::releaseUnusedResources()
-{
-	std::vector< Entry > killList;
-	
-	// Find unused resources and release dependencies
-	for( uint8_t t = 0; t < ResourceTypes::NumResourceTypes; t++ )
-	{
-		for( uint32_t i = 0; i < _resourcesDD[t].size(); ++i )
-		{
-			if( _resourcesDD[t][i] != NULL && _resourcesDD[t][i]->_refCount == 0 )
-			{
-				killList.push_back( Entry(t,i) );
-				_resourcesDD[t][i]->release();
-			}
-		}
-	}
-
-	// Delete unused resources
-	for( uint32_t i = 0; i < killList.size(); ++i )
-	{
-		Modules::log().info( "Removed resource '%s'", _resourcesDD[killList[i].type][killList[i].idx]->_name.c_str() );
-		delete _resourcesDD[killList[i].type][killList[i].idx]; 
-		_resourcesDD[killList[i].type][killList[i].idx] = NULL;
-	}
-
-	// Releasing a resource can remove dependencies from other resources which can also be released
-	if( !killList.empty() ) releaseUnusedResources();
-}
-
-
-Resource* ResourceManager::resolveResHandle( ResHandle handle )
-{
-	uint8_t type = get_handle_type(handle);  
-	uint32_t id  = get_handle_id(handle);
-	return (handle!=0 && id<_resourcesDD[type].size()) ? _resourcesDD[type][id] : NULL; 
-}
-
-
-
-
-}  // namespace

File temp/test_hordefiles/egResource.h

-// *************************************************************************************************
-//
-// Horde3D
-//   Next-Generation Graphics Engine
-// --------------------------------------
-// Copyright (C) 2006-2011 Nicolas Schulz
-//
-// This software is distributed under the terms of the Eclipse Public License v1.0.
-// A copy of the license may be obtained at: http://www.eclipse.org/legal/epl-v10.html
-//
-// *************************************************************************************************
-
-#ifndef _egResource_H_
-#define _egResource_H_
-
-#include "engine_basic_types.h"
-#include "egPrerequisites.h"
-#include "utMath.h"
-#include <string>
-#include <vector>
-#include <map>
-
-
-namespace Horde3D {
-
-class Resource;
-
-
-// =================================================================================================
-// Resource Manager
-// =================================================================================================
-
-typedef Resource* (*ResTypeFactoryFunc)( const std::string &name, int flags );
-
-struct ResourceRegEntry
-{
-    std::string         typeString;
-    ResTypeFactoryFunc  factoryFunc;  // Factory to create resource object
-
-    ResourceRegEntry()
-    : typeString(""), factoryFunc(NULL)
-    {}
-};
-
-// =================================================================================================
-
-
-class ResourceManager
-{
-public:
-    ResourceManager();
-    ~ResourceManager();
-
-    bool registerResType( uint8_t type, const std::string &typeString, ResTypeFactoryFunc ff );
-    
-    Resource *getResource( uint8_t type, const std::string &name );
-    ResHandle addResource( uint8_t type, const std::string &name, int flags );
-    ResHandle addResource( Resource &res );
-    void clear();
-    ResHandle queryUnloadedResource( int index );
-    void releaseUnusedResources();
-
-    Resource *resolveResHandle( ResHandle handle );
-
-    std::vector<Resource*> &get_shaders() { return _resourcesDD[ResourceTypes::Shader]; }
-
-protected:
-    std::vector<Resource*>  _resourcesDD[ResourceTypes::NumResourceTypes];
-    ResourceRegEntry        _registryDD[ResourceTypes::NumResourceTypes];
-
-};
-
-}
-#endif // _egResource_H_

File temp/test_hordefiles/egSceneGraphRes.cpp

View file
 // *************************************************************************************************
 
 #include "engine_logger.h"
+#include "engine_resource_manager.h"
 #include "egSceneGraphRes.h"
 #include "egModules.h"
 #include "egCom.h"
 		{
 			if( strcmp( xmlNode.getAttribute( "sceneGraph", "" ), "" ) != 0 )
 			{
-				Resource *res = Modules::resMan().resolveResHandle( Modules::resMan().addResource(
+				Resource *res = Modules::resMan().get_resource( Modules::resMan().add_resource(
 					ResourceTypes::SceneGraph, xmlNode.getAttribute( "sceneGraph" ), 0 ) );
 				if (res != 0x0 ) nodeTpl = new ReferenceNodeTpl( "", (SceneGraphResource *)res );
 			}

File temp/test_hordefiles/egShader.cpp

View file
 // *************************************************************************************************
 
 #include "engine_logger.h"
+#include "engine_resource_manager.h"
 #include "egShader.h"
 #include "egModules.h"
 #include "egCom.h"
 				{
 					std::string resName( nameBegin, nameEnd );
 					
-					ResHandle res =  Modules::resMan().addResource(
+					ResHandle res =  Modules::resMan().add_resource(
 						ResourceTypes::Code, resName, 0 );
-					CodeResource *codeRes = (CodeResource *)Modules::resMan().resolveResHandle( res );
+					CodeResource *codeRes = (CodeResource *)Modules::resMan().get_resource( res );
 					_includes.push_back( std::pair< PCodeResource, size_t >( codeRes, pCode - code ) );
 				}
 				else
 			if( tok.checkToken( "sampler2D" ) )
 			{	
 				sampler.type = TextureTypes::Tex2D;
-				sampler.defTex = (TextureResource *)Modules::resMan().getResource( ResourceTypes::Texture, "$Tex2D" );
+				sampler.defTex = (TextureResource *)Modules::resMan().get_resource( ResourceTypes::Texture, "$Tex2D" );
 			}
 			else if( tok.checkToken( "samplerCube" ) )
 			{
 				sampler.type = TextureTypes::TexCube;
-				sampler.defTex = (TextureResource *)Modules::resMan().getResource( ResourceTypes::Texture, "$TexCube" );
+				sampler.defTex = (TextureResource *)Modules::resMan().get_resource( ResourceTypes::Texture, "$TexCube" );
 			}
 			else if( tok.checkToken( "sampler3D" ) )
 			{
 				sampler.type = TextureTypes::Tex3D;
-				sampler.defTex = (TextureResource *)Modules::resMan().getResource( ResourceTypes::Texture, "$Tex3D" );
+				sampler.defTex = (TextureResource *)Modules::resMan().get_resource( ResourceTypes::Texture, "$Tex3D" );
 			}
 			sampler.id = tok.getToken( identifier );
 			if( sampler.id == "" ) return raiseError( "FX: Invalid identifier", tok.getLine() );
 					else if( tok.checkToken( "Texture" ) )
 					{
 						if( !tok.checkToken( "=" ) ) return raiseError( "FX: expected '='", tok.getLine() );
-						ResHandle texMap =  Modules::resMan().addResource(
+						ResHandle texMap =  Modules::resMan().add_resource(
 							ResourceTypes::Texture, tok.getToken( 0x0 ), 0 );
-						sampler.defTex = (TextureResource *)Modules::resMan().resolveResHandle( texMap );
+						sampler.defTex = (TextureResource *)Modules::resMan().get_resource( texMap );
 					}
 					else if( tok.checkToken( "TexUnit" ) )
 					{

File temp/test_hordefiles/egShader.h

View file
 #define _egShader_H_
 
 #include "egPrerequisites.h"
-#include "egResource.h"
+#include "engine_resource.h"
 #include "egTexture.h"
 #include <set>
 

File temp/test_hordefiles/engine_resource_manager.cpp

View file
+
+#include "engine_logger.h"
+#include "engine_resource.h"
+#include "engine_resource_manager.h"
+#include "egModules.h"
+#include "egCom.h"
+#include <sstream>
+#include <cstring>
+using namespace std;
+
+
+
+namespace Horde3D {
+
+
+
+
+// **********************************************************************************
+// Class ResourceManager
+// **********************************************************************************
+
+ResourceManager::ResourceManager()
+{
+	for (unsigned int i=0; i<ResourceTypes::NumResourceTypes; i++)
+		_resourcesDD[i].reserve( 100 );		// preallocate some space to reduce memory juggling
+}
+
+
+ResourceManager::~ResourceManager()
+{
+	clear();
+}
+
+
+bool ResourceManager::register_resource_type( uint8_t type, const string &typeString, ResTypeFactoryFunc ff )
+{
+	if (type>=ResourceTypes::NumResourceTypes)
+	{
+		Modules::log().error("invalid resource type: %s [%u]\n",typeString.c_str(), type);
+		return false;
+	}
+
+	ResourceRegEntry entry;
+	entry.typeString = typeString;
+	entry.factoryFunc = ff;
+	_registryDD[type] = entry;
+	return true;
+}
+
+
+Resource *ResourceManager::get_resource( uint8_t type, const string &name )
+{
+	ASSERT(type<ResourceTypes::NumResourceTypes);
+
+	for( size_t i = 0, s = _resourcesDD[type].size(); i < s; ++i )
+	{
+		if( _resourcesDD[type][i] != NULL && _resourcesDD[type][i]->_name == name )
+		{
+			return _resourcesDD[type][i];
+		}
+	}
+	
+	return NULL;
+}
+
+
+ResHandle ResourceManager::add_resource( Resource &resource )
+{
+	uint8_t type = resource.get_type();
+	// Try first to insert resource in a free slot
+	for( uint32_t i = 0; i < _resourcesDD[type].size(); ++i )
+	{
+		if( _resourcesDD[type][i] == NULL )
+		{
+			resource._handle = create_handle(type,i);	// keep the type in the handle and update the id part
+			_resourcesDD[type][i] = &resource;
+			return i + resource._handle;
+		}
+	}
+	
+	// If there is no free slot, add resource to end
+	resource._handle = create_handle(type,_resourcesDD[type].size());
+	_resourcesDD[type].push_back( &resource );
+	return resource._handle;
+}
+
+
+ResHandle ResourceManager::add_resource( uint8_t type, const string &name, int flags )
+{
+	if( name == "" )
+	{	
+		Modules::log().debug( "Invalid name for added resource of type %i (name='%s')", type, name.c_str() );
+		return 0;
+	}
+	ASSERT(type<ResourceTypes::NumResourceTypes);
+
+	// Check if resource is already in list and return index
+	for( uint32_t i = 0; i < _resourcesDD[type].size(); ++i )
+	{
+		if( _resourcesDD[type][i] != NULL && _resourcesDD[type][i]->_name == name )
+			return  _resourcesDD[type][i]->_handle;
+	}
+	
+	// Create resource
+	Resource *resource = NULL;
+	ResTypeFactoryFunc factoryFunc= _registryDD[type].factoryFunc;
+	if (factoryFunc!=NULL)
+		resource = factoryFunc( name, flags );
+
+	if( resource == NULL ) return 0;
+	resource->_handle = create_handle(type, RESHANDLE_ID_MASK);	// store only the type; the id part will be updated in add_resource
+
+	Modules::log().debug( "Adding %s resource '%s'", _registryDD[type].typeString.c_str(), name.c_str() );
+		
+	return add_resource( *resource );
+}
+
+
+void ResourceManager::clear()
+{
+	// Delete resources
+	for( uint8_t t = 0; t < ResourceTypes::NumResourceTypes; t++ )
+	{
+		for( uint32_t i = 0; i < _resourcesDD[t].size(); ++i )
+		{
+			if( _resourcesDD[t][i] != NULL )
+			{
+				_resourcesDD[t][i]->release();
+				delete _resourcesDD[t][i]; 
+				_resourcesDD[t][i] = NULL;
+			}
+		}
+		_resourcesDD[t].clear();
+	}
+}
+
+
+ResHandle ResourceManager::query_unloaded_resource( int index )
+{
+	int j = 0;
+
+	for( uint8_t t = 0; t < ResourceTypes::NumResourceTypes; t++ )
+	{
+		for( uint32_t i = 0; i < _resourcesDD[t].size(); i++ )
+		{
+			if( _resourcesDD[t][i] && !_resourcesDD[t][i]->_loaded )
+			{	
+				if( j == index ) return _resourcesDD[t][i]->_handle;
+				else ++j;
+			}
+		}
+	}
+	return 0; // return invalid handle: equivalent to create_handle(ResourceTypes::Undefined,0);
+}
+
+
+struct Entry
+{
+	uint8_t type;
+	uint32_t idx;
+	Entry(uint8_t _type, uint32_t _idx)
+	: type(_type), idx(_idx)
+	{}
+};
+void ResourceManager::release_unused_resources()
+{
+	std::vector< Entry > killList;
+	
+	// Find unused resources and release dependencies
+	for( uint8_t t = 0; t < ResourceTypes::NumResourceTypes; t++ )
+	{
+		for( uint32_t i = 0; i < _resourcesDD[t].size(); ++i )
+		{
+			if( _resourcesDD[t][i] != NULL && _resourcesDD[t][i]->_refCount == 0 )
+			{
+				killList.push_back( Entry(t,i) );
+				_resourcesDD[t][i]->release();
+			}
+		}
+	}
+
+	// Delete unused resources
+	for( uint32_t i = 0; i < killList.size(); ++i )
+	{
+		Modules::log().info( "Removed resource '%s'", _resourcesDD[killList[i].type][killList[i].idx]->_name.c_str() );
+		delete _resourcesDD[killList[i].type][killList[i].idx]; 
+		_resourcesDD[killList[i].type][killList[i].idx] = NULL;
+	}
+
+	// Releasing a resource can remove dependencies from other resources which can also be released
+	if( !killList.empty() ) release_unused_resources();
+}
+
+
+Resource* ResourceManager::get_resource( ResHandle handle )
+{
+	uint8_t type = get_handle_type(handle);  
+	uint32_t id  = get_handle_id(handle);
+	return (handle!=0 && id<_resourcesDD[type].size()) ? _resourcesDD[type][id] : NULL; 
+}
+
+
+
+
+}  // namespace

File temp/test_hordefiles/engine_resource_manager.h

View file
+#pragma once 
+
+#include "engine_basic_types.h"
+#include "egPrerequisites.h"
+#include "utMath.h"
+#include <string>
+#include <vector>
+#include <map>
+
+
+namespace Horde3D {
+
+class Resource;
+
+
+// =================================================================================================
+// Resource Manager
+// =================================================================================================
+
+typedef Resource* (*ResTypeFactoryFunc)( const std::string &name, int flags );
+
+struct ResourceRegEntry
+{
+    std::string         typeString;
+    ResTypeFactoryFunc  factoryFunc;  // Factory to create resource object
+
+    ResourceRegEntry()
+    : typeString(""), factoryFunc(NULL)
+    {}
+};
+
+// =================================================================================================
+
+
+class ResourceManager
+{
+public:
+    ResourceManager();
+    ~ResourceManager();
+
+    bool register_resource_type( uint8_t type, const std::string &typeString, ResTypeFactoryFunc ff );
+    
+    Resource *get_resource( ResHandle handle );
+    Resource *get_resource( uint8_t type, const std::string &name );
+
+    ResHandle add_resource( uint8_t type, const std::string &name, int flags );
+    ResHandle add_resource( Resource &res );
+
+    void clear();
+
+    ResHandle query_unloaded_resource( int startindex );
+    void release_unused_resources();
+
+    // specific resource retrieving
+    std::vector<Resource*> &get_shaders() { return _resourcesDD[ResourceTypes::Shader]; }
+
+protected:
+    std::vector<Resource*>  _resourcesDD[ResourceTypes::NumResourceTypes];
+    ResourceRegEntry        _registryDD[ResourceTypes::NumResourceTypes];
+
+};
+
+}
+
+