csgfx/textureformatstrings.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2006 by Jorrit Tyberghein 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_CSGFX_TEXTUREFORMATSTRINGS_H__ 00020 #define __CS_CSGFX_TEXTUREFORMATSTRINGS_H__ 00021 00022 #include "csextern.h" 00023 00024 #include "csutil/csstring.h" 00025 00030 namespace CS 00031 { 00126 class CS_CRYSTALSPACE_EXPORT StructuredTextureFormat 00127 { 00128 public: 00130 enum TextureFormat 00131 { 00133 Invalid = '-', 00135 Integer = 'i', 00137 Float = 'f', 00139 Special = '*' 00140 }; 00141 private: 00142 // Component data 00143 struct CompData 00144 { 00145 // Must be first entry. Will be '*' in case of special formats... 00146 unsigned char format; 00147 union 00148 { 00149 uint64 coded_components; 00150 char* specialStrPtr; 00151 }; 00152 }; 00153 enum 00154 { 00155 SpecialStrExtern = 0x80, 00156 SpecialStrMax = sizeof (CompData) 00157 }; 00158 union 00159 { 00160 CompData cd; 00161 // Store 'small' special formats inline. 00162 char specialStr[SpecialStrMax]; 00163 }; 00164 00165 void FreeSpecialStr (); 00166 public: 00168 StructuredTextureFormat () 00169 { 00170 cd.coded_components = CONST_UINT64 (0); 00171 cd.format = Invalid; 00172 } 00174 StructuredTextureFormat (TextureFormat fmt) 00175 { 00176 cd.coded_components = CONST_UINT64 (0); 00177 cd.format = fmt; 00178 } 00180 00181 StructuredTextureFormat (char cmp1, int size1, 00182 TextureFormat fmt = Integer) 00183 { 00184 cd.coded_components = CONST_UINT64 (0); 00185 cd.format = fmt; 00186 if (cmp1 != 0) AddComponent (cmp1, size1); 00187 } 00188 StructuredTextureFormat (char cmp1, int size1, 00189 char cmp2, int size2, 00190 TextureFormat fmt = Integer) 00191 { 00192 cd.coded_components = CONST_UINT64 (0); 00193 cd.format = fmt; 00194 if (cmp1 != 0) AddComponent (cmp1, size1); 00195 if (cmp2 != 0) AddComponent (cmp2, size2); 00196 } 00197 StructuredTextureFormat (char cmp1, int size1, 00198 char cmp2, int size2, 00199 char cmp3, int size3, 00200 TextureFormat fmt = Integer) 00201 { 00202 cd.coded_components = CONST_UINT64 (0); 00203 cd.format = fmt; 00204 if (cmp1 != 0) AddComponent (cmp1, size1); 00205 if (cmp2 != 0) AddComponent (cmp2, size2); 00206 if (cmp3 != 0) AddComponent (cmp3, size3); 00207 } 00208 StructuredTextureFormat (char cmp1, int size1, 00209 char cmp2, int size2, 00210 char cmp3, int size3, 00211 char cmp4, int size4, 00212 TextureFormat fmt = Integer) 00213 { 00214 cd.coded_components = CONST_UINT64 (0); 00215 cd.format = fmt; 00216 if (cmp1 != 0) AddComponent (cmp1, size1); 00217 if (cmp2 != 0) AddComponent (cmp2, size2); 00218 if (cmp3 != 0) AddComponent (cmp3, size3); 00219 if (cmp4 != 0) AddComponent (cmp4, size4); 00220 } 00222 00223 StructuredTextureFormat (const StructuredTextureFormat& other); 00225 ~StructuredTextureFormat (); 00226 00230 void SetSpecial (const char* special); 00231 00240 bool AddComponent (char cmp, int size) 00241 { 00242 if (GetFormat() == Special) return false; 00243 uint64 shifted = cd.coded_components << 16; 00244 if ((shifted >> 16) != cd.coded_components) 00245 return false; 00246 cd.coded_components = shifted + (CONST_UINT64 (256) * cmp) + size; 00247 return true; 00248 } 00249 00255 void SetFormat (TextureFormat format) 00256 { 00257 CS_ASSERT_MSG ("Use SetSpecial() to set special formats", format != Special); 00258 if (format == Special) return; 00259 FreeSpecialStr (); 00260 if (cd.format == Special) cd.coded_components = 0; 00261 cd.format = format; 00262 } 00263 00268 void FixSizes (int size); 00273 void FixSizes (); 00274 00279 csString GetCanonical () const; 00280 00281 bool operator== (const StructuredTextureFormat& other) const 00282 { 00283 if (GetFormat() != other.GetFormat()) return false; 00284 if (GetFormat() == Special) 00285 { 00286 const char* s1 = GetSpecial(); 00287 const char* s2 = GetSpecial(); 00288 if ((s1 == static_cast<const char*>(nullptr)) && (s2 == static_cast<const char*>(nullptr))) return true; 00289 if (s1 == static_cast<const char*>(nullptr)) return false; 00290 if (s1 == static_cast<const char*>(nullptr)) return false; 00291 return strcmp (s1, s2) == 0; 00292 } 00293 else 00294 { 00295 return (cd.coded_components == other.cd.coded_components); 00296 } 00297 } 00298 00299 bool operator!= (const StructuredTextureFormat& other) const 00300 { 00301 if (GetFormat() != other.GetFormat()) return true; 00302 if (GetFormat() == Special) 00303 { 00304 const char* s1 = GetSpecial(); 00305 const char* s2 = GetSpecial(); 00306 if ((s1 == static_cast<const char*>(nullptr)) && (s2 == static_cast<const char*>(nullptr))) return false; 00307 if (s1 == static_cast<const char*>(nullptr)) return true; 00308 if (s1 == static_cast<const char*>(nullptr)) return true; 00309 return strcmp (s1, s2) != 0; 00310 } 00311 else 00312 { 00313 return (cd.coded_components != other.cd.coded_components); 00314 } 00315 } 00316 00318 bool IsValid () const { return cd.format != Invalid; } 00319 00324 int GetComponentCount () const 00325 { 00326 if (((cd.format & ~SpecialStrExtern) == Special) 00327 || (cd.format == Invalid)) 00328 return 0; 00329 int n = 0; 00330 uint64 comp = cd.coded_components; 00331 while (comp != 0) 00332 { 00333 comp >>= 16; 00334 n++; 00335 } 00336 return n; 00337 } 00338 00342 char GetComponent (int n) const 00343 { 00344 int num = GetComponentCount (); 00345 if ((n < 0) || (n >= num)) return 0; 00346 return (cd.coded_components >> (16 * (num - 1 - n) + 8)) & 255; 00347 } 00348 00357 char GetComponentSize (int n) const 00358 { 00359 int num = GetComponentCount (); 00360 if ((n < 0) || (n >= num)) return 0; 00361 return (cd.coded_components >> (16 * (num - 1 - n))) & 255; 00362 } 00363 00368 TextureFormat GetFormat() const 00369 { return static_cast<TextureFormat> (cd.format & ~SpecialStrExtern); } 00370 00372 const char* GetSpecial() const 00373 { 00374 if ((cd.format & ~SpecialStrExtern) != Special) return 0; 00375 if (cd.format & SpecialStrExtern) 00376 return cd.specialStrPtr; 00377 else 00378 return specialStr; 00379 } 00380 00385 enum 00386 { 00388 compR = 0x01, 00390 compG = 0x02, 00392 compB = 0x04, 00394 compA = 0x08, 00396 compX = 0x10, 00398 compL = 0x20, 00400 compD = 0x40, 00402 compS = 0x80, 00403 00405 compRGB = compR | compB | compG, 00407 compRGBA = compR | compB | compG | compA, 00409 compLumA = compL | compA, 00411 compDepthStencil = compD | compS, 00412 00414 compUnknown = 0x80000000 00415 }; 00416 00435 uint GetComponentMask () const; 00436 }; 00437 00441 class CS_CRYSTALSPACE_EXPORT TextureFormatStrings 00442 { 00443 public: 00448 static csString ConvertCanonical (const char* in); 00449 00454 static StructuredTextureFormat ConvertStructured (const char* in); 00455 }; 00456 } 00457 00460 #endif // __CS_CSGFX_TEXTUREFORMATSTRINGS_H__ 00461
Generated for Crystal Space 2.1 by doxygen 1.6.1
