1 /** @file liblegacy.h  Common definitions for legacy support.
2  *
3  * @authors Copyright © 2012-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
4  *
5  * @par License
6  * GPL: http://www.gnu.org/licenses/gpl.html
7  *
8  * <small>This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version. This program is distributed in the hope that it
12  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details. You should have received a copy of the GNU
15  * General Public License along with this program; if not, write to the Free
16  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17  * 02110-1301 USA</small>
18  */
19 
20 #ifndef LIBLEGACY_H
21 #define LIBLEGACY_H
22 
23 /**
24  * @defgroup legacy Legacy Support
25  *
26  * Common functionality for supporting old, mostly C based code tracing back to
27  * the original id Software and Raven Software code bases.
28  */
29 
30 /**
31  * @defgroup legacyMath Legacy Math Utilities
32  * Math utilities for C based code. @ingroup legacy
33  */
34 
35 /**
36  * @defgroup legacyData Legacy Data Types
37  * Data types and structures for C based code. @ingroup legacy
38  */
39 
40 /**
41  * @defgroup legacyFlags Flags
42  * @ingroup legacy
43  */
44 
45 #if defined(__cplusplus) && !defined(DENG_NO_QT)
46 #  define DENG_USE_QT
47 #endif
48 
49 #if defined(__x86_64__) || defined(__x86_64) || defined(_LP64)
50 #  ifndef __64BIT__
51 #    define __64BIT__
52 #  endif
53 #endif
54 
55 #ifdef DENG2_USE_QT
56 #  include <QtCore/qglobal.h>
57 #endif
58 
59 #include <assert.h>
60 #include <stddef.h>
61 
62 /*
63  * The DENG_PUBLIC macro is used for declaring exported symbols. It must be
64  * applied in all exported classes and functions. DEF files are not used for
65  * exporting symbols out of liblegacy.
66  */
67 #if defined(_WIN32) && defined(_MSC_VER)
68 #  ifdef __DENG__
69 // This is defined when compiling the library.
70 #    define DENG_PUBLIC __declspec(dllexport)
71 #  else
72 #    define DENG_PUBLIC __declspec(dllimport)
73 #  endif
74 #  define DENG_NORETURN __declspec(noreturn)
75 #else
76 #  define DENG_PUBLIC
77 #  define DENG_NORETURN __attribute__((__noreturn__))
78 #endif
79 
80 #if defined (DENG_IOS)
81 #  define DENG_VISIBLE_SYMBOL __attribute__((visibility("default")))
82 #else
83 #  define DENG_VISIBLE_SYMBOL
84 #endif
85 
86 #if defined (DENG_STATIC_LINK)
87 #  define DENG_ENTRYPOINT static
88 #else
89 #  define DENG_ENTRYPOINT DENG_EXTERN_C
90 #endif
91 
92 #if !defined(_MSC_VER)
93 #endif
94 
95 #ifdef __cplusplus
96 #  define DENG_EXTERN_C extern "C"
97 #else
98 #  define DENG_EXTERN_C extern
99 #endif
100 
101 #ifndef NDEBUG
102 #  ifndef _DEBUG
103 #    define _DEBUG 1
104 #  endif
105 #  define DENG_DEBUG
106 #  ifdef DENG_USE_QT
107 #    define DENG_ASSERT(x) Q_ASSERT(x)
108 #  else
109 #    define DENG_ASSERT(x) assert(x)
110 #  endif
111 #  define DENG_DEBUG_ONLY(x) x
112 #else
113 #  define DENG_NO_DEBUG
114 #  define DENG_ASSERT(x)
115 #  define DENG_DEBUG_ONLY(x)
116 #endif
117 
118 /**
119  * Macro for hiding the warning about an unused parameter.
120  */
121 #define DENG_UNUSED(x)      (void)x
122 
123 /*
124  * Utility macros.
125  */
126 
127 #define DD_PI           3.14159265359f
128 #define DD_PI_D         3.14159265358979323846
129 #define DEG2RAD(a)      (((a) * DD_PI_D) / 180.0)
130 #define RAD2DEG(a)      (((a) / DD_PI_D) * 180.0)
131 #define FLOATEPSILON    .000001f
132 
133 /**
134  * Converts a numerical value to a C++ bool type. Zero is the only accepted
135  * 'false' value, any other value is considered 'true'.
136  *
137  * @param x  Any value that can be compared against zero.
138  *
139  * @return  @c true or @c false (C++ bool type).
140  */
141 #define CPP_BOOL(x)         ((x) != 0)
142 
143 #define INRANGE_OF(x, y, r) ((x) >= (y) - (r) && (x) <= (y) + (r))
144 
145 #define MAX_OF(x, y)        ((x) > (y)? (x) : (y))
146 
147 #define MIN_OF(x, y)        ((x) < (y)? (x) : (y))
148 
149 #define MINMAX_OF(a, x, b)  ((x) < (a)? (a) : (x) > (b)? (b) : (x))
150 
151 #define SIGN_OF(x)          ((x) > 0? +1 : (x) < 0? -1 : 0)
152 
153 #define INRANGE_OF(x, y, r) ((x) >= (y) - (r) && (x) <= (y) + (r))
154 
155 #define FEQUAL(x, y)        (INRANGE_OF(x, y, FLOATEPSILON))
156 
157 #define IS_ZERO(x)          FEQUAL(x, 0)
158 
159 #define NON_ZERO(x)         (!IS_ZERO(x))
160 
161 #define ROUND(x)            ((int) (((x) < 0.0f)? ((x) - 0.5f) : ((x) + 0.5f)))
162 
163 #ifdef ABS
164 #  undef ABS
165 #endif
166 #define ABS(x)              ((x) >= 0 ? (x) : -(x))
167 
168 /// Ceiling of integer quotient of @a a divided by @a b.
169 #define CEILING(a, b)       ((a) % (b) == 0 ? (a)/(b) : (a)/(b)+1)
170 
171 #define DENG_ISSPACE(c)     ((c) == 0 || (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
172 
173 // Automatically define the basic types for convenience.
174 #include <de/types.h>
175 
176 /*
177  * Main interface.
178  */
179 
180 #ifdef __cplusplus
181 extern "C" {
182 #endif
183 
184 /**
185  * Initializes the library. This must be the first function called before any other
186  * functions in the library.
187  */
188 DENG_PUBLIC void Libdeng_Init(void);
189 
190 /**
191  * Shuts down the library. Frees any internal resources allocated by the library's
192  * subsystems. Must be called when the library is no longer needed.
193  */
194 DENG_PUBLIC void Libdeng_Shutdown(void);
195 
196 /**
197  * Terminates the process immediately. Call this when a malloc fails to handle
198  * terminating gracefully instead of crashing with null pointer access.
199  */
200 DENG_PUBLIC DENG_NORETURN void Libdeng_BadAlloc(void);
201 
202 #ifdef __cplusplus
203 } // extern "C"
204 #endif
205 
206 #endif // LIBLEGACY_H
207