From f8d1e96ae7ac9a3483ff0a214796455946d7880f Mon Sep 17 00:00:00 2001 From: archshift Date: Wed, 3 Sep 2014 16:25:45 -0700 Subject: Use static casts instead of C casts, add floor-cast functions --- src/Entities/Entity.h | 6 +++--- src/Globals.h | 40 ++++++++++++++++++++++++++++++++++++---- src/Vector3.h | 33 ++++++++++++++++----------------- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/src/Entities/Entity.h b/src/Entities/Entity.h index 3fa7e80c1..0a03eb3f2 100644 --- a/src/Entities/Entity.h +++ b/src/Entities/Entity.h @@ -27,9 +27,9 @@ return super::GetClass(); \ } -#define POSX_TOINT (int)floor(GetPosX()) -#define POSY_TOINT (int)floor(GetPosY()) -#define POSZ_TOINT (int)floor(GetPosZ()) +#define POSX_TOINT FloorD(GetPosX()) +#define POSY_TOINT FloorD(GetPosY()) +#define POSZ_TOINT FloorD(GetPosZ()) #define POS_TOINT Vector3i(POSXTOINT, POSYTOINT, POSZTOINT) #define GET_AND_VERIFY_CURRENT_CHUNK(ChunkVarName, X, Z) cChunk * ChunkVarName = a_Chunk.GetNeighborChunk(X, Z); if ((ChunkVarName == NULL) || !ChunkVarName->IsValid()) { return; } diff --git a/src/Globals.h b/src/Globals.h index 0926457da..8bf7a0f0c 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -226,10 +226,10 @@ template class SizeChecker; // CRT stuff: #include -#include -#include -#include -#include +#include +#include +#include +#include @@ -400,6 +400,38 @@ T Clamp(T a_Value, T a_Min, T a_Max) +/** Floors a_Value, then casts it to C (an int by default) */ +template +C FloorD(double a_Value) +{ + return static_cast(std::floor(a_Value)); +} + +/** Floors a_Value, then casts it to C (an int by default) */ +template +C FloorF(double a_Value) +{ + return static_cast(std::floorf(a_Value)); +} + +/** Ciels a_Value, then casts it to C (an int by default) */ +template +C CeilD(double a_Value) +{ + return static_cast(std::ceil(a_Value)); +} + +/** Ciels a_Value, then casts it to C (an int by default) */ +template +C CeilF(double a_Value) +{ + return static_cast(std::ceilf(a_Value)); +} + + + + + #ifndef TOLUA_TEMPLATE_BIND #define TOLUA_TEMPLATE_BIND(x) #endif diff --git a/src/Vector3.h b/src/Vector3.h index 1dcb38f64..782b0d1c9 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -4,7 +4,6 @@ #define _USE_MATH_DEFINES // Enable non-standard math defines (MSVC) -#include #include #include @@ -29,9 +28,9 @@ public: // Hardcoded copy constructors (tolua++ does not support function templates .. yet) - Vector3(const Vector3 & a_Rhs) : x((T) a_Rhs.x), y((T) a_Rhs.y), z((T) a_Rhs.z) {} - Vector3(const Vector3 & a_Rhs) : x((T) a_Rhs.x), y((T) a_Rhs.y), z((T) a_Rhs.z) {} - Vector3(const Vector3 & a_Rhs) : x((T) a_Rhs.x), y((T) a_Rhs.y), z((T) a_Rhs.z) {} + Vector3(const Vector3 & a_Rhs) : x(static_cast(a_Rhs.x)), y(static_cast(a_Rhs.y)), z(static_cast(a_Rhs.z)) {} + Vector3(const Vector3 & a_Rhs) : x(static_cast(a_Rhs.x)), y(static_cast(a_Rhs.y)), z(static_cast(a_Rhs.z)) {} + Vector3(const Vector3 & a_Rhs) : x(static_cast(a_Rhs.x)), y(static_cast(a_Rhs.y)), z(static_cast(a_Rhs.z)) {} // tolua_end @@ -53,9 +52,9 @@ public: { double Len = 1.0 / Length(); - x = (T)(x * Len); - y = (T)(y * Len); - z = (T)(z * Len); + x = static_cast(x * Len); + y = static_cast(y * Len); + z = static_cast(z * Len); } inline Vector3 NormalizeCopy(void) const @@ -63,9 +62,9 @@ public: double Len = 1.0 / Length(); return Vector3( - (T)(x * Len), - (T)(y * Len), - (T)(z * Len) + static_cast(x * Len), + static_cast(y * Len), + static_cast(z * Len) ); } @@ -74,15 +73,15 @@ public: double Len = 1.0 / Length(); a_Rhs.Set( - (T)(x * Len), - (T)(y * Len), - (T)(z * Len) + static_cast(x * Len), + static_cast(y * Len), + static_cast(z * Len) ); } inline double Length(void) const { - return sqrt((double)(x * x + y * y + z * z)); + return sqrt(static_cast(x * x + y * y + z * z)); } inline double SqrLength(void) const @@ -138,9 +137,9 @@ public: inline Vector3 Floor(void) const { return Vector3( - (int)floor(x), - (int)floor(y), - (int)floor(z) + static_cast(floor(x)), + static_cast(floor(y)), + static_cast(floor(z)) ); } -- cgit v1.2.3 From 76b37acb421bf10e094182b2e9be111eb29c46f1 Mon Sep 17 00:00:00 2001 From: archshift Date: Wed, 3 Sep 2014 16:51:38 -0700 Subject: Float/Ciel: If it's going to use C++11, it might as well take advantage of it --- src/Entities/Entity.h | 6 +++--- src/Globals.h | 26 ++++++-------------------- src/Vector3.h | 6 +++--- 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/Entities/Entity.h b/src/Entities/Entity.h index 0a03eb3f2..f0577aba2 100644 --- a/src/Entities/Entity.h +++ b/src/Entities/Entity.h @@ -27,9 +27,9 @@ return super::GetClass(); \ } -#define POSX_TOINT FloorD(GetPosX()) -#define POSY_TOINT FloorD(GetPosY()) -#define POSZ_TOINT FloorD(GetPosZ()) +#define POSX_TOINT FloorC(GetPosX()) +#define POSY_TOINT FloorC(GetPosY()) +#define POSZ_TOINT FloorC(GetPosZ()) #define POS_TOINT Vector3i(POSXTOINT, POSYTOINT, POSZTOINT) #define GET_AND_VERIFY_CURRENT_CHUNK(ChunkVarName, X, Z) cChunk * ChunkVarName = a_Chunk.GetNeighborChunk(X, Z); if ((ChunkVarName == NULL) || !ChunkVarName->IsValid()) { return; } diff --git a/src/Globals.h b/src/Globals.h index 8bf7a0f0c..9959d92a9 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -400,34 +400,20 @@ T Clamp(T a_Value, T a_Min, T a_Max) -/** Floors a_Value, then casts it to C (an int by default) */ -template -C FloorD(double a_Value) +/** Floors a value, then casts it to C (an int by default) */ +template +typename std::enable_if::value, C>::type FloorC(T a_Value) { return static_cast(std::floor(a_Value)); } -/** Floors a_Value, then casts it to C (an int by default) */ -template -C FloorF(double a_Value) -{ - return static_cast(std::floorf(a_Value)); -} - -/** Ciels a_Value, then casts it to C (an int by default) */ -template -C CeilD(double a_Value) +/** Ceils a value, then casts it to C (an int by default) */ +template +typename std::enable_if::value, C>::type CeilC(T a_Value) { return static_cast(std::ceil(a_Value)); } -/** Ciels a_Value, then casts it to C (an int by default) */ -template -C CeilF(double a_Value) -{ - return static_cast(std::ceilf(a_Value)); -} - diff --git a/src/Vector3.h b/src/Vector3.h index 782b0d1c9..937c8fdfa 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -137,9 +137,9 @@ public: inline Vector3 Floor(void) const { return Vector3( - static_cast(floor(x)), - static_cast(floor(y)), - static_cast(floor(z)) + FloorC(x), + FloorC(y), + FloorC(z) ); } -- cgit v1.2.3