From a77201bb2010aacedd1b254c6199b2a2a1e02aef Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 24 Nov 2025 17:23:27 +0100 Subject: [PATCH] R_QuickCamDist & R_QuickDist: turn into function and make it fixed point since we mostly use it in fixed point maths, this just makes alot more sense moving it into inline functions ensures that in C code this wont evaluate its arguments multiple times --- src/hardware/hw_main.cpp | 10 +++++----- src/k_kart.c | 2 +- src/r_things.cpp | 4 ++-- src/r_things.h | 25 ++++++++++++++++--------- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/hardware/hw_main.cpp b/src/hardware/hw_main.cpp index a7ea883b7..f0d9f129c 100644 --- a/src/hardware/hw_main.cpp +++ b/src/hardware/hw_main.cpp @@ -382,7 +382,7 @@ void HWR_ObjectLightLevelPost(gl_vissprite_t *spr, const sector_t *sector, INT32 extralight = FixedMul(extralight, std::min(std::max(0, *lightlevel), 255) * FRACUNIT / 255); // simple OGL approximation - fixed_t tr = R_QuickCamDist(spr->mobj->x, spr->mobj->y) << FRACBITS; + fixed_t tr = R_QuickCamDist(spr->mobj->x, spr->mobj->y); fixed_t xscale = FixedDiv((vid.width / 2) << FRACBITS, tr); // Less change in contrast at further distances, to counteract DOOM diminished light @@ -2537,18 +2537,18 @@ static boolean HWR_CheckBBox(const fixed_t *bspcoord) if (viewx < bspcoord[BOXLEFT]) // 1,4,6 { if (viewy > bspcoord[BOXTOP]) // 1 - mindist = R_QuickCamDist(bspcoord[BOXLEFT], bspcoord[BOXTOP]) << FRACBITS; + mindist = R_QuickCamDist(bspcoord[BOXLEFT], bspcoord[BOXTOP]); else if (viewy < bspcoord[BOXBOTTOM]) // 6 - mindist = R_QuickCamDist(bspcoord[BOXLEFT], bspcoord[BOXBOTTOM]) << FRACBITS; + mindist = R_QuickCamDist(bspcoord[BOXLEFT], bspcoord[BOXBOTTOM]); else // 4 mindist = bspcoord[BOXLEFT] - viewx; } else if (viewx > bspcoord[BOXRIGHT]) // 3,5,8 { if (viewy > bspcoord[BOXTOP]) // 3 - mindist = R_QuickCamDist(bspcoord[BOXRIGHT], bspcoord[BOXTOP]) << FRACBITS; + mindist = R_QuickCamDist(bspcoord[BOXRIGHT], bspcoord[BOXTOP]); else if (viewy < bspcoord[BOXBOTTOM]) // 8 - mindist = R_QuickCamDist(bspcoord[BOXRIGHT], bspcoord[BOXBOTTOM]) << FRACBITS; + mindist = R_QuickCamDist(bspcoord[BOXRIGHT], bspcoord[BOXBOTTOM]); else // 5 mindist = viewx - bspcoord[BOXRIGHT]; } diff --git a/src/k_kart.c b/src/k_kart.c index bd3a0d736..ad68305c9 100644 --- a/src/k_kart.c +++ b/src/k_kart.c @@ -3469,7 +3469,7 @@ boolean K_CheckSlopeRollDist(mobj_t *mobj) return true; const fixed_t rolldist = (cv_sloperolldist.value * mapobjectscale); - const fixed_t m_dist = (R_QuickCamDist(mobj->x, mobj->y) << FRACBITS); + const fixed_t m_dist = R_QuickCamDist(mobj->x, mobj->y); return (m_dist <= rolldist); } diff --git a/src/r_things.cpp b/src/r_things.cpp index f0d902430..8a4b82314 100644 --- a/src/r_things.cpp +++ b/src/r_things.cpp @@ -2916,7 +2916,7 @@ boolean R_ThingWithinDist(mobj_t *thing, INT32 limit_dist) { if (limit_dist) { - if ((R_QuickCamDist(thing->x, thing->y) << FRACBITS)/mapobjectscale > limit_dist) + if (R_QuickCamDist(thing->x, thing->y)/mapobjectscale > limit_dist) { return false; } @@ -2960,7 +2960,7 @@ static boolean R_CheckInterpDist(T *thing) if (!R_UsingFrameInterpolation()) return false; - const INT32 dist = R_QuickCamDist(thing->x, thing->y); + const INT32 dist = R_QuickCamDist(thing->x, thing->y) >> FRACBITS; return (dist < cv_maxinterpdist.value); } diff --git a/src/r_things.h b/src/r_things.h index 55dc6174f..b9c012b79 100644 --- a/src/r_things.h +++ b/src/r_things.h @@ -35,18 +35,25 @@ extern "C" { #define FEETADJUST (4<(absx, absy); +#else + return max(absx, absy); +#endif +} + // Takes 2 fixed-point coordinates, returns "distance" between them and camera, -// as an non-fixed-point integer. +// as an fixed-point integer. // It is very rough, tho it is used only for optimizing out unnecessary // interpolation, so it is kinda ok on big distances. - -#ifdef __cplusplus -#define R_QuickCamDist(x, y) std::max(std::abs(((x)>>FRACBITS) - (viewx>>FRACBITS)), std::abs(((y)>>FRACBITS) - (viewy>>FRACBITS))) -#define R_QuickDist(x1, y1, x, y) std::max(std::abs(((x)>>FRACBITS) - (x1>>FRACBITS)), std::abs(((y)>>FRACBITS) - (y1>>FRACBITS))) -#else -#define R_QuickCamDist(x, y) max(abs(((x)>>FRACBITS) - (viewx>>FRACBITS)), abs(((y)>>FRACBITS) - (viewy>>FRACBITS))) -#define R_QuickDist(x1, y1, x, y) max(abs(((x)>>FRACBITS) - (x1>>FRACBITS)), abs(((y)>>FRACBITS) - (y1>>FRACBITS))) -#endif +FUNCINLINE static ATTRINLINE fixed_t R_QuickCamDist(fixed_t x, fixed_t y) +{ + return R_QuickDist(viewx, viewy, x, y); +} // Constant arrays used for psprite clipping // and initializing clipping.