From: <cn...@us...> - 2009-11-05 02:59:37
|
Revision: 599 http://hgengine.svn.sourceforge.net/hgengine/?rev=599&view=rev Author: cnlohr Date: 2009-11-05 02:59:30 +0000 (Thu, 05 Nov 2009) Log Message: ----------- Switch over assets to new system - still not 100% sure this is best, but it should be easier to change around the part of interest at a later time. Modified Paths: -------------- Mercury2/modules/Terrain.cpp Mercury2/modules/Terrain.h Mercury2/modules/TextNode.cpp Mercury2/modules/TextPlate.cpp Mercury2/src/FullscreenQuad.cpp Mercury2/src/FullscreenQuad.h Mercury2/src/HGMDLMesh.h Mercury2/src/HGMDLModel.cpp Mercury2/src/HGMDLModel.h Mercury2/src/Light.cpp Mercury2/src/MercuryAsset.cpp Mercury2/src/MercuryAsset.h Mercury2/src/MercuryNode.cpp Mercury2/src/MercuryVBO.cpp Mercury2/src/MercuryVBO.h Mercury2/src/Quad.cpp Mercury2/src/Quad.h Mercury2/src/Shader.cpp Mercury2/src/Shader.h Mercury2/src/StateChanger.cpp Mercury2/src/StateChanger.h Mercury2/src/Texture.cpp Mercury2/src/Texture.h Modified: Mercury2/modules/Terrain.cpp =================================================================== --- Mercury2/modules/Terrain.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/modules/Terrain.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -4,8 +4,8 @@ REGISTER_ASSET_TYPE(Terrain); -Terrain::Terrain() - :base() +Terrain::Terrain( const MString & key, bool bInstanced ) + : base( key, bInstanced ) { } @@ -13,21 +13,10 @@ { } -Terrain* Terrain::Generate() -{ - LOG.Write( "new Terrain" ); - return new Terrain(); -} - -MercuryAssetInstance* Terrain::GenerateInstanceData(MercuryNode* parentNode) -{ - return new TerrainAssetInstance(this, parentNode); -} - void Terrain::LoadedCallback() { + base::LoadedCallback(); BuildHash(); - base::LoadedCallback(); } void Terrain::BuildHash() @@ -40,6 +29,16 @@ } } +bool Terrain::ChangeKey( const MString & sNewKey ) +{ + HGMDLModel::ChangeKey( sNewKey ); + if( GetLoadState() == LOADED ) + BuildHash(); + + return true; +} + + void Terrain::ImportMeshToHash(const HGMDLMesh& mesh) { const float* vertice = mesh.GetVertexHandle() + MercuryVBO::VERTEX_OFFSET; @@ -145,7 +144,12 @@ return result; } +MercuryAssetInstance * Terrain::MakeAssetInstance( MercuryNode * ParentNode ) +{ + return new TerrainAssetInstance( this, ParentNode ); +} + TerrainAssetInstance::TerrainAssetInstance(MercuryAsset* asset, MercuryNode* parentNode) :base(asset, parentNode) { Modified: Mercury2/modules/Terrain.h =================================================================== --- Mercury2/modules/Terrain.h 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/modules/Terrain.h 2009-11-05 02:59:30 UTC (rev 599) @@ -11,15 +11,15 @@ class Terrain : public HGMDLModel { public: - Terrain(); + Terrain( const MString & key, bool bInstanced ); ~Terrain(); - static Terrain* Generate(); virtual void LoadedCallback(); //thread safe - virtual MercuryAssetInstance* GenerateInstanceData(MercuryNode* parentNode); MercuryVertex ComputePosition(const MercuryVertex& p); MercuryVertex ComputePositionLinear(const MercuryVertex& p); + virtual bool ChangeKey( const MString & sNewKey ); + virtual MercuryAssetInstance * MakeAssetInstance( MercuryNode * ParentNode ); GENRTTI( Terrain ); private: CLASS_HELPERS( HGMDLModel ); Modified: Mercury2/modules/TextNode.cpp =================================================================== --- Mercury2/modules/TextNode.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/modules/TextNode.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -106,8 +106,6 @@ if( !m_pThisFont ) return; -// AddAsset( AssetFactory::GetInstance().Generate( "Quad", "MyQuad" ) ); - if( !m_kTEX ) { m_kTEX = MAutoPtr< MercuryAsset >( Texture::LoadFromFile( m_pThisFont->m_sImage ) ); @@ -122,7 +120,7 @@ //Setup FBO and Texture if( !m_kVBO ) { - m_kVBO = new MercuryVBO; + m_kVBO = new MercuryVBO( GetName() + "VBO", true ); if( !m_kVBO ) { fprintf( stderr, "Could not create VBO for text.\n" ); Modified: Mercury2/modules/TextPlate.cpp =================================================================== --- Mercury2/modules/TextPlate.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/modules/TextPlate.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -13,13 +13,6 @@ { m_TextNode = (TextNode*)NODEFACTORY.Generate( "TextNode" ); AddChild( m_TextNode ); - - m_BackgroundColor = ASSETFACTORY.Generate( "StateChanger", "ColorChange:1,1,0,1" ); - ((StateChanger*)m_BackgroundColor.Ptr())->LoadFromString( "ColorChange:1,1,0,1" ); - AddAsset( m_BackgroundColor ); - - m_BackPlane = ASSETFACTORY.Generate( "Quad", "TBQ" ); - AddAsset( m_BackPlane ); } void TextPlate::Update(float dTime) @@ -61,11 +54,23 @@ m_TextNode->SetText(sText); m_TextNode->RenderText(); - ((Quad*)m_BackPlane.Ptr())->LoadFromString( ssprintf( "%f,%f,%f,%f,-.01", + MString quadstr = ssprintf( "%f,%f,%f,%f,-.01", m_TextNode->GetRMinX() * 1.1 , m_TextNode->GetRMinY() * 1.1 , m_TextNode->GetRMaxX() * 1.1 , - m_TextNode->GetRMaxY() * 1.1 ) ); + m_TextNode->GetRMaxY() * 1.1 ); + if( !m_BackPlane ) + { + + m_BackgroundColor = ASSETFACTORY.Generate( "StateChanger", "ColorChange:1,1,0,1" ); + ((StateChanger*)m_BackgroundColor.Ptr())->ChangeKey( "ColorChange:1,1,0,1" ); + AddAsset( m_BackgroundColor ); + + m_BackPlane = ASSETFACTORY.Generate( "Quad", quadstr ); + AddAsset( m_BackPlane ); + } + else + ((Quad*)m_BackPlane.Ptr())->ChangeKey( quadstr ); } Modified: Mercury2/src/FullscreenQuad.cpp =================================================================== --- Mercury2/src/FullscreenQuad.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/FullscreenQuad.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -3,7 +3,8 @@ REGISTER_ASSET_TYPE(FullscreenQuad); -FullscreenQuad::FullscreenQuad() +FullscreenQuad::FullscreenQuad( const MString & key, bool bInstanced ) + : Quad( key, bInstanced ) { m_matrix = MercuryMatrix::Identity(); m_matrix.Transotale(0,0,-1,0,0,0,2,2,0.01); @@ -29,11 +30,6 @@ GLCALL( glPopMatrix() ); } -FullscreenQuad* FullscreenQuad::Generate() -{ - return new FullscreenQuad(); -} - /**************************************************************************** * Copyright (C) 2009 by Joshua Allen * * * Modified: Mercury2/src/FullscreenQuad.h =================================================================== --- Mercury2/src/FullscreenQuad.h 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/FullscreenQuad.h 2009-11-05 02:59:30 UTC (rev 599) @@ -7,12 +7,11 @@ class FullscreenQuad : public Quad { public: - FullscreenQuad(); + FullscreenQuad( const MString & key, bool bInstanced ); // virtual ~FullscreenQuad(); virtual void Render(const MercuryNode* node); - - static FullscreenQuad* Generate(); + GENRTTI( FullscreenQuad ); private: MercuryMatrix m_matrix; Modified: Mercury2/src/HGMDLMesh.h =================================================================== --- Mercury2/src/HGMDLMesh.h 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/HGMDLMesh.h 2009-11-05 02:59:30 UTC (rev 599) @@ -7,6 +7,8 @@ class HGMDLMesh : public MercuryVBO { public: + HGMDLMesh( const MString & key, bool bInstanced ) : MercuryVBO( key, bInstanced ), m_cachable( true ) { } + void LoadFromFile(MercuryFile* hgmdl); void ReadExtraData(MercuryFile* hgmdl); void LoadOBB(MercuryFile* hgmdl); Modified: Mercury2/src/HGMDLModel.cpp =================================================================== --- Mercury2/src/HGMDLModel.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/HGMDLModel.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -6,20 +6,19 @@ const uint16_t EXPCTMJRV = 2; const uint16_t EXPCTMNRV = 3; -HGMDLModel::HGMDLModel() - :MercuryAsset() +HGMDLModel::HGMDLModel( const MString & key, bool bInstanced ) + :MercuryAsset( key, bInstanced ) { } HGMDLModel::~HGMDLModel() { - REMOVE_ASSET_INSTANCE(HGMDLModel, m_path); } void HGMDLModel::LoadFromXML(const XMLNode& node) { MString path = node.Attribute("file"); - LoadHGMDL( path ); + ChangeKey( path ); MercuryAsset::LoadFromXML( node ); } @@ -55,7 +54,7 @@ hgmdl->Read( &numMeshes, sizeof( uint16_t ) ); for (uint16_t i = 0; i < numMeshes; ++i) { - MAutoPtr< HGMDLMesh > mesh( new HGMDLMesh() ); + MAutoPtr< HGMDLMesh > mesh = new HGMDLMesh( model->m_path + "MODEL", model->GetIsInstanced() ); mesh->LoadFromFile( hgmdl ); model->m_meshes.push_back(mesh); } @@ -92,28 +91,23 @@ } } -void HGMDLModel::LoadHGMDL( const MString& path ) +bool HGMDLModel::ChangeKey( const MString & sNewKey ) { - if ( m_isInstanced ) return; - if ( !path.empty() ) + if( GetLoadState() != NONE && sNewKey == m_path ) + return true; + + if ( !sNewKey.empty() ) { SetLoadState(LOADING); - ADD_ASSET_INSTANCE(HGMDLModel, path, this); - m_path = path; LoaderThreadData* ltd = new LoaderThreadData( this ); MercuryThread loaderThread; loaderThread.HaltOnDestroy(false); loaderThread.Create( LoaderThread, ltd ); } + return MercuryAsset::ChangeKey( sNewKey ); } -HGMDLModel* HGMDLModel::Generate() -{ - LOG.Write( "new HGMDL" ); - return new HGMDLModel(); -} - void* HGMDLModel::LoaderThread(void* d) { LoaderThreadData *pd = (LoaderThreadData*)d; Modified: Mercury2/src/HGMDLModel.h =================================================================== --- Mercury2/src/HGMDLModel.h 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/HGMDLModel.h 2009-11-05 02:59:30 UTC (rev 599) @@ -5,30 +5,26 @@ #include <HGMDLMesh.h> #include <MercuryFile.h> -#include <vector> - class HGMDLModel : public MercuryAsset { public: - HGMDLModel(); + HGMDLModel( const MString & key, bool bInstanced ); ~HGMDLModel(); virtual void LoadFromXML(const XMLNode& node); static void LoadModel(MercuryFile* hgmdl, HGMDLModel* model); - - static HGMDLModel* Generate(); - + virtual bool DoCullingTests(OcclusionResult& occlusion, const MercuryMatrix& matrix); virtual void PreRender(const MercuryNode* node); + virtual bool ChangeKey( const MString & sNewKey ); virtual void Render(const MercuryNode* node); GENRTTI( HGMDLModel ); protected: - std::vector< MAutoPtr< HGMDLMesh > > m_meshes; + MVector< MAutoPtr< HGMDLMesh > > m_meshes; private: CLASS_HELPERS( MercuryAsset ); - void LoadHGMDL( const MString& path ); static void* LoaderThread(void* d); }; Modified: Mercury2/src/Light.cpp =================================================================== --- Mercury2/src/Light.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/Light.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -10,7 +10,7 @@ REGISTER_NODE_TYPE(Light); Light::Light() - :MercuryNode(), m_boundingVolume( NULL ) + :MercuryNode(), m_fullScreenQuad( "", true ), m_boundingVolume( NULL ) { m_atten[0] = m_atten[1] = m_atten[2] = 0.0f; m_color[0] = m_color[1] = m_color[2] = 1.0f; @@ -60,8 +60,7 @@ if ( asset.IsValid() ) { Shader* shader = dynamic_cast<Shader*>( asset.Ptr() ); -// shader->LoadFromXML( node ); - shader->LoadShader(key, 0); + shader->ChangeKey(key); SetShader( shader ); } Modified: Mercury2/src/MercuryAsset.cpp =================================================================== --- Mercury2/src/MercuryAsset.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/MercuryAsset.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -5,13 +5,18 @@ extern bool DOOCCLUSIONCULL; -MercuryAsset::MercuryAsset() - :m_isInstanced(false), m_boundingVolume(NULL), m_loadState(NONE), m_ignoreCull(false), m_iPasses( DEFAULT_PASSES ) +MercuryAsset::MercuryAsset( const MString & key, bool bInstanced ) + :m_isInstanced(bInstanced), m_boundingVolume(NULL), + m_path( key ), m_loadState(NONE), m_ignoreCull(false), m_iPasses( DEFAULT_PASSES ), slType( 0 ) { } MercuryAsset::~MercuryAsset() { + //If this isn't set, we were never a registered instance to begin with, so, don't unregister. + if( slType ) + AssetFactory::GetInstance().RemoveAssetInstance( ToUpper(slType)+m_path ); + SAFE_DELETE(m_boundingVolume); } @@ -109,11 +114,24 @@ GLCALL( glEnd() ); } -MercuryAssetInstance* MercuryAsset::GenerateInstanceData(MercuryNode* parentNode) +bool MercuryAsset::ChangeKey( const MString & sNewKey ) { - return new MercuryAssetInstance(this, parentNode); + if( m_path == sNewKey ) return true; + + AssetFactory::GetInstance().AddAssetInstance( ToUpper(GetType())+m_path, this ); + + m_path = sNewKey; + + AssetFactory::GetInstance().RemoveAssetInstance( ToUpper(GetType())+m_path ); + + return true; } +MercuryAssetInstance * MercuryAsset::MakeAssetInstance( MercuryNode * ParentNode ) +{ + return new MercuryAssetInstance( this, ParentNode ); +} + MercuryAssetInstance::MercuryAssetInstance(MercuryAsset* asset, MercuryNode* parentNode) :m_parentNode(parentNode), m_asset( asset ), m_isCulled( false ), m_iPasses( asset->GetPasses() ) { @@ -127,44 +145,51 @@ return *instance; } -bool AssetFactory::RegisterFactoryCallback(const MString & type, Callback0R< MAutoPtr<MercuryAsset> > functor) +bool AssetFactory::RegisterFactoryCallback(const MString & type, MAutoPtr< MercuryAsset > (*functor)( const MString &, bool ) ) { MString t = ToUpper( type ); - std::pair<MString , Callback0R< MAutoPtr<MercuryAsset> > > pp(t, functor); - m_factoryCallbacks.push_back( pp ); + m_factoryCallbacks[t] = functor; return true; } -MAutoPtr<MercuryAsset> AssetFactory::Generate(const MString& type, const MString& key) +MAutoPtr<MercuryAsset> AssetFactory::Generate(const MString& type, const MString& key, bool bInstanced ) { MString t = ToUpper( type ); - MercuryAsset *asset = LocateAsset(t+key); - if ( asset ) return asset; + if( bInstanced ) + { + MercuryAsset *asset = LocateAsset(t+key); + if ( asset ) return asset; + } + printf( "Asset (%s) not found, generating\n", (t+key).c_str() ); - std::list< std::pair< MString, Callback0R< MAutoPtr<MercuryAsset> > > >::iterator i; - for (i = m_factoryCallbacks.begin(); i != m_factoryCallbacks.end(); ++i) - if (i->first == t) return i->second(); + MAutoPtr< MercuryAsset > (**generator)( const MString &, bool ) = m_factoryCallbacks.get( t ); + + if( generator ) + { + MAutoPtr< MercuryAsset > g = (**generator)(key, bInstanced); + AddAssetInstance( t+key, g.Ptr() ); + g->slType = g->GetType(); + return g; + } + LOG.Write( "WARNING: Asset type " + type + " not found." ); return NULL; } void AssetFactory::AddAssetInstance(const MString& key, MercuryAsset* asset) { - asset->IsInstanced(true); m_assetInstances[key] = asset; } void AssetFactory::RemoveAssetInstance(const MString& key) { m_assetInstances.remove( key ); - LOG.Write( "removed asset "+key ); + puts( "removed asset "+key ); } -MHash< MercuryAsset*> AssetFactory::m_assetInstances; - /**************************************************************************** - * Copyright (C) 2009 - 2009 by Joshua Allen * + * Copyright (C) 2008 - 2009 by Joshua Allen * * Charles Lohr * * * * * Modified: Mercury2/src/MercuryAsset.h =================================================================== --- Mercury2/src/MercuryAsset.h 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/MercuryAsset.h 2009-11-05 02:59:30 UTC (rev 599) @@ -29,7 +29,7 @@ class MercuryAsset : public RefBase, public MessageHandler { public: - MercuryAsset(); + MercuryAsset( const MString & key, bool bInstanced ); virtual ~MercuryAsset(); virtual void Init(); @@ -56,7 +56,8 @@ virtual void LoadedCallback(); //thread safe - inline void IsInstanced(bool b) { m_isInstanced = b; } + inline void SetIsInstanced(bool b) { m_isInstanced = b; } + inline bool GetIsInstanced() { return m_isInstanced; } inline BoundingVolume* GetBoundingVolume() const { return m_boundingVolume; } inline const MString& Path() const { return m_path; } @@ -70,12 +71,16 @@ inline unsigned short GetPasses() { return m_iPasses; } inline void SetPasses( unsigned short p ) { m_iPasses = p; } - - virtual MercuryAssetInstance* GenerateInstanceData(MercuryNode* parentNode); + virtual bool ChangeKey( const MString & sNewKey ); + LoadState GetLoadState(); //thread safe + virtual MercuryAssetInstance * MakeAssetInstance( MercuryNode * ParentNode ); + + GENRTTI( MercuryAsset ); + const char * slType; //Tricky - we need to know our type in the destructor. Don't touch this. protected: void SetLoadState(LoadState ls); //thread safe @@ -123,10 +128,7 @@ class LoaderThreadData { public: - LoaderThreadData(MercuryAsset* a) - { - asset = a; - } + LoaderThreadData(MercuryAsset* a) : asset( a ) { } //use and autoptr here to prevent crashes if asset is removed during load MAutoPtr< MercuryAsset > asset; @@ -136,19 +138,20 @@ { public: static AssetFactory& GetInstance(); - bool RegisterFactoryCallback(const MString& type, Callback0R< MAutoPtr<MercuryAsset> >); - MAutoPtr<MercuryAsset> Generate(const MString& type, const MString& key); - + bool RegisterFactoryCallback(const MString& type, MAutoPtr< MercuryAsset > (*)( const MString &, bool ) ); + + MAutoPtr<MercuryAsset> Generate(const MString& type, const MString& key, bool bInstanced = true ); + void AddAssetInstance(const MString& key, MercuryAsset* asset); void RemoveAssetInstance(const MString& key); private: - MercuryAsset* LocateAsset( const MString& key ) { MercuryAsset ** a = m_assetInstances.get( key ); return a?(*a):0; } + MAutoPtr< MercuryAsset > LocateAsset( const MString& key ) { MAutoPtr< MercuryAsset > * a = m_assetInstances.get( key ); return a?(*a):0; } - std::list< std::pair< MString, Callback0R< MAutoPtr<MercuryAsset> > > > m_factoryCallbacks; + MHash< MAutoPtr< MercuryAsset > (*)( const MString &, bool ) > m_factoryCallbacks; //the actual storage point is in MercuryAssetInstance - static MHash< MercuryAsset*> m_assetInstances; + MHash< MAutoPtr< MercuryAsset > > m_assetInstances; }; @@ -157,23 +160,16 @@ static InstanceCounter<AssetFactory> AFcounter("AssetFactory"); #define REGISTER_ASSET_TYPE(class)\ - MAutoPtr<MercuryAsset> FactoryFunct##class() { return MAutoPtr<MercuryAsset>(class::Generate()); } \ - Callback0R< MAutoPtr<MercuryAsset> > factoryclbk##class( FactoryFunct##class ); \ - bool GlobalRegisterSuccess##class = AssetFactory::GetInstance().RegisterFactoryCallback(#class, factoryclbk##class); + MAutoPtr<MercuryAsset> FactoryFunct##class( const MString & key, bool bInstanced ) { return new class( key, bInstanced ); } \ + bool GlobalRegisterSuccess##class = AssetFactory::GetInstance().RegisterFactoryCallback(#class, FactoryFunct##class); -#define ADD_ASSET_INSTANCE(class, key, ptr)\ - AssetFactory::GetInstance().AddAssetInstance( ToUpper(#class)+key, ptr ); - -#define REMOVE_ASSET_INSTANCE(class, key)\ - AssetFactory::GetInstance().RemoveAssetInstance( ToUpper(#class)+key ); - #define CLASS_HELPERS(baseClass)\ typedef baseClass base; #endif /**************************************************************************** - * Copyright (C) 2009 - 2009 by Joshua Allen * + * Copyright (C) 2008 - 2009 by Joshua Allen * * Charles Lohr * * * * * Modified: Mercury2/src/MercuryNode.cpp =================================================================== --- Mercury2/src/MercuryNode.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/MercuryNode.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -43,7 +43,7 @@ void MercuryNode::AddAsset(MercuryAsset* asset) { - m_assets.push_back( asset->GenerateInstanceData(this) ); + m_assets.push_back( asset->MakeAssetInstance( this ) ); } void MercuryNode::ClearAssets() @@ -51,6 +51,8 @@ list< MercuryAssetInstance* >::iterator i; for (i = m_assets.begin(); i != m_assets.end(); ++i ) SAFE_DELETE(*i); + + m_assets.clear( ); } void MercuryNode::AddChild(MercuryNode* n) @@ -374,6 +376,7 @@ mai->Culled( a.DoCullingTests( mai->GetOcclusionResult(), matrix ) ); culled = culled && mai->Culled(); } + printf( "CULL: %s (%d) %d\n", mai->Asset().GetType(), a.IgnoreCull(), mai->Culled() ); if ( !mai->Culled() ) a.PreRender(this); } Modified: Mercury2/src/MercuryVBO.cpp =================================================================== --- Mercury2/src/MercuryVBO.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/MercuryVBO.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -10,8 +10,8 @@ extern bool SHOWBOUNDINGVOLUME; extern bool SHOWAXISES; -MercuryVBO::MercuryVBO() - :MercuryAsset(), m_initiated(false) +MercuryVBO::MercuryVBO( const MString & key, bool bInstanced ) + :MercuryAsset( key, bInstanced ), m_initiated(false) { m_bufferIDs[0] = m_bufferIDs[1] = 0; m_bDirtyIndices = m_bDirtyVertices = 0; Modified: Mercury2/src/MercuryVBO.h =================================================================== --- Mercury2/src/MercuryVBO.h 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/MercuryVBO.h 2009-11-05 02:59:30 UTC (rev 599) @@ -13,7 +13,7 @@ static const uint16_t STRIDE = 8; static const uint16_t VERTEX_OFFSET = 5; - MercuryVBO(); + MercuryVBO( const MString & key, bool bInstanced ); virtual ~MercuryVBO(); virtual void Render(const MercuryNode* node); Modified: Mercury2/src/Quad.cpp =================================================================== --- Mercury2/src/Quad.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/Quad.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -5,50 +5,40 @@ REGISTER_ASSET_TYPE(Quad); -Quad::Quad() - :MercuryVBO() +Quad::Quad( const MString & key, bool bInstanced ) + :MercuryVBO( key, bInstanced ) { + ChangeKey( key ); } Quad::~Quad() { - REMOVE_ASSET_INSTANCE(Quad,m_path); } -Quad* Quad::Generate() -{ - Quad *asset = new Quad(); - ADD_ASSET_INSTANCE(Quad,"",asset); - return asset; -} - void Quad::LoadFromXML(const XMLNode& node) { - LoadFromString( node.Attribute("file") ); + //If we've already started with the same key - this function will just return. + ChangeKey( node.Attribute("file") ); MercuryAsset::LoadFromXML( node ); } -bool Quad::LoadFromString( const MString & sDescription ) +bool Quad::ChangeKey( const MString & sDescription ) { + if( sDescription == m_path && GetLoadState() != NONE ) + return true; + float lX = -0.5; float lY = -0.5; float hX = 0.5; float hY = 0.5; float zp = 0; - bool bResetRegistration = sDescription != m_path; AllocateIndexSpace(6); AllocateVertexSpace(4); - if( bResetRegistration ) - REMOVE_ASSET_INSTANCE(Quad,m_path); - m_path = sDescription; - if( bResetRegistration ) - ADD_ASSET_INSTANCE(Quad,m_path,this); - MVector< MString > vsDescription; SplitStrings( sDescription, vsDescription, ",", " ", 1, 1 ); @@ -75,6 +65,7 @@ else { LOG.Write( ssprintf( "Invalid number of parameters passed into new Quad: \"%s\"", sDescription.c_str() ) ); + return false; } // float* buffer = m_vertexData.m_vertexData(); @@ -103,9 +94,9 @@ m_indexData[3] = m_indexData[2] = 2; m_indexData[4] = 3; - m_path = sDescription; + SetLoadState( LOADED ); - return true; + return MercuryVBO::ChangeKey( sDescription ); } Modified: Mercury2/src/Quad.h =================================================================== --- Mercury2/src/Quad.h 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/Quad.h 2009-11-05 02:59:30 UTC (rev 599) @@ -7,13 +7,12 @@ class Quad : public MercuryVBO { public: - Quad(); + Quad( const MString & key, bool bInstanced ); ~Quad(); - void LoadFromXML(const XMLNode& node ); - bool LoadFromString( const MString & sDescription ); + virtual void LoadFromXML(const XMLNode& node ); + virtual bool ChangeKey( const MString & sDescription ); - static Quad* Generate(); GENRTTI( Quad ); private: }; Modified: Mercury2/src/Shader.cpp =================================================================== --- Mercury2/src/Shader.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/Shader.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -32,7 +32,8 @@ return ret; } -Shader::Shader() +Shader::Shader( const MString & key, bool bInstanced ) : + MercuryAsset( key, bInstanced ), fPriority( 0 ) { iProgramID = (GLhandleARB)NULL; vertexShader = (GLhandleARB)NULL; @@ -89,17 +90,21 @@ void Shader::LoadFromXML(const XMLNode& node) { - LoadShader( node.Attribute("file"), StrToFloat( node.Attribute("priority") ) ); + //LoadShader( node.Attribute("file"), ) ) ); + + fPriority = StrToFloat( node.Attribute("priority" ) ); + ChangeKey( node.Attribute("file") ); } -void Shader::LoadShader( const MString& path, float priority ) +bool Shader::ChangeKey( const MString& path ) { - if (m_isInstanced) return; + if( GetLoadState() != NONE && path == m_path ) + return true; sShaderName = path; - fPriority = priority; - ADD_ASSET_INSTANCE(Shader, sShaderName, this); LoadShader( ); + + return MercuryAsset::ChangeKey( path ); } bool Shader::LoadShader( ) Modified: Mercury2/src/Shader.h =================================================================== --- Mercury2/src/Shader.h 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/Shader.h 2009-11-05 02:59:30 UTC (rev 599) @@ -74,21 +74,22 @@ class Shader : public MercuryAsset { public: - Shader(); + Shader( const MString & key, bool bInstanced ); virtual ~Shader(); virtual void Render(const MercuryNode* node); virtual void PostRender(const MercuryNode* node); - static Shader* Generate() { return new Shader; } + virtual void LoadFromXML(const XMLNode& node); static void SetAttribute(const MString& name, const ShaderAttribute& x); static void RemoveAttribute(const MString& name); + virtual bool ChangeKey( const MString & sNewKey ); + ///Explicitly get the OpenGL ProgramID in the event you need it for advanced techniques unsigned int GetProgramID() { return iProgramID; } inline static Shader* GetCurrentShader() { return CurrentShader; } - void LoadShader( const MString& path, float priority ); private: int32_t GetUniformLocation(const MString& n); Modified: Mercury2/src/StateChanger.cpp =================================================================== --- Mercury2/src/StateChanger.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/StateChanger.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -85,17 +85,18 @@ REGISTER_ASSET_TYPE(StateChanger); -StateChanger::StateChanger() - :MercuryAsset() +StateChanger::StateChanger( const MString & key, bool bInstanced ) + :MercuryAsset( key, bInstanced ) { //Make sure our state stack is correctly sized if( m_StateSet.size() < (unsigned)StateChangeRegister::Instance().GetStateCount() ) m_StateSet.resize( StateChangeRegister::Instance().GetStateCount() ); + + LoadInternal( key ); } StateChanger::~StateChanger() { - REMOVE_ASSET_INSTANCE(TEXTURE, m_path); } void StateChanger::Render(const MercuryNode* node) @@ -127,8 +128,18 @@ } } -bool StateChanger::LoadFromString( const MString & sFile ) +bool StateChanger::ChangeKey( const MString & sFile ) { + if( m_path == sFile ) + return true; + + LoadInternal( sFile ); + + return MercuryAsset::ChangeKey( sFile ); +} + +bool StateChanger::LoadInternal( const MString & sFile ) +{ int f = sFile.find( ":", 0 ); if( f <= 0 ) { @@ -151,7 +162,6 @@ return false; } - return true; } @@ -161,7 +171,7 @@ if ( !node.Attribute("file").empty() ) { MString sFile = node.Attribute("file"); - LoadFromString( sFile ); + ChangeKey( sFile ); } } @@ -177,11 +187,6 @@ MercuryAsset::SaveToXMLTag( sXMLStream ); } -StateChanger* StateChanger::Generate() -{ - return new StateChanger(); -} - MVector< MVector< MAutoPtr< StateChange > > > StateChanger::m_StateSet; Modified: Mercury2/src/StateChanger.h =================================================================== --- Mercury2/src/StateChanger.h 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/StateChanger.h 2009-11-05 02:59:30 UTC (rev 599) @@ -49,7 +49,7 @@ class StateChanger : public MercuryAsset { public: - StateChanger(); + StateChanger( const MString & key, bool bInstanced ); virtual ~StateChanger(); virtual void Render(const MercuryNode* node); @@ -57,10 +57,9 @@ virtual void LoadFromXML(const XMLNode& node); virtual void SaveToXMLTag( MString & sXMLStream ); - static StateChanger* Generate(); - bool LoadFromString( const MString & sDescription ); - + virtual bool ChangeKey( const MString & sNewKey ); + virtual bool LoadInternal( const MString & sFile ); GENRTTI( StateChanger ); private: Modified: Mercury2/src/Texture.cpp =================================================================== --- Mercury2/src/Texture.cpp 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/Texture.cpp 2009-11-05 02:59:30 UTC (rev 599) @@ -12,8 +12,8 @@ #define BUFFER_OFFSET(i) ((char*)NULL + (i)) -Texture::Texture() - :MercuryAsset(), m_raw(NULL),m_textureID(0) +Texture::Texture( const MString & key, bool bInstanced ) + :MercuryAsset( key, bInstanced ), m_raw(NULL),m_textureID(0),m_dynamic(false) { if (!m_initTextureSuccess) { @@ -26,10 +26,7 @@ Texture::~Texture() { - REMOVE_ASSET_INSTANCE(TEXTURE, m_path); - - Clean(); - + Clean(); SAFE_DELETE(m_raw); } @@ -89,6 +86,7 @@ { if (GetLoadState() == LOADED) { + printf( "Rendering Texture (%s), but state is: %d\n", m_path.c_str(), GetLoadState() ); LoadFromRaw(); SetLoadState(NONE); } @@ -102,16 +100,27 @@ void Texture::LoadFromXML(const XMLNode& node) { - bool dynamic = false; if ( !node.Attribute("dynamic").empty() ) - dynamic = node.Attribute("dynamic")=="true"?true:false; + m_dynamic = node.Attribute("dynamic")=="true"?true:false; MString file = node.Attribute("file"); - - if ( dynamic ) - MakeDynamic( 0, 0, RGBA, file ); + + ChangeKey( file ); +} + +bool Texture::ChangeKey( const MString & sNewKey ) +{ + if( sNewKey == m_path && GetLoadState() != NONE ) + return true; + + if ( m_dynamic ) + MakeDynamic( 0, 0, RGBA, sNewKey ); else - LoadImagePath( file ); + LoadImagePath( sNewKey ); + + if( sNewKey != m_path ) + return MercuryAsset::ChangeKey( sNewKey ); + return true; } void Texture::BindTexture() @@ -213,13 +222,10 @@ void Texture::LoadImagePath(const MString& path) { - if (m_isInstanced) return; if ( !path.empty() ) { SetLoadState(LOADING); - ADD_ASSET_INSTANCE(Texture, path, this); - m_path = path; - + m_path = path; // MercuryThread loaderThread; // ImageLoader::LoadImageThreaded(this, m_filename ); ImageLoader::GetInstance().LoadImageThreaded(this); @@ -241,10 +247,8 @@ // Clean(); SetLoadState(LOADED); - - REMOVE_ASSET_INSTANCE(TEXTURE, m_path); + m_path = "DYNATEXT"+name; - ADD_ASSET_INSTANCE(Texture, m_path, this); if (m_textureID == 0) { GLCALL( glGenTextures( 1, &m_textureID ) ); } GLCALL( glBindTexture( GL_TEXTURE_2D, m_textureID ) ); @@ -256,11 +260,6 @@ GLERRORCHECK; } -Texture* Texture::Generate() -{ - return new Texture(); -} - MAutoPtr< Texture > Texture::LoadFromFile(const MString& path) { MAutoPtr< MercuryAsset > t( AssetFactory::GetInstance().Generate("Texture", path) ); Modified: Mercury2/src/Texture.h =================================================================== --- Mercury2/src/Texture.h 2009-11-05 02:44:00 UTC (rev 598) +++ Mercury2/src/Texture.h 2009-11-05 02:59:30 UTC (rev 599) @@ -7,7 +7,7 @@ class Texture : public MercuryAsset { public: - Texture(); + Texture( const MString & key, bool bInstanced ); virtual ~Texture(); void Clean(); @@ -16,7 +16,9 @@ virtual void PostRender(const MercuryNode* node); virtual void LoadFromXML(const XMLNode& node); - + + virtual bool ChangeKey( const MString & sNewKey ); + void LoadFromRaw(); inline static uint8_t NumberActiveTextures() { return m_numActiveTextures; } @@ -25,7 +27,6 @@ void MakeDynamic(uint16_t width, uint16_t height, ColorByteType cbt, const MString& name); - static Texture* Generate(); static MAutoPtr< Texture > LoadFromFile(const MString& path); static MAutoPtr< Texture > LoadDynamicTexture(const MString& name); static const std::list< Texture* >& GetActiveTextures() { return m_activeTextures; } @@ -56,6 +57,8 @@ static uint8_t m_maxActiveTextures; static Texture** m_lastBound; + + bool m_dynamic; // MString m_filename; }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |