Chipmunk2D Pro API Reference  7.0.0
 All Classes Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
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 #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 
63 
64 #if CP_USE_DOUBLES
65 
66 
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 #ifndef M_PI
115  #define M_PI 3.14159265358979323846264338327950288
116 #endif
117 
118 #ifndef M_E
119  #define M_E 2.71828182845904523536028747135266250
120 #endif
121 
122 
124 static inline cpFloat cpfmax(cpFloat a, cpFloat b)
125 {
126  return (a > b) ? a : b;
127 }
128 
130 static inline cpFloat cpfmin(cpFloat a, cpFloat b)
131 {
132  return (a < b) ? a : b;
133 }
134 
136 static inline cpFloat cpfabs(cpFloat f)
137 {
138  return (f < 0) ? -f : f;
139 }
140 
142 static inline cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max)
143 {
144  return cpfmin(cpfmax(f, min), max);
145 }
146 
148 static inline cpFloat cpfclamp01(cpFloat f)
149 {
150  return cpfmax(0.0f, cpfmin(f, 1.0f));
151 }
152 
153 
154 
156 static inline cpFloat cpflerp(cpFloat f1, cpFloat f2, cpFloat t)
157 {
158  return f1*(1.0f - t) + f2*t;
159 }
160 
162 static inline cpFloat cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d)
163 {
164  return f1 + cpfclamp(f2 - f1, -d, d);
165 }
166 
168 #ifdef CP_HASH_VALUE_TYPE
169  typedef CP_HASH_VALUE_TYPE cpHashValue;
170 #else
171  typedef uintptr_t cpHashValue;
172 #endif
173 
176 typedef uint32_t cpCollisionID;
177 
178 // Oh C, how we love to define our own boolean types to get compiler compatibility
180 #ifdef CP_BOOL_TYPE
181  typedef CP_BOOL_TYPE cpBool;
182 #else
183  typedef unsigned char cpBool;
184 #endif
185 
186 #ifndef cpTrue
187 
188  #define cpTrue 1
189 #endif
190 
191 #ifndef cpFalse
192 
193  #define cpFalse 0
194 #endif
195 
196 #ifdef CP_DATA_POINTER_TYPE
197  typedef CP_DATA_POINTER_TYPE cpDataPointer;
198 #else
199 
200  typedef void * cpDataPointer;
201 #endif
202 
203 #ifdef CP_COLLISION_TYPE_TYPE
204  typedef CP_COLLISION_TYPE_TYPE cpCollisionType;
205 #else
206 
207  typedef uintptr_t cpCollisionType;
208 #endif
209 
210 #ifdef CP_GROUP_TYPE
211  typedef CP_GROUP_TYPE cpGroup;
212 #else
213 
214  typedef uintptr_t cpGroup;
215 #endif
216 
217 #ifdef CP_BITMASK_TYPE
218  typedef CP_BITMASK_TYPE cpBitmask;
219 #else
220 
221  typedef unsigned int cpBitmask;
222 #endif
223 
224 #ifdef CP_TIMESTAMP_TYPE
225  typedef CP_TIMESTAMP_TYPE cpTimestamp;
226 #else
227 
228  typedef unsigned int cpTimestamp;
229 #endif
230 
231 #ifndef CP_NO_GROUP
232 
233  #define CP_NO_GROUP ((cpGroup)0)
234 #endif
235 
236 #ifndef CP_ALL_CATEGORIES
237 
238  #define CP_ALL_CATEGORIES (~(cpBitmask)0)
239 #endif
240 
241 #ifndef CP_WILDCARD_COLLISION_TYPE
242 
243  #define CP_WILDCARD_COLLISION_TYPE (~(cpCollisionType)0)
244 #endif
245 
247 
248 // CGPoints are structurally the same, and allow
249 // easy interoperability with other Cocoa libraries
250 #if CP_USE_CGTYPES
251  typedef CGPoint cpVect;
252 #else
253 
254 
255  typedef struct cpVect{cpFloat x,y;} cpVect;
256 #endif
257 
258 #if CP_USE_CGTYPES
259  typedef CGAffineTransform cpTransform;
260 #else
261 
262  typedef struct cpTransform {
263  cpFloat a, b, c, d, tx, ty;
264  } cpTransform;
265 #endif
266 
267 // NUKE
268 typedef struct cpMat2x2 {
269  // Row major [[a, b][c d]]
270  cpFloat a, b, c, d;
271 } cpMat2x2;
272 
273 #endif