From: <axl...@us...> - 2008-12-31 04:37:05
|
Revision: 123 http://hgengine.svn.sourceforge.net/hgengine/?rev=123&view=rev Author: axlecrusher Date: 2008-12-31 04:37:02 +0000 (Wed, 31 Dec 2008) Log Message: ----------- Use the unified asset instances in the asset factory Modified Paths: -------------- Mercury2/src/MercuryAsset.cpp Mercury2/src/MercuryAsset.h Mercury2/src/Quad.cpp Mercury2/src/Quad.h Mercury2/src/RenderableNode.cpp Mercury2/src/Texture.cpp Mercury2/src/Texture.h Modified: Mercury2/src/MercuryAsset.cpp =================================================================== --- Mercury2/src/MercuryAsset.cpp 2008-12-31 00:35:14 UTC (rev 122) +++ Mercury2/src/MercuryAsset.cpp 2008-12-31 04:37:02 UTC (rev 123) @@ -24,9 +24,13 @@ return true; } -MAutoPtr<MercuryAsset> AssetFactory::Generate(const MString& type) +MAutoPtr<MercuryAsset> AssetFactory::Generate(const MString& type, const MString& key) { MString t = ToUpper( type ); + + MercuryAsset *asset = LocateAsset(t+key); + if ( asset ) return asset; + 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(); @@ -34,6 +38,27 @@ return NULL; } +MercuryAsset* AssetFactory::LocateAsset( const MString& key ) +{ + std::map<MString, MercuryAsset*>::iterator asset = m_assetInstances.find(key); + if ( asset != m_assetInstances.end() ) return asset->second; + return NULL; +} + +void AssetFactory::AddAssetInstance(const MString& key, MercuryAsset* asset) +{ + m_assetInstances[key] = asset; +} + +void AssetFactory::RemoveAssetInstance(const MString& key) +{ + std::map<MString, MercuryAsset*>::iterator asset = m_assetInstances.find(key); + if ( asset != m_assetInstances.end() ) + m_assetInstances.erase( asset ); +} + +std::map<MString, MercuryAsset*> AssetFactory::m_assetInstances; + /*************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/MercuryAsset.h =================================================================== --- Mercury2/src/MercuryAsset.h 2008-12-31 00:35:14 UTC (rev 122) +++ Mercury2/src/MercuryAsset.h 2008-12-31 04:37:02 UTC (rev 123) @@ -4,6 +4,8 @@ #include <MAutoPtr.h> #include <MercuryNode.h> +#include <map> + class MercuryAsset : public RefBase { public: @@ -24,10 +26,18 @@ public: static AssetFactory& GetInstance(); bool RegisterFactoryCallback(const MString& type, Callback0R< MAutoPtr<MercuryAsset> >); - MAutoPtr<MercuryAsset> Generate(const MString& type); + MAutoPtr<MercuryAsset> Generate(const MString& type, const MString& key); + + void AddAssetInstance(const MString& key, MercuryAsset* asset); + void RemoveAssetInstance(const MString& key); - private: + MercuryAsset* LocateAsset( const MString& key ); + + private: std::list< std::pair< MString, Callback0R< MAutoPtr<MercuryAsset> > > > m_factoryCallbacks; + + static std::map<MString, MercuryAsset*> m_assetInstances; + }; static InstanceCounter<AssetFactory> AFcounter("AssetFactory"); Modified: Mercury2/src/Quad.cpp =================================================================== --- Mercury2/src/Quad.cpp 2008-12-31 00:35:14 UTC (rev 122) +++ Mercury2/src/Quad.cpp 2008-12-31 04:37:02 UTC (rev 123) @@ -39,19 +39,17 @@ Quad::~Quad() { - if (m_myInstance == this) - m_myInstance = NULL; + AssetFactory::GetInstance().RemoveAssetInstance( "QUAD" ); } Quad* Quad::Generate() { - if ( !m_myInstance ) - m_myInstance = new Quad(); - return m_myInstance; + Quad *asset = new Quad(); + AssetFactory::GetInstance().AddAssetInstance( "QUAD", asset ); + printf("new quad\n"); + return asset; } -Quad* Quad::m_myInstance = NULL; - /*************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/Quad.h =================================================================== --- Mercury2/src/Quad.h 2008-12-31 00:35:14 UTC (rev 122) +++ Mercury2/src/Quad.h 2008-12-31 04:37:02 UTC (rev 123) @@ -13,7 +13,6 @@ static Quad* Generate(); private: - static Quad* m_myInstance; }; #endif Modified: Mercury2/src/RenderableNode.cpp =================================================================== --- Mercury2/src/RenderableNode.cpp 2008-12-31 00:35:14 UTC (rev 122) +++ Mercury2/src/RenderableNode.cpp 2008-12-31 04:37:02 UTC (rev 123) @@ -143,7 +143,8 @@ { if ( child.Name() == "asset" ) { - MAutoPtr< MercuryAsset > asset( AssetFactory::GetInstance().Generate(child.Attribute("type") ) ); + MString key = child.Attribute("file"); + MAutoPtr< MercuryAsset > asset( AssetFactory::GetInstance().Generate( child.Attribute("type"), key ) ); if ( asset.IsValid() ) { asset->LoadFromXML( child ); Modified: Mercury2/src/Texture.cpp =================================================================== --- Mercury2/src/Texture.cpp 2008-12-31 00:35:14 UTC (rev 122) +++ Mercury2/src/Texture.cpp 2008-12-31 04:37:02 UTC (rev 123) @@ -12,7 +12,7 @@ REGISTER_ASSET_TYPE(Texture); Texture::Texture() - :m_raw(NULL),m_textureID(0) + :m_raw(NULL),m_textureID(0),m_isLoaded(false) { if (!m_initTextureSuccess) { @@ -23,6 +23,8 @@ Texture::~Texture() { + AssetFactory::GetInstance().RemoveAssetInstance( "TEXTURE"+m_filename ); + if (m_textureID) glDeleteTextures(1, &m_textureID); m_textureID = 0; @@ -92,12 +94,8 @@ void Texture::LoadFromXML(const XMLNode& node) { - if ( !node.Attribute("imagefile").empty() ) - { -// RawImageData* d = LoadBMP( node.Attribute("imagefile") ); - RawImageData* d = ImageLoader::GetInstance().LoadImage( node.Attribute("imagefile") ); - if (d) LoadFromRaw( d ); - } + if (m_isLoaded) return; + LoadImage( node.Attribute("file") ); } void Texture::BindTexture() @@ -121,9 +119,34 @@ --m_activeTextures; } +void Texture::LoadImage(const MString& path) +{ + if ( !path.empty() ) + { + m_isLoaded = true; + m_filename = path; + AssetFactory::GetInstance().AddAssetInstance("TEXTURE" + m_filename, this); + RawImageData* d = ImageLoader::GetInstance().LoadImage( m_filename ); + if (d) LoadFromRaw( d ); + } +} + +Texture* Texture::Generate() +{ + return new Texture(); +} + +Texture* Texture::LoadFromFile(const MString& path) +{ + Texture *t = (Texture*)AssetFactory::GetInstance().LocateAsset("TEXTURE" + path); + if (!t) t = Generate(); + t->LoadImage( path ); +} + bool Texture::m_initTextureSuccess = false; unsigned short Texture::m_activeTextures = 0; + /*************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/Texture.h =================================================================== --- Mercury2/src/Texture.h 2008-12-31 00:35:14 UTC (rev 122) +++ Mercury2/src/Texture.h 2008-12-31 04:37:02 UTC (rev 123) @@ -21,8 +21,11 @@ inline static unsigned short NumberActiveTextures() { return m_activeTextures; } - static Texture* Generate() { return new Texture(); } + static Texture* Generate(); + static Texture* LoadFromFile(const MString& path); private: + void LoadImage(const MString& path); + void BindTexture(); void UnbindTexture(); @@ -32,6 +35,9 @@ static bool m_initTextureSuccess; static unsigned short m_activeTextures; + + MString m_filename; + bool m_isLoaded; }; #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |