Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00143 struct CompData
00144 {
00145
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
00162 char specialStr[SpecialStrMax];
00163 };
00164
00165 void FreeSpecialStr ();
00166 public:
00168 StructuredTextureFormat ();
00170 StructuredTextureFormat (char cmp1, int size1,
00171 char cmp2 = 0, int size2 = 0,
00172 char cmp3 = 0, int size3 = 0,
00173 char cmp4 = 0, int size4 = 0,
00174 TextureFormat fmt = Integer);
00176 StructuredTextureFormat (const StructuredTextureFormat& other);
00178 ~StructuredTextureFormat ();
00179
00183 void SetSpecial (const char* special);
00184
00193 bool AddComponent (char cmp, int size);
00194
00200 void SetFormat (TextureFormat format)
00201 {
00202 CS_ASSERT_MSG ("Use SetSpecial() to set special formats", format != Special);
00203 if (format == Special) return;
00204 FreeSpecialStr ();
00205 if (cd.format == Special) cd.coded_components = 0;
00206 cd.format = format;
00207 }
00208
00213 void FixSizes (int size);
00214
00219 csString GetCanonical ();
00220
00221 bool operator== (const StructuredTextureFormat& other) const
00222 {
00223 if (GetFormat() != other.GetFormat()) return false;
00224 if (GetFormat() == Special)
00225 {
00226 const char* s1 = GetSpecial();
00227 const char* s2 = GetSpecial();
00228 if ((s1 == static_cast<const char*>(nullptr)) && (s2 == static_cast<const char*>(nullptr))) return true;
00229 if (s1 == static_cast<const char*>(nullptr)) return false;
00230 if (s1 == static_cast<const char*>(nullptr)) return false;
00231 return strcmp (s1, s2) == 0;
00232 }
00233 else
00234 {
00235 return (cd.coded_components == other.cd.coded_components);
00236 }
00237 }
00238
00239 bool operator!= (const StructuredTextureFormat& other) const
00240 {
00241 if (GetFormat() != other.GetFormat()) return true;
00242 if (GetFormat() == Special)
00243 {
00244 const char* s1 = GetSpecial();
00245 const char* s2 = GetSpecial();
00246 if ((s1 == static_cast<const char*>(nullptr)) && (s2 == static_cast<const char*>(nullptr))) return false;
00247 if (s1 == static_cast<const char*>(nullptr)) return true;
00248 if (s1 == static_cast<const char*>(nullptr)) return true;
00249 return strcmp (s1, s2) != 0;
00250 }
00251 else
00252 {
00253 return (cd.coded_components != other.cd.coded_components);
00254 }
00255 }
00256
00258 bool IsValid () { return cd.format != Invalid; }
00259
00264 int GetComponentCount () const
00265 {
00266 if (((cd.format & ~SpecialStrExtern) == Special)
00267 || (cd.format == Invalid))
00268 return 0;
00269 int n = 0;
00270 uint64 comp = cd.coded_components;
00271 while (comp != 0)
00272 {
00273 comp >>= 16;
00274 n++;
00275 }
00276 return n;
00277 }
00278
00282 char GetComponent (int n) const
00283 {
00284 int num = GetComponentCount ();
00285 if ((n < 0) || (n >= num)) return 0;
00286 return (cd.coded_components >> (16 * (num - 1 - n) + 8)) & 255;
00287 }
00288
00297 char GetComponentSize (int n) const
00298 {
00299 int num = GetComponentCount ();
00300 if ((n < 0) || (n >= num)) return 0;
00301 return (cd.coded_components >> (16 * (num - 1 - n))) & 255;
00302 }
00303
00308 TextureFormat GetFormat() const
00309 { return static_cast<TextureFormat> (cd.format & ~SpecialStrExtern); }
00310
00312 const char* GetSpecial() const
00313 {
00314 if ((cd.format & ~SpecialStrExtern) != Special) return 0;
00315 if (cd.format & SpecialStrExtern)
00316 return cd.specialStrPtr;
00317 else
00318 return specialStr;
00319 }
00320
00325 enum
00326 {
00328 compR = 0x01,
00330 compG = 0x02,
00332 compB = 0x04,
00334 compA = 0x08,
00336 compX = 0x10,
00338 compL = 0x20,
00340 compD = 0x40,
00342 compS = 0x80,
00343
00345 compRGB = compR | compB | compG,
00347 compRGBA = compR | compB | compG | compA,
00349 compLumA = compL | compA,
00351 compDepthStencil = compD | compS,
00352
00354 compUnknown = 0x80000000
00355 };
00356
00375 uint GetComponentMask () const;
00376 };
00377
00381 class CS_CRYSTALSPACE_EXPORT TextureFormatStrings
00382 {
00383 public:
00388 static csString ConvertCanonical (const char* in);
00389
00394 static StructuredTextureFormat ConvertStructured (const char* in);
00395 };
00396 }
00397
00400 #endif // __CS_CSGFX_TEXTUREFORMATSTRINGS_H__
00401