From dfd6bc3c91c7bd508b5bde820366784bd980a677 Mon Sep 17 00:00:00 2001
From: Jared Adams <jaxad0127@gmail.com>
Date: Tue, 14 Apr 2009 16:44:03 -0600
Subject: Start a MathUtils file

It has fast(inv)sqrt and weightedAverage.

Also cleanup the last ocmmit a bit.
---
 src/CMakeLists.txt   |  3 ++-
 src/Makefile.am      |  3 ++-
 src/gui/status.cpp   | 32 +++++++++++++-------------------
 src/gui/status.h     |  5 -----
 src/particle.cpp     |  2 +-
 src/utils/fastsqrt.h | 24 ------------------------
 6 files changed, 18 insertions(+), 51 deletions(-)
 delete mode 100644 src/utils/fastsqrt.h

(limited to 'src')

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 82a0267d..1e2fe6b3 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -305,8 +305,9 @@ SET(SRCS
     utils/base64.cpp
     utils/base64.h
     utils/dtor.h
-    utils/fastsqrt.h
     utils/gettext.h
+    utils/mathutils.cpp
+    utils/mathutils.h
     utils/sha256.cpp
     utils/sha256.h
     utils/stringutils.cpp
diff --git a/src/Makefile.am b/src/Makefile.am
index 2d093127..45bab9fd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -258,8 +258,9 @@ tmw_SOURCES = gui/widgets/avatar.cpp \
 	      utils/base64.cpp \
 	      utils/base64.h \
 	      utils/dtor.h \
-	      utils/fastsqrt.h \
 	      utils/gettext.h \
+	      utils/mathutils.cpp \
+	      utils/mathutils.h \
 	      utils/sha256.cpp \
 	      utils/sha256.h \
 	      utils/stringutils.cpp \
diff --git a/src/gui/status.cpp b/src/gui/status.cpp
index 9d85a317..27269acf 100644
--- a/src/gui/status.cpp
+++ b/src/gui/status.cpp
@@ -34,6 +34,7 @@
 #include "net/ea/playerhandler.h"
 
 #include "utils/gettext.h"
+#include "utils/mathutils.h"
 #include "utils/strprintf.h"
 #include "utils/stringutils.h"
 
@@ -308,22 +309,22 @@ void StatusWindow::updateHPBar(ProgressBar *bar, bool showMax)
     {
         r1 = 0xcc; g1 = 0x00; b1 = 0x00;
         r2 = 0xcc; g2 = 0xcc; b2 = 0x00;
-        weight = (float)(curHP-(thresholdLevel)) / (float)thresholdLevel;
+        weight = (float) (curHP - (thresholdLevel)) / (float) thresholdLevel;
         // Reddish Brown -> Red
     }
     else if (curHP < thresholdLevel*2)
     {
         r1 = 0xcc; g1 = 0xcc; b1 = 0x00;
         r2 = 0xff; g2 = 0xcc; b2 = 0x00;
-        weight = (float)(curHP-(thresholdLevel*2)) / (float)thresholdLevel;
-        // Orange->Reddish Brown
+        weight = (float) (curHP - (thresholdLevel * 2)) / (float) thresholdLevel;
+        // Orange -> Reddish Brown
     }
     else
     {
         r1 = 0xff; g1 = 0xcc; b1 = 0x00;
         r2 = 0x99; g2 = 0xff; b2 = 0x99;
-        weight = (float)(curHP-(thresholdLevel*3)) / (float)thresholdLevel;
-        // Green ->Orange
+        weight = (float) (curHP - (thresholdLevel * 3)) / (float) thresholdLevel;
+        // Green -> Orange
     }
 
     //safety checks
@@ -331,27 +332,20 @@ void StatusWindow::updateHPBar(ProgressBar *bar, bool showMax)
     if (weight<0.0f) weight=0.0f;
 
     //Do the color blend
-    r1 = (int)weightedAverage(r1,r2,weight);
-    g1 = (int)weightedAverage(g1,g2,weight);
-    b1 = (int)weightedAverage(b1,b2,weight);
+    r1 = (int) weightedAverage(r1, r2, weight);
+    g1 = (int) weightedAverage(g1, g2, weight);
+    b1 = (int) weightedAverage(b1, b2, weight);
 
     //more safety checks
-    if (r1>255) r1=255;
-    if (g1>255) g1=255;
-    if (b1>255) b1=255;
+    if (r1 > 255) r1 = 255;
+    if (g1 > 255) g1 = 255;
+    if (b1 > 255) b1 = 255;
 
-    bar->setColor(r1,g1,b1);
+    bar->setColor(r1, g1, b1);
 
     bar->setProgress((float) player_node->getHp() / (float) player_node->getMaxHp());
 }
 
-float StatusWindow::weightedAverage(float n1, float n2, float w) {
-    if (w<0.0f) return n1;
-    if (w>1.0f) return n2;
-
-    return (w*n2 + (1.0f-w)*n1) / 2.0f;
-}
-
 void StatusWindow::updateMPBar(ProgressBar *bar, bool showMax)
 {
     if (showMax)
diff --git a/src/gui/status.h b/src/gui/status.h
index 78d35a32..403a7d59 100644
--- a/src/gui/status.h
+++ b/src/gui/status.h
@@ -62,11 +62,6 @@ class StatusWindow : public Window, public gcn::ActionListener
         static void updateXPBar(ProgressBar *bar, bool percent = true);
         static void updateJobBar(ProgressBar *bar, bool percent = true);
 
-        /**
-         * Used by updateHPBar to get the weighted average of two colors
-         */
-        static float weightedAverage(float n1, float n2, float w);
-
     private:
         LocalPlayer *mPlayer;
 
diff --git a/src/particle.cpp b/src/particle.cpp
index 21844f01..78db2a58 100644
--- a/src/particle.cpp
+++ b/src/particle.cpp
@@ -36,7 +36,7 @@
 #include "resources/resourcemanager.h"
 
 #include "utils/dtor.h"
-#include "utils/fastsqrt.h"
+#include "utils/mathutils.h"
 #include "utils/xml.h"
 
 #define SIN45 0.707106781f
diff --git a/src/utils/fastsqrt.h b/src/utils/fastsqrt.h
deleted file mode 100644
index 78768149..00000000
--- a/src/utils/fastsqrt.h
+++ /dev/null
@@ -1,24 +0,0 @@
-/* A very fast function to calculate the approximate inverse square root of a
- * floating point value and a helper function that uses it for getting the
- * normal squareroot. For an explanation of the inverse squareroot function
- * read:
- * http://www.math.purdue.edu/~clomont/Math/Papers/2003/InvSqrt.pdf
- *
- * Unfortunately the original creator of this function seems to be unknown.
- */
-
-float fastInvSqrt(float x)
-{
-    union { int i; float x; } tmp;
-    float xhalf = 0.5f * x;
-    tmp.x = x;
-    tmp.i = 0x5f375a86 - (tmp.i >> 1);
-    x = tmp.x;
-    x = x * (1.5f - xhalf * x * x);
-    return x;
-}
-
-float fastSqrt(float x)
-{
-    return 1.0f / fastInvSqrt(x);
-}
-- 
cgit v1.2.3-70-g09d2