about summary refs log tree commit diff
path: root/src/game/g_math.hpp
diff options
context:
space:
mode:
authorMagnus Auvinen <magnus.auvinen@gmail.com>2008-06-12 12:09:34 +0000
committerMagnus Auvinen <magnus.auvinen@gmail.com>2008-06-12 12:09:34 +0000
commit3705064b109580103a3d13f44693503da9927281 (patch)
tree0de409d2c67e74866e2bbe0b53a3791c20dab80d /src/game/g_math.hpp
parentf6c67c29cd10d41b877ee1319801edc01b625e72 (diff)
downloadzcatch-3705064b109580103a3d13f44693503da9927281.tar.gz
zcatch-3705064b109580103a3d13f44693503da9927281.zip
renamed .h to .hpp in game because they are c++ headers
Diffstat (limited to 'src/game/g_math.hpp')
-rw-r--r--src/game/g_math.hpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/game/g_math.hpp b/src/game/g_math.hpp
new file mode 100644
index 00000000..5e3f7ede
--- /dev/null
+++ b/src/game/g_math.hpp
@@ -0,0 +1,61 @@
+/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
+#ifndef BASE_MATH_H
+#define BASE_MATH_H
+
+#include <stdlib.h>
+
+template <typename T>
+inline T clamp(T val, T min, T max)
+{
+	if(val < min)
+		return min;
+	if(val > max)
+		return max;
+	return val;
+}
+
+inline float sign(float f)
+{
+	return f<0.0f?-1.0f:1.0f;
+}
+
+template<typename T, typename TB>
+inline T mix(const T a, const T b, TB amount)
+{
+	return a + (b-a)*amount;
+}
+
+inline float frandom() { return rand()/(float)(RAND_MAX); }
+
+// float to fixed
+inline int f2fx(float v) { return (int)(v*(float)(1<<10)); }
+inline float fx2f(int v) { return v*(1.0f/(1<<10)); }
+
+class fxp
+{
+	int value;
+public:
+	void set(int v) { value = v; }
+	int get() const { return value; }
+	fxp &operator = (int v) { value = v<<10; return *this; }
+	fxp &operator = (float v) { value = (int)(v*(float)(1<<10)); return *this; }
+	operator float() const { return value/(float)(1<<10); }
+};
+
+class tune_param
+{
+	int value;
+public:
+	void set(int v) { value = v; }
+	int get() const { return value; }
+	tune_param &operator = (int v) { value = (int)(v*100.0f); return *this; }
+	tune_param &operator = (float v) { value = (int)(v*100.0f); return *this; }
+	operator float() const { return value/100.0f; }
+};
+
+const float pi = 3.1415926535897932384626433f;
+
+template <typename T> inline T min(T a, T b) { return a<b?a:b; }
+template <typename T> inline T max(T a, T b) { return a>b?a:b; }
+
+#endif // BASE_MATH_H