File: common.h

package info (click to toggle)
bzflag 2.4.30-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,488 kB
  • sloc: cpp: 150,376; ansic: 3,463; sh: 2,535; makefile: 2,194; perl: 486; python: 260; objc: 246; php: 206
file content (339 lines) | stat: -rw-r--r-- 7,114 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* bzflag
 * Copyright (c) 1993-2025 Tim Riker
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named COPYING that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

/*
 * common definitions
 */

#ifndef BZF_COMMON_H
#define BZF_COMMON_H

/* this should always be the very FIRST header */
#include "config.h"

#ifdef _WIN32
#  undef NOMINMAX
#  define NOMINMAX 1
#  include "win32.h"
#endif

#include <stdio.h>
#include <stdlib.h> /* needed for bzfrand */
#include <math.h>
#ifdef __cplusplus
#  include <cmath>
#endif


extern int debugLevel;


/* Provide a means to conveniently test the version of the GNU
 * compiler.  Use it like this:
 *
 * #if GCC_PREREQ(2,8)
 * ... code requiring gcc 2.8 or later ...
 * #endif
 *
 * WARNING: THIS MACRO IS CONSIDERED PRIVATE AND SHOULD NOT BE USED
 * OUTSIDE OF THIS HEADER FILE.  DO NOT RELY ON IT.
 */
#ifndef GCC_PREREQ
#  if defined __GNUC__
#    define GCC_PREREQ(major, minor) __GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))
#  else
#    define GCC_PREREQ(major, minor) 0
#  endif
#else
#  warning "GCC_PREREQ is already defined.  See the common.h header."
#endif


/**
 * UNUSED provides a common mechanism for declaring unused parameters.
 * Use it like this:
 *
 * int
 * my_function(int argc, char **UNUSED(argv))
 * {
 *   ...
 * }
 *
 */
#ifndef UNUSED
#  if GCC_PREREQ(2, 5)
/* GCC-style */
#    define UNUSED(parameter) (parameter) __attribute__((unused))
#  else
/* MSVC/C++ */
#    ifdef __cplusplus
#      if defined(NDEBUG)
#   define UNUSED(parameter) /* parameter */
#      else /* some of them are asserted */
#    define UNUSED(parameter) (parameter)
#      endif
#    else
#      define UNUSED(parameter) (parameter)
#    endif
#  endif
#else
#  undef UNUSED
#  define UNUSED(parameter) (parameter)
#  warning "UNUSED was previously defined.  Parameter declaration behavior is unknown, see common.h"
#endif


/* near zero by some epsilon convenience define since relying on
 * the floating point unit for proper equivalence is not safe
 */
#define NEAR_ZERO(_value,_epsilon)  ( ((_value) > -_epsilon) && ((_value) < _epsilon) )

/* (radians <--> degrees) conversion values */
#define DEG2RAD 0.0174532925199432957692369076848861271344287189
#define RAD2DEG 57.29577951308232087679815481410517033240547247
#define DEG2RADf ((float)DEG2RAD)
#define RAD2DEGf ((float)RAD2DEG)


/* seven places of precision is pretty safe, so something less precise */
#ifdef FLT_EPSILON
#  define ZERO_TOLERANCE FLT_EPSILON
#else
#  define ZERO_TOLERANCE 1.0e-06f
#endif

/* Might we be BSDish? sys/param.h has BSD defined if so */
#ifdef HAVE_SYS_PARAM_H
#  include <sys/param.h>
#endif

#ifdef HAVE__STRICMP
#  define strcasecmp _stricmp
#endif
#ifdef HAVE__STRNICMP
#  define strncasecmp _strnicmp
#endif
#ifndef HAVE_VSNPRINTF
#  ifdef HAVE__VSNPRINTF
#    define vsnprintf _vsnprintf
#  else
#    define vsnprintf(buf, size, fmt, list) vsprintf(buf, fmt, list)
#  endif
#endif

/* some platforms don't have float versions of the math library */
#ifndef HAVE_ASINF
#  define   asinf       (float)asin
#endif
#ifndef HAVE_ATAN2F
#  define   atan2f      (float)atan2
#endif
#ifndef HAVE_ATANF
#  define   atanf       (float)atan
#endif
#ifndef HAVE_COSF
#  define   cosf        (float)cos
#endif
#ifndef HAVE_EXPF
#  define   expf        (float)exp
#endif
#ifndef HAVE_FABSF
#  define   fabsf       (float)fabs
#endif
#ifndef HAVE_FLOORF
#  define   floorf      (float)floor
#endif
#ifndef HAVE_FMODF
#  define   fmodf       (float)fmod
#endif
#ifndef HAVE_HYPOTF
#  define   hypotf      (float)hypot
#endif
#ifndef HAVE_LOGF
#  define   logf        (float)log
#endif
#ifndef HAVE_LOG10F
#  define   log10f      (float)log10
#endif
#ifndef HAVE_POWF
#  define   powf        (float)pow
#endif
#ifndef HAVE_SINF
#  define   sinf        (float)sin
#endif
#ifndef HAVE_SQRTF
#  define   sqrtf       (float)sqrt
#endif
#ifndef HAVE_TANF
#  define   tanf        (float)tan
#endif


