1 /* bzflag
2 * Copyright (c) 1993-2021 Tim Riker
3 *
4 * This package is free software; you can redistribute it and/or
5 * modify it under the terms of the license found in the file
6 * named COPYING that should have accompanied this file.
7 *
8 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 */
12
13 /*
14 * common definitions
15 */
16
17 #ifndef BZF_COMMON_H
18 #define BZF_COMMON_H
19
20 /* this should always be the very FIRST header */
21 #include "config.h"
22
23 #ifdef _WIN32
24 # undef NOMINMAX
25 # define NOMINMAX 1
26 # include "win32.h"
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h> /* needed for bzfrand */
31 #include <math.h>
32 #ifdef __cplusplus
33 # include <cmath>
34 #endif
35
36
37 extern int debugLevel;
38
39
40 /* Provide a means to conveniently test the version of the GNU
41 * compiler. Use it like this:
42 *
43 * #if GCC_PREREQ(2,8)
44 * ... code requiring gcc 2.8 or later ...
45 * #endif
46 *
47 * WARNING: THIS MACRO IS CONSIDERED PRIVATE AND SHOULD NOT BE USED
48 * OUTSIDE OF THIS HEADER FILE. DO NOT RELY ON IT.
49 */
50 #ifndef GCC_PREREQ
51 # if defined __GNUC__
52 # define GCC_PREREQ(major, minor) __GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))
53 # else
54 # define GCC_PREREQ(major, minor) 0
55 # endif
56 #else
57 # warning "GCC_PREREQ is already defined. See the common.h header."
58 #endif
59
60
61 /**
62 * UNUSED provides a common mechanism for declaring unused parameters.
63 * Use it like this:
64 *
65 * int
66 * my_function(int argc, char **UNUSED(argv))
67 * {
68 * ...
69 * }
70 *
71 */
72 #ifndef UNUSED
73 # if GCC_PREREQ(2, 5)
74 /* GCC-style */
75 # define UNUSED(parameter) (parameter) __attribute__((unused))
76 # else
77 /* MSVC/C++ */
78 # ifdef __cplusplus
79 # if defined(NDEBUG)
80 # define UNUSED(parameter) /* parameter */
81 # else /* some of them are asserted */
82 # define UNUSED(parameter) (parameter)
83 # endif
84 # else
85 # define UNUSED(parameter) (parameter)
86 # endif
87 # endif
88 #else
89 # undef UNUSED
90 # define UNUSED(parameter) (parameter)
91 # warning "UNUSED was previously defined. Parameter declaration behavior is unknown, see common.h"
92 #endif
93
94
95 /* near zero by some epsilon convenience define since relying on
96 * the floating point unit for proper equivalence is not safe
97 */
98 #define NEAR_ZERO(_value,_epsilon) ( ((_value) > -_epsilon) && ((_value) < _epsilon) )
99
100 /* (radians <--> degrees) conversion values */
101 #define DEG2RAD 0.0174532925199432957692369076848861271344287189
102 #define RAD2DEG 57.29577951308232087679815481410517033240547247
103 #define DEG2RADf ((float)DEG2RAD)
104 #define RAD2DEGf ((float)RAD2DEG)
105
106
107 /* seven places of precision is pretty safe, so something less precise */
108 #ifdef FLT_EPSILON
109 # define ZERO_TOLERANCE FLT_EPSILON
110 #else
111 # define ZERO_TOLERANCE 1.0e-06f
112 #endif
113
114 /* Might we be BSDish? sys/param.h has BSD defined if so */
115 #ifdef HAVE_SYS_PARAM_H
116 # include <sys/param.h>
117 #endif
118
119 #ifdef HAVE__STRICMP
120 # define strcasecmp _stricmp
121 #endif
122 #ifdef HAVE__STRNICMP
123 # define strncasecmp _strnicmp
124 #endif
125 #ifndef HAVE_VSNPRINTF
126 # ifdef HAVE__VSNPRINTF
127 # define vsnprintf _vsnprintf
128 # else
129 # define vsnprintf(buf, size, fmt, list) vsprintf(buf, fmt, list)
130 # endif
131 #endif
132
133 /* some platforms don't have float versions of the math library */
134 #ifndef HAVE_ASINF
135 # define asinf (float)asin
136 #endif
137 #ifndef HAVE_ATAN2F
138 # define atan2f (float)atan2
139 #endif
140 #ifndef HAVE_ATANF
141 # define atanf (float)atan
142 #endif
143 #ifndef HAVE_COSF
144 # define cosf (float)cos
145 #endif
146 #ifndef HAVE_EXPF
147 # define expf (float)exp
148 #endif
149 #ifndef HAVE_FABSF
150 # define fabsf (float)fabs
151 #endif
152 #ifndef HAVE_FLOORF
153 # define floorf (float)floor
154 #endif
155 #ifndef HAVE_FMODF
156 # define fmodf (float)fmod
157 #endif
158 #ifndef HAVE_HYPOTF
159 # define hypotf (float)hypot
160 #endif
161 #ifndef HAVE_LOGF
162 # define logf (float)log
163 #endif
164 #ifndef HAVE_LOG10F
165 # define log10f (float)log10
166 #endif
167 #ifndef HAVE_POWF
168 # define powf (float)pow
169 #endif
170 #ifndef HAVE_SINF
171 # define sinf (float)sin
172 #endif
173 #ifndef HAVE_SQRTF
174 # define sqrtf (float)sqrt
175 #endif
176 #ifndef HAVE_TANF
177 # define tanf (float)tan
178 #endif
179
180
181 /* random number stuff */
182 #define bzfrand() ((double)rand() / ((double)RAND_MAX + 1.0))
183 #define bzfsrand(_s) srand(_s)
184
185 #ifndef __BEOS__
186 # ifdef HAVE_VALUES_H
187 # include <values.h>
188 # endif
189 # ifdef HAVE_LIMITS_H
190 # include <limits.h>
191 # endif
192 #else
193 # include <limits.h>
194 /* BeOS: FIXME */
195 # define MAXSHORT SHORT_MAX
196 # define MAXINT INT_MAX
197 # define MAXLONG LONG_MAX
198 #endif /* __BEOS__ */
199
200 #ifdef HAVE_SYS_TYPES_H
201 # include <sys/types.h>
202 #endif
203
204 /* need some integer types */
205 #ifdef HAVE_INTTYPES_H
206 # include <inttypes.h>
207 #endif
208
209 #ifdef HAVE_STDINT_H
210 # include <stdint.h>
211 #else
212 # if defined(__linux) || (defined(__sgi) && !defined(__INTTYPES_MAJOR))
213 typedef u_int16_t uint16_t;
214 typedef u_int32_t uint32_t;
215 # endif
216 # if defined(sun)
217 typedef signed short int16_t;
218 typedef ushort_t uint16_t;
219 typedef signed int int32_t;
220 typedef uint_t uint32_t;
221 # endif
222 typedef unsigned char uint8_t;
223 #endif
224
225
226 /* missing constants */
227
228 #ifndef MAXFLOAT
229 # define MAXFLOAT 3.402823466e+38f
230 #endif
231
232 #ifndef M_PI
233 # define M_PI 3.14159265358979323846f
234 #endif
235
236 #ifndef M_SQRT1_2
237 # define M_SQRT1_2 0.70710678118654752440f
238 #endif
239
240
241 #ifdef __BEOS__
242 # ifndef setenv
243 # define setenv(a,b,c)
244 # endif
245 # ifndef putenv
246 # define putenv(a)
247 # endif
248 #endif /* __BEOS__ */
249
250 #define bzcountof(__x) (sizeof(__x) / sizeof(__x[0]))
251
252
253 #ifdef HAVE_STD__ISNAN
254 # ifdef isnan
255 # undef isnan
256 # endif
257 # define isnan std::isnan
258 #elif defined(HAVE__ISNAN)
259 # ifdef isnan
260 # undef isnan
261 # endif
262 # define isnan _isnan
263 #else
264 # ifndef HAVE_ISNAN
265 # ifdef __cplusplus
266 # ifdef isnan
267 # undef isnan
268 # endif
269 template<typename Tp>
isnan(Tp f)270 inline int isnan(Tp f)
271 {
272 return (f!=f);
273 }
274 # else
275 # define isnan(f) ((f) != (f))
276 # endif /* __cplusplus */
277 # endif /* HAVE_ISNAN */
278 #endif /* HAVE_STD__ISNAN */
279
280
281 #ifndef HAVE_STD__MAX
282 # ifdef __cplusplus
283 # ifdef max
284 # undef max
285 # endif
286 namespace std
287 {
288 template<typename comparable>
max(const comparable & a,const comparable & b)289 inline const comparable& max(const comparable& a, const comparable& b)
290 {
291 return a < b ? b : a;
292 }
293 }
294 # else
295 # ifdef max
296 # undef max
297 # endif
298 # define max(a,b) a < b ? b : a
299 # endif /* __cplusplus */
300 #endif /* HAVE_STD__MAX */
301
302 #ifndef HAVE_STD__MIN
303 # ifdef __cpluscplus
304 # ifdef min
305 # undef min
306 # endif
307 namespace std
308 {
309 template<typename comparable>
min(const comparable & a,const comparable & b)310 inline const comparable& min(const comparable& a, const comparable& b)
311 {
312 return b < a ? b : a;
313 }
314 }
315 # else
316 # ifdef min
317 # undef min
318 # endif
319 # define min(a,b) b < a ? b : a
320 # endif /* __cplusplus */
321 #endif /* HAVE_STD_MIN */
322
323 #if defined(HAVE_REGEX_H)
324 # include <regex.h>
325 #else
326 # define regex_t void
327 #endif /* HAVE_REGEX_H */
328
329 #endif /* BZF_COMMON_H */
330
331
332 /* Local Variables: ***
333 * mode: C++ ***
334 * tab-width: 8 ***
335 * c-basic-offset: 2 ***
336 * indent-tabs-mode: t ***
337 * End: ***
338 * ex: shiftwidth=2 tabstop=8
339 */
340