20 #ifndef _STELTEXTURECACHE_HPP_
21 #define _STELTEXTURECACHE_HPP_
52 template<
class TextureBackend>
57 struct TextureBackendRefCounted
62 TextureBackend* backend;
65 TextureBackendRefCounted(){}
67 TextureBackendRefCounted(TextureBackend* backend):refCount(1),backend(backend){}
83 virtual TextureBackend*
get(
const QString& name,
const TextureLoadingMode loadingMode)
85 Q_ASSERT_X(!name.isEmpty(), Q_FUNC_INFO,
"Can't cache textures without a name");
87 if(!cache.contains(name)) {
return NULL;}
89 TextureBackend* result = cache[name].backend;
93 case TextureLoadingMode_Normal:
96 if(result->getStatus() == TextureStatus_Uninitialized)
98 result->startAsynchronousLoading();
101 while(result->getStatus() == TextureStatus_Loading)
105 case TextureLoadingMode_Asynchronous:
107 if(result->getStatus() == TextureStatus_Uninitialized)
109 result->startAsynchronousLoading();
112 case TextureLoadingMode_LazyAsynchronous:
115 Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown texture loading mode");
118 (cache[name].refCount)++;
129 virtual void add(TextureBackend* backend)
131 const QString& name = backend->getName();
132 Q_ASSERT_X(!name.isEmpty(), Q_FUNC_INFO,
"Can't cache textures without a name");
133 Q_ASSERT_X(!cache.contains(name), Q_FUNC_INFO,
134 "Adding the same texture to the cache more than once");
136 cache.insert(name, TextureBackendRefCounted(backend));
145 virtual void remove(TextureBackend* backend)
147 const QString& name = backend->getName();
148 Q_ASSERT_X(!name.isEmpty(), Q_FUNC_INFO,
"Can't cache textures without a name");
149 Q_ASSERT_X(cache.contains(name), Q_FUNC_INFO,
150 "Trying to remove unknown texture from cache");
152 TextureBackendRefCounted& cached = cache[name];
154 Q_ASSERT_X(cached.refCount >= 0, Q_FUNC_INFO,
155 "Negative reference count of a texture in texture cache");
158 if(cached.refCount == 0)
162 Q_ASSERT_X(backend == cached.backend, Q_FUNC_INFO,
163 "Texture in cache has the same name as but is not specified texture");
171 QMap<QString, TextureBackendRefCounted> cache;
174 #endif // _STELTEXTURECACHE_HPP_