From be2b8a35ab64ff98a3ec0f07de09bed190a3af9f Mon Sep 17 00:00:00 2001 From: Dennis Schridde Date: Tue, 13 May 2008 23:19:10 +0000 Subject: [PATCH] clip() function for integers (Like CLIP macro) and some slightly altered comments/variable names. git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@5104 4a71c877-e1ca-e34f-864e-861f7616d084 --- lib/framework/math-help.h | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/framework/math-help.h b/lib/framework/math-help.h index 0366305fd..297e45723 100644 --- a/lib/framework/math-help.h +++ b/lib/framework/math-help.h @@ -72,29 +72,42 @@ static inline WZ_DECL_CONST float rad2degf(float x) } /*! - * Moves x into the range 0 - y + * Moves x into the range 0 - max * \param x Value to clip - * \param y Upper range - * \return Value in the range 0 - y + * \param max Upper range + * \return Value in the range 0 - max */ -static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT int wrap(int x, int y) +static inline WZ_DECL_CONST int wrap(int x, int max) { - while(x < 0) x += y; - while(x >= y) x -= y; + while(x < 0) x += max; + while(x >= max) x -= max; return x; } /*! - * Moves x into the range 0.0f - y + * Moves x into the range 0.0f - max * \param x Value to clip - * \param y Upper range - * \return Value in the range 0.0f - y + * \param max Upper range + * \return Value in the range 0.0f - max */ -static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT float wrapf(float x, float y) +static inline WZ_DECL_CONST float wrapf(float x, float max) { - while(x < 0.0f) x += y; - while(x >= y) x -= y; + while(x < 0.0f) x += max; + while(x >= max) x -= max; + return x; +} + +/*! + * Clips x to boundaries + * \param x Value to clip + * \param min Lower bound + * \param max Upper bound + */ +static inline WZ_DECL_CONST int clip(int x, int min, int max) +{ + if (x < min) return min; + if (x > max) return max; return x; }