From 695f921c2d17e61773b49e5e5b41ad399f19e4e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Mon, 3 Sep 2018 16:55:30 +0200 Subject: [PATCH] Add wrap function --- raycastlib.h | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/raycastlib.h b/raycastlib.h index 1491795..8e7658e 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -221,6 +221,12 @@ inline Unit absVal(Unit value) return value < 0 ? -1 * value : 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); +} + /// Performs division, rounding down, NOT towards zero. inline Unit divRoundDown(Unit value, Unit divisor) { @@ -238,10 +244,7 @@ Unit cosInt(Unit input) // TODO: could be optimized with LUT - input = input % UNITS_PER_SQUARE; - - if (input < 0) - input = UNITS_PER_SQUARE + input; + input = wrap(input,UNITS_PER_SQUARE); if (input < UNITS_PER_SQUARE / 4) return trigHelper(input); @@ -473,13 +476,8 @@ void castRayMultiHit(Ray ray, ArrayFunction arrayFunc, HitResult *hitResults, break; } - ray.start.x = currentPos.x < 0 ? - (UNITS_PER_SQUARE + currentPos.x % UNITS_PER_SQUARE - 1) : - (currentPos.x % UNITS_PER_SQUARE); - - ray.start.y = currentPos.y < 0 ? - (UNITS_PER_SQUARE + currentPos.y % UNITS_PER_SQUARE - 1) : - (currentPos.y % UNITS_PER_SQUARE); + ray.start.x = wrap(currentPos.x,UNITS_PER_SQUARE); + ray.start.y = wrap(currentPos.y,UNITS_PER_SQUARE); castRaySquare(ray,&no,&co); @@ -554,7 +552,8 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray) int_maybe32_t y2 = 0; // screen y (for ceil), will only fo down Unit worldZPrev = _startHeight; - Unit worldZPrevCeil = UNITS_PER_SQUARE * 5 - _startHeight - 2 * _camera.height; + Unit worldZPrevCeil = + UNITS_PER_SQUARE * 5 - _startHeight - 2 * _camera.height; PixelInfo p; p.position.x = x;