This commit is contained in:
natural-law 2011-07-11 15:17:24 +08:00
commit 58733417e0
35 changed files with 222 additions and 163 deletions

View File

@ -200,25 +200,36 @@ public class Cocos2dxBitmap{
LinkedList<String> strList = new LinkedList<String>(); LinkedList<String> strList = new LinkedList<String>();
/* /*
* 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)); tempWidth = (int)Math.ceil(paint.measureText(content, start, i));
if (tempWidth >= width){ if (tempWidth >= width){
/* int lastIndexOfSpace = content.substring(0, i).lastIndexOf(" ");
* Should not exceed the width
*/ if (lastIndexOfSpace != -1){
if (tempWidth > width){ /**
strList.add(content.substring(start, i - 1)); * Should wrap the word
/*
* compute from previous char
*/ */
--i; strList.add(content.substring(start, lastIndexOfSpace));
i = lastIndexOfSpace;
} }
else { 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; start = i;
} }
} }

View File

@ -200,25 +200,36 @@ public class Cocos2dxBitmap{
LinkedList<String> strList = new LinkedList<String>(); LinkedList<String> strList = new LinkedList<String>();
/* /*
* 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)); tempWidth = (int)Math.ceil(paint.measureText(content, start, i));
if (tempWidth >= width){ if (tempWidth >= width){
/* int lastIndexOfSpace = content.substring(0, i).lastIndexOf(" ");
* Should not exceed the width
*/ if (lastIndexOfSpace != -1){
if (tempWidth > width){ /**
strList.add(content.substring(start, i - 1)); * Should wrap the word
/*
* compute from previous char
*/ */
--i; strList.add(content.substring(start, lastIndexOfSpace));
i = lastIndexOfSpace;
} }
else { 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; start = i;
} }
} }

View File

@ -51,26 +51,6 @@ void cpMessage(const char *message, const char *condition, const char *file, int
#include "chipmunk_types.h" #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 // Maximum allocated size for various Chipmunk buffers
#define CP_BUFFER_BYTES (32*1024) #define CP_BUFFER_BYTES (32*1024)

View File

@ -58,6 +58,35 @@
#define cpfceil ceilf #define cpfceil ceilf
#endif #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 static inline cpFloat
cpfmax(cpFloat a, cpFloat b) cpfmax(cpFloat a, cpFloat b)
{ {
@ -138,7 +167,7 @@ typedef unsigned int cpHashValue;
#endif #endif
#ifdef CP_LAYERS_TYPE #ifdef CP_LAYERS_TYPE
typedef CP_GROUP_TYPE cpLayers; typedef CP_LAYERS_TYPE cpLayers;
#else #else
typedef unsigned int cpLayers; typedef unsigned int cpLayers;
#endif #endif

View File

@ -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); 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 static inline void
apply_impulses(cpBody *a , cpBody *b, cpVect r1, cpVect r2, cpVect j) apply_impulses(cpBody *a , cpBody *b, cpVect r1, cpVect r2, cpVect j)
{ {
cpBodyApplyImpulse(a, cpvneg(j), r1); apply_impulse(a, cpvneg(j), r1);
cpBodyApplyImpulse(b, j, r2); apply_impulse(b, j, r2);
} }
static inline void static inline void

View File

@ -157,7 +157,7 @@ cpArbiterGetPoint(const cpArbiter *arb, int i)
} }
static inline cpFloat static inline cpFloat
cpArbiteGetDepth(const cpArbiter *arb, int i) cpArbiterGetDepth(const cpArbiter *arb, int i)
{ {
return arb->CP_PRIVATE(contacts)[i].CP_PRIVATE(dist); return arb->CP_PRIVATE(contacts)[i].CP_PRIVATE(dist);
} }

View File

@ -192,18 +192,12 @@ cpBodyWorld2Local(const cpBody *body, const cpVect v)
return cpvunrotate(cpvsub(v, body->p), body->rot); 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. // Zero the forces on a body.
void cpBodyResetForces(cpBody *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). // 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); 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 static inline cpFloat
cpBodyKineticEnergy(const cpBody *body) cpBodyKineticEnergy(const cpBody *body)

