Commits

dbacchet committed 38b0749

refactored resource class

Comments (0)

Files changed (22)

temp/test_hordefiles/egAnimation.cpp

 AnimationResource::AnimationResource( const string &name, int flags ) :
 	Resource( ResourceTypes::Animation, name, flags )
 {
-	initDefault();	
+	init_default();	
 }
 
 
 }
 
 
-void AnimationResource::initDefault()
+void AnimationResource::init_default()
 {
 	_numFrames = 0;
 }
 {
 	// Reset
 	release();
-	initDefault();
+	init_default();
 
 	Modules::log().error( "Animation resource '%s': %s", _name.c_str(), msg.c_str() );
 	

temp/test_hordefiles/egAnimation.h

 #define _egAnimation_H_
 
 #include "egPrerequisites.h"
-#include "egResource.h"
+#include "engine_resource.h"
+// #include "egResource.h"
 #include "utMath.h"
 
 
 	~AnimationResource();
 	Resource *clone();
 	
-	void initDefault();
+	void init_default();
 	void release();
 	bool load( const char *data, int size );
 

temp/test_hordefiles/egGeometry.cpp

 GeometryResource::GeometryResource( const string &name, int flags ) :
 	Resource( ResourceTypes::Geometry, name, flags )
 {
-	initDefault();
+	init_default();
 }
 
 
 }
 
 
