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