1 //------------------------------------------------------------------------------
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
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
20 * @brief Safe deallocation functions, result codes, trace functions, and macros.
22 #ifndef __SKYUTIL_HPP__
23 #define __SKYUTIL_HPP__
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 const float SKY_PI = 4.0f * (float) atan(1.0f);
35 const float SKY_INV_PI = 1.0f / SKY_PI;
37 const float SKY_INV_4PI = 1.0f / (4.0f * SKY_PI);
39 //-----------------------------------------------------------------------------
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; } }
49 //------------------------------------------------------------------------------
51 //------------------------------------------------------------------------------
52 //! Convert Degrees to Radians
53 #define SKYDEGREESTORADS 0.01745329252f
54 //! Convert Radians to Degrees
55 #define SKYRADSTODEGREES 57.2957795131f
57 //------------------------------------------------------------------------------
58 // Function : SkyGetLogBaseTwo
60 //------------------------------------------------------------------------------
62 * @fn SkyGetLogBaseTwo(int iNum)
63 * @brief Returns the integer base two logarithm of the integer input.
65 inline int SkyGetLogBaseTwo(int iNum)
68 for(i = iNum-1, n = 0; i > 0; i >>= 1, n++ );
73 //------------------------------------------------------------------------------
74 // Function : SkyTrace
76 //------------------------------------------------------------------------------
77 void SkyTrace( char* strMsg, ... );
80 //.----------------------------------------------------------------------------.
82 //.----------------------------------------------------------------------------.
83 //! SKYRESULTs are used for returning error information that can be used to trace bugs.
84 typedef int SKYRESULT;
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)
91 //! SKYRESULTs are used for returning error information that can be used to trace bugs.
94 // SUCCESS CODES: non-negative
96 // FAILURE CODES: negative
101 //-----------------------------------------------------------------------------
103 //-----------------------------------------------------------------------------
104 // Print debug messages to the WIN32 debug window
105 //-----------------------------------------------------------------------------
107 //------------------------------------------------------------------------------
108 // Function : FAIL_RETURN
110 //------------------------------------------------------------------------------
113 * @brief Prints a trace message if @a p failed, and returns the failure code.
115 * Outputs in a format that can be double-clicked in DevStudio to open the
116 * appropriate file and location.
118 #if defined(DEBUG) | defined(_DEBUG)
119 #define FAIL_RETURN(p) \
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__; \
128 #define FAIL_RETURN(p) p
132 //------------------------------------------------------------------------------
133 // Function : FAIL_RETURN_MSG
135 //------------------------------------------------------------------------------
137 * @fn FAIL_RETURN_MSG(p,str)
138 * @brief Similar to FAIL_RETURN, but also appends a user-supplied message.
140 * @see FAIL_RETURN, FAIL_RETURN_MSG
142 #if defined(DEBUG) | defined(_DEBUG)
143 #define FAIL_RETURN_MSG(p,str) \
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__; \
152 #define FAIL_RETURN_MSG(p,str) p
156 //------------------------------------------------------------------------------
157 // Function : FAIL_RETURN_MSGBOX
159 //------------------------------------------------------------------------------
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).
164 * @see FAIL_RETURN_MSG, FAIL_RETURN
166 #if defined(DEBUG) | defined(_DEBUG)
167 #ifdef USEWINDOWSOUTPUT
168 #define FAIL_RETURN_MSGBOX(p,str) \
170 SKYRESULT __SKYUTIL__result__; \
171 if ( SKYFAILED( __SKYUTIL__result__ = (p) ) ) { \
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__; \
179 #define FAIL_RETURN_MSGBOX(p,str) \
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__; \
189 #define FAIL_RETURN_MSGBOX(p,str) p
192 #endif //__SKYUTIL_HPP__