MercuryTextureManager.h

Go to the documentation of this file.
00001 #ifndef MERCURYTEXTUREMANAGER_H
00002 #define MERCURYTEXTUREMANAGER_H
00003 
00004 #include "MercuryString.h"
00005 #include "MercuryTexture.h"
00006 #include "MercuryUtil.h"
00007 #include "MercuryFiles.h"
00008 #include "MercuryThreads.h"
00009 #include "MercuryCallback.h"
00010 #include "MercuryMessages.h"
00011 #include "MercuryTexture.h"
00012 
00013 using namespace std;
00014 
00016 class TextureID
00017 {
00018 public:
00019     ~TextureID();
00020     TextureID(const MString& path, bool loadPending = false);
00021     bool RemoveTexturePtr(const MercuryTexture* texture);
00022     void UpdateTextureData(MercuryTexture* texture);
00023     void CreateCache(MercuryTexture* texture);
00024 
00025     unsigned int m_ID;
00026     MVector<const MercuryTexture*> m_textures;  //Pointers to textures using this ID
00027 
00028     ImageAttrs m_attrs;
00029     MString m_path;
00030     bool m_loadPending;
00031 };
00032 
00034 class MercuryTextureManager : public MercuryMessageHandler
00035 {
00036     struct ThreadLoadData
00037     {
00038         ThreadLoadData(const MString& p, const MercuryTexture* t, RawImageData* data = NULL)
00039             :path(p), texture(t), d(data)
00040         {}
00041         MString path;
00042         const MercuryTexture* texture;
00043         RawImageData* d;
00044     };
00045 public:
00046     MercuryTextureManager();
00047     virtual ~MercuryTextureManager();
00048 
00049     static MercuryTexture* CreateTexture(const MString& path);
00050 
00051     virtual void Message( int Message, PStack & data, const MString & name );
00052 
00053     void CreateThread();
00054     void ThreadLoop();
00055 
00056     void UpdateTexture(MercuryTexture* texture);
00057     void Remove(MercuryTexture* texture);
00058     void Update( const float dTime );
00059     bool IsTexture(const MercuryTexture* texture);
00060 
00061     inline unsigned int TextureCount() const { return m_textureCount; }
00062     inline unsigned int UniqueTextureCount() const { return MLockPtr< map<MString, TextureID*> >(m_textureMap, m_mtxMap)->size(); }
00063     TextureID* FindTexture(const MString& path) const;
00064     TextureID* FindTexture(const MercuryTexture* texture) { return FindTexture(texture->GetPath()); }
00065 private:
00066     void Remove(const MString& path, const MercuryTexture* texture);
00067     void ProcessLoadQueue();
00068     void ProcessFinalizeQueue();
00069 
00070     bool TryFromExisting(ThreadLoadData* tld);
00071 
00072     TextureID* RegisterNewTexture(const MercuryTexture* t, RawImageData* d, const MString& path);
00073     RawImageData * LoadDyn( const MString & sFullName, MString & sShortName );
00074     RawImageData * LoadCube( const MString & sFullName, MString & sShortName );
00075     RawImageData * LoadDynShadow( const MString & sFullName, MString & sShortName );
00076     RawImageData * LoadDynCube( const MString & sFullName, MString & sShortName );
00077 
00078     RawImageData* ImageLoader(const MString& path);
00079 
00080     unsigned int m_textureCount;
00081     volatile map<MString, TextureID*> m_textureMap;
00082 
00083     volatile MDeque<ThreadLoadData*> m_loadQueue;
00084     volatile MDeque<ThreadLoadData*> m_finalizeLoadQueue;
00085 
00086     MercuryThread m_thread;
00087 
00088     MercuryMutex m_mtxLoadQueue;
00089     MercuryMutex m_mtxMap;
00090     MercuryMutex m_mtxFinalize;
00091 };
00092 
00093 
00094 extern MercuryTextureManager* TEXTMAN;
00095 
00096 typedef RawImageData * (*ImageDecoder)( MercuryFile * );
00097 
00098 class MercuryImageReaderRegister
00099 {
00100 public:
00101     void CheckInit();
00102     void AddDecoder( ImageDecoder, const char * sFingerPrint );
00103     RawImageData * Decode( MercuryFile * f );
00104 private:
00105     bool bInit;
00106     MHash< ImageDecoder > * m_hDecoders;
00107 };
00108 
00109 extern MercuryImageReaderRegister IMAGEREADERREGISTER;
00110 
00111 #endif
00112 
00113 /* 
00114  * Copyright (c) 2005-2006, Joshua Allen
00115  * All rights reserved.
00116  *
00117  * Redistribution and use in source and binary forms, with or
00118  * without modification, are permitted provided that the following
00119  * conditions are met:
00120  *  -   Redistributions of source code must retain the above
00121  *      copyright notice, this list of conditions and the following disclaimer.
00122  *  -   Redistributions in binary form must reproduce the above copyright
00123  *      notice, this list of conditions and the following disclaimer in
00124  *      the documentation and/or other materials provided with the distribution.
00125  *  -   Neither the name of the Mercury Engine nor the names of its
00126  *      contributors may be used to endorse or promote products derived from
00127  *      this software without specific prior written permission.
00128  *
00129  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00130  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00131  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00132  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00133  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00134  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00135  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00136  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00137  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00138  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00139  */

Hosted by SourceForge.net Logo