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