Chipmunk Pro API Reference  6.1.3
 All Classes Functions Variables Typedefs Properties Groups Pages
chipmunk.h
1 /* Copyright (c) 2007 Scott Lembcke
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_HEADER
23 #define CHIPMUNK_HEADER
24 
25 #include <stdlib.h>
26 #include <math.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #ifndef CP_ALLOW_PRIVATE_ACCESS
33  #define CP_ALLOW_PRIVATE_ACCESS 0
34 #endif
35 
36 #if CP_ALLOW_PRIVATE_ACCESS == 1
37  #define CP_PRIVATE(symbol) symbol
38 #else
39  #define CP_PRIVATE(symbol) symbol##_private
40 #endif
41 
42 void cpMessage(const char *condition, const char *file, int line, int isError, int isHardError, const char *message, ...);
43 #ifdef NDEBUG
44  #define cpAssertWarn(condition, ...)
45 #else
46  #define cpAssertWarn(condition, ...) if(!(condition)) cpMessage(#condition, __FILE__, __LINE__, 0, 0, __VA_ARGS__)
47 #endif
48 
49 #ifdef NDEBUG
50  #define cpAssertSoft(condition, ...)
51 #else
52  #define cpAssertSoft(condition, ...) if(!(condition)) cpMessage(#condition, __FILE__, __LINE__, 1, 0, __VA_ARGS__)
53 #endif
54 
55 // Hard assertions are important and cheap to execute. They are not disabled by compiling as debug.
56 #define cpAssertHard(condition, ...) if(!(condition)) cpMessage(#condition, __FILE__, __LINE__, 1, 1, __VA_ARGS__)
57 
58 
59 #include "chipmunk_types.h"
60 
63 
65 #ifndef CP_BUFFER_BYTES
66  #define CP_BUFFER_BYTES (32*1024)
67 #endif
68 
69 #ifndef cpcalloc
70 
71  #define cpcalloc calloc
72 #endif
73 
74 #ifndef cprealloc
75 
76  #define cprealloc realloc
77 #endif
78 
79 #ifndef cpfree
80 
81  #define cpfree free
82 #endif
83 
84 typedef struct cpArray cpArray;
85 typedef struct cpHashSet cpHashSet;
86 
87 typedef struct cpBody cpBody;
88 typedef struct cpShape cpShape;
89 typedef struct cpConstraint cpConstraint;
90 
91 typedef struct cpCollisionHandler cpCollisionHandler;
92 typedef struct cpArbiter cpArbiter;
93 
94 typedef struct cpSpace cpSpace;
95 
96 #include "cpVect.h"
97 #include "cpBB.h"
98 #include "cpSpatialIndex.h"
99 
100 #include "cpBody.h"
101 #include "cpShape.h"
102 #include "cpPolyShape.h"
103 
104 #include "cpArbiter.h"
105 #include "constraints/cpConstraint.h"
106 
107 #include "cpSpace.h"
108 
109 // Chipmunk 6.1.3
110 #define CP_VERSION_MAJOR 6
111 #define CP_VERSION_MINOR 1
112 #define CP_VERSION_RELEASE 4
113 
115 extern const char *cpVersionString;
116 
118 void cpInitChipmunk(void);
119 
122 
123 
127 
131 
135 
138 
140 cpFloat cpMomentForPoly(cpFloat m, int numVerts, const cpVect *verts, cpVect offset);
141 
144 cpFloat cpAreaForPoly(const int numVerts, const cpVect *verts);
145 
147 cpVect cpCentroidForPoly(const int numVerts, const cpVect *verts);
148 
150 void cpRecenterPoly(const int numVerts, cpVect *verts);
151 
153 cpFloat cpMomentForBox(cpFloat m, cpFloat width, cpFloat height);
154 
157 
162 int cpConvexHull(int count, cpVect *verts, cpVect *result, int *first, cpFloat tol);
163 
164 #ifdef _MSC_VER
165 #include "malloc.h"
166 #endif
167 
172 #define CP_CONVEX_HULL(__count__, __verts__, __count_var__, __verts_var__) \
173 cpVect *__verts_var__ = (cpVect *)alloca(__count__*sizeof(cpVect)); \
174 int __count_var__ = cpConvexHull(__count__, __verts__, __verts_var__, NULL, 0.0); \
175 
176 #if defined(__has_extension)
177 #if __has_extension(blocks)
178 // Define alternate block based alternatives for a few of the callback heavy functions.
179 // Collision handlers are post-step callbacks are not included to avoid memory management issues.
180 // If you want to use blocks for those and are aware of how to correctly manage the memory, the implementation is trivial.
181 
182 void cpSpaceEachBody_b(cpSpace *space, void (^block)(cpBody *body));
183 void cpSpaceEachShape_b(cpSpace *space, void (^block)(cpShape *shape));
184 void cpSpaceEachConstraint_b(cpSpace *space, void (^block)(cpConstraint *constraint));
185 
186 void cpBodyEachShape_b(cpBody *body, void (^block)(cpShape *shape));
187 void cpBodyEachConstraint_b(cpBody *body, void (^block)(cpConstraint *constraint));
188 void cpBodyEachArbiter_b(cpBody *body, void (^block)(cpArbiter *arbiter));
189 
190 typedef void (^cpSpaceNearestPointQueryBlock)(cpShape *shape, cpFloat distance, cpVect point);
191 void cpSpaceNearestPointQuery_b(cpSpace *space, cpVect point, cpFloat maxDistance, cpLayers layers, cpGroup group, cpSpaceNearestPointQueryBlock block);
192 
193 typedef void (^cpSpaceSegmentQueryBlock)(cpShape *shape, cpFloat t, cpVect n);
194 void cpSpaceSegmentQuery_b(cpSpace *space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSpaceSegmentQueryBlock block);
195 
196 typedef void (^cpSpaceBBQueryBlock)(cpShape *shape);
197 void cpSpaceBBQuery_b(cpSpace *space, cpBB bb, cpLayers layers, cpGroup group, cpSpaceBBQueryBlock block);
198 
199 typedef void (^cpSpaceShapeQueryBlock)(cpShape *shape, cpContactPointSet *points);
200 cpBool cpSpaceShapeQuery_b(cpSpace *space, cpShape *shape, cpSpaceShapeQueryBlock block);
201 
202 #endif
203 #endif
204 
205 
207 
208 #ifdef __cplusplus
209 }
210 
211 static inline cpVect operator *(const cpVect v, const cpFloat s){return cpvmult(v, s);}
212 static inline cpVect operator +(const cpVect v1, const cpVect v2){return cpvadd(v1, v2);}
213 static inline cpVect operator -(const cpVect v1, const cpVect v2){return cpvsub(v1, v2);}
214 static inline cpBool operator ==(const cpVect v1, const cpVect v2){return cpveql(v1, v2);}
215 static inline cpVect operator -(const cpVect v){return cpvneg(v);}
216 
217 #endif
218 #endif