Chipmunk2D Pro API Reference  7.0.0
chipmunk_types.h
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 // Visual Studio 2013 Update 4 is still broken with C99 inline
30 #ifdef _MSC_VER
31 #define inline __inline
32 #endif
33 
34 #ifdef __APPLE__
35  #include "TargetConditionals.h"
36 #endif
37 
38 // Use CGTypes by default on iOS and Mac.
39 // Also enables usage of doubles on 64 bit.
40 // Performance is usually very comparable when the CPU cache is well utilised.
41 #if (TARGET_OS_IPHONE || TARGET_OS_MAC) && (!defined CP_USE_CGTYPES)
42  #define CP_USE_CGTYPES 1
43 #endif
44 
45 #if CP_USE_CGTYPES
46  #if TARGET_OS_IPHONE
47  #import <CoreGraphics/CGGeometry.h>
48  #import <CoreGraphics/CGAffineTransform.h>
49  #elif TARGET_OS_MAC
50  #include <ApplicationServices/ApplicationServices.h>
51  #endif
52 
53  #if defined(__LP64__) && __LP64__
54  #define CP_USE_DOUBLES 1
55  #else
56  #define CP_USE_DOUBLES 0
57  #endif
58 #endif
59 
60 #ifndef CP_USE_DOUBLES
61  // EW: Extending logic to decide doubles to allow for NEON on Raspberry Pi (and probably Android 32-bit)
62  #if defined(__ARM_NEON__)
63  #if defined(__arm64) || defined(__arm64__)
64  #define CP_USE_DOUBLES 1
65  #else
66  #define CP_USE_DOUBLES 0
67  #endif
68  #elif defined(__arm__) || defined(__i386__) || defined(_M_IX86)
69  #define CP_USE_DOUBLES 0
70  #else
71  // Use doubles by default for higher precision.
72  #define CP_USE_DOUBLES 1
73  #endif
74 #endif
75 
79 
80 #if CP_USE_DOUBLES
81  typedef double cpFloat;
84  #define cpfsqrt sqrt
85  #define cpfsin sin
86  #define cpfcos cos
87  #define cpfacos acos
88  #define cpfatan2 atan2
89  #define cpfmod fmod
90  #define cpfexp exp
91  #define cpfpow pow
92  #define cpffloor floor
93  #define cpfceil ceil
94  #define CPFLOAT_MIN DBL_MIN
95 #else
96  typedef float cpFloat;
97  #define cpfsqrt sqrtf
98  #define cpfsin sinf
99  #define cpfcos cosf
100  #define cpfacos acosf
101  #define cpfatan2 atan2f
102  #define cpfmod fmodf
103  #define cpfexp expf
104  #define cpfpow powf
105  #define cpffloor floorf
106  #define cpfceil ceilf
107  #define CPFLOAT_MIN FLT_MIN
108 #endif
109 
110 #ifndef INFINITY
111  #ifdef _MSC_VER
112  union MSVC_EVIL_FLOAT_HACK
113  {
114  unsigned __int8 Bytes[4];
115  float Value;
116  };
117  static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};
118  #define INFINITY (INFINITY_HACK.Value)
119  #endif
120 
121  #ifdef __GNUC__
122  #define INFINITY (__builtin_inf())
123  #endif
124 
125  #ifndef INFINITY
126  #define INFINITY (1e1000)
127  #endif
128 #endif
129 
130 #ifndef M_PI
131  #define M_PI 3.14159265358979323846264338327950288
132 #endif
133 
134 #ifndef M_E
135  #define M_E 2.71828182845904523536028747135266250
136 #endif
137 
138 
140 static inline cpFloat cpfmax(cpFloat a, cpFloat b)
141 {
142  return (a > b) ? a : b;
143 }
144 
146 static inline cpFloat cpfmin(cpFloat a, cpFloat b)
147 {
148  return (a < b) ? a : b;
149 }
150 
152 static inline cpFloat cpfabs(cpFloat f)
153 {
154  return (f < 0) ? -f : f;
155 }
156 
158 static inline cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max)
159 {
160  return cpfmin(cpfmax(f, min), max);
161 }
162 
164 static inline cpFloat cpfclamp01(cpFloat f)
165 {
166  return cpfmax(0.0f, cpfmin(f, 1.0f));
167 }
168 
169 
170 
172 static inline cpFloat cpflerp(cpFloat f1, cpFloat f2, cpFloat t)
173 {
174  return f1*(1.0f - t) + f2*t;
175 }
176 
178 static inline cpFloat cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d)
179 {
180  return f1 + cpfclamp(f2 - f1, -d, d);
181 }
182 
184 #ifdef CP_HASH_VALUE_TYPE
185  typedef CP_HASH_VALUE_TYPE cpHashValue;
186 #else
187  typedef uintptr_t cpHashValue;
188 #endif
189 
192 typedef uint32_t cpCollisionID;
193 
194 // Oh C, how we love to define our own boolean types to get compiler compatibility
196 #ifdef CP_BOOL_TYPE
197  typedef CP_BOOL_TYPE cpBool;
198 #else
199  typedef unsigned char cpBool;
200 #endif
201 
202 #ifndef cpTrue
203  #define cpTrue 1
205 #endif
206 
207 #ifndef cpFalse
208  #define cpFalse 0
210 #endif
211 
212 #ifdef CP_DATA_POINTER_TYPE
213  typedef CP_DATA_POINTER_TYPE cpDataPointer;
214 #else
215  typedef void * cpDataPointer;
217 #endif
218 
219 #ifdef CP_COLLISION_TYPE_TYPE
220  typedef CP_COLLISION_TYPE_TYPE cpCollisionType;
221 #else
222  typedef uintptr_t cpCollisionType;
224 #endif
225 
226 #ifdef CP_GROUP_TYPE
227  typedef CP_GROUP_TYPE cpGroup;
228 #else
229  typedef uintptr_t cpGroup;
231 #endif
232 
233 #ifdef CP_BITMASK_TYPE
234  typedef CP_BITMASK_TYPE cpBitmask;
235 #else
236  typedef unsigned int cpBitmask;
238 #endif
239 
240 #ifdef CP_TIMESTAMP_TYPE
241  typedef CP_TIMESTAMP_TYPE cpTimestamp;
242 #else
243  typedef unsigned int cpTimestamp;
245 #endif
246 
247 #ifndef CP_NO_GROUP
248  #define CP_NO_GROUP ((cpGroup)0)
250 #endif
251 
252 #ifndef CP_ALL_CATEGORIES
253  #define CP_ALL_CATEGORIES (~(cpBitmask)0)
255 #endif
256 
257 #ifndef CP_WILDCARD_COLLISION_TYPE
258  #define CP_WILDCARD_COLLISION_TYPE (~(cpCollisionType)0)
260 #endif
261 
263 
264 // CGPoints are structurally the same, and allow
265 // easy interoperability with other Cocoa libraries
266 #if CP_USE_CGTYPES
267  typedef CGPoint cpVect;
268 #else
269  typedef struct cpVect{cpFloat x,y;} cpVect;
272 #endif
273 
274 #if CP_USE_CGTYPES
275  typedef CGAffineTransform cpTransform;
276 #else
277  typedef struct cpTransform {
279  cpFloat a, b, c, d, tx, ty;
280  } cpTransform;
281 #endif
282 
283 // NUKE
284 typedef struct cpMat2x2 {
285  // Row major [[a, b][c d]]
286  cpFloat a, b, c, d;
287 } cpMat2x2;
288 
289 #endif
static cpFloat cpfclamp01(cpFloat f)
Clamp f to be between 0 and 1.
Definition: chipmunk_types.h:164
void * cpDataPointer
Type used for user data pointers.
Definition: chipmunk_types.h:216
static cpFloat cpfabs(cpFloat f)
Return the absolute value of a cpFloat.
Definition: chipmunk_types.h:152
unsigned char cpBool
Chipmunk&#39;s boolean type.
Definition: chipmunk_types.h:199
static cpFloat cpfmin(cpFloat a, cpFloat b)
Return the min of two cpFloats.
Definition: chipmunk_types.h:146
Definition: chipmunk_types.h:284
uintptr_t cpCollisionType
Type used for cpSpace.collision_type.
Definition: chipmunk_types.h:223
unsigned int cpTimestamp
Type used for various timestamps in Chipmunk.
Definition: chipmunk_types.h:244
static cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max)
Clamp f to be between min and max.
Definition: chipmunk_types.h:158
double cpFloat
Chipmunk&#39;s floating point type.
Definition: chipmunk_types.h:83
unsigned int cpBitmask
Type used for cpShapeFilter category and mask.
Definition: chipmunk_types.h:237
Definition: chipmunk_types.h:271
uint32_t cpCollisionID
Type used internally to cache colliding object info for cpCollideShapes().
Definition: chipmunk_types.h:192
static cpFloat cpfmax(cpFloat a, cpFloat b)
Return the max of two cpFloats.
Definition: chipmunk_types.h:140
uintptr_t cpGroup
Type used for cpShape.group.
Definition: chipmunk_types.h:230
static cpFloat cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d)
Linearly interpolate from f1 to f2 by no more than d.
Definition: chipmunk_types.h:178
uintptr_t cpHashValue
Hash value type.
Definition: chipmunk_types.h:187
Column major affine transform.
Definition: chipmunk_types.h:278
static cpFloat cpflerp(cpFloat f1, cpFloat f2, cpFloat t)
Linearly interpolate (or extrapolate) between f1 and f2 by t percent.
Definition: chipmunk_types.h:172