Created
June 29, 2013 20:40
-
-
Save josephg/5892575 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export=['_main'] | |
a.out.js: test.c src/*.c src/constraints/*.c Demo/Bench.c | |
emcc -O2 -DNDEBUG $^ -Iinclude/chipmunk -s EXPORTED_FUNCTIONS="${export}" | |
a.out: test.c src/*.c src/constraints/*.c Demo/Bench.c | |
clang -O2 -DNDEBUG $^ -Iinclude/chipmunk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "chipmunk.h" | |
#include "Demo/ChipmunkDemo.h" | |
#include <stdio.h> | |
extern ChipmunkDemo bench_list[]; | |
extern int bench_count; | |
#ifdef EMSCRIPTEN | |
#include "emscripten.h" | |
float GetMilliseconds() { | |
return emscripten_get_now(); | |
} | |
#else | |
#include <sys/time.h> | |
#include <unistd.h> | |
static double GetMilliseconds(){ | |
struct timeval time; | |
gettimeofday(&time, NULL); | |
return (time.tv_sec*1000.0 + time.tv_usec/1000.0); | |
} | |
#endif | |
static void shapeFreeWrap(cpSpace *space, cpShape *shape, void *unused){ | |
cpSpaceRemoveShape(space, shape); | |
cpShapeFree(shape); | |
} | |
static void postShapeFree(cpShape *shape, cpSpace *space){ | |
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)shapeFreeWrap, shape, NULL); | |
} | |
static void constraintFreeWrap(cpSpace *space, cpConstraint *constraint, void *unused){ | |
cpSpaceRemoveConstraint(space, constraint); | |
cpConstraintFree(constraint); | |
} | |
static void postConstraintFree(cpConstraint *constraint, cpSpace *space){ | |
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)constraintFreeWrap, constraint, NULL); | |
} | |
static void bodyFreeWrap(cpSpace *space, cpBody *body, void *unused){ | |
cpSpaceRemoveBody(space, body); | |
cpBodyFree(body); | |
} | |
static void postBodyFree(cpBody *body, cpSpace *space){ | |
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)bodyFreeWrap, body, NULL); | |
} | |
void | |
ChipmunkDemoFreeSpaceChildren(cpSpace *space) | |
{ | |
// Must remove these BEFORE freeing the body or you will access dangling pointers. | |
cpSpaceEachShape(space, (cpSpaceShapeIteratorFunc)postShapeFree, space); | |
cpSpaceEachConstraint(space, (cpSpaceConstraintIteratorFunc)postConstraintFree, space); | |
cpSpaceEachBody(space, (cpSpaceBodyIteratorFunc)postBodyFree, space); | |
} | |
void time_trial(ChipmunkDemo *demos, int index, int count) | |
{ | |
cpSpace *space = demos[index].initFunc(); | |
double start_time = GetMilliseconds(); | |
for(int i=0; i<count; i++) | |
demos[index].updateFunc(space); | |
double end_time = GetMilliseconds(); | |
demos[index].destroyFunc(space); | |
printf("Time(%c) = %8.2f ms (%s)\n", index + 'a', end_time - start_time, demos[index].name); | |
} | |
int main() { | |
//for(int i=0; i<demoCount; i++) time_trial(i, 1000); | |
for(int i=0; i<bench_count; i++) time_trial(bench_list, i, 1000); | |
// time_trial('d' - 'a', 10000); | |
/* | |
cpSpace *space = cpSpaceNew(); | |
cpBody *staticBody = cpSpaceGetStaticBody(space); | |
cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-100, 0), cpv(100, 0), 10.0f)); | |
for (int i = 0; i < 10; i++) { | |
cpSpaceStep(space, 1.0f/30); | |
//printf("hi %d\n", i); | |
} | |
*/ | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="a.out.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment