1 /** \file lvtypes.h
2     \brief CREngine common types definition
3 
4     (c) Vadim Lopatin, 2000-2006
5     This source code is distributed under the terms of
6     GNU General Public License.
7     See LICENSE file for details.
8 */
9 
10 #ifndef LVTYPES_H_INCLUDED
11 #define LVTYPES_H_INCLUDED
12 
13 #include <stdlib.h>
14 #include "crsetup.h"
15 
16 #ifdef _WIN32
17 typedef long lInt32;            ///< signed 32 bit int
18 typedef unsigned long lUInt32;  ///< unsigned 32 bit int
19 #else
20 typedef int lInt32;            ///< signed 32 bit int
21 typedef unsigned int lUInt32;  ///< unsigned 32 bit int
22 #endif
23 
24 typedef short int lInt16;           ///< signed 16 bit int
25 typedef unsigned short int lUInt16; ///< unsigned 16 bit int
26 
27 typedef signed char lInt8;          ///< signed 8 bit int
28 typedef unsigned char lUInt8;       ///< unsigned 8 bit int
29 
30 typedef char32_t lChar32;           ///< 32 bit char
31 typedef char lChar8;                ///< 8 bit char
32 
33 #ifdef _WIN32
34 typedef wchar_t lChar16;            ///< 16 bit char, only for Windows
35 #else
36 typedef char16_t lChar16;           ///< 16 bit char
37 #endif
38 
39 #if defined(_WIN32) && !defined(CYGWIN)
40 typedef __int64 lInt64;             ///< signed 64 bit int
41 typedef unsigned __int64 lUInt64;   ///< unsigned 64 bit int
42 #else
43 typedef long long int lInt64;       ///< signed 64 bit int
44 typedef unsigned long long int lUInt64; ///< unsigned 64 bit int
45 #endif
46 
47 /// platform-dependent path separator
48 #if defined(_WIN32) && !defined(__WINE__)
49 #define PATH_SEPARATOR_CHAR '\\'
50 #elif __SYMBIAN32__
51 #define PATH_SEPARATOR_CHAR '\\'
52 #else
53 #define PATH_SEPARATOR_CHAR '/'
54 #endif
55 
56 /// point
57 class lvPoint {
58 public:
59     int x;
60     int y;
lvPoint()61     lvPoint() : x(0), y(0) { }
lvPoint(int nx,int ny)62     lvPoint(int nx, int ny) : x(nx), y(ny) { }
lvPoint(const lvPoint & v)63     lvPoint( const lvPoint & v ) : x(v.x), y(v.y) { }
64     lvPoint & operator = ( const lvPoint & v ) { x = v.x; y = v.y; return *this; }
65 };
66 
67 /// rectangle
68 class lvRect {
69 public:
70     int left;
71     int top;
72     int right;
73     int bottom;
74     /// returns true if rectangle is empty
isEmpty()75     bool isEmpty() const { return left>=right || bottom<=top; }
lvRect()76     lvRect() : left(0), top(0), right(0), bottom(0) { }
lvRect(int x0,int y0,int x1,int y1)77     lvRect( int x0, int y0, int x1, int y1) : left(x0), top(y0), right(x1), bottom(y1) { }
topLeft()78     lvPoint topLeft() const { return lvPoint( left, top ); }
bottomRight()79     lvPoint bottomRight() const { return lvPoint( right, bottom ); }
setTopLeft(const lvPoint & pt)80     void setTopLeft( const lvPoint & pt ) { top=pt.y; left=pt.x; }
setBottomRight(const lvPoint & pt)81     void setBottomRight( const lvPoint & pt ) { bottom=pt.y; right=pt.x; }
82     /// returns true if rectangles are equal
83     bool operator ==( const lvRect & rc ) const
84     {
85         return rc.left == left && rc.right == right && rc.top == top && rc.bottom == bottom;
86     }
87     /// returns true if rectangles are not equal
88     bool operator !=( const lvRect & rc ) const
89     {
90         return !(rc.left == left && rc.right == right && rc.top == top && rc.bottom == bottom);
91     }
92     /// returns non-NULL pointer to trimming values for 4 sides of rc, if clipping is necessary
clipBy(lvRect & cliprc)93     lvRect * clipBy(lvRect & cliprc) {
94     	if (intersects(cliprc) && !cliprc.isRectInside(*this)) {
95     		lvRect * res = new lvRect();
96     		if (cliprc.left > left)
97     			res->left = cliprc.left - left;
98     		if (cliprc.top > top)
99     			res->top = cliprc.top - top;
100     		if (right > cliprc.right)
101     			res->right = right - cliprc.right;
102     		if (bottom > cliprc.bottom)
103     			res->bottom = bottom - cliprc.bottom;
104     		return res;
105     	} else {
106     		return NULL;
107     	}
108     }
109 
110 
111 
112     /// returns rectangle width
width()113     int width() const { return right - left; }
114     /// returns rectangle height
height()115     int height() const { return bottom - top; }
minDimension()116     int minDimension() { return (right - left < bottom - top) ? right - left : bottom - top; }
size()117     lvPoint size() const { return lvPoint(right-left, bottom - top); }
shrink(int delta)118     void shrink( int delta ) { left+=delta; right-=delta; top+=delta; bottom-=delta; }
shrinkBy(const lvRect & rc)119     void shrinkBy( const lvRect & rc ) { left+=rc.left; right-=rc.right; top+=rc.top; bottom-=rc.bottom; }
extend(int delta)120     void extend( int delta ) { shrink(-delta); }
extendBy(const lvRect & rc)121     void extendBy( const lvRect & rc ) { left-=rc.left; right+=rc.right; top-=rc.top; bottom+=rc.bottom; }
122     /// makes this rect to cover both this and specified rect (bounding box for two rectangles)
extend(lvRect rc)123     void extend( lvRect rc )
124     {
125         if ( rc.isEmpty() )
126             return;
127         if ( isEmpty() ) {
128             left = rc.left;
129             top = rc.top;
130             right = rc.right;
131             bottom = rc.bottom;
132             return;
133         }
134         if ( left > rc.left )
135             left = rc.left;
136         if ( top > rc.top )
137             top = rc.top;
138         if ( right < rc.right )
139             right = rc.right;
140         if ( bottom < rc.bottom )
141             bottom = rc.bottom;
142     }
143     /// returns true if specified rectangle is fully covered by this rectangle
isRectInside(lvRect rc)144     bool isRectInside( lvRect rc ) const
145     {
146         // This was wrong: a 0-height or 0-width rect can be inside another rect
147         // if ( rc.isEmpty() || isEmpty() )
148         //    return false;
149         if ( rc.left < left || rc.right > right || rc.top < top || rc.bottom > bottom )
150             return false;
151         return true;
152     }
153 
154     /// returns true if specified rectangle has common part with this rectangle
intersects(const lvRect & rc)155     bool intersects(const lvRect & rc) const
156     {
157         if ( rc.isEmpty() || isEmpty() )
158             return false;
159         if ( rc.right <= left || rc.left >= right || rc.bottom <= top || rc.top >= bottom )
160             return false;
161         return true;
162     }
163 
164     /// returns true if point is inside this rectangle
isPointInside(const lvPoint & pt)165     bool isPointInside ( const lvPoint & pt ) const
166     {
167         return left<=pt.x && top<=pt.y && right>pt.x && bottom > pt.y;
168     }
clear()169 	void clear() { left=right=top=bottom=0; }
170 
intersect(const lvRect & rc)171     bool intersect(const lvRect &rc)
172 	{
173         if (left < rc.left)
174             left = rc.left;
175         if (right > rc.right)
176             right = rc.right;
177         if (top < rc.top)
178             top = rc.top;
179         if (bottom > rc.bottom)
180             bottom = rc.bottom;
181         bool ret = !isEmpty();
182 		if (!ret)
183 			clear();
184 		return ret;
185 	}
186 };
187 
188 class lvColor
189 {
190     lUInt32 value;
191 public:
lvColor(lUInt32 cl)192     lvColor( lUInt32 cl ) : value(cl) { }
lvColor(lUInt32 r,lUInt32 g,lUInt32 b)193     lvColor(  lUInt32 r, lUInt32 g, lUInt32 b ) : value(((r&255)<<16) | ((g&255)<<8) | (b&255)) { }
lvColor(lUInt32 r,lUInt32 g,lUInt32 b,lUInt32 a)194     lvColor( lUInt32 r, lUInt32 g, lUInt32 b, lUInt32 a ) : value(((a&255)<<24) | ((r&255)<<16) | ((g&255)<<8) | (b&255)) { }
lUInt32()195     operator lUInt32 () const { return value; }
get()196     lUInt32 get() const { return value; }
r()197     lUInt8 r() const { return (lUInt8)(value>>16)&255; }
g()198     lUInt8 g() const { return (lUInt8)(value>>8)&255; }
b()199     lUInt8 b() const { return (lUInt8)(value)&255; }
a()200     lUInt8 a() const { return (lUInt8)(value>>24)&255; }
201 };
202 
203 /// byte order convertor
204 class lvByteOrderConv {
205     bool _lsf;
206 public:
lvByteOrderConv()207     lvByteOrderConv()
208     {
209         union {
210             lUInt16 word;
211             lUInt8 bytes[2];
212         } test;
213         test.word = 1;
214         _lsf = test.bytes[0]!=0;
215     }
216     /// reverse 32 bit word
rev(lUInt32 w)217     inline static lUInt32 rev( lUInt32 w )
218     {
219         return
220             ((w&0xFF000000)>>24)|
221             ((w&0x00FF0000)>>8) |
222             ((w&0x0000FF00)<<8) |
223             ((w&0x000000FF)<<24);
224     }
225     /// reverse 16bit word
rev(lUInt16 w)226     inline static lUInt16 rev( lUInt16 w )
227     {
228         return
229             (lUInt16)(
230             ((w&0xFF00)>>8)|
231             ((w&0x00FF)<<8) );
232     }
233     /// make 32 bit word least-significant-first byte order (Intel)
lsf(lUInt32 w)234     lUInt32 lsf( lUInt32 w )
235     {
236         return ( _lsf ) ? w : rev(w);
237     }
238     /// make 32 bit word most-significant-first byte order (PPC)
msf(lUInt32 w)239     lUInt32 msf( lUInt32 w )
240     {
241         return ( !_lsf ) ? w : rev(w);
242     }
243     /// make 16 bit word least-significant-first byte order (Intel)
lsf(lUInt16 w)244     lUInt16 lsf( lUInt16 w )
245     {
246         return ( _lsf ) ? w : rev(w);
247     }
248     /// make 16 bit word most-significant-first byte order (PPC)
msf(lUInt16 w)249     lUInt16 msf( lUInt16 w )
250     {
251         return ( !_lsf ) ? w : rev(w);
252     }
rev(lUInt32 * w)253     void rev( lUInt32 * w )
254     {
255         *w = rev(*w);
256     }
rev(lUInt16 * w)257     void rev( lUInt16 * w )
258     {
259         *w = rev(*w);
260     }
msf(lUInt32 * w)261     void msf( lUInt32 * w )
262     {
263         if ( _lsf )
264             *w = rev(*w);
265     }
lsf(lUInt32 * w)266     void lsf( lUInt32 * w )
267     {
268         if ( !_lsf )
269             *w = rev(*w);
270     }
msf(lUInt32 * w,int len)271     void msf( lUInt32 * w, int len )
272     {
273         if ( _lsf ) {
274             for ( int i=0; i<len; i++)
275                 w[i] = rev(w[i]);
276         }
277     }
lsf(lUInt32 * w,int len)278     void lsf( lUInt32 * w, int len )
279     {
280         if ( !_lsf ) {
281             for ( int i=0; i<len; i++)
282                 w[i] = rev(w[i]);
283         }
284     }
msf(lUInt16 * w,int len)285     void msf( lUInt16 * w, int len )
286     {
287         if ( _lsf ) {
288             for ( int i=0; i<len; i++)
289                 w[i] = rev(w[i]);
290         }
291     }
lsf(lUInt16 * w,int len)292     void lsf( lUInt16 * w, int len )
293     {
294         if ( !_lsf ) {
295             for ( int i=0; i<len; i++)
296                 w[i] = rev(w[i]);
297         }
298     }
msf(lUInt16 * w)299     void msf( lUInt16 * w )
300     {
301         if ( _lsf )
302             *w = rev(*w);
303     }
lsf(lUInt16 * w)304     void lsf( lUInt16 * w )
305     {
306         if ( !_lsf )
307             *w = rev(*w);
308     }
lsf()309     bool lsf()
310     {
311         return (_lsf);
312     }
msf()313     bool msf()
314     {
315         return (!_lsf);
316     }
317 };
318 
319 // MACROS to avoid UNUSED PARAM warning
320 #define CR_UNUSED(x) (void)x;
321 #define CR_UNUSED2(x,x2) (void)x;(void)x2;
322 #define CR_UNUSED3(x,x2,x3) (void)x;(void)x2;(void)x3;
323 #define CR_UNUSED4(x,x2,x3,x4) (void)x;(void)x2;(void)x3;(void)x4;
324 #define CR_UNUSED5(x,x2,x3,x4,x5) (void)x;(void)x2;(void)x3;(void)x4;(void)x5;
325 #define CR_UNUSED6(x,x2,x3,x4,x5,x6) (void)x;(void)x2;(void)x3;(void)x4;(void)x5;(void)x6;
326 #define CR_UNUSED7(x,x2,x3,x4,x5,x6,x7) (void)x;(void)x2;(void)x3;(void)x4;(void)x5;(void)x6;(void)x7;
327 #define CR_UNUSED8(x,x2,x3,x4,x5,x6,x7,x8) (void)x;(void)x2;(void)x3;(void)x4;(void)x5;(void)x6;(void)x7;(void)x8;
328 #define CR_UNUSED9(x,x2,x3,x4,x5,x6,x7,x8,x9) (void)x;(void)x2;(void)x3;(void)x4;(void)x5;(void)x6;(void)x7;(void)x8;(void)x9;
329 #define CR_UNUSED10(x,x2,x3,x4,x5,x6,x7,x8,x9,x10) (void)x;(void)x2;(void)x3;(void)x4;(void)x5;(void)x6;(void)x7;(void)x8;(void)x9;(void)x10;
330 #define CR_UNUSED11(x,x2,x3,x4,x5,x6,x7,x8,x9,x11) (void)x;(void)x2;(void)x3;(void)x4;(void)x5;(void)x6;(void)x7;(void)x8;(void)x9;(void)x10;(void)x11;
331 
332 #endif//LVTYPES_H_INCLUDED
333