csplugincommon/rendermanager/texturecache.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2007 by Frank Richter 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_CSPLUGINCOMMON_RENDERMANAGER_TEXTURECACHE_H__ 00020 #define __CS_CSPLUGINCOMMON_RENDERMANAGER_TEXTURECACHE_H__ 00021 00026 #include "igraphic/image.h" 00027 #include "ivideo/texture.h" 00028 #include "ivideo/txtmgr.h" 00029 00030 #include "csutil/genericresourcecache.h" 00031 00032 struct iTextureHandle; 00033 00034 namespace CS 00035 { 00036 namespace RenderManager 00037 { 00038 namespace Implementation 00039 { 00043 struct TextureSizeConstraint 00044 { 00045 struct KeyType 00046 { 00047 int w, h; 00048 }; 00049 00050 static bool IsEqual (const csRef<iTextureHandle>& t1, 00051 const csRef<iTextureHandle>& t2) 00052 { 00053 int tw1, th1, tw2, th2; 00054 t1->GetRendererDimensions (tw1, th1); 00055 t2->GetRendererDimensions (tw2, th2); 00056 00057 if ((tw1 == tw2) && (th1 == th2)) return true; 00058 return false; 00059 } 00060 00061 static bool IsLargerEqual (const csRef<iTextureHandle>& t1, 00062 const csRef<iTextureHandle>& t2) 00063 { 00064 int tw1, th1, tw2, th2; 00065 t1->GetRendererDimensions (tw1, th1); 00066 t2->GetRendererDimensions (tw2, th2); 00067 00068 return ((tw1 >= tw2) && (th1 >= th2)); 00069 } 00070 00071 static bool IsEqual (const csRef<iTextureHandle>& t1, 00072 const KeyType& t2) 00073 { 00074 int tw1, th1; 00075 t1->GetRendererDimensions (tw1, th1); 00076 00077 if ((tw1 == t2.w) && (th1 == t2.h)) return true; 00078 return false; 00079 } 00080 00081 static bool IsLargerEqual (const csRef<iTextureHandle>& t1, 00082 const KeyType& t2) 00083 { 00084 int tw1, th1; 00085 t1->GetRendererDimensions (tw1, th1); 00086 00087 if ((tw1 >= t2.w) && (th1 >= t2.h)) return true; 00088 return false; 00089 } 00090 00091 static bool IsLargerEqual (const KeyType& t1, 00092 const csRef<iTextureHandle>& t2) 00093 { 00094 int tw2, th2; 00095 t2->GetRendererDimensions (tw2, th2); 00096 00097 if ((t1.w >= tw2) && (t1.h >= th2)) return true; 00098 return false; 00099 } 00100 00101 }; 00102 } // namespace Implementation 00103 00115 template< 00116 typename ReuseCondition = CS::Utility::ResourceCache::ReuseConditionAfterTime<csTicks>, 00117 typename PurgeCondition = CS::Utility::ResourceCache::PurgeConditionAfterTime<csTicks> > 00118 class TextureCacheT 00119 { 00120 public: 00122 enum 00123 { 00128 tcacheExactSizeMatch = 1, 00135 tcachePowerOfTwo = 2 00136 }; 00137 00150 TextureCacheT (csImageType imgtype, const char* format, int textureFlags, 00151 const char* texClass, uint options, 00152 const ReuseCondition& reuse = 00153 CS::Utility::ResourceCache::ReuseConditionAfterTime<uint> (), 00154 const PurgeCondition& purge = 00155 CS::Utility::ResourceCache::PurgeConditionAfterTime<uint> (10000)) : g3d (0), 00156 backend (reuse, purge), 00157 imgtype (imgtype), 00158 format (format), 00159 textureFlags (textureFlags & ~(CS_TEXTURE_NPOTS | CS_TEXTURE_SCALE_DOWN | CS_TEXTURE_SCALE_UP)), 00160 texClass (texClass), 00161 options (options) 00162 { 00163 backend.agedPurgeInterval = 5000; 00164 00165 if (options & tcachePowerOfTwo) 00166 options &= ~tcacheExactSizeMatch; 00167 00168 if (options & tcachePowerOfTwo) 00169 this->textureFlags |= CS_TEXTURE_SCALE_UP; 00170 else 00171 this->textureFlags |= CS_TEXTURE_NPOTS; 00172 } 00173 00175 void SetG3D (iGraphics3D* g3d) 00176 { 00177 this->g3d = g3d; 00178 } 00179 00183 void Clear () 00184 { 00185 backend.Clear (); 00186 } 00187 00192 void AdvanceFrame (csTicks currentTime) 00193 { 00194 backend.AdvanceTime (currentTime); 00195 } 00196 00203 iTextureHandle* QueryUnusedTexture (int width, int height, 00204 int& real_w, int& real_h) 00205 { 00206 Implementation::TextureSizeConstraint::KeyType queryKey; 00207 queryKey.w = width; queryKey.h = height; 00208 csRef<iTextureHandle>* tex = backend.Query (queryKey, 00209 (options & tcacheExactSizeMatch)); 00210 00211 if (tex != 0) 00212 { 00213 (*tex)->GetRendererDimensions (real_w, real_h); 00214 return *tex; 00215 } 00216 00217 CS_ASSERT_MSG("SetG3D () not called", g3d); 00218 csRef<iTextureHandle> newTex ( 00219 g3d->GetTextureManager()->CreateTexture ( 00220 width, height, imgtype, format, textureFlags)); 00221 newTex->SetTextureClass (texClass); 00222 00223 backend.AddActive (newTex); 00224 00225 newTex->GetRendererDimensions (real_w, real_h); 00226 return newTex; 00227 } 00228 00233 iTextureHandle* QueryUnusedTexture (int width, int height) 00234 { 00235 int dummyW, dummyH; 00236 return QueryUnusedTexture (width, height, dummyW, dummyH); 00237 } 00238 00240 const char* GetFormat() const { return format; } 00242 int GetFlags() const { return textureFlags; } 00244 const char* GetClass() const { return texClass; } 00245 00250 void SetFormat (const char* format) 00251 { 00252 this->format = format; 00253 Clear(); 00254 } 00259 void SetFlags (int flags) 00260 { 00261 flags &= ~(CS_TEXTURE_NPOTS | CS_TEXTURE_SCALE_DOWN | CS_TEXTURE_SCALE_UP); 00262 00263 if (options & tcachePowerOfTwo) 00264 flags |= CS_TEXTURE_SCALE_UP; 00265 else 00266 flags |= CS_TEXTURE_NPOTS; 00267 00268 textureFlags = flags; 00269 Clear(); 00270 } 00275 void SetClass (const char* texClass) 00276 { 00277 this->texClass = texClass; 00278 Clear(); 00279 } 00280 protected: 00281 csRef<iGraphics3D> g3d; 00282 00283 CS::Utility::GenericResourceCache<csRef<iTextureHandle>, 00284 csTicks, Implementation::TextureSizeConstraint, 00285 ReuseCondition, PurgeCondition> backend; 00286 00287 csImageType imgtype; 00288 csString format; 00289 int textureFlags; 00290 csString texClass; 00291 uint options; 00292 }; 00293 00295 typedef TextureCacheT<> TextureCache; 00296 } // namespace RenderManager 00297 } // namespace CS 00298 00299 #endif // __CS_CSPLUGINCOMMON_RENDERMANAGER_TEXTURECACHE_H__
Generated for Crystal Space 2.1 by doxygen 1.6.1
