diff --git a/raycastlib.h b/raycastlib.h index c43e4c8..4f18ecd 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -200,6 +200,8 @@ void render(Camera cam, ArrayFunction arrayFunc, PixelFunction pixelFunc, uint32_t profile_normalize = 0; uint32_t profile_vectorsAngleCos = 0; uint32_t profile_perspectiveScale = 0; + uint32_t profile_wrap = 0; + uint32_t profile_divRoundDown = 0; #define profileCall(c) profile_##c += 1 #define printProfile() {\ @@ -217,7 +219,9 @@ void render(Camera cam, ArrayFunction arrayFunc, PixelFunction pixelFunc, printf(" normalize: %d\n",profile_normalize);\ printf(" vectorsAngleCos: %d\n",profile_vectorsAngleCos);\ printf(" absVal: %d\n",profile_absVal);\ - printf(" perspectiveScale: %d\n",profile_perspectiveScale); } + printf(" perspectiveScale: %d\n",profile_perspectiveScale);\ + printf(" wrap: %d\n",profile_wrap);\ + printf(" divRoundDown: %d\n",profile_divRoundDown); } #else #define profileCall(c) #endif @@ -226,32 +230,38 @@ Unit clamp(Unit value, Unit valueMin, Unit valueMax) { profileCall(clamp); - if (value < valueMin) + if (value >= valueMin) + { + if (value <= valueMax) + return value; + else + return valueMax; + } + else return valueMin; - - if (value > valueMax) - return valueMax; - - return value; } inline Unit absVal(Unit value) { profileCall(absVal); - return value < 0 ? -1 * value : value; + return value >= 0 ? value: -1 * value; } /// Like mod, but behaves differently for negative values. inline Unit wrap(Unit value, Unit mod) { - return value < 0 ? (mod + (value % mod) - 1) : (value % mod); + profileCall(wrap); + + return value >= 0 ? (value % mod) : (mod + (value % mod) - 1); } /// Performs division, rounding down, NOT towards zero. inline Unit divRoundDown(Unit value, Unit divisor) { - return value / divisor - ( (value < 0) ? 1 : 0); + profileCall(divRoundDown); + + return value / divisor - ((value >= 0) ? 0 : 1); } // Bhaskara's cosine approximation formula @@ -830,12 +840,10 @@ Unit perspectiveScale(Unit originalSize, Unit distance) { profileCall(perspectiveScale); - if (distance == 0) - return 0; - - return (originalSize * UNITS_PER_SQUARE) / - ((VERTICAL_FOV * 2 * distance) / UNITS_PER_SQUARE); - // ^ approximation of tan function + return distance != 0 ? + (originalSize * UNITS_PER_SQUARE) / + ((VERTICAL_FOV * 2 * distance) / UNITS_PER_SQUARE) + : 0; } #endif diff --git a/test.c b/test.c index 6551847..021703b 100644 --- a/test.c +++ b/test.c @@ -167,6 +167,31 @@ void benchmarkMapping() } } +void pixelFunc(PixelInfo p) +{ +} + +void benchmarkRender() +{ + Camera c; + + c.resolution.x = 640; + c.resolution.y = 300; + c.position.x = 10; + c.position.y = 12; + c.direction = 100; + c.height = 200; + c.fovAngle = UNITS_PER_SQUARE / 3; + + RayConstraints constraints; + + constraints.maxHits = 10; + constraints.maxSteps = 12; + + for (int i = 0; i < 100; ++i) + render(c,testArrayFunc,pixelFunc,constraints); +} + int main() { printf("Testing raycastlib.\n"); @@ -232,7 +257,10 @@ int main() t = measureTime(benchmarkMapping); printf("map point to screen 1000000 times: %ld ms\n",t); - + + t = measureTime(benchmarkRender); + printf("render 100 times: %ld ms\n",t); + printf("\n"); printProfile();