]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkyUtil.hpp
A first attempt at making the clouds3d endian aware. Almost there.
[simgear.git] / simgear / scene / sky / clouds3d / SkyUtil.hpp
1 //------------------------------------------------------------------------------
2 // File : SkyUtil.hpp
3 //------------------------------------------------------------------------------
4 // SkyWorks : Copyright 2002 Mark J. Harris and
5 //                                              The University of North Carolina at Chapel Hill
6 //------------------------------------------------------------------------------
7 // Permission to use, copy, modify, distribute and sell this software and its 
8 // documentation for any purpose is hereby granted without fee, provided that 
9 // the above copyright notice appear in all copies and that both that copyright 
10 // notice and this permission notice appear in supporting documentation. 
11 // Binaries may be compiled with this software without any royalties or 
12 // restrictions. 
13 //
14 // The author(s) and The University of North Carolina at Chapel Hill make no 
15 // representations about the suitability of this software for any purpose. 
16 // It is provided "as is" without express or 
17 // implied warranty.
18 /**
19  * @file  SkyUtil.hpp
20  * @brief Safe deallocation functions, result codes, trace functions, and macros.
21  */
22 #ifndef __SKYUTIL_HPP__
23 #define __SKYUTIL_HPP__
24
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <math.h>
28
29 //-----------------------------------------------------------------------------
30 // Useful constants
31 //-----------------------------------------------------------------------------
32 //! Pi.
33 const float SKY_PI      = 4.0f * (float) atan(1.0f);
34 //! 1.0 / Pi
35 const float SKY_INV_PI  = 1.0f / SKY_PI;
36 //! 1.0 / (4.0 * Pi)
37 const float SKY_INV_4PI = 1.0f / (4.0f * SKY_PI);
38
39 //-----------------------------------------------------------------------------
40 // Safe deallocation
41 //-----------------------------------------------------------------------------
42 //! Delete and set pointer to NULL.
43 #define SAFE_DELETE(p)       { delete   (p); (p)=NULL; }
44 //! Delete an array and set pointer to NULL.
45 #define SAFE_DELETE_ARRAY(p) { delete[] (p); (p)=NULL; }
46 //#define SAFE_RELEASE(p)      { (p) = NULL; } 
47 //{ if(p) {  (p)->Release(); (p)=NULL; } }
48
49 //------------------------------------------------------------------------------
50 // Useful Macros
51 //------------------------------------------------------------------------------
52 //! Convert Degrees to Radians
53 #define SKYDEGREESTORADS 0.01745329252f
54 //! Convert Radians to Degrees
55 #define SKYRADSTODEGREES 57.2957795131f
56
57 //------------------------------------------------------------------------------
58 // Function               : SkyGetLogBaseTwo
59 // Description      : 
60 //------------------------------------------------------------------------------
61 /**
62  * @fn SkyGetLogBaseTwo(int iNum)
63  * @brief Returns the integer base two logarithm of the integer input.
64  */ 
65 inline int SkyGetLogBaseTwo(int iNum)
66 {
67   int i, n;
68   for(i = iNum-1, n = 0; i > 0; i >>= 1, n++ );
69   return n;
70 }
71
72
73 //------------------------------------------------------------------------------
74 // Function               : SkyTrace
75 // Description      : 
76 //------------------------------------------------------------------------------
77 void SkyTrace( char* strMsg, ... );
78
79
80 //.----------------------------------------------------------------------------.
81 //|   Result Codes                                                             |
82 //.----------------------------------------------------------------------------.
83 //! SKYRESULTs are used for returning error information that can be used to trace bugs.
84 typedef int SKYRESULT;
85
86 //! Returns true if the SKYRESULT is a success result.
87 #define SKYSUCCEEDED(Status) ((SKYRESULT)(Status) >= 0)
88 //! Returns true if the SKYRESULT is a failure result.
89 #define SKYFAILED(Status) ((SKYRESULT)(Status) < 0)
90
91 //! SKYRESULTs are used for returning error information that can be used to trace bugs.
92 enum SKYRESULT_CODES
93 {
94     // SUCCESS CODES: non-negative
95     SKYRESULT_OK = 1,
96     // FAILURE CODES: negative
97     SKYRESULT_FAIL = -1
98 };
99
100
101 //-----------------------------------------------------------------------------
102 // FAIL_RETURN
103 //-----------------------------------------------------------------------------
104 // Print debug messages to the WIN32 debug window 
105 //-----------------------------------------------------------------------------
106
107 //------------------------------------------------------------------------------
108 // Function               : FAIL_RETURN
109 // Description      : 
110 //------------------------------------------------------------------------------
111 /**
112  * @fn FAIL_RETURN(p)
113  * @brief Prints a trace message if @a p failed, and returns the failure code.
114  *
115  * Outputs in a format that can be double-clicked in DevStudio to open the 
116  * appropriate file and location.
117  */ 
118 #if defined(DEBUG) | defined(_DEBUG)
119         #define FAIL_RETURN(p) \
120         { \
121                 SKYRESULT __SKYUTIL__result__; \
122                         if ( SKYFAILED( __SKYUTIL__result__ = (p) ) ) { \
123                                 fprintf(stderr, "!!!! FAIL_RETURN TRAP !!!! %s: %d: %d\n",__FILE__, __LINE__, __SKYUTIL__result__); \
124                                 return __SKYUTIL__result__; \
125                         } \
126         }
127 #else
128         #define FAIL_RETURN(p) p
129 #endif
130
131
132 //------------------------------------------------------------------------------
133 // Function               : FAIL_RETURN_MSG
134 // Description      : 
135 //------------------------------------------------------------------------------
136 /**
137  * @fn FAIL_RETURN_MSG(p,str)
138  * @brief Similar to FAIL_RETURN, but also appends a user-supplied message.
139  * 
140  * @see FAIL_RETURN, FAIL_RETURN_MSG
141  */ 
142 #if defined(DEBUG) | defined(_DEBUG)
143         #define FAIL_RETURN_MSG(p,str) \
144         { \
145                 SKYRESULT __SKYUTIL__result__; \
146                         if ( SKYFAILED( __SKYUTIL__result__ = (p) ) ) { \
147                                 fprintf(stderr, "!!!! FAIL_RETURN_MSG TRAP !!!! %s: %d: %d: %s\n",__FILE__,__LINE__,__SKYUTIL__result__,str); \
148                                 return __SKYUTIL__result__; \
149                         } \
150         }
151 #else
152         #define FAIL_RETURN_MSG(p,str) p
153 #endif
154
155
156 //------------------------------------------------------------------------------
157 // Function               : FAIL_RETURN_MSGBOX
158 // Description      : 
159 //------------------------------------------------------------------------------
160 /**
161  * @fn FAIL_RETURN_MSGBOX(p,str)
162  * @brief Similar to FAIL_RETURN_MSG, but also displays the error in a message box (in Windows).
163  * 
164  * @see FAIL_RETURN_MSG, FAIL_RETURN
165  */ 
166 #if defined(DEBUG) | defined(_DEBUG)
167 #ifdef USEWINDOWSOUTPUT
168         #define FAIL_RETURN_MSGBOX(p,str) \
169         { \
170                 SKYRESULT __SKYUTIL__result__; \
171                         if ( SKYFAILED( __SKYUTIL__result__ = (p) ) ) { \
172                 char msg[512]; \
173                 sprintf(msg, "%s: %d: %d: %s\n",__FILE__,__LINE__,__SKYUTIL__result__,str); \
174                 MessageBox(NULL, msg, "!!!! FAIL_RETURN_MSG TRAP !!!!", MB_OK); \
175                                 return __SKYUTIL__result__; \
176                         } \
177         }
178 #else
179     #define FAIL_RETURN_MSGBOX(p,str) \
180         { \
181                 SKYRESULT __SKYUTIL__result__; \
182                         if ( SKYFAILED( __SKYUTIL__result__ = (p) ) ) { \
183                 fprintf(stderr, "!!!! FAIL_RETURN_MSG TRAP !!!! %s: %d: %d: %s\n",__FILE__,__LINE__,__D3DUTIL__hres__,str); \
184                                 return __SKYUTIL__result__; \
185                         } \
186         }
187 #endif
188 #else
189         #define FAIL_RETURN_MSGBOX(p,str) p
190 #endif
191
192 #endif //__SKYUTIL_HPP__