PVRTGlobal.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*!****************************************************************************
  2. @file PVRTGlobal.h
  3. @copyright Copyright (c) Imagination Technologies Limited.
  4. @brief Global defines and typedefs for PVRTools
  5. ******************************************************************************/
  6. #ifndef _PVRTGLOBAL_H_
  7. #define _PVRTGLOBAL_H_
  8. /*!***************************************************************************
  9. Macros
  10. *****************************************************************************/
  11. #define PVRT_MIN(a,b) (((a) < (b)) ? (a) : (b))
  12. #define PVRT_MAX(a,b) (((a) > (b)) ? (a) : (b))
  13. #define PVRT_CLAMP(x, l, h) (PVRT_MIN((h), PVRT_MAX((x), (l))))
  14. // avoid warning about unused parameter
  15. #define PVRT_UNREFERENCED_PARAMETER(x) ((void) x)
  16. #if defined(_WIN32) && !defined(__QT__) && !defined(UNDER_CE) /* Windows desktop */
  17. #if !defined(_CRTDBG_MAP_ALLOC)
  18. #define _CRTDBG_MAP_ALLOC
  19. #endif
  20. #include <windows.h>
  21. #include <crtdbg.h>
  22. #include <tchar.h>
  23. #endif
  24. #if defined(UNDER_CE)
  25. #include <windows.h>
  26. #ifndef _ASSERT
  27. #ifdef _DEBUG
  28. #define _ASSERT(X) { (X) ? 0 : DebugBreak(); }
  29. #else
  30. #define _ASSERT(X)
  31. #endif
  32. #endif
  33. #ifndef _ASSERTE
  34. #ifdef _DEBUG
  35. #define _ASSERTE _ASSERT
  36. #else
  37. #define _ASSERTE(X)
  38. #endif
  39. #endif
  40. #define _RPT0(a,b)
  41. #define _RPT1(a,b,c)
  42. #define _RPT2(a,b,c,d)
  43. #define _RPT3(a,b,c,d,e)
  44. #define _RPT4(a,b,c,d,e,f)
  45. #else
  46. #if defined(_WIN32) && !defined(__QT__)
  47. #else
  48. #if defined(__linux__) || defined(__APPLE__)
  49. #include <assert.h>
  50. #ifdef _DEBUG
  51. #ifndef _RPT0
  52. #define _RPT0(a,b) printf(b)
  53. #endif
  54. #ifndef _RPT1
  55. #define _RPT1(a,b,c) printf(b,c)
  56. #endif
  57. #ifndef _ASSERT
  58. #define _ASSERT(a) assert(a)
  59. #endif
  60. #ifndef _ASSERTE
  61. #define _ASSERTE(a) assert(a)
  62. #endif
  63. #else
  64. #ifndef _RPT0
  65. #define _RPT0(a,b)((void)0)
  66. #endif
  67. #ifndef _RPT1
  68. #define _RPT1(a,b,c)((void)0)
  69. #endif
  70. #ifndef _ASSERT
  71. #define _ASSERT(a)((void)0)
  72. #endif
  73. #ifndef _ASSERTE
  74. #define _ASSERTE(a)((void)0)
  75. #endif
  76. #endif
  77. #ifndef _RPT2
  78. #define _RPT2(a,b,c,d)((void)0)
  79. #endif
  80. #ifndef _RPT3
  81. #define _RPT3(a,b,c,d,e)((void)0)
  82. #endif
  83. #ifndef _RPT4
  84. #define _RPT4(a,b,c,d,e,f)((void)0)
  85. #endif
  86. #include <stdlib.h>
  87. #include <string.h>
  88. #ifndef BYTE
  89. #define BYTE unsigned char
  90. #endif
  91. #ifndef WORD
  92. #define WORD unsigned short
  93. #endif
  94. #ifndef DWORD
  95. #define DWORD unsigned int
  96. #endif
  97. #if !defined(BOOL) && !defined(OBJC_BOOL_DEFINED)
  98. #define BOOL int
  99. #endif
  100. typedef struct tagRGBQUAD
  101. {
  102. BYTE rgbBlue;
  103. BYTE rgbGreen;
  104. BYTE rgbRed;
  105. BYTE rgbReserved;
  106. } RGBQUAD;
  107. #if !defined(TRUE)
  108. #define TRUE 1
  109. #endif
  110. #if !defined(FALSE)
  111. #define FALSE 0
  112. #endif
  113. #else
  114. #define _CRT_WARN 0
  115. #define _RPT0(a,b)
  116. #define _RPT1(a,b,c)
  117. #define _RPT2(a,b,c,d)
  118. #define _RPT3(a,b,c,d,e)
  119. #define _RPT4(a,b,c,d,e,f)
  120. #define _ASSERT(X)
  121. #define _ASSERTE(X)
  122. #endif
  123. #endif
  124. #endif
  125. #include <stdio.h>
  126. #define FREE(X) { if(X) { free(X); (X) = 0; } }
  127. // This macro is used to check at compile time that types are of a certain size
  128. // If the size does not equal the expected size, this typedefs an array of size 0
  129. // which causes a compile error
  130. #define PVRTSIZEASSERT(T, size) typedef int (sizeof_##T)[sizeof(T) == (size)]
  131. #define PVRTCOMPILEASSERT(T, expr) typedef int (assert_##T)[expr]
  132. /****************************************************************************
  133. ** Integer types
  134. ****************************************************************************/
  135. typedef char PVRTchar8;
  136. typedef signed char PVRTint8;
  137. typedef signed short PVRTint16;
  138. typedef signed int PVRTint32;
  139. typedef unsigned char PVRTuint8;
  140. typedef unsigned short PVRTuint16;
  141. typedef unsigned int PVRTuint32;
  142. typedef float PVRTfloat32;
  143. #if (defined(__int64) || defined(_WIN32))
  144. typedef signed __int64 PVRTint64;
  145. typedef unsigned __int64 PVRTuint64;
  146. #elif defined(__GNUC__)
  147. __extension__ typedef signed long long PVRTint64;
  148. __extension__ typedef unsigned long long PVRTuint64;
  149. #else
  150. typedef signed long long PVRTint64;
  151. typedef unsigned long long PVRTuint64;
  152. #endif
  153. #if __SIZEOF_WCHAR_T__ == 4 || __WCHAR_MAX__ > 0x10000
  154. #define PVRTSIZEOFWCHAR 4
  155. #else
  156. #define PVRTSIZEOFWCHAR 2
  157. #endif
  158. PVRTSIZEASSERT(PVRTchar8, 1);
  159. PVRTSIZEASSERT(PVRTint8, 1);
  160. PVRTSIZEASSERT(PVRTuint8, 1);
  161. PVRTSIZEASSERT(PVRTint16, 2);
  162. PVRTSIZEASSERT(PVRTuint16, 2);
  163. PVRTSIZEASSERT(PVRTint32, 4);
  164. PVRTSIZEASSERT(PVRTuint32, 4);
  165. PVRTSIZEASSERT(PVRTint64, 8);
  166. PVRTSIZEASSERT(PVRTuint64, 8);
  167. PVRTSIZEASSERT(PVRTfloat32, 4);
  168. /*!**************************************************************************
  169. @enum ETextureFilter
  170. @brief Enum values for defining texture filtering
  171. ****************************************************************************/
  172. enum ETextureFilter
  173. {
  174. eFilter_Nearest,
  175. eFilter_Linear,
  176. eFilter_None,
  177. eFilter_Size,
  178. eFilter_Default = eFilter_Linear,
  179. eFilter_MipDefault = eFilter_None
  180. };
  181. /*!**************************************************************************
  182. @enum ETextureWrap
  183. @brief Enum values for defining texture wrapping
  184. ****************************************************************************/
  185. enum ETextureWrap
  186. {
  187. eWrap_Clamp,
  188. eWrap_Repeat,
  189. eWrap_Size,
  190. eWrap_Default = eWrap_Repeat
  191. };
  192. /****************************************************************************
  193. ** swap template function
  194. ****************************************************************************/
  195. /*!***************************************************************************
  196. @brief A swap template function that swaps a and b
  197. @param[in] a Type a
  198. @param[in] b Type b
  199. *****************************************************************************/
  200. template <typename T>
  201. inline void PVRTswap(T& a, T& b)
  202. {
  203. T temp = a;
  204. a = b;
  205. b = temp;
  206. }
  207. /*!***************************************************************************
  208. @brief A clamp template function that clamps val between min and max.
  209. @param[in] val Value to clamp
  210. @param[in] min Minimum legal value
  211. @param[in] max Maximum legal value
  212. *****************************************************************************/
  213. template <typename T>
  214. inline T PVRTClamp(const T& val, const T& min, const T& max)
  215. {
  216. if (val > max)
  217. { return max; }
  218. if (val < min)
  219. { return min; }
  220. return val;
  221. }
  222. /*!***************************************************************************
  223. @brief Swaps the endianness of pBytes in place
  224. @param[in] pBytes A number
  225. @param[in] i32ByteNo Number of bytes in pBytes
  226. *****************************************************************************/
  227. inline void PVRTByteSwap(unsigned char* pBytes, int i32ByteNo)
  228. {
  229. int i = 0, j = i32ByteNo - 1;
  230. while (i < j)
  231. { PVRTswap<unsigned char>(pBytes[i++], pBytes[j--]); }
  232. }
  233. /*!***************************************************************************
  234. @brief Converts the endianness of an unsigned int
  235. @param[in] ui32Long A number
  236. @return ui32Long with its endianness changed
  237. *****************************************************************************/
  238. inline unsigned int PVRTByteSwap32(unsigned int ui32Long)
  239. {
  240. return ((ui32Long & 0x000000FF) << 24) + ((ui32Long & 0x0000FF00) << 8) + ((ui32Long & 0x00FF0000) >> 8) + ((ui32Long & 0xFF000000) >> 24);
  241. }
  242. /*!***************************************************************************
  243. @brief Converts the endianness of a unsigned short
  244. @param[in] ui16Short A number
  245. @return ui16Short with its endianness changed
  246. *****************************************************************************/
  247. inline unsigned short PVRTByteSwap16(unsigned short ui16Short)
  248. {
  249. return (ui16Short >> 8) | (ui16Short << 8);
  250. }
  251. /*!***************************************************************************
  252. @brief Returns true if the platform the code is ran on is little endian
  253. @return True if the platform the code is ran on is little endian
  254. *****************************************************************************/
  255. inline bool PVRTIsLittleEndian()
  256. {
  257. static bool bLittleEndian;
  258. static bool bIsInit = false;
  259. if (!bIsInit)
  260. {
  261. short int word = 0x0001;
  262. char* byte = (char*) &word;
  263. bLittleEndian = byte[0] ? true : false;
  264. bIsInit = true;
  265. }
  266. return bLittleEndian;
  267. }
  268. /*!***************************************************************************
  269. @brief Minimum of a, b. In case of tie, a is returned
  270. @return Returns b if a > b, otherwise a
  271. *****************************************************************************/
  272. template<typename T>
  273. inline const T& PVRTMin(const T& a, const T& b)
  274. {
  275. return a > b ? b : a;
  276. }
  277. /*!***************************************************************************
  278. @brief Maximum of a, b. In case of tie, a is returned
  279. @return Returns b if a < b. otherwise a
  280. *****************************************************************************/
  281. template<typename T>
  282. inline const T& PVRTMax(const T& a, const T& b)
  283. {
  284. return a < b ? b : a;
  285. }
  286. #endif // _PVRTGLOBAL_H_
  287. /*****************************************************************************
  288. End of file (Tools.h)
  289. *****************************************************************************/