From: cloudwalk Date: Tue, 20 Oct 2020 10:58:28 +0000 (+0000) Subject: mathlib: Implement Q_rint and use it X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=7157f37efc23fa0295fa120351431ea2ab366997;p=xonotic%2Fdarkplaces.git mathlib: Implement Q_rint and use it git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@13025 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/com_msg.c b/com_msg.c index 0de4fc67..0c4542bb 100644 --- a/com_msg.c +++ b/com_msg.c @@ -182,18 +182,12 @@ void MSG_WriteUnterminatedString (sizebuf_t *sb, const char *s) void MSG_WriteCoord13i (sizebuf_t *sb, float f) { - if (f >= 0) - MSG_WriteShort (sb, (int)(f * 8.0 + 0.5)); - else - MSG_WriteShort (sb, (int)(f * 8.0 - 0.5)); + MSG_WriteShort (sb, Q_rint(f*8)); } void MSG_WriteCoord16i (sizebuf_t *sb, float f) { - if (f >= 0) - MSG_WriteShort (sb, (int)(f + 0.5)); - else - MSG_WriteShort (sb, (int)(f - 0.5)); + MSG_WriteShort (sb, Q_rint(f)); } void MSG_WriteCoord32f (sizebuf_t *sb, float f) @@ -223,18 +217,12 @@ void MSG_WriteVector (sizebuf_t *sb, const vec3_t v, protocolversion_t protocol) // LadyHavoc: round to nearest value, rather than rounding toward zero, fixes crosshair problem void MSG_WriteAngle8i (sizebuf_t *sb, float f) { - if (f >= 0) - MSG_WriteByte (sb, (int)(f*(256.0/360.0) + 0.5) & 255); - else - MSG_WriteByte (sb, (int)(f*(256.0/360.0) - 0.5) & 255); + MSG_WriteByte (sb, (int)Q_rint(f*(256.0/360.0)) & 255); } void MSG_WriteAngle16i (sizebuf_t *sb, float f) { - if (f >= 0) - MSG_WriteShort (sb, (int)(f*(65536.0/360.0) + 0.5) & 65535); - else - MSG_WriteShort (sb, (int)(f*(65536.0/360.0) - 0.5) & 65535); + MSG_WriteShort (sb, (int)Q_rint(f*(65536.0/360.0)) & 65535); } void MSG_WriteAngle32f (sizebuf_t *sb, float f) diff --git a/mathlib.h b/mathlib.h index aeab1db8..55f1218e 100644 --- a/mathlib.h +++ b/mathlib.h @@ -83,6 +83,8 @@ unsigned int CeilPowerOf2(unsigned int value); #define RAD2DEG(a) ((a) * (180.0f / (float) M_PI)) #define ANGLEMOD(a) ((a) - 360.0 * floor((a) / 360.0)) +#define Q_rint(x) ((x) > 0 ? (int)((x) + 0.5) : (int)((x) - 0.5)) //johnfitz -- from joequake + #define DotProduct2(a,b) ((a)[0]*(b)[0]+(a)[1]*(b)[1]) #define Vector2Clear(a) ((a)[0]=(a)[1]=0) #define Vector2Compare(a,b) (((a)[0]==(b)[0])&&((a)[1]==(b)[1]))