From: Cloudwalk <cloudwalk009@gmail.com>
Date: Mon, 19 Oct 2020 04:30:49 +0000 (-0400)
Subject: mathlib: Implement Q_rint and use it
X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=1174caee68e3f52c23946ab33a4830270a21ad92;p=xonotic%2Fdarkplaces.git

mathlib: Implement Q_rint and use it
---

diff --git a/com_msg.c b/com_msg.c
index 63d857f2..4564cbaf 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)
@@ -225,18 +219,12 @@ void MSG_WriteVector32f (sizebuf_t *sb, const vec3_t v)
 // 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]))