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