1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 #ifndef CHIPMUNK_TYPES_H
23 #define CHIPMUNK_TYPES_H
30 #include "TargetConditionals.h"
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
42 #include <CoreGraphics/CGGeometry.h>
43 #include <CoreGraphics/CGAffineTransform.h>
45 #include <ApplicationServices/ApplicationServices.h>
48 #if defined(__LP64__) && __LP64__
49 #define CP_USE_DOUBLES 1
51 #define CP_USE_DOUBLES 0
55 #ifndef CP_USE_DOUBLES
57 // Use doubles by default for higher precision (but not for 32 bit ARM).
58 #define CP_USE_DOUBLES 1
62 /// @defgroup basicTypes Basic Types
63 /// Most of these types can be configured at compile time.
67 /// Chipmunk's floating point type.
68 /// Can be reconfigured at compile time.
69 typedef double cpFloat;
74 #define cpfatan2 atan2
78 #define cpffloor floor
80 #define CPFLOAT_MIN DBL_MIN
82 typedef float cpFloat;
87 #define cpfatan2 atan2f
91 #define cpffloor floorf
93 #define CPFLOAT_MIN FLT_MIN
98 union MSVC_EVIL_FLOAT_HACK
100 unsigned __int8 Bytes[4];
103 static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};
104 #define INFINITY (INFINITY_HACK.Value)
108 #define INFINITY (__builtin_inf())
112 #define INFINITY (1e1000)
117 #define CP_PI ((cpFloat)3.14159265358979323846264338327950288)
120 /// Return the max of two cpFloats.
121 static inline cpFloat cpfmax(cpFloat a, cpFloat b)
123 return (a > b) ? a : b;
126 /// Return the min of two cpFloats.
127 static inline cpFloat cpfmin(cpFloat a, cpFloat b)
129 return (a < b) ? a : b;
132 /// Return the absolute value of a cpFloat.
133 static inline cpFloat cpfabs(cpFloat f)
135 return (f < 0) ? -f : f;
138 /// Clamp @c f to be between @c min and @c max.
139 static inline cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max)
141 return cpfmin(cpfmax(f, min), max);
144 /// Clamp @c f to be between 0 and 1.
145 static inline cpFloat cpfclamp01(cpFloat f)
147 return cpfmax(0.0f, cpfmin(f, 1.0f));
152 /// Linearly interpolate (or extrapolate) between @c f1 and @c f2 by @c t percent.
153 static inline cpFloat cpflerp(cpFloat f1, cpFloat f2, cpFloat t)
155 return f1*(1.0f - t) + f2*t;
158 /// Linearly interpolate from @c f1 to @c f2 by no more than @c d.
159 static inline cpFloat cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d)
161 return f1 + cpfclamp(f2 - f1, -d, d);
165 #ifdef CP_HASH_VALUE_TYPE
166 typedef CP_HASH_VALUE_TYPE cpHashValue;
168 typedef uintptr_t cpHashValue;
171 /// Type used internally to cache colliding object info for cpCollideShapes().
172 /// Should be at least 32 bits.
173 typedef uint32_t cpCollisionID;
175 // Oh C, how we love to define our own boolean types to get compiler compatibility
176 /// Chipmunk's boolean type.
178 typedef CP_BOOL_TYPE cpBool;
180 typedef unsigned char cpBool;
193 #ifdef CP_DATA_POINTER_TYPE
194 typedef CP_DATA_POINTER_TYPE cpDataPointer;
196 /// Type used for user data pointers.
197 typedef void * cpDataPointer;
200 #ifdef CP_COLLISION_TYPE_TYPE
201 typedef CP_COLLISION_TYPE_TYPE cpCollisionType;
203 /// Type used for cpSpace.collision_type.
204 typedef uintptr_t cpCollisionType;
208 typedef CP_GROUP_TYPE cpGroup;
210 /// Type used for cpShape.group.
211 typedef uintptr_t cpGroup;
214 #ifdef CP_BITMASK_TYPE
215 typedef CP_BITMASK_TYPE cpBitmask;
217 /// Type used for cpShapeFilter category and mask.
218 typedef unsigned int cpBitmask;
221 #ifdef CP_TIMESTAMP_TYPE
222 typedef CP_TIMESTAMP_TYPE cpTimestamp;
224 /// Type used for various timestamps in Chipmunk.
225 typedef unsigned int cpTimestamp;
229 /// Value for cpShape.group signifying that a shape is in no group.
230 #define CP_NO_GROUP ((cpGroup)0)
233 #ifndef CP_ALL_CATEGORIES
234 /// Value for cpShape.layers signifying that a shape is in every layer.
235 #define CP_ALL_CATEGORIES (~(cpBitmask)0)
238 #ifndef CP_WILDCARD_COLLISION_TYPE
239 /// cpCollisionType value internally reserved for hashing wildcard handlers.
240 #define CP_WILDCARD_COLLISION_TYPE (~(cpCollisionType)0)
245 // CGPoints are structurally the same, and allow
246 // easy interoperability with other Cocoa libraries
248 typedef CGPoint cpVect;
250 /// Chipmunk's 2D vector type.
251 /// @addtogroup cpVect
252 typedef struct cpVect{cpFloat x,y;} cpVect;
256 typedef CGAffineTransform cpTransform;
258 /// Column major affine transform.
259 typedef struct cpTransform {
260 cpFloat a, b, c, d, tx, ty;
265 typedef struct cpMat2x2 {
266 // Row major [[a, b][c d]]