/* random number stuff */
#define bzfrand()   ((double)rand() / ((double)RAND_MAX + 1.0))
#define bzfsrand(_s)    srand(_s)

#ifndef __BEOS__
#  ifdef HAVE_VALUES_H
#    include <values.h>
#  endif
#  ifdef HAVE_LIMITS_H
#    include <limits.h>
#  endif
#else
#  include <limits.h>
/* BeOS: FIXME */
#  define MAXSHORT SHORT_MAX
#  define MAXINT INT_MAX
#  define MAXLONG LONG_MAX
#endif /* __BEOS__ */

#ifdef HAVE_SYS_TYPES_H
#  include <sys/types.h>
#endif

/* need some integer types */
#ifdef HAVE_INTTYPES_H
#  include <inttypes.h>
#endif

#ifdef HAVE_STDINT_H
#  include <stdint.h>
#else
#  if defined(__linux) || (defined(__sgi) && !defined(__INTTYPES_MAJOR))
typedef u_int16_t   uint16_t;
typedef u_int32_t   uint32_t;
#  endif
#  if defined(sun)
typedef signed short    int16_t;
typedef ushort_t    uint16_t;
typedef signed int  int32_t;
typedef uint_t      uint32_t;
#  endif
typedef unsigned char   uint8_t;
#endif


/* missing constants */

#ifndef MAXFLOAT
#  define   MAXFLOAT    3.402823466e+38f
#endif

#ifndef M_PI
#  define   M_PI        3.14159265358979323846f
#endif

#ifndef M_SQRT1_2
#  define   M_SQRT1_2   0.70710678118654752440f
#endif


#ifdef __BEOS__
#  ifndef setenv
#    define setenv(a,b,c)
#  endif
#  ifndef putenv
#    define putenv(a)
#  endif
#endif /* __BEOS__ */

#define bzcountof(__x)   (sizeof(__x) / sizeof(__x[0]))


#ifdef HAVE_STD__ISNAN
#  ifdef isnan
#    undef isnan
#  endif
#  define isnan std::isnan
#elif defined(HAVE__ISNAN)
#  ifdef isnan
#    undef isnan
#  endif
#  define isnan _isnan
#else
#  ifndef HAVE_ISNAN
#    ifdef __cplusplus
#      ifdef isnan
#   undef isnan
#      endif
template<typename Tp>
inline int isnan(Tp f)
{
    return (f!=f);
}
#    else
#      define isnan(f) ((f) != (f))
#    endif /* __cplusplus */
#  endif /* HAVE_ISNAN */
#endif /* HAVE_STD__ISNAN */


#ifndef HAVE_STD__MAX
#  ifdef __cplusplus
#    ifdef max
#      undef max
#    endif
namespace std
{
template<typename comparable>
inline const comparable& max(const comparable& a, const comparable& b)
{
    return  a < b ? b : a;
}
}
#  else
#    ifdef max
#      undef max
#    endif
#    define max(a,b) a < b ? b : a
#  endif /* __cplusplus */
#endif /* HAVE_STD__MAX */

#ifndef HAVE_STD__MIN
#  ifdef __cpluscplus
#    ifdef min
#      undef min
#    endif
namespace std
{
template<typename comparable>
inline const comparable& min(const comparable& a, const comparable& b)
{
    return b < a ? b : a;
}
}
#  else
#    ifdef min
#      undef min
#    endif
#    define min(a,b) b < a ? b : a
#  endif /* __cplusplus */
#endif /* HAVE_STD_MIN */

#if defined(HAVE_REGEX_H)
#  include <regex.h>
#else
#  define regex_t void
#endif  /* HAVE_REGEX_H */

#endif /* BZF_COMMON_H */


/* Local Variables: ***
 * mode: C++ ***
 * tab-width: 8 ***
 * c-basic-offset: 2 ***
 * indent-tabs-mode: t ***
 * End: ***
 * ex: shiftwidth=2 tabstop=8
 */