1 //------------------------------------------------------------------------------
2 // File : SkyTextureManager.hpp
3 //------------------------------------------------------------------------------
4 // SkyWorks : Copyright 2002 Mark J. Harris and
5 // The University of North Carolina at Chapel Hill
6 //------------------------------------------------------------------------------
7 // Permission to use, copy, modify, distribute and sell this software and its
8 // documentation for any purpose is hereby granted without fee, provided that
9 // the above copyright notice appear in all copies and that both that copyright
10 // notice and this permission notice appear in supporting documentation.
11 // Binaries may be compiled with this software without any royalties or
14 // The author(s) and The University of North Carolina at Chapel Hill make no
15 // representations about the suitability of this software for any purpose.
16 // It is provided "as is" without express or implied warranty.
18 * @file SkyTextureManager.hpp
20 * Definition of a manager that keeps track of texture locations and sharing of texture files. |
22 #ifndef SKYTEXTUREMANAGER_HPP
23 #define SKYTEXTUREMANAGER_HPP
25 #pragma warning( disable : 4786)
27 #include "SkySingleton.hpp"
28 #include "SkyTexture.hpp"
35 // forward declaration for singleton
36 class SkyTextureManager;
38 //! A singleton of the SkyTextureManager. Can only create the TextureManager with TextureManager::Instantiate();
39 typedef SkySingleton<SkyTextureManager> TextureManager;
41 //------------------------------------------------------------------------------
43 * @class SkyTextureManager
44 * @brief A resource manager for textures.
46 * This manager allows textures to be shared. It keeps a mapping of
47 * filenames to texture objects, and makes it easy to use the same texture
48 * for multiple objects without the objects having to be aware of the
49 * sharing. Supports cube map textures, and 2D textures. Can also be used
50 * to "clone textures", which creates unmanaged texture objects from files
51 * that are not kept in the mapping, and thus are not shared.
53 class SkyTextureManager
56 typedef list<string> StringList;
59 //.-------------------------------------------------------------------------.
60 //| Paths to texture directories
61 //.-------------------------------------------------------------------------.
62 void AddPath(const string& path);
63 //! Return the list of texture paths that will be searched by Get2DTexture() and Get3DTexture().
64 const StringList& GetPaths() const { return _texturePaths; }
65 //! Clear the list of texture paths that will be searched by Get2DTexture() and Get3DTexture().
66 void ClearPaths() { _texturePaths.clear(); }
68 //.-------------------------------------------------------------------------.
70 //.-------------------------------------------------------------------------.
71 SKYRESULT Get2DTexture( const string &filename,
73 bool bMipmap = false);
74 SKYRESULT Get3DTexture( const string &filename,
78 bool bLoadFromSliceFiles = false);
79 SKYRESULT GetCubeMapTexture( const string &filename,
81 bool bMipmap = false);
83 //.-------------------------------------------------------------------------.
84 //| Texture cloning: create a duplicate texture object: not added to set!
85 //.-------------------------------------------------------------------------.
86 SKYRESULT Clone2DTexture( const string &filename,
88 bool bMipmap = false);
89 SKYRESULT Clone3DTexture( const string &filename,
93 bool bLoadFromSliceFiles = false );
94 SKYRESULT CloneCubeMapTexture( const string &filename,
96 bool bMipmap = false);
98 //.-------------------------------------------------------------------------.
99 //| Texture Object Creation: not added to the texture set (no filename!)
100 //.-------------------------------------------------------------------------.
101 inline SKYRESULT Create2DTextureObject(SkyTexture &texture,
103 unsigned int iHeight,
104 unsigned int iFormat,
105 unsigned char *pData);
106 inline SKYRESULT Create3DTextureObject(SkyTexture &texture,
108 unsigned int iHeight,
110 unsigned int iFormat,
111 unsigned char *pData);
113 //.-------------------------------------------------------------------------.
114 //| Texture Object Destruction: use this because texture objects are structs
115 //| that use shallow copies!
116 //.-------------------------------------------------------------------------.
117 static void DestroyTextureObject( SkyTexture &texture);
121 SkyTextureManager(bool bSlice3DTextures = false);
122 ~SkyTextureManager();
124 SKYRESULT _Create2DTextureObject( SkyTexture &texture,
126 unsigned int iHeight,
127 unsigned int iFormat,
128 unsigned char *pData);
129 SKYRESULT _Create3DTextureObject( SkyTexture &texture,
131 unsigned int iHeight,
133 unsigned int iFormat,
134 unsigned char *pData);
136 typedef list<SkyTexture> TextureList;
137 typedef map<string, SkyTexture> TextureSet;
138 typedef TextureSet::iterator TextureIterator;
140 //.-------------------------------------------------------------------------.
142 //.-------------------------------------------------------------------------.
144 // paths searched for textures specified by filename.
145 StringList _texturePaths;
148 TextureSet _textures; // loaded textures
149 // textures created directly, not loaded from file and cached.
150 TextureList _uncachedTextures;
152 // if this is true, then 3D textures will be represented as a set of 2D slices.
153 static bool s_bSlice3DTextures;
157 //------------------------------------------------------------------------------
158 // Function : SkyTextureManager::Create2DTextureObject
160 //------------------------------------------------------------------------------
162 * @fn SkyTextureManager::Create2DTextureObject(SkyTexture &texture, unsigned int iWidth, unsigned int iHeight, unsigned int iFormat, unsigned char *pData)
163 * @brief Creates a 2D texture.
165 * Creates an OpenGL texture object and returns its ID and dimensions in a SkyTexture structure.
166 * This texture will be deleted by the texture manager at shutdown.
169 inline SKYRESULT SkyTextureManager::Create2DTextureObject(SkyTexture &texture,
171 unsigned int iHeight,
172 unsigned int iFormat,
173 unsigned char *pData)
175 SKYRESULT retval = _Create2DTextureObject(texture, iWidth, iHeight, iFormat, pData);
176 if SKYSUCCEEDED(retval)
177 _uncachedTextures.push_back(texture);
182 //------------------------------------------------------------------------------
183 // Function : SkyTextureManager::Create3DTextureObject
185 //------------------------------------------------------------------------------
187 * @fn SkyTextureManager::Create3DTextureObject(SkyTexture &texture, unsigned int iWidth, unsigned int iHeight, unsigned int iDepth, unsigned int iFormat, unsigned char *pData)
188 * @brief Creates a 3D texture.
190 * Creates an OpenGL texture object and returns its ID and dimensions in a SkyTexture structure.
191 * This texture will be deleted by the texture manager at shutdown, and should not be destroyed
195 inline SKYRESULT SkyTextureManager::Create3DTextureObject(SkyTexture &texture,
197 unsigned int iHeight,
199 unsigned int iFormat,
200 unsigned char *pData)
202 SKYRESULT retval = _Create3DTextureObject(texture, iWidth, iHeight, iDepth, iFormat, pData);
203 if SKYSUCCEEDED(retval)
204 _uncachedTextures.push_back(texture);
208 #endif //QGLVUTEXTUREMANAGER_HPP