| 1 |
/* Copyright (c) 2013 Scott Lembcke and Howling Moon Software |
| 2 |
* |
| 3 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
* of this software and associated documentation files (the "Software"), to deal |
| 5 |
* in the Software without restriction, including without limitation the rights |
| 6 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
* copies of the Software, and to permit persons to whom the Software is |
| 8 |
* furnished to do so, subject to the following conditions: |
| 9 |
* |
| 10 |
* The above copyright notice and this permission notice shall be included in |
| 11 |
* all copies or substantial portions of the Software. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
* SOFTWARE. |
| 20 |
*/ |
| 21 |
|
| 22 |
#import <stdlib.h> |
| 23 |
#import <stdio.h> |
| 24 |
#import "chipmunk/chipmunk.h" |
| 25 |
|
| 26 |
#import "ChipmunkDemo.h" |
| 27 |
|
| 28 |
static void shapeFreeWrap(cpSpace *space, cpShape *shape, void *unused){ |
| 29 |
cpSpaceRemoveShape(space, shape); |
| 30 |
cpShapeFree(shape); |
| 31 |
} |
| 32 |
|
| 33 |
static void postShapeFree(cpShape *shape, cpSpace *space){ |
| 34 |
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)shapeFreeWrap, shape, NULL); |
| 35 |
} |
| 36 |
|
| 37 |
static void constraintFreeWrap(cpSpace *space, cpConstraint *constraint, void *unused){ |
| 38 |
cpSpaceRemoveConstraint(space, constraint); |
| 39 |
cpConstraintFree(constraint); |
| 40 |
} |
| 41 |
|
| 42 |
static void postConstraintFree(cpConstraint *constraint, cpSpace *space){ |
| 43 |
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)constraintFreeWrap, constraint, NULL); |
| 44 |
} |
| 45 |
|
| 46 |
static void bodyFreeWrap(cpSpace *space, cpBody *body, void *unused){ |
| 47 |
cpSpaceRemoveBody(space, body); |
| 48 |
cpBodyFree(body); |
| 49 |
} |
| 50 |
|
| 51 |
static void postBodyFree(cpBody *body, cpSpace *space){ |
| 52 |
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)bodyFreeWrap, body, NULL); |
| 53 |
} |
| 54 |
|
| 55 |
// Safe and future proof way to remove and free all objects that have been added to the space. |
| 56 |
void |
| 57 |
ChipmunkDemoFreeSpaceChildren(cpSpace *space) |
| 58 |
{ |
| 59 |
// Must remove these BEFORE freeing the body or you will access dangling pointers. |
| 60 |
cpSpaceEachShape(space, (cpSpaceShapeIteratorFunc)postShapeFree, space); |
| 61 |
cpSpaceEachConstraint(space, (cpSpaceConstraintIteratorFunc)postConstraintFree, space); |
| 62 |
|
| 63 |
cpSpaceEachBody(space, (cpSpaceBodyIteratorFunc)postBodyFree, space); |
| 64 |
} |
| 65 |
|
| 66 |
void ChipmunkDemoDefaultDrawImpl(cpSpace *space){} |
| 67 |
|
| 68 |
|
| 69 |
extern ChipmunkDemo bench_list[]; |
| 70 |
extern int bench_count; |
| 71 |
|
| 72 |
#include <sys/time.h> |
| 73 |
#include <unistd.h> |
| 74 |
|
| 75 |
static double GetMilliseconds(){ |
| 76 |
struct timeval time; |
| 77 |
gettimeofday(&time, NULL); |
| 78 |
|
| 79 |
return (time.tv_sec*1000.0 + time.tv_usec/1000.0); |
| 80 |
} |
| 81 |
|
| 82 |
static void time_trial(int index, int count) |
| 83 |
{ |
| 84 |
cpSpace *space = bench_list[index].initFunc(); |
| 85 |
|
| 86 |
double start_time = GetMilliseconds(); |
| 87 |
|
| 88 |
for(int i=0; i<count; i++) |
| 89 |
bench_list[index].updateFunc(space, 1.0/60.0); |
| 90 |
|
| 91 |
double end_time = GetMilliseconds(); |
| 92 |
|
| 93 |
bench_list[index].destroyFunc(space); |
| 94 |
|
| 95 |
printf("Time(%c) = %8.2f ms (%s)\n", index + 'a', end_time - start_time, bench_list[index].name); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
int main(int argc, char *argv[]) |
| 100 |
{ |
| 101 |
printf("Starting Benchmarks.\n"); |
| 102 |
for(int i=0; i<bench_count; i++) time_trial(i, 1000); |
| 103 |
// time_trial('g' - 'a', 10000); |
| 104 |
|
| 105 |
printf("Benchmarks Complete.\n"); |
| 106 |
return 0; |
| 107 |
} |