]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
mathlib: Implement Q_rint and use it
authorcloudwalk <cloudwalk@d7cf8633-e32d-0410-b094-e92efae38249>
Tue, 20 Oct 2020 10:58:28 +0000 (10:58 +0000)
committercloudwalk <cloudwalk@d7cf8633-e32d-0410-b094-e92efae38249>
Tue, 20 Oct 2020 10:58:28 +0000 (10:58 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@13025 d7cf8633-e32d-0410-b094-e92efae38249

com_msg.c
mathlib.h

index 0de4fc677032e548d7da4969da6138edc069156c..0c4542bb467772bb4fce7950b0474f4495af6667 100644 (file)
--- 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)
index aeab1db8b40b03cf40b4bc0169e08863ef874571..55f1218e462d0571a44fd3313ed51292d9f6697b 100644 (file)
--- 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]))