1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 * SOFTWARE.
20 */
21
22 #ifndef CHIPMUNK_TYPES_H
23 #define CHIPMUNK_TYPES_H
24
25 #include <stdint.h>
26 #include <float.h>
27 #include <math.h>
28
29 #ifdef __APPLE__
30 #include "TargetConditionals.h"
31 #endif
32
33 // Use CGTypes by default on iOS and Mac.
34 // Also enables usage of doubles on 64 bit.
35 // Performance is usually very comparable when the CPU cache is well utilised.
36 #if (TARGET_OS_IPHONE || TARGET_OS_MAC) && (!defined CP_USE_CGTYPES)
37 #define CP_USE_CGTYPES 1
38 #endif
39
40 #if CP_USE_CGTYPES
41 #if TARGET_OS_IPHONE
42 #import <CoreGraphics/CGGeometry.h>
43 #import <CoreGraphics/CGAffineTransform.h>
44 #elif TARGET_OS_MAC
45 #include <ApplicationServices/ApplicationServices.h>
46 #endif
47
48 #if defined(__LP64__) && __LP64__
49 #define CP_USE_DOUBLES 1
50 #else
51 #define CP_USE_DOUBLES 0
52 #endif
53 #endif
54
55 #ifndef CP_USE_DOUBLES
56 // Use doubles by default for higher precision.
57 #define CP_USE_DOUBLES 1
58 #endif
59
60 /// @defgroup basicTypes Basic Types
61 /// Most of these types can be configured at compile time.
62 /// @{
63
64 #if CP_USE_DOUBLES
65 /// Chipmunk's floating point type.
66 /// Can be reconfigured at compile time.
67 typedef double cpFloat;
68 #define cpfsqrt sqrt
69 #define cpfsin sin
70 #define cpfcos cos
71 #define cpfacos acos
72 #define cpfatan2 atan2
73 #define cpfmod fmod
74 #define cpfexp exp
75 #define cpfpow pow
76 #define cpffloor floor
77 #define cpfceil ceil
78 #define CPFLOAT_MIN DBL_MIN
79 #else
80 typedef float cpFloat;
81 #define cpfsqrt sqrtf
82 #define cpfsin sinf
83 #define cpfcos cosf
84 #define cpfacos acosf
85 #define cpfatan2 atan2f
86 #define cpfmod fmodf
87 #define cpfexp expf
88 #define cpfpow powf
89 #define cpffloor floorf
90 #define cpfceil ceilf
91 #define CPFLOAT_MIN FLT_MIN
92 #endif
93
94 #ifndef INFINITY
95 #ifdef _MSC_VER
96 union MSVC_EVIL_FLOAT_HACK
97 {
98 unsigned __int8 Bytes[4];
99 float Value;
100 };
101 static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};
102 #define INFINITY (INFINITY_HACK.Value)
103 #endif
104
105 #ifdef __GNUC__
106 #define INFINITY (__builtin_inf())
107 #endif
108
109 #ifndef INFINITY
110 #define INFINITY (1e1000)
111 #endif
112 #endif
113
114
115 #define CP_PI ((cpFloat)3.14159265358979323846264338327950288)
116
117
118 /// Return the max of two cpFloats.
cpfmax(cpFloat a,cpFloat b)119 static inline cpFloat cpfmax(cpFloat a, cpFloat b)
120 {
121 return (a > b) ? a : b;
122 }
123
124 /// Return the min of two cpFloats.
cpfmin(cpFloat a,cpFloat b)125 static inline cpFloat cpfmin(cpFloat a, cpFloat b)
126 {
127 return (a < b) ? a : b;
128 }
129
130 /// Return the absolute value of a cpFloat.
cpfabs(cpFloat f)131 static inline cpFloat cpfabs(cpFloat f)
132 {
133 return (f < 0) ? -f : f;
134 }
135
136 /// Clamp @c f to be between @c min and @c max.
cpfclamp(cpFloat f,cpFloat min,cpFloat max)137 static inline cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max)
138 {
139 return cpfmin(cpfmax(f, min), max);
140 }
141
142 /// Clamp @c f to be between 0 and 1.
cpfclamp01(cpFloat f)143 static inline cpFloat cpfclamp01(cpFloat f)
144 {
145 return cpfmax(0.0f, cpfmin(f, 1.0f));
146 }
147
148
149
150 /// Linearly interpolate (or extrapolate) between @c f1 and @c f2 by @c t percent.
cpflerp(cpFloat f1,cpFloat f2,cpFloat t)151 static inline cpFloat cpflerp(cpFloat f1, cpFloat f2, cpFloat t)
152 {
153 return f1*(1.0f - t) + f2*t;
154 }
155
156 /// Linearly interpolate from @c f1 to @c f2 by no more than @c d.
cpflerpconst(cpFloat f1,cpFloat f2,cpFloat d)157 static inline cpFloat cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d)
158 {
159 return f1 + cpfclamp(f2 - f1, -d, d);
160 }
161
162 /// Hash value type.
163 #ifdef CP_HASH_VALUE_TYPE
164 typedef CP_HASH_VALUE_TYPE cpHashValue;
165 #else
166 typedef uintptr_t cpHashValue;
167 #endif
168
169 /// Type used internally to cache colliding object info for cpCollideShapes().
170 /// Should be at least 32 bits.
171 typedef uint32_t cpCollisionID;
172
173 // Oh C, how we love to define our own boolean types to get compiler compatibility
174 /// Chipmunk's boolean type.
175 #ifdef CP_BOOL_TYPE
176 typedef CP_BOOL_TYPE cpBool;
177 #else
178 typedef unsigned char cpBool;
179 #endif
180
181 #ifndef cpTrue
182 /// true value.
183 #define cpTrue 1
184 #endif
185
186 #ifndef cpFalse
187 /// false value.
188 #define cpFalse 0
189 #endif
190
191 #ifdef CP_DATA_POINTER_TYPE
192 typedef CP_DATA_POINTER_TYPE cpDataPointer;
193 #else
194 /// Type used for user data pointers.
195 typedef void * cpDataPointer;
196 #endif
197
198 #ifdef CP_COLLISION_TYPE_TYPE
199 typedef CP_COLLISION_TYPE_TYPE cpCollisionType;
200 #else
201 /// Type used for cpSpace.collision_type.
202 typedef uintptr_t cpCollisionType;
203 #endif
204
205 #ifdef CP_GROUP_TYPE
206 typedef CP_GROUP_TYPE cpGroup;
207 #else
208 /// Type used for cpShape.group.
209 typedef uintptr_t cpGroup;
210 #endif
211
212 #ifdef CP_BITMASK_TYPE
213 typedef CP_BITMASK_TYPE cpBitmask;
214 #else
215 /// Type used for cpShapeFilter category and mask.
216 typedef unsigned int cpBitmask;
217 #endif
218
219 #ifdef CP_TIMESTAMP_TYPE
220 typedef CP_TIMESTAMP_TYPE cpTimestamp;
221 #else
222 /// Type used for various timestamps in Chipmunk.
223 typedef unsigned int cpTimestamp;
224 #endif
225
226 #ifndef CP_NO_GROUP
227 /// Value for cpShape.group signifying that a shape is in no group.
228 #define CP_NO_GROUP ((cpGroup)0)
229 #endif
230
231 #ifndef CP_ALL_CATEGORIES
232 /// Value for cpShape.layers signifying that a shape is in every layer.
233 #define CP_ALL_CATEGORIES (~(cpBitmask)0)
234 #endif
235
236 #ifndef CP_WILDCARD_COLLISION_TYPE
237 /// cpCollisionType value internally reserved for hashing wildcard handlers.
238 #define CP_WILDCARD_COLLISION_TYPE (~(cpCollisionType)0)
239 #endif
240
241 /// @}
242
243 // CGPoints are structurally the same, and allow
244 // easy interoperability with other Cocoa libraries
245 #if CP_USE_CGTYPES
246 typedef CGPoint cpVect;
247 #else
248 /// Chipmunk's 2D vector type.
249 /// @addtogroup cpVect
250 typedef struct cpVect{cpFloat x,y;} cpVect;
251 #endif
252
253 #if CP_USE_CGTYPES
254 typedef CGAffineTransform cpTransform;
255 #else
256 /// Column major affine transform.
257 typedef struct cpTransform {
258 cpFloat a, b, c, d, tx, ty;
259 } cpTransform;
260 #endif
261
262 // NUKE
263 typedef struct cpMat2x2 {
264 // Row major [[a, b][c d]]
265 cpFloat a, b, c, d;
266 } cpMat2x2;
267
268 #endif
269