View File

@ -29,7 +29,7 @@ typedef struct cpPolyShapeAxis{
// Convex polygon shape structure. // Convex polygon shape structure.
typedef struct cpPolyShape{ typedef struct cpPolyShape{
CP_PRIVATE(cpShape shape); cpShape shape;
// Vertex and axis lists. // Vertex and axis lists.
CP_PRIVATE(int numVerts); CP_PRIVATE(int numVerts);

View File

@ -114,7 +114,7 @@ cpBool cpShapePointQuery(cpShape *shape, cpVect p);
// Circle shape structure. // Circle shape structure.
typedef struct cpCircleShape{ typedef struct cpCircleShape{
CP_PRIVATE(cpShape shape); cpShape shape;
// Center in body space coordinates // Center in body space coordinates
CP_PRIVATE(cpVect c); CP_PRIVATE(cpVect c);
@ -135,7 +135,7 @@ CP_DeclareShapeGetter(cpCircleShape, cpFloat, Radius);
// Segment shape structure. // Segment shape structure.
typedef struct cpSegmentShape{ typedef struct cpSegmentShape{
CP_PRIVATE(cpShape shape); cpShape shape;
// Endpoints and normal of the segment. (body space coordinates) // Endpoints and normal of the segment. (body space coordinates)
cpVect CP_PRIVATE(a), CP_PRIVATE(b), CP_PRIVATE(n); cpVect CP_PRIVATE(a), CP_PRIVATE(b), CP_PRIVATE(n);

View File

@ -42,6 +42,8 @@ typedef struct cpCollisionHandler {
void *data; void *data;
} cpCollisionHandler; } cpCollisionHandler;
extern cpCollisionHandler cpSpaceDefaultHandler;
typedef struct cpContactBufferHeader { typedef struct cpContactBufferHeader {
cpTimestamp stamp; cpTimestamp stamp;
struct cpContactBufferHeader *next; struct cpContactBufferHeader *next;

View File

@ -9,7 +9,7 @@ if(BUILD_SHARED)
${chipmunk_source_files} ${chipmunk_source_files}
) )
# set the lib's version number # 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) install(TARGETS chipmunk RUNTIME DESTINATION lib LIBRARY DESTINATION lib)
endif(BUILD_SHARED) endif(BUILD_SHARED)

View File

@ -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 void
cpInitChipmunk(void) cpInitChipmunk(void)

View File

@ -83,7 +83,7 @@ CP_DefineClassGetter(cpDampedRotarySpring)
cpDampedRotarySpring * cpDampedRotarySpring *
cpDampedRotarySpringAlloc(void) cpDampedRotarySpringAlloc(void)
{ {
return (cpDampedRotarySpring *)cpmalloc(sizeof(cpDampedRotarySpring)); return (cpDampedRotarySpring *)cpcalloc(1, sizeof(cpDampedRotarySpring));
} }
cpDampedRotarySpring * cpDampedRotarySpring *

View File

@ -89,7 +89,7 @@ CP_DefineClassGetter(cpDampedSpring)
cpDampedSpring * cpDampedSpring *
cpDampedSpringAlloc(void) cpDampedSpringAlloc(void)
{ {
return (cpDampedSpring *)cpmalloc(sizeof(cpDampedSpring)); return (cpDampedSpring *)cpcalloc(1, sizeof(cpDampedSpring));
} }
cpDampedSpring * cpDampedSpring *

View File

@ -80,7 +80,7 @@ CP_DefineClassGetter(cpGearJoint)
cpGearJoint * cpGearJoint *
cpGearJointAlloc(void) cpGearJointAlloc(void)
{ {
return (cpGearJoint *)cpmalloc(sizeof(cpGearJoint)); return (cpGearJoint *)cpcalloc(1, sizeof(cpGearJoint));
} }
cpGearJoint * cpGearJoint *

View File

@ -111,7 +111,7 @@ CP_DefineClassGetter(cpGrooveJoint)
cpGrooveJoint * cpGrooveJoint *
cpGrooveJointAlloc(void) cpGrooveJointAlloc(void)
{ {
return (cpGrooveJoint *)cpmalloc(sizeof(cpGrooveJoint)); return (cpGrooveJoint *)cpcalloc(1, sizeof(cpGrooveJoint));
} }
cpGrooveJoint * cpGrooveJoint *

View File

@ -88,7 +88,7 @@ CP_DefineClassGetter(cpPinJoint);
cpPinJoint * cpPinJoint *
cpPinJointAlloc(void) cpPinJointAlloc(void)
{ {
return (cpPinJoint *)cpmalloc(sizeof(cpPinJoint)); return (cpPinJoint *)cpcalloc(1, sizeof(cpPinJoint));
} }
cpPinJoint * cpPinJoint *

View File

@ -83,7 +83,7 @@ CP_DefineClassGetter(cpPivotJoint)
cpPivotJoint * cpPivotJoint *
cpPivotJointAlloc(void) cpPivotJointAlloc(void)
{ {
return (cpPivotJoint *)cpmalloc(sizeof(cpPivotJoint)); return (cpPivotJoint *)cpcalloc(1, sizeof(cpPivotJoint));
} }
cpPivotJoint * cpPivotJoint *

View File

@ -101,7 +101,7 @@ CP_DefineClassGetter(cpRatchetJoint)
cpRatchetJoint * cpRatchetJoint *
cpRatchetJointAlloc(void) cpRatchetJointAlloc(void)
{ {
return (cpRatchetJoint *)cpmalloc(sizeof(cpRatchetJoint)); return (cpRatchetJoint *)cpcalloc(1, sizeof(cpRatchetJoint));
} }
cpRatchetJoint * cpRatchetJoint *

View File

@ -97,7 +97,7 @@ CP_DefineClassGetter(cpRotaryLimitJoint)
cpRotaryLimitJoint * cpRotaryLimitJoint *
cpRotaryLimitJointAlloc(void) cpRotaryLimitJointAlloc(void)
{ {
return (cpRotaryLimitJoint *)cpmalloc(sizeof(cpRotaryLimitJoint)); return (cpRotaryLimitJoint *)cpcalloc(1, sizeof(cpRotaryLimitJoint));
} }
cpRotaryLimitJoint * cpRotaryLimitJoint *

View File

@ -75,7 +75,7 @@ CP_DefineClassGetter(cpSimpleMotor)
cpSimpleMotor * cpSimpleMotor *
cpSimpleMotorAlloc(void) cpSimpleMotorAlloc(void)
{ {
return (cpSimpleMotor *)cpmalloc(sizeof(cpSimpleMotor)); return (cpSimpleMotor *)cpcalloc(1, sizeof(cpSimpleMotor));
} }
cpSimpleMotor * cpSimpleMotor *

View File

@ -104,7 +104,7 @@ CP_DefineClassGetter(cpSlideJoint)
cpSlideJoint * cpSlideJoint *
cpSlideJointAlloc(void) cpSlideJointAlloc(void)
{ {
return (cpSlideJoint *)cpmalloc(sizeof(cpSlideJoint)); return (cpSlideJoint *)cpcalloc(1, sizeof(cpSlideJoint));
} }
cpSlideJoint * cpSlideJoint *

View File

@ -42,7 +42,7 @@ cpArrayInit(cpArray *arr, int size)
size = (size ? size : 4); size = (size ? size : 4);
arr->max = size; arr->max = size;
arr->arr = (void **)cpmalloc(size*sizeof(void**)); arr->arr = (void **)cpcalloc(size, sizeof(void**));
return arr; return arr;
} }

View File

@ -24,6 +24,7 @@
#include <stdarg.h> #include <stdarg.h>
#include "chipmunk_private.h" #include "chipmunk_private.h"
#include "constraints/util.h"
// function declaration // function declaration
cpBody *cpBodyNewStatic(void); cpBody *cpBodyNewStatic(void);
@ -34,13 +35,13 @@ cpBody cpStaticBodySingleton;
cpBody* cpBody*
cpBodyAlloc(void) cpBodyAlloc(void)
{ {
return (cpBody *)cpmalloc(sizeof(cpBody)); return (cpBody *)cpcalloc(1, sizeof(cpBody));
} }
cpBodyVelocityFunc cpBodyUpdateVelocityDefault = cpBodyUpdateVelocity; cpBodyVelocityFunc cpBodyUpdateVelocityDefault = cpBodyUpdateVelocity;
cpBodyPositionFunc cpBodyUpdatePositionDefault = cpBodyUpdatePosition; cpBodyPositionFunc cpBodyUpdatePositionDefault = cpBodyUpdatePosition;
cpBody* cpBody *
cpBodyInit(cpBody *body, cpFloat m, cpFloat i) cpBodyInit(cpBody *body, cpFloat m, cpFloat i)
{ {
body->velocity_func = cpBodyUpdateVelocityDefault; body->velocity_func = cpBodyUpdateVelocityDefault;
@ -155,17 +156,28 @@ cpBodyUpdatePosition(cpBody *body, cpFloat dt)
void void
cpBodyResetForces(cpBody *body) cpBodyResetForces(cpBody *body)
{ {
cpBodyActivate(body);
body->f = cpvzero; body->f = cpvzero;
body->t = 0.0f; body->t = 0.0f;
} }
void 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->f = cpvadd(body->f, force);
body->t += cpvcross(r, force); body->t += cpvcross(r, force);
} }
void
cpBodyApplyImpulse(cpBody *body, const cpVect j, const cpVect r)
{
cpBodyActivate(body);
apply_impulse(body, j, r);
}
void void
cpApplyDampedSpring(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat rlen, cpFloat k, cpFloat dmp, cpFloat dt) cpApplyDampedSpring(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat rlen, cpFloat k, cpFloat dmp, cpFloat dt)
{ {

View File

@ -33,7 +33,7 @@ cpHashSetDestroy(cpHashSet *set)
// Free the table. // Free the table.
cpfree(set->table); cpfree(set->table);
cpArrayEach(set->allocatedBuffers, freeWrap, NULL); if(set->allocatedBuffers) cpArrayEach(set->allocatedBuffers, freeWrap, NULL);
cpArrayFree(set->allocatedBuffers); cpArrayFree(set->allocatedBuffers);
} }
@ -133,7 +133,7 @@ getUnusedBin(cpHashSet *set)
int count = CP_BUFFER_BYTES/sizeof(cpHashSetBin); int count = CP_BUFFER_BYTES/sizeof(cpHashSetBin);
cpAssert(count, "Buffer size is too small."); 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); cpArrayPush(set->allocatedBuffers, buffer);
// push all but the first one, return the first instead // push all but the first one, return the first instead

View File

@ -70,7 +70,7 @@ cpShapeInit(cpShape *shape, const cpShapeClass *klass, cpBody *body)
void void
cpShapeDestroy(cpShape *shape) cpShapeDestroy(cpShape *shape)
{ {
if(shape->klass->destroy) shape->klass->destroy(shape); if(shape->klass && shape->klass->destroy) shape->klass->destroy(shape);
} }
void void

View File

@ -49,7 +49,7 @@ contactSetTrans(cpShape **shapes, cpSpace *space)
int count = CP_BUFFER_BYTES/sizeof(cpArbiter); int count = CP_BUFFER_BYTES/sizeof(cpArbiter);
cpAssert(count, "Buffer size too small."); 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); cpArrayPush(space->allocatedBuffers, buffer);
for(int i=0; i<count; i++) cpArrayPush(space->pooledArbiters, buffer + i); for(int i=0; i<count; i++) cpArrayPush(space->pooledArbiters, buffer + i);
@ -71,7 +71,7 @@ collFuncSetEql(cpCollisionHandler *check, cpCollisionHandler *pair)
static void * static void *
collFuncSetTrans(cpCollisionHandler *handler, void *unused) collFuncSetTrans(cpCollisionHandler *handler, void *unused)
{ {
cpCollisionHandler *copy = (cpCollisionHandler *)cpmalloc(sizeof(cpCollisionHandler)); cpCollisionHandler *copy = (cpCollisionHandler *)cpcalloc(1, sizeof(cpCollisionHandler));
(*copy) = (*handler); (*copy) = (*handler);
return copy; return copy;
@ -105,7 +105,7 @@ cpSpaceAlloc(void)
#define DEFAULT_ITERATIONS 10 #define DEFAULT_ITERATIONS 10
#define DEFAULT_ELASTIC_ITERATIONS 0 #define DEFAULT_ELASTIC_ITERATIONS 0
cpCollisionHandler defaultHandler = {0, 0, alwaysCollide, alwaysCollide, nothing, nothing, NULL}; cpCollisionHandler cpSpaceDefaultHandler = {0, 0, alwaysCollide, alwaysCollide, nothing, nothing, NULL};
cpSpace* cpSpace*
cpSpaceInit(cpSpace *space) cpSpaceInit(cpSpace *space)
@ -140,9 +140,9 @@ cpSpaceInit(cpSpace *space)
space->constraints = cpArrayNew(0); space->constraints = cpArrayNew(0);
space->defaultHandler = defaultHandler; space->defaultHandler = cpSpaceDefaultHandler;
space->collFuncSet = cpHashSetNew(0, (cpHashSetEqlFunc)collFuncSetEql, (cpHashSetTransFunc)collFuncSetTrans); space->collFuncSet = cpHashSetNew(0, (cpHashSetEqlFunc)collFuncSetEql, (cpHashSetTransFunc)collFuncSetTrans);
space->collFuncSet->default_value = &space->defaultHandler; space->collFuncSet->default_value = &cpSpaceDefaultHandler;
space->postStepCallbacks = NULL; space->postStepCallbacks = NULL;
@ -174,20 +174,14 @@ cpSpaceDestroy(cpSpace *space)
cpArrayFree(space->arbiters); cpArrayFree(space->arbiters);
cpArrayFree(space->pooledArbiters); cpArrayFree(space->pooledArbiters);
if(space->allocatedBuffers){ if(space->allocatedBuffers) cpArrayEach(space->allocatedBuffers, freeWrap, NULL);
cpArrayEach(space->allocatedBuffers, freeWrap, NULL); cpArrayFree(space->allocatedBuffers);
cpArrayFree(space->allocatedBuffers);
}
if(space->postStepCallbacks){ if(space->postStepCallbacks) cpHashSetEach(space->postStepCallbacks, freeWrap, NULL);
cpHashSetEach(space->postStepCallbacks, freeWrap, NULL); cpHashSetFree(space->postStepCallbacks);
cpHashSetFree(space->postStepCallbacks);
}
if(space->collFuncSet){ if(space->collFuncSet) cpHashSetEach(space->collFuncSet, freeWrap, NULL);
cpHashSetEach(space->collFuncSet, freeWrap, NULL); cpHashSetFree(space->collFuncSet);
cpHashSetFree(space->collFuncSet);
}
} }
void void
@ -211,6 +205,12 @@ cpSpaceFreeChildren(cpSpace *space)
cpArrayEach(space->constraints, (cpArrayIter)&constraintFreeWrap, NULL); 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 #pragma mark Collision Handler Function Management
void void
@ -223,6 +223,8 @@ cpSpaceAddCollisionHandler(
cpCollisionSeparateFunc separate, cpCollisionSeparateFunc separate,
void *data void *data
){ ){
cpAssertSpaceUnlocked(space);
// Remove any old function so the new one will get added. // Remove any old function so the new one will get added.
cpSpaceRemoveCollisionHandler(space, a, b); cpSpaceRemoveCollisionHandler(space, a, b);
@ -241,6 +243,8 @@ cpSpaceAddCollisionHandler(
void void
cpSpaceRemoveCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b) cpSpaceRemoveCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b)
{ {
cpAssertSpaceUnlocked(space);
struct{cpCollisionType a, b;} ids = {a, b}; struct{cpCollisionType a, b;} ids = {a, b};
cpCollisionHandler *old_handler = (cpCollisionHandler *) cpHashSetRemove(space->collFuncSet, CP_HASH_PAIR(a, b), &ids); cpCollisionHandler *old_handler = (cpCollisionHandler *) cpHashSetRemove(space->collFuncSet, CP_HASH_PAIR(a, b), &ids);
cpfree(old_handler); cpfree(old_handler);
@ -255,6 +259,8 @@ cpSpaceSetDefaultCollisionHandler(
cpCollisionSeparateFunc separate, cpCollisionSeparateFunc separate,
void *data void *data
){ ){
cpAssertSpaceUnlocked(space);
cpCollisionHandler handler = { cpCollisionHandler handler = {
0, 0, 0, 0,
begin ? begin : alwaysCollide, begin ? begin : alwaysCollide,
@ -265,16 +271,10 @@ cpSpaceSetDefaultCollisionHandler(
}; };
space->defaultHandler = handler; space->defaultHandler = handler;
space->collFuncSet->default_value = &space->defaultHandler;
} }
#pragma mark Body, Shape, and Joint Management #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 static void
cpBodyAddShape(cpBody *body, cpShape *shape) cpBodyAddShape(cpBody *body, cpShape *shape)
{ {
@ -479,21 +479,23 @@ cpSpaceRehashShape(cpSpace *space, cpShape *shape)
void void
cpSpaceEachBody(cpSpace *space, cpSpaceBodyIterator func, void *data) cpSpaceEachBody(cpSpace *space, cpSpaceBodyIterator func, void *data)
{ {
cpArray *bodies = space->bodies; cpSpaceLock(space); {
cpArray *bodies = space->bodies;
for(int i=0; i<bodies->num; i++){
func((cpBody *)bodies->arr[i], data); for(int i=0; i<bodies->num; i++){
} func((cpBody *)bodies->arr[i], data);
}
cpArray *components = space->sleepingComponents;
for(int i=0; i<components->num; i++){ cpArray *components = space->sleepingComponents;
cpBody *root = (cpBody *)components->arr[i]; for(int i=0; i<components->num; i++){
cpBody *body = root, *next; cpBody *root = (cpBody *)components->arr[i];
do { cpBody *body = root, *next;
next = body->node.next; do {
func(body, data); next = body->node.next;
} while((body = next) != root); func(body, data);
} } while((body = next) != root);
}
} cpSpaceUnlock(space);
} }

View File

@ -97,7 +97,10 @@ componentActivate(cpBody *root)
void void
cpBodyActivate(cpBody *body) cpBodyActivate(cpBody *body)
{ {
componentActivate(componentNodeRoot(body)); if(!cpBodyIsRogue(body)){
body->node.idleTime = 0.0f;
componentActivate(componentNodeRoot(body));
}
} }
static inline void static inline void

View File

@ -72,7 +72,7 @@ handleSetTrans(void *obj, cpSpaceHash *hash)
int count = CP_BUFFER_BYTES/sizeof(cpHandle); int count = CP_BUFFER_BYTES/sizeof(cpHandle);
cpAssert(count, "Buffer size is too small."); 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); cpArrayPush(hash->allocatedBuffers, buffer);
for(int i=0; i<count; i++) cpArrayPush(hash->pooledHandles, buffer + i); for(int i=0; i<count; i++) cpArrayPush(hash->pooledHandles, buffer + i);
@ -144,15 +144,15 @@ static void freeWrap(void *ptr, void *unused){cpfree(ptr);}
void void
cpSpaceHashDestroy(cpSpaceHash *hash) cpSpaceHashDestroy(cpSpaceHash *hash)
{ {
clearHash(hash); if(hash->table) clearHash(hash);
cpfree(hash->table);
cpHashSetFree(hash->handleSet); cpHashSetFree(hash->handleSet);
cpArrayEach(hash->allocatedBuffers, freeWrap, NULL); if(hash->allocatedBuffers) cpArrayEach(hash->allocatedBuffers, freeWrap, NULL);
cpArrayFree(hash->allocatedBuffers); cpArrayFree(hash->allocatedBuffers);
cpArrayFree(hash->pooledHandles);
cpfree(hash->table); cpArrayFree(hash->pooledHandles);
} }
void void
@ -200,7 +200,7 @@ getEmptyBin(cpSpaceHash *hash)
int count = CP_BUFFER_BYTES/sizeof(cpSpaceHashBin); int count = CP_BUFFER_BYTES/sizeof(cpSpaceHashBin);
cpAssert(count, "Buffer size is too small."); 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); cpArrayPush(hash->allocatedBuffers, buffer);
// push all but the first one, return the first instead // push all but the first one, return the first instead

View File

@ -201,7 +201,7 @@ shapeQueryHelper(cpShape *a, cpShape *b, shapeQueryContext *context)
if( if(
(a->group && a->group == b->group) || (a->group && a->group == b->group) ||
!(a->layers & b->layers) || !(a->layers & b->layers) ||
a->sensor || b->sensor a == b
) return; ) return;
cpContact contacts[CP_MAX_CONTACTS_PER_ARBITER]; cpContact contacts[CP_MAX_CONTACTS_PER_ARBITER];
@ -216,7 +216,7 @@ shapeQueryHelper(cpShape *a, cpShape *b, shapeQueryContext *context)
} }
if(numContacts){ if(numContacts){
context->anyCollision = cpTrue; context->anyCollision = !(a->sensor || b->sensor);
if(context->func){ if(context->func){
cpContactPointSet set = {numContacts, {}}; cpContactPointSet set = {numContacts, {}};

View File

@ -41,7 +41,7 @@ postStepFuncSetEql(PostStepCallback *a, PostStepCallback *b){
static void * static void *
postStepFuncSetTrans(PostStepCallback *callback, void *ignored) postStepFuncSetTrans(PostStepCallback *callback, void *ignored)
{ {
PostStepCallback *value = (PostStepCallback *)cpmalloc(sizeof(PostStepCallback)); PostStepCallback *value = (PostStepCallback *)cpcalloc(1, sizeof(PostStepCallback));
(*value) = (*callback); (*value) = (*callback);
return value; return value;
@ -81,7 +81,7 @@ typedef struct cpContactBuffer {
static cpContactBufferHeader * static cpContactBufferHeader *
cpSpaceAllocContactBuffer(cpSpace *space) cpSpaceAllocContactBuffer(cpSpace *space)
{ {
cpContactBuffer *buffer = (cpContactBuffer *)cpmalloc(sizeof(cpContactBuffer)); cpContactBuffer *buffer = (cpContactBuffer *)cpcalloc(1, sizeof(cpContactBuffer));
cpArrayPush(space->allocatedBuffers, buffer); cpArrayPush(space->allocatedBuffers, buffer);
return (cpContactBufferHeader *)buffer; return (cpContactBufferHeader *)buffer;
} }
@ -143,6 +143,13 @@ cpSpacePopContacts(cpSpace *space, int count){
#pragma mark Collision Detection Functions #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 static inline cpBool
queryReject(cpShape *a, cpShape *b) queryReject(cpShape *a, cpShape *b)
{ {
@ -164,13 +171,10 @@ queryFunc(cpShape *a, cpShape *b, cpSpace *space)
// Reject any of the simple cases // Reject any of the simple cases
if(queryReject(a,b)) return; if(queryReject(a,b)) return;
// Find the collision pair function for the shapes. cpCollisionHandler *handler = lookupCollisionHandler(space, a->collision_type, b->collision_type);
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);
cpBool sensor = a->sensor || b->sensor; 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() ) // Shape 'a' should have the lower shape type. (required by cpCollideShapes() )
if(a->klass->type > b->klass->type){ if(a->klass->type > b->klass->type){
@ -255,7 +259,9 @@ contactSetFilter(cpArbiter *arb, cpSpace *space)
// was used last frame, but not this one // was used last frame, but not this one
if(ticks >= 1 && arb->state != cpArbiterStateCached){ 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; arb->state = cpArbiterStateCached;
} }

View File

@ -200,25 +200,36 @@ public class Cocos2dxBitmap{
LinkedList<String> strList = new LinkedList<String>(); LinkedList<String> strList = new LinkedList<String>();
/* /*
* 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)); tempWidth = (int)Math.ceil(paint.measureText(content, start, i));
if (tempWidth >= width){ if (tempWidth >= width){
/* int lastIndexOfSpace = content.substring(0, i).lastIndexOf(" ");
* Should not exceed the width
*/ if (lastIndexOfSpace != -1){
if (tempWidth > width){ /**
strList.add(content.substring(start, i - 1)); * Should wrap the word
/*
* compute from previous char
*/ */
--i; strList.add(content.substring(start, lastIndexOfSpace));
i = lastIndexOfSpace;
} }
else { 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; start = i;
} }
} }

View File

@ -1 +1 @@
ca6671a2edbcbfdddbb140f60d4a868feab7747f 33ad1fd0694c56b7b34f9ebd3e563782e572c270

View File

@ -65,11 +65,14 @@ blockerSeparate(cpArbiter *arb, cpSpace *space, void *unused)
static void static void
postStepRemove(cpSpace *space, cpShape *shape, void *unused) postStepRemove(cpSpace *space, cpShape *shape, void *unused)
{ {
// cocos2d-x: bring cpSpaceRemoveShape here to avoid
// the crash on win32 platform
cpSpaceRemoveShape(space, shape); cpSpaceRemoveShape(space, shape);
cpSpaceRemoveBody(space, shape->body); cpSpaceRemoveBody(space, shape->body);
cpBodyFree(shape->body); cpBodyFree(shape->body);
cpShapeFree(shape); cpShapeFree(shape);
} }

View File

@ -500,17 +500,12 @@ drawSpace(cpSpace *space, drawSpaceOptions *options)
glPointSize(options->bodyPointSize); glPointSize(options->bodyPointSize);
cpArray *bodies = space->bodies; cpArray *bodies = space->bodies;
// cocos2d-x: use ccDrawPoints to optimize the speed
CCPoint *aPoints = new CCPoint[bodies->num];
glColor4f(LINE_COLOR); glColor4f(LINE_COLOR);
for(int i=0, count = bodies->num; i<count; i++){ for(int i=0, count = bodies->num; i<count; i++){
cpBody *body = (cpBody *)bodies->arr[i]; cpBody *body = (cpBody *)bodies->arr[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); // glColor3f(0.5f, 0.5f, 0.5f);
// cpArray *components = space->components; // cpArray *components = space->components;
@ -529,18 +524,12 @@ drawSpace(cpSpace *space, drawSpaceOptions *options)
cpArray *arbiters = space->arbiters; cpArray *arbiters = space->arbiters;
for(int i=0; i<arbiters->num; i++){ for(int i=0; i<arbiters->num; i++){
cpArbiter *arb = (cpArbiter*)arbiters->arr[i]; cpArbiter *arb = (cpArbiter*)arbiters->arr[i];
// cocos2d-x: use ccDrawPoints to optimze the speed
CCPoint *aPoints = new CCPoint[arb->numContacts];
glColor4f(COLLISION_COLOR); glColor4f(COLLISION_COLOR);
for(int i=0; i<arb->numContacts; i++){ for(int i=0; i<arb->numContacts; i++){
cpVect v = arb->contacts[i].p; cpVect v = arb->contacts[i].p;
// ccDrawPoint( ccp(v.x, v.y) ); ccDrawPoint( ccp(v.x, v.y) );
aPoints[i] = CCPoint(v.x, v.y);
} }
ccDrawPoints( aPoints, arb->numContacts );
delete []aPoints;
} }
} }
} }