From 63ffc574d71dd230c1b92d3b116da6fc6e9f2201 Mon Sep 17 00:00:00 2001 From: Walzer Date: Mon, 11 Jul 2011 10:34:03 +0800 Subject: [PATCH 1/3] fixed #601, update Chipmunk from 5.3.4 to 5.3.5. Only tested on iOS now. --- chipmunk/include/chipmunk/chipmunk.h | 20 ----- chipmunk/include/chipmunk/chipmunk_types.h | 31 ++++++- chipmunk/include/chipmunk/constraints/util.h | 10 ++- chipmunk/include/chipmunk/cpArbiter.h | 2 +- chipmunk/include/chipmunk/cpBody.h | 10 +-- chipmunk/include/chipmunk/cpPolyShape.h | 2 +- chipmunk/include/chipmunk/cpShape.h | 4 +- chipmunk/include/chipmunk/cpSpace.h | 2 + chipmunk/src/CMakeLists.txt | 2 +- chipmunk/src/chipmunk.c | 2 +- .../src/constraints/cpDampedRotarySpring.c | 2 +- chipmunk/src/constraints/cpDampedSpring.c | 2 +- chipmunk/src/constraints/cpGearJoint.c | 2 +- chipmunk/src/constraints/cpGrooveJoint.c | 2 +- chipmunk/src/constraints/cpPinJoint.c | 2 +- chipmunk/src/constraints/cpPivotJoint.c | 2 +- chipmunk/src/constraints/cpRatchetJoint.c | 2 +- chipmunk/src/constraints/cpRotaryLimitJoint.c | 2 +- chipmunk/src/constraints/cpSimpleMotor.c | 2 +- chipmunk/src/constraints/cpSlideJoint.c | 2 +- chipmunk/src/cpArray.c | 2 +- chipmunk/src/cpBody.c | 18 ++++- chipmunk/src/cpHashSet.c | 4 +- chipmunk/src/cpShape.c | 2 +- chipmunk/src/cpSpace.c | 80 ++++++++++--------- chipmunk/src/cpSpaceComponent.c | 5 +- chipmunk/src/cpSpaceHash.c | 12 +-- chipmunk/src/cpSpaceQuery.c | 4 +- chipmunk/src/cpSpaceStep.c | 22 +++-- .../project.pbxproj.REMOVED.git-id | 2 +- tests/tests/ChipmunkTest/Sensors.cpp | 6 +- tests/tests/ChipmunkTest/drawSpace.cpp | 17 +--- 32 files changed, 151 insertions(+), 128 deletions(-) diff --git a/chipmunk/include/chipmunk/chipmunk.h b/chipmunk/include/chipmunk/chipmunk.h index 03581b0616..f025aaffe1 100644 --- a/chipmunk/include/chipmunk/chipmunk.h +++ b/chipmunk/include/chipmunk/chipmunk.h @@ -51,26 +51,6 @@ void cpMessage(const char *message, const char *condition, const char *file, int #include "chipmunk_types.h" -#ifndef INFINITY - #ifdef _MSC_VER - union MSVC_EVIL_FLOAT_HACK - { - unsigned __int8 Bytes[4]; - float Value; - }; - static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}}; - #define INFINITY (INFINITY_HACK.Value) - #endif - - #ifdef __GNUC__ - #define INFINITY (__builtin_inf()) - #endif - - #ifndef INFINITY - #define INFINITY (1e1000) - #endif -#endif - // Maximum allocated size for various Chipmunk buffers #define CP_BUFFER_BYTES (32*1024) diff --git a/chipmunk/include/chipmunk/chipmunk_types.h b/chipmunk/include/chipmunk/chipmunk_types.h index 0354206f7b..8143dd0e37 100644 --- a/chipmunk/include/chipmunk/chipmunk_types.h +++ b/chipmunk/include/chipmunk/chipmunk_types.h @@ -58,6 +58,35 @@ #define cpfceil ceilf #endif +#ifndef INFINITY + #ifdef _MSC_VER + union MSVC_EVIL_FLOAT_HACK + { + unsigned __int8 Bytes[4]; + float Value; + }; + static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}}; + #define INFINITY (INFINITY_HACK.Value) + #endif + + #ifdef __GNUC__ + #define INFINITY (__builtin_inf()) + #endif + + #ifndef INFINITY + #define INFINITY (1e1000) + #endif +#endif + +#ifndef M_PI + #define M_PI 3.14159265358979323846264338327950288 +#endif + +#ifndef M_E + #define M_E 2.71828182845904523536028747135266250 +#endif + + static inline cpFloat cpfmax(cpFloat a, cpFloat b) { @@ -138,7 +167,7 @@ typedef unsigned int cpHashValue; #endif #ifdef CP_LAYERS_TYPE - typedef CP_GROUP_TYPE cpLayers; + typedef CP_LAYERS_TYPE cpLayers; #else typedef unsigned int cpLayers; #endif diff --git a/chipmunk/include/chipmunk/constraints/util.h b/chipmunk/include/chipmunk/constraints/util.h index a1ce500806..af6b2b724d 100644 --- a/chipmunk/include/chipmunk/constraints/util.h +++ b/chipmunk/include/chipmunk/constraints/util.h @@ -51,11 +51,17 @@ normal_relative_velocity(cpBody *a, cpBody *b, cpVect r1, cpVect r2, cpVect n){ return cpvdot(relative_velocity(a, b, r1, r2), n); } +static inline void +apply_impulse(cpBody *body, cpVect j, cpVect r){ + body->v = cpvadd(body->v, cpvmult(j, body->m_inv)); + body->w += body->i_inv*cpvcross(r, j); +} + static inline void apply_impulses(cpBody *a , cpBody *b, cpVect r1, cpVect r2, cpVect j) { - cpBodyApplyImpulse(a, cpvneg(j), r1); - cpBodyApplyImpulse(b, j, r2); + apply_impulse(a, cpvneg(j), r1); + apply_impulse(b, j, r2); } static inline void diff --git a/chipmunk/include/chipmunk/cpArbiter.h b/chipmunk/include/chipmunk/cpArbiter.h index 31bb929ff3..f76da203d3 100644 --- a/chipmunk/include/chipmunk/cpArbiter.h +++ b/chipmunk/include/chipmunk/cpArbiter.h @@ -157,7 +157,7 @@ cpArbiterGetPoint(const cpArbiter *arb, int i) } static inline cpFloat -cpArbiteGetDepth(const cpArbiter *arb, int i) +cpArbiterGetDepth(const cpArbiter *arb, int i) { return arb->CP_PRIVATE(contacts)[i].CP_PRIVATE(dist); } diff --git a/chipmunk/include/chipmunk/cpBody.h b/chipmunk/include/chipmunk/cpBody.h index ba1a3372b5..8fa2f51881 100644 --- a/chipmunk/include/chipmunk/cpBody.h +++ b/chipmunk/include/chipmunk/cpBody.h @@ -192,18 +192,12 @@ cpBodyWorld2Local(const cpBody *body, const cpVect v) return cpvunrotate(cpvsub(v, body->p), body->rot); } -// Apply an impulse (in world coordinates) to the body at a point relative to the center of gravity (also in world coordinates). -static inline void -cpBodyApplyImpulse(cpBody *body, const cpVect j, const cpVect r) -{ - body->v = cpvadd(body->v, cpvmult(j, body->m_inv)); - body->w += body->i_inv*cpvcross(r, j); -} - // Zero the forces on a body. void cpBodyResetForces(cpBody *body); // Apply a force (in world coordinates) to a body at a point relative to the center of gravity (also in world coordinates). void cpBodyApplyForce(cpBody *body, const cpVect f, const cpVect r); +// Apply an impulse (in world coordinates) to the body at a point relative to the center of gravity (also in world coordinates). +void cpBodyApplyImpulse(cpBody *body, const cpVect j, const cpVect r); static inline cpFloat cpBodyKineticEnergy(const cpBody *body) diff --git a/chipmunk/include/chipmunk/cpPolyShape.h b/chipmunk/include/chipmunk/cpPolyShape.h index 12816ce56b..3f1772e75a 100644 --- a/chipmunk/include/chipmunk/cpPolyShape.h +++ b/chipmunk/include/chipmunk/cpPolyShape.h @@ -29,7 +29,7 @@ typedef struct cpPolyShapeAxis{ // Convex polygon shape structure. typedef struct cpPolyShape{ - CP_PRIVATE(cpShape shape); + cpShape shape; // Vertex and axis lists. CP_PRIVATE(int numVerts); diff --git a/chipmunk/include/chipmunk/cpShape.h b/chipmunk/include/chipmunk/cpShape.h index 5335642aad..1e9ed5140b 100644 --- a/chipmunk/include/chipmunk/cpShape.h +++ b/chipmunk/include/chipmunk/cpShape.h @@ -114,7 +114,7 @@ cpBool cpShapePointQuery(cpShape *shape, cpVect p); // Circle shape structure. typedef struct cpCircleShape{ - CP_PRIVATE(cpShape shape); + cpShape shape; // Center in body space coordinates CP_PRIVATE(cpVect c); @@ -135,7 +135,7 @@ CP_DeclareShapeGetter(cpCircleShape, cpFloat, Radius); // Segment shape structure. typedef struct cpSegmentShape{ - CP_PRIVATE(cpShape shape); + cpShape shape; // Endpoints and normal of the segment. (body space coordinates) cpVect CP_PRIVATE(a), CP_PRIVATE(b), CP_PRIVATE(n); diff --git a/chipmunk/include/chipmunk/cpSpace.h b/chipmunk/include/chipmunk/cpSpace.h index 2f6832a082..3911506be1 100644 --- a/chipmunk/include/chipmunk/cpSpace.h +++ b/chipmunk/include/chipmunk/cpSpace.h @@ -42,6 +42,8 @@ typedef struct cpCollisionHandler { void *data; } cpCollisionHandler; +extern cpCollisionHandler cpSpaceDefaultHandler; + typedef struct cpContactBufferHeader { cpTimestamp stamp; struct cpContactBufferHeader *next; diff --git a/chipmunk/src/CMakeLists.txt b/chipmunk/src/CMakeLists.txt index 2b3cbc8ba0..1f7112b786 100644 --- a/chipmunk/src/CMakeLists.txt +++ b/chipmunk/src/CMakeLists.txt @@ -9,7 +9,7 @@ if(BUILD_SHARED) ${chipmunk_source_files} ) # set the lib's version number - set_target_properties(chipmunk PROPERTIES VERSION 5.3.4) + set_target_properties(chipmunk PROPERTIES VERSION 5.3.5) install(TARGETS chipmunk RUNTIME DESTINATION lib LIBRARY DESTINATION lib) endif(BUILD_SHARED) diff --git a/chipmunk/src/chipmunk.c b/chipmunk/src/chipmunk.c index 1862e78288..20538b7c43 100644 --- a/chipmunk/src/chipmunk.c +++ b/chipmunk/src/chipmunk.c @@ -45,7 +45,7 @@ cpMessage(const char *message, const char *condition, const char *file, int line } -const char *cpVersionString = "5.3.4"; +const char *cpVersionString = "5.3.5"; void cpInitChipmunk(void) diff --git a/chipmunk/src/constraints/cpDampedRotarySpring.c b/chipmunk/src/constraints/cpDampedRotarySpring.c index 48597de721..dcd47b3a6c 100644 --- a/chipmunk/src/constraints/cpDampedRotarySpring.c +++ b/chipmunk/src/constraints/cpDampedRotarySpring.c @@ -83,7 +83,7 @@ CP_DefineClassGetter(cpDampedRotarySpring) cpDampedRotarySpring * cpDampedRotarySpringAlloc(void) { - return (cpDampedRotarySpring *)cpmalloc(sizeof(cpDampedRotarySpring)); + return (cpDampedRotarySpring *)cpcalloc(1, sizeof(cpDampedRotarySpring)); } cpDampedRotarySpring * diff --git a/chipmunk/src/constraints/cpDampedSpring.c b/chipmunk/src/constraints/cpDampedSpring.c index 406af3af63..3c50dc6317 100644 --- a/chipmunk/src/constraints/cpDampedSpring.c +++ b/chipmunk/src/constraints/cpDampedSpring.c @@ -89,7 +89,7 @@ CP_DefineClassGetter(cpDampedSpring) cpDampedSpring * cpDampedSpringAlloc(void) { - return (cpDampedSpring *)cpmalloc(sizeof(cpDampedSpring)); + return (cpDampedSpring *)cpcalloc(1, sizeof(cpDampedSpring)); } cpDampedSpring * diff --git a/chipmunk/src/constraints/cpGearJoint.c b/chipmunk/src/constraints/cpGearJoint.c index dd694d35b9..ce80657819 100644 --- a/chipmunk/src/constraints/cpGearJoint.c +++ b/chipmunk/src/constraints/cpGearJoint.c @@ -80,7 +80,7 @@ CP_DefineClassGetter(cpGearJoint) cpGearJoint * cpGearJointAlloc(void) { - return (cpGearJoint *)cpmalloc(sizeof(cpGearJoint)); + return (cpGearJoint *)cpcalloc(1, sizeof(cpGearJoint)); } cpGearJoint * diff --git a/chipmunk/src/constraints/cpGrooveJoint.c b/chipmunk/src/constraints/cpGrooveJoint.c index 4a067ef929..7a80d00285 100644 --- a/chipmunk/src/constraints/cpGrooveJoint.c +++ b/chipmunk/src/constraints/cpGrooveJoint.c @@ -111,7 +111,7 @@ CP_DefineClassGetter(cpGrooveJoint) cpGrooveJoint * cpGrooveJointAlloc(void) { - return (cpGrooveJoint *)cpmalloc(sizeof(cpGrooveJoint)); + return (cpGrooveJoint *)cpcalloc(1, sizeof(cpGrooveJoint)); } cpGrooveJoint * diff --git a/chipmunk/src/constraints/cpPinJoint.c b/chipmunk/src/constraints/cpPinJoint.c index 3955143ff5..fc41c2cfc5 100644 --- a/chipmunk/src/constraints/cpPinJoint.c +++ b/chipmunk/src/constraints/cpPinJoint.c @@ -88,7 +88,7 @@ CP_DefineClassGetter(cpPinJoint); cpPinJoint * cpPinJointAlloc(void) { - return (cpPinJoint *)cpmalloc(sizeof(cpPinJoint)); + return (cpPinJoint *)cpcalloc(1, sizeof(cpPinJoint)); } cpPinJoint * diff --git a/chipmunk/src/constraints/cpPivotJoint.c b/chipmunk/src/constraints/cpPivotJoint.c index 634bd02336..e6d98c27e3 100644 --- a/chipmunk/src/constraints/cpPivotJoint.c +++ b/chipmunk/src/constraints/cpPivotJoint.c @@ -83,7 +83,7 @@ CP_DefineClassGetter(cpPivotJoint) cpPivotJoint * cpPivotJointAlloc(void) { - return (cpPivotJoint *)cpmalloc(sizeof(cpPivotJoint)); + return (cpPivotJoint *)cpcalloc(1, sizeof(cpPivotJoint)); } cpPivotJoint * diff --git a/chipmunk/src/constraints/cpRatchetJoint.c b/chipmunk/src/constraints/cpRatchetJoint.c index a7103c7f01..24a24284d3 100644 --- a/chipmunk/src/constraints/cpRatchetJoint.c +++ b/chipmunk/src/constraints/cpRatchetJoint.c @@ -101,7 +101,7 @@ CP_DefineClassGetter(cpRatchetJoint) cpRatchetJoint * cpRatchetJointAlloc(void) { - return (cpRatchetJoint *)cpmalloc(sizeof(cpRatchetJoint)); + return (cpRatchetJoint *)cpcalloc(1, sizeof(cpRatchetJoint)); } cpRatchetJoint * diff --git a/chipmunk/src/constraints/cpRotaryLimitJoint.c b/chipmunk/src/constraints/cpRotaryLimitJoint.c index 9bfa1ea708..8598704fcc 100644 --- a/chipmunk/src/constraints/cpRotaryLimitJoint.c +++ b/chipmunk/src/constraints/cpRotaryLimitJoint.c @@ -97,7 +97,7 @@ CP_DefineClassGetter(cpRotaryLimitJoint) cpRotaryLimitJoint * cpRotaryLimitJointAlloc(void) { - return (cpRotaryLimitJoint *)cpmalloc(sizeof(cpRotaryLimitJoint)); + return (cpRotaryLimitJoint *)cpcalloc(1, sizeof(cpRotaryLimitJoint)); } cpRotaryLimitJoint * diff --git a/chipmunk/src/constraints/cpSimpleMotor.c b/chipmunk/src/constraints/cpSimpleMotor.c index c4f771e441..0d08798632 100644 --- a/chipmunk/src/constraints/cpSimpleMotor.c +++ b/chipmunk/src/constraints/cpSimpleMotor.c @@ -75,7 +75,7 @@ CP_DefineClassGetter(cpSimpleMotor) cpSimpleMotor * cpSimpleMotorAlloc(void) { - return (cpSimpleMotor *)cpmalloc(sizeof(cpSimpleMotor)); + return (cpSimpleMotor *)cpcalloc(1, sizeof(cpSimpleMotor)); } cpSimpleMotor * diff --git a/chipmunk/src/constraints/cpSlideJoint.c b/chipmunk/src/constraints/cpSlideJoint.c index 79c627c7f7..c64e22fd18 100644 --- a/chipmunk/src/constraints/cpSlideJoint.c +++ b/chipmunk/src/constraints/cpSlideJoint.c @@ -104,7 +104,7 @@ CP_DefineClassGetter(cpSlideJoint) cpSlideJoint * cpSlideJointAlloc(void) { - return (cpSlideJoint *)cpmalloc(sizeof(cpSlideJoint)); + return (cpSlideJoint *)cpcalloc(1, sizeof(cpSlideJoint)); } cpSlideJoint * diff --git a/chipmunk/src/cpArray.c b/chipmunk/src/cpArray.c index 98dbb5e4fd..929f3935d5 100644 --- a/chipmunk/src/cpArray.c +++ b/chipmunk/src/cpArray.c @@ -42,7 +42,7 @@ cpArrayInit(cpArray *arr, int size) size = (size ? size : 4); arr->max = size; - arr->arr = (void **)cpmalloc(size*sizeof(void**)); + arr->arr = (void **)cpcalloc(size, sizeof(void**)); return arr; } diff --git a/chipmunk/src/cpBody.c b/chipmunk/src/cpBody.c index 0b40c5c5f9..e466896e0c 100644 --- a/chipmunk/src/cpBody.c +++ b/chipmunk/src/cpBody.c @@ -24,6 +24,7 @@ #include #include "chipmunk_private.h" +#include "constraints/util.h" // function declaration cpBody *cpBodyNewStatic(void); @@ -34,13 +35,13 @@ cpBody cpStaticBodySingleton; cpBody* cpBodyAlloc(void) { - return (cpBody *)cpmalloc(sizeof(cpBody)); + return (cpBody *)cpcalloc(1, sizeof(cpBody)); } cpBodyVelocityFunc cpBodyUpdateVelocityDefault = cpBodyUpdateVelocity; cpBodyPositionFunc cpBodyUpdatePositionDefault = cpBodyUpdatePosition; -cpBody* +cpBody * cpBodyInit(cpBody *body, cpFloat m, cpFloat i) { body->velocity_func = cpBodyUpdateVelocityDefault; @@ -155,17 +156,28 @@ cpBodyUpdatePosition(cpBody *body, cpFloat dt) void cpBodyResetForces(cpBody *body) { + cpBodyActivate(body); body->f = cpvzero; body->t = 0.0f; } void -cpBodyApplyForce(cpBody *body, cpVect force, cpVect r) +cpBodyApplyForce(cpBody *body, const cpVect force, const cpVect r) { + cpBodyActivate(body); + body->f = cpvadd(body->f, force); body->t += cpvcross(r, force); } +void +cpBodyApplyImpulse(cpBody *body, const cpVect j, const cpVect r) +{ + cpBodyActivate(body); + apply_impulse(body, j, r); +} + + void cpApplyDampedSpring(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat rlen, cpFloat k, cpFloat dmp, cpFloat dt) { diff --git a/chipmunk/src/cpHashSet.c b/chipmunk/src/cpHashSet.c index c7f3cba605..890a59a3f4 100644 --- a/chipmunk/src/cpHashSet.c +++ b/chipmunk/src/cpHashSet.c @@ -33,7 +33,7 @@ cpHashSetDestroy(cpHashSet *set) // Free the table. cpfree(set->table); - cpArrayEach(set->allocatedBuffers, freeWrap, NULL); + if(set->allocatedBuffers) cpArrayEach(set->allocatedBuffers, freeWrap, NULL); cpArrayFree(set->allocatedBuffers); } @@ -133,7 +133,7 @@ getUnusedBin(cpHashSet *set) int count = CP_BUFFER_BYTES/sizeof(cpHashSetBin); cpAssert(count, "Buffer size is too small."); - cpHashSetBin *buffer = (cpHashSetBin *)cpmalloc(CP_BUFFER_BYTES); + cpHashSetBin *buffer = (cpHashSetBin *)cpcalloc(1, CP_BUFFER_BYTES); cpArrayPush(set->allocatedBuffers, buffer); // push all but the first one, return the first instead diff --git a/chipmunk/src/cpShape.c b/chipmunk/src/cpShape.c index a190891920..5ed43c1a4d 100644 --- a/chipmunk/src/cpShape.c +++ b/chipmunk/src/cpShape.c @@ -70,7 +70,7 @@ cpShapeInit(cpShape *shape, const cpShapeClass *klass, cpBody *body) void cpShapeDestroy(cpShape *shape) { - if(shape->klass->destroy) shape->klass->destroy(shape); + if(shape->klass && shape->klass->destroy) shape->klass->destroy(shape); } void diff --git a/chipmunk/src/cpSpace.c b/chipmunk/src/cpSpace.c index 8dc3b60ec4..3913336d6f 100644 --- a/chipmunk/src/cpSpace.c +++ b/chipmunk/src/cpSpace.c @@ -49,7 +49,7 @@ contactSetTrans(cpShape **shapes, cpSpace *space) int count = CP_BUFFER_BYTES/sizeof(cpArbiter); cpAssert(count, "Buffer size too small."); - cpArbiter *buffer = (cpArbiter *)cpmalloc(CP_BUFFER_BYTES); + cpArbiter *buffer = (cpArbiter *)cpcalloc(1, CP_BUFFER_BYTES); cpArrayPush(space->allocatedBuffers, buffer); for(int i=0; ipooledArbiters, buffer + i); @@ -71,7 +71,7 @@ collFuncSetEql(cpCollisionHandler *check, cpCollisionHandler *pair) static void * collFuncSetTrans(cpCollisionHandler *handler, void *unused) { - cpCollisionHandler *copy = (cpCollisionHandler *)cpmalloc(sizeof(cpCollisionHandler)); + cpCollisionHandler *copy = (cpCollisionHandler *)cpcalloc(1, sizeof(cpCollisionHandler)); (*copy) = (*handler); return copy; @@ -105,7 +105,7 @@ cpSpaceAlloc(void) #define DEFAULT_ITERATIONS 10 #define DEFAULT_ELASTIC_ITERATIONS 0 -cpCollisionHandler defaultHandler = {0, 0, alwaysCollide, alwaysCollide, nothing, nothing, NULL}; +cpCollisionHandler cpSpaceDefaultHandler = {0, 0, alwaysCollide, alwaysCollide, nothing, nothing, NULL}; cpSpace* cpSpaceInit(cpSpace *space) @@ -140,9 +140,9 @@ cpSpaceInit(cpSpace *space) space->constraints = cpArrayNew(0); - space->defaultHandler = defaultHandler; + space->defaultHandler = cpSpaceDefaultHandler; space->collFuncSet = cpHashSetNew(0, (cpHashSetEqlFunc)collFuncSetEql, (cpHashSetTransFunc)collFuncSetTrans); - space->collFuncSet->default_value = &space->defaultHandler; + space->collFuncSet->default_value = &cpSpaceDefaultHandler; space->postStepCallbacks = NULL; @@ -174,20 +174,14 @@ cpSpaceDestroy(cpSpace *space) cpArrayFree(space->arbiters); cpArrayFree(space->pooledArbiters); - if(space->allocatedBuffers){ - cpArrayEach(space->allocatedBuffers, freeWrap, NULL); - cpArrayFree(space->allocatedBuffers); - } + if(space->allocatedBuffers) cpArrayEach(space->allocatedBuffers, freeWrap, NULL); + cpArrayFree(space->allocatedBuffers); - if(space->postStepCallbacks){ - cpHashSetEach(space->postStepCallbacks, freeWrap, NULL); - cpHashSetFree(space->postStepCallbacks); - } + if(space->postStepCallbacks) cpHashSetEach(space->postStepCallbacks, freeWrap, NULL); + cpHashSetFree(space->postStepCallbacks); - if(space->collFuncSet){ - cpHashSetEach(space->collFuncSet, freeWrap, NULL); - cpHashSetFree(space->collFuncSet); - } + if(space->collFuncSet) cpHashSetEach(space->collFuncSet, freeWrap, NULL); + cpHashSetFree(space->collFuncSet); } void @@ -211,6 +205,12 @@ cpSpaceFreeChildren(cpSpace *space) cpArrayEach(space->constraints, (cpArrayIter)&constraintFreeWrap, NULL); } +#define cpAssertSpaceUnlocked(space) \ + cpAssert(!space->locked, \ + "This addition/removal cannot be done safely during a call to cpSpaceStep() or during a query. " \ + "Put these calls into a post-step callback." \ + ); + #pragma mark Collision Handler Function Management void @@ -223,6 +223,8 @@ cpSpaceAddCollisionHandler( cpCollisionSeparateFunc separate, void *data ){ + cpAssertSpaceUnlocked(space); + // Remove any old function so the new one will get added. cpSpaceRemoveCollisionHandler(space, a, b); @@ -241,6 +243,8 @@ cpSpaceAddCollisionHandler( void cpSpaceRemoveCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b) { + cpAssertSpaceUnlocked(space); + struct{cpCollisionType a, b;} ids = {a, b}; cpCollisionHandler *old_handler = (cpCollisionHandler *) cpHashSetRemove(space->collFuncSet, CP_HASH_PAIR(a, b), &ids); cpfree(old_handler); @@ -255,6 +259,8 @@ cpSpaceSetDefaultCollisionHandler( cpCollisionSeparateFunc separate, void *data ){ + cpAssertSpaceUnlocked(space); + cpCollisionHandler handler = { 0, 0, begin ? begin : alwaysCollide, @@ -265,16 +271,10 @@ cpSpaceSetDefaultCollisionHandler( }; space->defaultHandler = handler; + space->collFuncSet->default_value = &space->defaultHandler; } #pragma mark Body, Shape, and Joint Management - -#define cpAssertSpaceUnlocked(space) \ - cpAssert(!space->locked, \ - "This addition/removal cannot be done safely during a call to cpSpaceStep() or during a query. " \ - "Put these calls into a post-step callback." \ - ); - static void cpBodyAddShape(cpBody *body, cpShape *shape) { @@ -479,21 +479,23 @@ cpSpaceRehashShape(cpSpace *space, cpShape *shape) void cpSpaceEachBody(cpSpace *space, cpSpaceBodyIterator func, void *data) { - cpArray *bodies = space->bodies; - - for(int i=0; inum; i++){ - func((cpBody *)bodies->arr[i], data); - } - - cpArray *components = space->sleepingComponents; - for(int i=0; inum; i++){ - cpBody *root = (cpBody *)components->arr[i]; - cpBody *body = root, *next; - do { - next = body->node.next; - func(body, data); - } while((body = next) != root); - } + cpSpaceLock(space); { + cpArray *bodies = space->bodies; + + for(int i=0; inum; i++){ + func((cpBody *)bodies->arr[i], data); + } + + cpArray *components = space->sleepingComponents; + for(int i=0; inum; i++){ + cpBody *root = (cpBody *)components->arr[i]; + cpBody *body = root, *next; + do { + next = body->node.next; + func(body, data); + } while((body = next) != root); + } + } cpSpaceUnlock(space); } diff --git a/chipmunk/src/cpSpaceComponent.c b/chipmunk/src/cpSpaceComponent.c index cee3e76205..2b1d3f2651 100644 --- a/chipmunk/src/cpSpaceComponent.c +++ b/chipmunk/src/cpSpaceComponent.c @@ -97,7 +97,10 @@ componentActivate(cpBody *root) void cpBodyActivate(cpBody *body) { - componentActivate(componentNodeRoot(body)); + if(!cpBodyIsRogue(body)){ + body->node.idleTime = 0.0f; + componentActivate(componentNodeRoot(body)); + } } static inline void diff --git a/chipmunk/src/cpSpaceHash.c b/chipmunk/src/cpSpaceHash.c index b3f7917354..8a7ce8627d 100644 --- a/chipmunk/src/cpSpaceHash.c +++ b/chipmunk/src/cpSpaceHash.c @@ -72,7 +72,7 @@ handleSetTrans(void *obj, cpSpaceHash *hash) int count = CP_BUFFER_BYTES/sizeof(cpHandle); cpAssert(count, "Buffer size is too small."); - cpHandle *buffer = (cpHandle *)cpmalloc(CP_BUFFER_BYTES); + cpHandle *buffer = (cpHandle *)cpcalloc(1, CP_BUFFER_BYTES); cpArrayPush(hash->allocatedBuffers, buffer); for(int i=0; ipooledHandles, buffer + i); @@ -144,15 +144,15 @@ static void freeWrap(void *ptr, void *unused){cpfree(ptr);} void cpSpaceHashDestroy(cpSpaceHash *hash) { - clearHash(hash); + if(hash->table) clearHash(hash); + cpfree(hash->table); cpHashSetFree(hash->handleSet); - cpArrayEach(hash->allocatedBuffers, freeWrap, NULL); + if(hash->allocatedBuffers) cpArrayEach(hash->allocatedBuffers, freeWrap, NULL); cpArrayFree(hash->allocatedBuffers); - cpArrayFree(hash->pooledHandles); - cpfree(hash->table); + cpArrayFree(hash->pooledHandles); } void @@ -200,7 +200,7 @@ getEmptyBin(cpSpaceHash *hash) int count = CP_BUFFER_BYTES/sizeof(cpSpaceHashBin); cpAssert(count, "Buffer size is too small."); - cpSpaceHashBin *buffer = (cpSpaceHashBin *)cpmalloc(CP_BUFFER_BYTES); + cpSpaceHashBin *buffer = (cpSpaceHashBin *)cpcalloc(1, CP_BUFFER_BYTES); cpArrayPush(hash->allocatedBuffers, buffer); // push all but the first one, return the first instead diff --git a/chipmunk/src/cpSpaceQuery.c b/chipmunk/src/cpSpaceQuery.c index 642ae4cada..5143c7d1b8 100644 --- a/chipmunk/src/cpSpaceQuery.c +++ b/chipmunk/src/cpSpaceQuery.c @@ -201,7 +201,7 @@ shapeQueryHelper(cpShape *a, cpShape *b, shapeQueryContext *context) if( (a->group && a->group == b->group) || !(a->layers & b->layers) || - a->sensor || b->sensor + a == b ) return; cpContact contacts[CP_MAX_CONTACTS_PER_ARBITER]; @@ -216,7 +216,7 @@ shapeQueryHelper(cpShape *a, cpShape *b, shapeQueryContext *context) } if(numContacts){ - context->anyCollision = cpTrue; + context->anyCollision = !(a->sensor || b->sensor); if(context->func){ cpContactPointSet set = {numContacts, {}}; diff --git a/chipmunk/src/cpSpaceStep.c b/chipmunk/src/cpSpaceStep.c index 7dd2f7691a..4ead44cb00 100644 --- a/chipmunk/src/cpSpaceStep.c +++ b/chipmunk/src/cpSpaceStep.c @@ -41,7 +41,7 @@ postStepFuncSetEql(PostStepCallback *a, PostStepCallback *b){ static void * postStepFuncSetTrans(PostStepCallback *callback, void *ignored) { - PostStepCallback *value = (PostStepCallback *)cpmalloc(sizeof(PostStepCallback)); + PostStepCallback *value = (PostStepCallback *)cpcalloc(1, sizeof(PostStepCallback)); (*value) = (*callback); return value; @@ -81,7 +81,7 @@ typedef struct cpContactBuffer { static cpContactBufferHeader * cpSpaceAllocContactBuffer(cpSpace *space) { - cpContactBuffer *buffer = (cpContactBuffer *)cpmalloc(sizeof(cpContactBuffer)); + cpContactBuffer *buffer = (cpContactBuffer *)cpcalloc(1, sizeof(cpContactBuffer)); cpArrayPush(space->allocatedBuffers, buffer); return (cpContactBufferHeader *)buffer; } @@ -143,6 +143,13 @@ cpSpacePopContacts(cpSpace *space, int count){ #pragma mark Collision Detection Functions +static inline cpCollisionHandler * +lookupCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b) +{ + cpCollisionType types[] = {a, b}; + return (cpCollisionHandler *)cpHashSetFind(space->collFuncSet, CP_HASH_PAIR(a, b), types); +} + static inline cpBool queryReject(cpShape *a, cpShape *b) { @@ -164,13 +171,10 @@ queryFunc(cpShape *a, cpShape *b, cpSpace *space) // Reject any of the simple cases if(queryReject(a,b)) return; - // Find the collision pair function for the shapes. - struct{cpCollisionType a, b;} ids = {a->collision_type, b->collision_type}; - cpHashValue collHashID = CP_HASH_PAIR(a->collision_type, b->collision_type); - cpCollisionHandler *handler = (cpCollisionHandler *)cpHashSetFind(space->collFuncSet, collHashID, &ids); + cpCollisionHandler *handler = lookupCollisionHandler(space, a->collision_type, b->collision_type); cpBool sensor = a->sensor || b->sensor; - if(sensor && handler == &space->defaultHandler) return; + if(sensor && handler == &cpSpaceDefaultHandler) return; // Shape 'a' should have the lower shape type. (required by cpCollideShapes() ) if(a->klass->type > b->klass->type){ @@ -255,7 +259,9 @@ contactSetFilter(cpArbiter *arb, cpSpace *space) // was used last frame, but not this one if(ticks >= 1 && arb->state != cpArbiterStateCached){ - arb->handler->separate(arb, space, arb->handler->data); + // The handler needs to be looked up again as the handler cached on the arbiter may have been deleted since the last step. + cpCollisionHandler *handler = lookupCollisionHandler(space, arb->a->collision_type, arb->b->collision_type); + handler->separate(arb, space, handler->data); arb->state = cpArbiterStateCached; } diff --git a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id index 808b161465..f5ad76e4aa 100644 --- a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -57222087555cb82a58a60b5b605e2a63d9c4fa25 \ No newline at end of file +2a6419e2f46f039761c135c554fd04f4a9551313 \ No newline at end of file diff --git a/tests/tests/ChipmunkTest/Sensors.cpp b/tests/tests/ChipmunkTest/Sensors.cpp index e515fc6dc2..053dc33ae5 100644 --- a/tests/tests/ChipmunkTest/Sensors.cpp +++ b/tests/tests/ChipmunkTest/Sensors.cpp @@ -65,11 +65,11 @@ blockerSeparate(cpArbiter *arb, cpSpace *space, void *unused) static void postStepRemove(cpSpace *space, cpShape *shape, void *unused) -{ - cpSpaceRemoveShape(space, shape); +{ cpSpaceRemoveBody(space, shape->body); cpBodyFree(shape->body); - + + cpSpaceRemoveShape(space, shape); cpShapeFree(shape); } diff --git a/tests/tests/ChipmunkTest/drawSpace.cpp b/tests/tests/ChipmunkTest/drawSpace.cpp index cf0735a5b3..aa4487f36b 100644 --- a/tests/tests/ChipmunkTest/drawSpace.cpp +++ b/tests/tests/ChipmunkTest/drawSpace.cpp @@ -500,17 +500,12 @@ drawSpace(cpSpace *space, drawSpaceOptions *options) glPointSize(options->bodyPointSize); cpArray *bodies = space->bodies; - // cocos2d-x: use ccDrawPoints to optimize the speed - CCPoint *aPoints = new CCPoint[bodies->num]; - + glColor4f(LINE_COLOR); for(int i=0, count = bodies->num; iarr[i]; - aPoints[i] = CCPoint(body->p.x, body->p.y); - // ccDrawPoint( ccp(body->p.x, body->p.y) ); + ccDrawPoint( ccp(body->p.x, body->p.y) ); } - ccDrawPoints( aPoints, bodies->num ); - delete []aPoints; // glColor3f(0.5f, 0.5f, 0.5f); // cpArray *components = space->components; @@ -529,18 +524,12 @@ drawSpace(cpSpace *space, drawSpaceOptions *options) cpArray *arbiters = space->arbiters; for(int i=0; inum; i++){ cpArbiter *arb = (cpArbiter*)arbiters->arr[i]; - // cocos2d-x: use ccDrawPoints to optimze the speed - CCPoint *aPoints = new CCPoint[arb->numContacts]; glColor4f(COLLISION_COLOR); for(int i=0; inumContacts; i++){ cpVect v = arb->contacts[i].p; - // ccDrawPoint( ccp(v.x, v.y) ); - aPoints[i] = CCPoint(v.x, v.y); + ccDrawPoint( ccp(v.x, v.y) ); } - - ccDrawPoints( aPoints, arb->numContacts ); - delete []aPoints; } } } From 28bf48f665e151083ba055bc706bb1a20807098a Mon Sep 17 00:00:00 2001 From: Walzer Date: Mon, 11 Jul 2011 11:21:11 +0800 Subject: [PATCH 2/3] issue #569 #601, fix ChipmunkTest/Sensors crash after update to Chipmunk v5.3.5 --- tests/tests/ChipmunkTest/Sensors.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/tests/ChipmunkTest/Sensors.cpp b/tests/tests/ChipmunkTest/Sensors.cpp index 053dc33ae5..b1a0c145f4 100644 --- a/tests/tests/ChipmunkTest/Sensors.cpp +++ b/tests/tests/ChipmunkTest/Sensors.cpp @@ -66,10 +66,13 @@ blockerSeparate(cpArbiter *arb, cpSpace *space, void *unused) static void postStepRemove(cpSpace *space, cpShape *shape, void *unused) { + // cocos2d-x: bring cpSpaceRemoveShape here to avoid + // the crash on win32 platform + cpSpaceRemoveShape(space, shape); cpSpaceRemoveBody(space, shape->body); cpBodyFree(shape->body); - cpSpaceRemoveShape(space, shape); + cpShapeFree(shape); } From fb436a633975d2c5b01b2a7f6b6b4972c9f3603c Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 11 Jul 2011 15:02:41 +0800 Subject: [PATCH 3/3] [Android] break string & wrap word --- .../src/org/cocos2dx/lib/Cocos2dxBitmap.java | 35 ++++++++++++------- .../src/org/cocos2dx/lib/Cocos2dxBitmap.java | 35 ++++++++++++------- .../src/org/cocos2dx/lib/Cocos2dxBitmap.java | 35 ++++++++++++------- 3 files changed, 69 insertions(+), 36 deletions(-) diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index 53041575c0..cac65d69f8 100644 --- a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -200,25 +200,36 @@ public class Cocos2dxBitmap{ LinkedList strList = new LinkedList(); /* - * Break a String into String[] by the width + * Break a String into String[] by the width & should wrap the word */ - for (int i = 1; i <= charLength; ++i){ + for (int i = 1; i <= charLength; ++i){ tempWidth = (int)Math.ceil(paint.measureText(content, start, i)); if (tempWidth >= width){ - /* - * Should not exceed the width - */ - if (tempWidth > width){ - strList.add(content.substring(start, i - 1)); - /* - * compute from previous char + int lastIndexOfSpace = content.substring(0, i).lastIndexOf(" "); + + if (lastIndexOfSpace != -1){ + /** + * Should wrap the word */ - --i; + strList.add(content.substring(start, lastIndexOfSpace)); + i = lastIndexOfSpace; } else { - strList.add(content.substring(start, i)); + /* + * Should not exceed the width + */ + if (tempWidth > width){ + strList.add(content.substring(start, i - 1)); + /* + * compute from previous char + */ + --i; + } + else { + strList.add(content.substring(start, i)); + } } - + start = i; } } diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index 53041575c0..cac65d69f8 100644 --- a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -200,25 +200,36 @@ public class Cocos2dxBitmap{ LinkedList strList = new LinkedList(); /* - * Break a String into String[] by the width + * Break a String into String[] by the width & should wrap the word */ - for (int i = 1; i <= charLength; ++i){ + for (int i = 1; i <= charLength; ++i){ tempWidth = (int)Math.ceil(paint.measureText(content, start, i)); if (tempWidth >= width){ - /* - * Should not exceed the width - */ - if (tempWidth > width){ - strList.add(content.substring(start, i - 1)); - /* - * compute from previous char + int lastIndexOfSpace = content.substring(0, i).lastIndexOf(" "); + + if (lastIndexOfSpace != -1){ + /** + * Should wrap the word */ - --i; + strList.add(content.substring(start, lastIndexOfSpace)); + i = lastIndexOfSpace; } else { - strList.add(content.substring(start, i)); + /* + * Should not exceed the width + */ + if (tempWidth > width){ + strList.add(content.substring(start, i - 1)); + /* + * compute from previous char + */ + --i; + } + else { + strList.add(content.substring(start, i)); + } } - + start = i; } } diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index 53041575c0..cac65d69f8 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -200,25 +200,36 @@ public class Cocos2dxBitmap{ LinkedList strList = new LinkedList(); /* - * Break a String into String[] by the width + * Break a String into String[] by the width & should wrap the word */ - for (int i = 1; i <= charLength; ++i){ + for (int i = 1; i <= charLength; ++i){ tempWidth = (int)Math.ceil(paint.measureText(content, start, i)); if (tempWidth >= width){ - /* - * Should not exceed the width - */ - if (tempWidth > width){ - strList.add(content.substring(start, i - 1)); - /* - * compute from previous char + int lastIndexOfSpace = content.substring(0, i).lastIndexOf(" "); + + if (lastIndexOfSpace != -1){ + /** + * Should wrap the word */ - --i; + strList.add(content.substring(start, lastIndexOfSpace)); + i = lastIndexOfSpace; } else { - strList.add(content.substring(start, i)); + /* + * Should not exceed the width + */ + if (tempWidth > width){ + strList.add(content.substring(start, i - 1)); + /* + * compute from previous char + */ + --i; + } + else { + strList.add(content.substring(start, i)); + } } - + start = i; } }