diff --git a/raycastlib.h b/raycastlib.h index 11788a8..0975899 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -245,7 +245,6 @@ void castRaySquare(Ray localRay, Vector2D *nextCellOffset, collisionPointOffset->c2 = \ (((int32_t) collisionPointOffset->c1) * localRay.direction.c2) /\ ((localRay.direction.c1 == 0) ? 1 : localRay.direction.c1);\ - Unit nextC2 = localRay.start.c2 + collisionPointOffset->c2;\ } #define helper2(n1,n2,c)\ @@ -355,6 +354,7 @@ void castRayMultiHit(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t), currentSquare.x += no.x; currentSquare.y += no.y; + // offset into the next cell currentPos.x += co.x; currentPos.y += co.y; } diff --git a/test.c b/test.c index 167d268..be58c56 100644 --- a/test.c +++ b/test.c @@ -6,7 +6,7 @@ #include #include "raycastlib.h" -#include +#include int16_t testArrayFunc(int16_t x, int16_t y) { @@ -55,6 +55,42 @@ int testSingleRay(Unit startX, Unit startY, Unit dirX, Unit dirY, return result; } +// returns milliseconds +long measureTime(void (*func)(void)) +{ + long start, end; + struct timeval timecheck; + + gettimeofday(&timecheck, NULL); + start = (long) timecheck.tv_sec * 1000 + (long) timecheck.tv_usec / 1000; + + func(); + + gettimeofday(&timecheck, NULL); + end = (long) timecheck.tv_sec * 1000 + (long) timecheck.tv_usec / 1000; + + return end - start; +} + +void benchCastRays() +{ + Ray r; + + r.start.x = UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2; + r.start.y = 2 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 4; + + Vector2D directions[8]; + + for (int i = 0; i < 8; ++i) + directions[i] = angleToDirection(UNITS_PER_SQUARE / 8 * i); + + for (int i = 0; i < 1000000; ++i) + { + r.direction = directions[i % 8]; + castRay(r,testArrayFunc,30); + } +} + int main() { printf("Testing raycastlib.\n"); @@ -100,18 +136,9 @@ int main() printf("benchmark:\n"); -time_t start,end; -double dif; - - time (&start); - -for (int i = 0; i < 1000; ++i) - printf("*"); - - time (&end); - dif = difftime (end,start); - printf ("Your calculations took %.2lf seconds to run.\n", dif ); - - + long t; + t = measureTime(benchCastRays); + printf("cast 1000000 rays: %ld ms\n",t); + return 0; }