1 //------------------------------------------------------------------------
2 // UTILITY : general purpose functions
3 //------------------------------------------------------------------------
4 //
5 //  GL-Friendly Node Builder (C) 2000-2007 Andrew Apted
6 //
7 //  Based on 'BSP 2.3' by Colin Reed, Lee Killough and others.
8 //
9 //  This program is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU General Public License
11 //  as published by the Free Software Foundation; either version 2
12 //  of the License, or (at your option) any later version.
13 //
14 //  This program is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 //  GNU General Public License for more details.
18 //
19 //------------------------------------------------------------------------
20 
21 #ifndef __GLBSP_UTIL_H__
22 #define __GLBSP_UTIL_H__
23 
24 /* ----- useful macros ---------------------------- */
25 
26 #ifndef M_PI
27 #define M_PI  3.14159265358979323846
28 #endif
29 
30 #ifndef MAX
31 #define MAX(x,y)  ((x) > (y) ? (x) : (y))
32 #endif
33 
34 #ifndef MIN
35 #define MIN(x,y)  ((x) < (y) ? (x) : (y))
36 #endif
37 
38 #ifndef ABS
39 #define ABS(x)  ((x) >= 0 ? (x) : -(x))
40 #endif
41 
42 #ifndef I_ROUND
43 #define I_ROUND(x)  ((int) (((x) < 0.0f) ? ((x) - 0.5f) : ((x) + 0.5f)))
44 #endif
45 
46 /* ----- function prototypes ---------------------------- */
47 
48 // allocate and clear some memory.  guaranteed not to fail.
49 void *UtilCalloc(int size);
50 
51 // re-allocate some memory.  guaranteed not to fail.
52 void *UtilRealloc(void *old, int size);
53 
54 // duplicate a string.
55 char *UtilStrDup(const char *str);
56 char *UtilStrNDup(const char *str, int size);
57 
58 // format the string and return the allocated memory.
59 // The memory must be freed with UtilFree.
60 char *UtilFormat(const char *str, ...) GCCATTR((format (printf, 1, 2)));
61 
62 // free some memory or a string.
63 void UtilFree(void *data);
64 
65 // compare two strings case insensitively.
66 int UtilStrCaseCmp(const char *A, const char *B);
67 
68 // return an allocated string for the current data and time,
69 // or NULL if an error occurred.
70 char *UtilTimeString(void);
71 
72 // round a positive value up to the nearest power of two.
73 int UtilRoundPOW2(int x);
74 
75 // compute angle & distance from (0,0) to (dx,dy)
76 angle_g UtilComputeAngle(float_g dx, float_g dy);
77 #define UtilComputeDist(dx,dy)  sqrt((dx) * (dx) + (dy) * (dy))
78 
79 // compute the parallel and perpendicular distances from a partition
80 // line to a point.
81 //
82 #define UtilParallelDist(part,x,y)  \
83     (((x) * (part)->pdx + (y) * (part)->pdy + (part)->p_para)  \
84      / (part)->p_length)
85 
86 #define UtilPerpDist(part,x,y)  \
87     (((x) * (part)->pdy - (y) * (part)->pdx + (part)->p_perp)  \
88      / (part)->p_length)
89 
90 // check if the file exists.
91 int UtilFileExists(const char *filename);
92 
93 // checksum functions
94 void Adler32_Begin(uint32_g *crc);
95 void Adler32_AddBlock(uint32_g *crc, const uint8_g *data, int length);
96 void Adler32_Finish(uint32_g *crc);
97 
98 #endif /* __GLBSP_UTIL_H__ */
99