static cpContactBufferHeader * cpSpaceGetFreeContactBuffer(cpSpace *space) { if(space->stamp - space->contactBuffersTail->stamp > cp_contact_persistence){ cpContactBufferHeader *header = space->contactBuffersTail; space->contactBuffersTail = header->next; return cpContactBufferHeaderInit(header, space); } else { cpContactBufferHeader *header = cpSpaceAllocContactBuffer(space); return cpContactBufferHeaderInit(header, space); } }
static void cpSpacePushFreshContactBuffer(cpSpace *space) { cpTimestamp stamp = space->stamp; cpContactBufferHeader *head = space->contactBuffersHead; if(!head){ // No buffers have been allocated, make one space->contactBuffersHead = cpContactBufferHeaderInit(cpSpaceAllocContactBuffer(space), stamp, NULL); } else if(stamp - head->next->stamp > cp_contact_persistence){ // The tail buffer is available, rotate the ring cpContactBufferHeader *tail = head->next; space->contactBuffersHead = cpContactBufferHeaderInit(tail, stamp, tail); } else { // Allocate a new buffer and push it into the ring cpContactBufferHeader *buffer = cpContactBufferHeaderInit(cpSpaceAllocContactBuffer(space), stamp, head); space->contactBuffersHead = head->next = buffer; } }
cpSpace* cpSpaceInit(cpSpace *space) { space->iterations = DEFAULT_ITERATIONS; space->elasticIterations = DEFAULT_ELASTIC_ITERATIONS; // space->sleepTicks = 300; space->gravity = cpvzero; space->damping = 1.0f; space->locked = 0; space->stamp = 0; space->staticShapes = cpSpaceHashNew(DEFAULT_DIM_SIZE, DEFAULT_COUNT, (cpSpaceHashBBFunc)shapeBBFunc); space->activeShapes = cpSpaceHashNew(DEFAULT_DIM_SIZE, DEFAULT_COUNT, (cpSpaceHashBBFunc)shapeBBFunc); space->allocatedBuffers = cpArrayNew(0); space->bodies = cpArrayNew(0); space->sleepingComponents = cpArrayNew(0); space->sleepTimeThreshold = INFINITY; space->idleSpeedThreshold = 0.0f; space->arbiters = cpArrayNew(0); space->pooledArbiters = cpArrayNew(0); cpContactBufferHeader *header = cpContactBufferHeaderInit(cpSpaceAllocContactBuffer(space), space); space->contactBuffersHead = header; space->contactBuffersTail = header; header->next = header; // Buffers will form a ring, start the ring explicitly space->contactSet = cpHashSetNew(0, (cpHashSetEqlFunc)contactSetEql, (cpHashSetTransFunc)contactSetTrans); space->constraints = cpArrayNew(0); space->defaultHandler = defaultHandler; space->collFuncSet = cpHashSetNew(0, (cpHashSetEqlFunc)collFuncSetEql, (cpHashSetTransFunc)collFuncSetTrans); space->collFuncSet->default_value = &space->defaultHandler; space->postStepCallbacks = cpHashSetNew(0, (cpHashSetEqlFunc)postStepFuncSetEql, (cpHashSetTransFunc)postStepFuncSetTrans); cpBodyInit(&space->staticBody, INFINITY, INFINITY); space->staticBody.space = space; return space; }