-void GeometryResource::initDefault()
+void GeometryResource::init_default()
 {
 	_indexCount = 0;
 	_vertCount = 0;
 {
 	// Reset
 	release();
-	initDefault();
+	init_default();
 
 	Modules::log().error( "Geometry resource '%s': %s", _name.c_str(), msg.c_str() );
 	

temp/test_hordefiles/egGeometry.h

 #define _egGeometry_H_
 
 #include "egPrerequisites.h"
-#include "egResource.h"
+#include "engine_resource.h"
+// #include "egResource.h"
 #include "engine_math_primitives.h"
 #include "utMath.h"
 
 	~GeometryResource();
 	Resource *clone();
 	
-	void initDefault();
+	void init_default();
 	void release();
 	bool load( const char *data, int size );
 

temp/test_hordefiles/egMaterial.cpp

 MaterialResource::MaterialResource( const string &name, int flags ) :
 	Resource( ResourceTypes::Material, name, flags )
 {
-	initDefault();	
+	init_default();	
 }
 
 
 }
 
 
-void MaterialResource::initDefault()
+void MaterialResource::init_default()
 {
 	_shaderRes = 0x0;
 	_combMask = 0;
 {
 	// Reset
 	release();
-	initDefault();
+	init_default();
 
 	if( line < 0 )
 		Modules::log().error( "Material resource '%s': %s", _name.c_str(), msg.c_str() );

temp/test_hordefiles/egMaterial.h

 	~MaterialResource();
 	Resource *clone();
 	
-	void initDefault();
+	void init_default();
 	void release();
 	bool load( const char *data, int size );
 	bool setUniform( const std::string &name, float a, float b, float c, float d );

temp/test_hordefiles/egParticle.cpp

 ParticleEffectResource::ParticleEffectResource( const string &name, int flags ) :
 	Resource( ResourceTypes::ParticleEffect, name, flags )
 {
-	initDefault();	
+	init_default();	
 }
 
 
 }
 
 
-void ParticleEffectResource::initDefault()
+void ParticleEffectResource::init_default()
 {
 	_lifeMin = 0; _lifeMax = 0;
 	_moveVel.reset();
 {
 	// Reset
 	release();
-	initDefault();
+	init_default();
 
 	if( line < 0 )
 		Modules::log().error( "ParticleEffect resource '%s': %s", _name.c_str(), msg.c_str() );

temp/test_hordefiles/egParticle.h

 	ParticleEffectResource( const std::string &name, int flags );
 	~ParticleEffectResource();
 	
-	void initDefault();
+	void init_default();
 	void release();
 	bool load( const char *data, int size );
 

temp/test_hordefiles/egPipeline.cpp

 PipelineResource::PipelineResource( const string &name, int flags ) :
 	Resource( ResourceTypes::Pipeline, name, flags )
 {
-	initDefault();	
+	init_default();	
 }
 
 
 }
 
 
-void PipelineResource::initDefault()
+void PipelineResource::init_default()
 {
 	_baseWidth = 320; _baseHeight = 240;
 }
 {
 	// Reset
 	release();
-	initDefault();
+	init_default();
 
 	if( line < 0 )
 		Modules::log().error( "Pipeline resource '%s': %s", _name.c_str(), msg.c_str() );

temp/test_hordefiles/egPipeline.h

 	PipelineResource( const std::string &name, int flags );
 	~PipelineResource();
 	
-	void initDefault();
+	void init_default();
 	void release();
 	bool load( const char *data, int size );
 	void resize( uint32_t width, uint32_t height );

temp/test_hordefiles/egResource.cpp

 // *************************************************************************************************
 
 #include "engine_logger.h"
+#include "engine_resource.h"
 #include "egResource.h"
 #include "egModules.h"
 #include "egCom.h"
 #include <sstream>
 #include <cstring>
+using namespace std;
 
 
 #include "egGeometry.h"
 
 	const unsigned int GeometryHandleOffset = 1+1E6;	// handle offset for identifying geometries
 
-using namespace std;
 
-// **********************************************************************************
-// Class Resource
-// **********************************************************************************
-
-Resource::Resource( uint8_t type, const string &name, int flags )
-{
-	_name = name;
-	_handle = 0;
-	_loaded = false;
-	_refCount = 0;
-	_flags = flags;
-}
-
-
-Resource::~Resource()
-{
-	// Remove all references
-	// Nothing to do here
-}
-
-
-
-void Resource::initDefault()
-{
-}
-
-
-void Resource::release()
-{
-}
-
-
-bool Resource::load( const char *data, int size )
-{	
-	// Resources can only be loaded once
-	if( _loaded ) return false;
-	
-	// A NULL pointer can be used if the file could not be loaded
-	if( data == 0x0 || size <= 0 )
-	{	
-		Modules::log().warning( "Resource '%s' of type %i: No data loaded (file not found?)", _name.c_str(), get_handle_type(_handle) );
-		return false;
-	}
-
-	_loaded = true;
-	
-	return true;
-}
-
-
-void Resource::unload()
-{
-	release();
-	initDefault();
-	_loaded = false;
-}
-
-
-// int Resource::getElemCount( int elem )
-// {
-// 	Modules::setError( "Invalid elem in h3dGetResElemCount" );
-// 	return 0;
-// }
-
-int Resource::getElemParamI( int elem, int elemIdx, int param )
-{
-	Modules::setError( "Invalid elem or param in h3dGetResParamI" );
-	return Math::MinInt32;
-}
-
-void Resource::setElemParamI( int elem, int elemIdx, int param, int value )
-{
-	Modules::setError( "Invalid elem or param in h3dSetResParamI" );
-}
-
-float Resource::getElemParamF( int elem, int elemIdx, int param, int compIdx )
-{
-	Modules::setError( "Invalid elem, param or component in h3dGetResParamF" );
-	return Math::NaN;
-}
-
-void Resource::setElemParamF( int elem, int elemIdx, int param, int compIdx, float value )
-{
-	Modules::setError( "Invalid elem, param or component in h3dSetResParamF" );
-}
 
 
 // **********************************************************************************

temp/test_hordefiles/egResource.h

 
 namespace Horde3D {
 
-
+class Resource;
 // =================================================================================================
 
-class Resource
-{
-public:
-    Resource( uint8_t type, const std::string &name, int flags );
-    virtual ~Resource();
-    
-    virtual void initDefault();
-    virtual void release();
-    virtual bool load( const char *data, int size );
-    void unload();
-    
-    // virtual int getElemCount( int elem );
-    virtual int getElemParamI( int elem, int elemIdx, int param );
-    virtual void setElemParamI( int elem, int elemIdx, int param, int value );
-    virtual float getElemParamF( int elem, int elemIdx, int param, int compIdx );
-    virtual void setElemParamF( int elem, int elemIdx, int param, int compIdx, float value );
-
-    uint8_t get_type()  const                       { return get_handle_type(_handle); }
-    int32_t get_flags() const                       { return _flags; }
-    const std::string &get_name() const             { return _name; }
-    ResHandle get_handle() const                    { return _handle; }
-    bool is_loaded() const                          { return _loaded; }
-
-    // reference count
-    void addRef()                                   { ++_refCount; }
-    void subRef()                                   { --_refCount; }
-
-protected:
-    std::string          _name;
-    ResHandle            _handle;
-    int32_t              _flags;
-    
-    uint32_t             _refCount;  // Number of other objects referencing this resource
-
-    bool                 _loaded;
-
-    friend class ResourceManager;
-};
-
-// =================================================================================================
-
-template< class T > class SmartResPtr
-{
-public:
-    SmartResPtr( T *ptr = 0x0 ) : _ptr( ptr ) { addRef(); }
-    SmartResPtr( const SmartResPtr &smp ) : _ptr( smp._ptr ) { addRef(); }
-    ~SmartResPtr() { subRef(); }
-
-    T &operator*() const { return *_ptr; }
-    T *operator->() const { return _ptr; }
-    operator T*() const { return _ptr; }
-    operator const T*() const { return _ptr; }
-    operator bool() const { return _ptr != 0x0; }
-    T *getPtr() { return _ptr; }
-    
-    SmartResPtr &operator=( const SmartResPtr &smp ) { return *this = smp._ptr; }
-    SmartResPtr &operator=( T *ptr )
-    {
-        subRef(); _ptr = ptr; addRef();
-        return *this;
-    }
-
-private:
-    void addRef() { if( _ptr != 0x0 ) _ptr->addRef(); }
-    void subRef() { if( _ptr != 0x0 ) _ptr->subRef(); }
-
-private:
-    T  *_ptr;
-};
-
-typedef SmartResPtr< Resource > PResource;
 
 
 // =================================================================================================

temp/test_hordefiles/egSceneGraphRes.cpp

 SceneGraphResource::SceneGraphResource( const string &name, int flags ) :
 	Resource( ResourceTypes::SceneGraph, name, flags )
 {
-	initDefault();
+	init_default();
 }
 
 
 }
 
 
-void SceneGraphResource::initDefault()
+void SceneGraphResource::init_default()
 {
 	// Create default root node
 	_rootNode = new GroupNodeTpl( _name );

temp/test_hordefiles/egSceneGraphRes.h

 	SceneGraphResource( const std::string &name, int flags );
 	~SceneGraphResource();
 	
-	void initDefault();
+	void init_default();
 	void release();
 	bool load( const char *data, int size );
 

temp/test_hordefiles/egShader.cpp

 CodeResource::CodeResource( const string &name, int flags ) :
 	Resource( ResourceTypes::Code, name, flags )
 {
-	initDefault();
+	init_default();
 }
 
 
 }
 
 
-void CodeResource::initDefault()
+void CodeResource::init_default()
 {
 	_flagMask = 0;
 	_code.clear();
 {
 	// Reset
 	release();
-	initDefault();
+	init_default();
 	
 	Modules::log().error( "Code resource '%s': %s", _name.c_str(), msg.c_str() );
 
 ShaderResource::ShaderResource( const string &name, int flags ) :
 	Resource( ResourceTypes::Shader, name, flags )
 {
-	initDefault();
+	init_default();
 }
 
 
 }
 
 
-void ShaderResource::initDefault()
+void ShaderResource::init_default()
 {
 }
 
 {
 	// Reset
 	release();
-	initDefault();
+	init_default();
 
 	if( line < 0 )
 		Modules::log().error( "Shader resource '%s': %s", _name.c_str(), msg.c_str() );

temp/test_hordefiles/egShader.h

 	~CodeResource();
 	Resource *clone();
 	
-	void initDefault();
+	void init_default();
 	void release();
 	bool load( const char *data, int size );
 
 	ShaderResource( const std::string &name, int flags );
 	~ShaderResource();
 	
-	void initDefault();
+	void init_default();
 	void release();
 	bool load( const char *data, int size );
 	void preLoadCombination( uint32_t combMask );

temp/test_hordefiles/egTexture.cpp

 	Resource( ResourceTypes::Texture, name, flags )
 {
 	_texType = TextureTypes::Tex2D;
-	initDefault();
+	init_default();
 }
 
 
 		gRDI->uploadTextureData( _texObject, 0, 0, pixels );
 		
 		delete[] pixels;
-		if( _texObject == 0 ) initDefault();
+		if( _texObject == 0 ) init_default();
 	}
 }
 
 }
 
 
-void TextureResource::initDefault()
+void TextureResource::init_default()
 {
 	_rbObj = 0;
 	_texFormat = TextureFormats::BGRA8;
 {
 	// Reset
 	release();
-	initDefault();
+	init_default();
 
 	Modules::log().error( "Texture resource '%s': %s", _name.c_str(), msg.c_str() );
 	

temp/test_hordefiles/egTexture.h

 #define _egTexture_H_
 
 #include "egPrerequisites.h"
-#include "egResource.h"
+// #include "egResource.h"
+#include "engine_resource.h"
 
 namespace Horde3D {
 
                     TextureFormats::List fmt, int flags );
 	~TextureResource();
 	
-	void initDefault();
+	void init_default();
 	void release();
 	bool load( const char *data, int size );
 

temp/test_hordefiles/engine_geometry.h

 	~GeometryResource();
 	Resource *clone();
 	
-	void initDefault();
+	void init_default();
 	void release();
 	bool load( const char *data, int size );
 

temp/test_hordefiles/engine_resource.cpp

+#include "engine_logger.h"
+#include "engine_resource.h"
+#include "egModules.h"
+#include "egCom.h"
+#include <sstream>
+#include <cstring>
+using namespace std;
+
+
+
+
+namespace Horde3D {
+
+
+
+
+// **********************************************************************************
+// Class Resource
+// **********************************************************************************
+
+Resource::Resource( uint8_t type, const string &name, int flags )
+{
+	_name = name;
+	_handle = 0;
+	_loaded = false;
+	_refCount = 0;
+	_flags = flags;
+}
+
+
+Resource::~Resource()
+{
+	// Remove all references
+	// Nothing to do here
+}
+
+
+
+void Resource::init_default()
+{
+}
+
+
+void Resource::release()
+{
+}
+
+
+bool Resource::load( const char *data, int size )
+{	
+	// Resources can only be loaded once
+	if( _loaded ) return false;
+	
+	// A NULL pointer can be used if the file could not be loaded
+	if( data == 0x0 || size <= 0 )
+	{	
+		Modules::log().warning( "Resource '%s' of type %i: No data loaded (file not found?)", _name.c_str(), get_handle_type(_handle) );
+		return false;
+	}
+
+	_loaded = true;
+	
+	return true;
+}
+
+
+void Resource::unload()
+{
+	release();
+	init_default();
+	_loaded = false;
+}
+
+
+// int Resource::getElemCount( int elem )
+// {
+// 	Modules::setError( "Invalid elem in h3dGetResElemCount" );
+// 	return 0;
+// }
+
+int Resource::getElemParamI( int elem, int elemIdx, int param )
+{
+	Modules::setError( "Invalid elem or param in h3dGetResParamI" );
+	return Math::MinInt32;
+}
+
+void Resource::setElemParamI( int elem, int elemIdx, int param, int value )
+{
+	Modules::setError( "Invalid elem or param in h3dSetResParamI" );
+}
+
+float Resource::getElemParamF( int elem, int elemIdx, int param, int compIdx )
+{
+	Modules::setError( "Invalid elem, param or component in h3dGetResParamF" );
+	return Math::NaN;
+}
+
+void Resource::setElemParamF( int elem, int elemIdx, int param, int compIdx, float value )
+{
+	Modules::setError( "Invalid elem, param or component in h3dSetResParamF" );
+}
+
+
+
+}  // namespace

temp/test_hordefiles/engine_resource.h

+#pragma once
+
+#include "egPrerequisites.h"
+#include "utMath.h"
+#include <string>
+#include <vector>
+#include <map>
+
+
+namespace Horde3D {
+
+
+// =================================================================================================
+
+class Resource
+{
+public:
+    Resource( uint8_t type, const std::string &name, int flags );
+    virtual ~Resource();
+    
+    virtual void init_default();
+    virtual void release();
+    virtual bool load( const char *data, int size );
+    void unload();
+    
+    // virtual int getElemCount( int elem );
+    virtual int getElemParamI( int elem, int elemIdx, int param );
+    virtual void setElemParamI( int elem, int elemIdx, int param, int value );
+    virtual float getElemParamF( int elem, int elemIdx, int param, int compIdx );
+    virtual void setElemParamF( int elem, int elemIdx, int param, int compIdx, float value );
+
+    uint8_t get_type()  const                       { return get_handle_type(_handle); }
+    int32_t get_flags() const                       { return _flags; }
+    const std::string &get_name() const             { return _name; }
+    ResHandle get_handle() const                    { return _handle; }
+    bool is_loaded() const                          { return _loaded; }
+
+    // reference count
+    void addRef()                                   { ++_refCount; }
+    void subRef()                                   { --_refCount; }
+
+protected:
+    std::string          _name;
+    ResHandle            _handle;
+    int32_t              _flags;
+    
+    uint32_t             _refCount;  // Number of other objects referencing this resource
+
+    bool                 _loaded;
+
+    friend class ResourceManager;
+};
+
+// =================================================================================================
+
+template< class T > class SmartResPtr
+{
+public:
+    SmartResPtr( T *ptr = 0x0 ) : _ptr( ptr ) { addRef(); }
+    SmartResPtr( const SmartResPtr &smp ) : _ptr( smp._ptr ) { addRef(); }
+    ~SmartResPtr() { subRef(); }
+
+    T &operator*() const { return *_ptr; }
+    T *operator->() const { return _ptr; }
+    operator T*() const { return _ptr; }
+    operator const T*() const { return _ptr; }
+    operator bool() const { return _ptr != 0x0; }
+    T *getPtr() { return _ptr; }
+    
+    SmartResPtr &operator=( const SmartResPtr &smp ) { return *this = smp._ptr; }
+    SmartResPtr &operator=( T *ptr )
+    {
+        subRef(); _ptr = ptr; addRef();
+        return *this;
+    }
+
+private:
+    void addRef() { if( _ptr != 0x0 ) _ptr->addRef(); }
+    void subRef() { if( _ptr != 0x0 ) _ptr->subRef(); }
+
+private:
+    T  *_ptr;
+};
+
+typedef SmartResPtr< Resource > PResource;
+
+
+}   // namespace
+

temp/test_hordefiles/engine_timer.h

     // Read high performance counter
     LARGE_INTEGER curTick;
     QueryPerformanceCounter( &curTick );
-    return (double)curTick.QuadPart / (double)_timerFreq.QuadPart * 1000000.0;
+    return (double)curTick.QuadPart / (double)frequency.QuadPart * 1000000.0;
 #else
     timeval tv;
     gettimeofday( &tv, 0x0 );