From b59f44048b8c7d5cdb4247b6de4e33a22d62b836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Tue, 4 Sep 2018 13:01:14 +0200 Subject: [PATCH] Add tests --- raycastlib.h | 13 ++++++++++- test.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/raycastlib.h b/raycastlib.h index e12ce31..f0aae46 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -58,6 +58,17 @@ typedef uint16_t uint_maybe32_t; printf(" dist: %d\n", h.distance);\ printf(" texcoord: %d\n", h.textureCoord);}\ +#define logPixelInfo(p){\ + printf("pixel:\n");\ + printf(" position: ");\ + logVector2D(p.position);\ + printf(" depth: %d\n", p.depth);\ + printf(" wall: %d\n", p.isWall);\ + printf(" textCoordY: %d\n", p.textureCoordY);\ + printf(" hit: ");\ + logHitResult(p.hit);\ + }\ + /// Position in 2D space. typedef struct { @@ -800,7 +811,7 @@ PixelInfo mapToScreen(Vector2D worldPosition, Unit height, Camera camera) result.position.x = (a * middleColumn) / b; result.position.x = 2 * (middleColumn - result.position.x); - + // TODO: ^ why is this 2 here? return result; } diff --git a/test.c b/test.c index 6e47cbb..a204855 100644 --- a/test.c +++ b/test.c @@ -57,6 +57,49 @@ int testSingleRay(Unit startX, Unit startY, Unit dirX, Unit dirY, return result; } +int testSingleMapping(Unit posX, Unit posY, Unit posZ, uint32_t resX, + uint32_t resY, Unit camX, Unit camY, Unit camZ, Unit camDir, Unit fov, + Unit expectX, Unit expectY, Unit expectZ) +{ + int result; + + Camera c; + + c.resolution.x = resX; + c.resolution.y = resY; + c.position.x = camY; + c.position.y = camY; + c.direction = camDir; + c.height = posZ; + c.fovAngle = fov; + + Vector2D pos; + Unit height; + + pos.x = posX; + pos.y = posY; + height = posZ; + + PixelInfo p; + + printf("- mapping pixel: %d %d %d\n",posX,posY,posZ); + + p = mapToScreen(pos,height,c); + + printf("- result:\n"); + logPixelInfo(p); + + result = p.position.x == expectX && p.position.y == expectY && + p.depth == expectZ; + + if (result) + printf("\nOK\n\n"); + else + printf("\nFAIL\n\n"); + + return result; +} + // returns milliseconds long measureTime(void (*func)(void)) { @@ -95,7 +138,6 @@ void benchCastRays() void benchmarkMapping() { - Vector2D v; Camera c; c.resolution.x = 1024; @@ -165,6 +207,23 @@ int main() 16)) return 1; + if (!testSingleMapping( + -UNITS_PER_SQUARE, + 0, + UNITS_PER_SQUARE / 2, + 1280, + 640, + 0, + 0, + 0, + UNITS_PER_SQUARE / 2, + UNITS_PER_SQUARE / 4, + 1280, // shouldn't be half? + 320, + 1024 + )) + return -1; + for (Unit i = -UNITS_PER_SQUARE; i <= UNITS_PER_SQUARE; i += 64) { Unit v = sinInt(i);