2012-03-21 14:53:57 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "chipmunk_private.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
cpSpatialIndexFree(cpSpatialIndex *index)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if(index){
|
|
|
|
cpSpatialIndexDestroy(index);
|
|
|
|
cpfree(index);
|
|
|
|
}
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cpSpatialIndex *
|
|
|
|
cpSpatialIndexInit(cpSpatialIndex *index, cpSpatialIndexClass *klass, cpSpatialIndexBBFunc bbfunc, cpSpatialIndex *staticIndex)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
index->klass = klass;
|
|
|
|
index->bbfunc = bbfunc;
|
|
|
|
index->staticIndex = staticIndex;
|
|
|
|
|
|
|
|
if(staticIndex){
|
|
|
|
cpAssertHard(!staticIndex->dynamicIndex, "This static index is already associated with a dynamic index.");
|
|
|
|
staticIndex->dynamicIndex = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct dynamicToStaticContext {
|
2012-04-19 14:35:52 +08:00
|
|
|
cpSpatialIndexBBFunc bbfunc;
|
|
|
|
cpSpatialIndex *staticIndex;
|
|
|
|
cpSpatialIndexQueryFunc queryFunc;
|
|
|
|
void *data;
|
2012-03-21 14:53:57 +08:00
|
|
|
} dynamicToStaticContext;
|
|
|
|
|
|
|
|
static void
|
|
|
|
dynamicToStaticIter(void *obj, dynamicToStaticContext *context)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpSpatialIndexQuery(context->staticIndex, obj, context->bbfunc(obj), context->queryFunc, context->data);
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpSpatialIndexCollideStatic(cpSpatialIndex *dynamicIndex, cpSpatialIndex *staticIndex, cpSpatialIndexQueryFunc func, void *data)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if(cpSpatialIndexCount(staticIndex) > 0){
|
|
|
|
dynamicToStaticContext context = {dynamicIndex->bbfunc, staticIndex, func, data};
|
|
|
|
cpSpatialIndexEach(dynamicIndex, (cpSpatialIndexIteratorFunc)dynamicToStaticIter, &context);
|
|
|
|
}
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|