Skip to content

Commit

Permalink
Use static casts instead of C casts, add floor-cast functions
Browse files Browse the repository at this point in the history
  • Loading branch information
archshift committed Oct 9, 2014
1 parent f91aa6f commit f8d1e96
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/Entities/Entity.h
Expand Up @@ -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; }
Expand Down
40 changes: 36 additions & 4 deletions src/Globals.h
Expand Up @@ -226,10 +226,10 @@ template class SizeChecker<UInt16, 2>;

// CRT stuff:
#include <sys/stat.h>
#include <assert.h>
#include <stdio.h>
#include <math.h>
#include <stdarg.h>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstdarg>



Expand Down Expand Up @@ -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 <typename C = int>
C FloorD(double a_Value)
{
return static_cast<C>(std::floor(a_Value));
}

/** Floors a_Value, then casts it to C (an int by default) */
template <typename C = int>
C FloorF(double a_Value)
{
return static_cast<C>(std::floorf(a_Value));
}

/** Ciels a_Value, then casts it to C (an int by default) */
template <typename C = int>
C CeilD(double a_Value)
{
return static_cast<C>(std::ceil(a_Value));
}

/** Ciels a_Value, then casts it to C (an int by default) */
template <typename C = int>
C CeilF(double a_Value)
{
return static_cast<C>(std::ceilf(a_Value));
}





#ifndef TOLUA_TEMPLATE_BIND
#define TOLUA_TEMPLATE_BIND(x)
#endif
Expand Down
33 changes: 16 additions & 17 deletions src/Vector3.h
Expand Up @@ -4,7 +4,6 @@


#define _USE_MATH_DEFINES // Enable non-standard math defines (MSVC)
#include <math.h>
#include <list>
#include <vector>

Expand All @@ -29,9 +28,9 @@ class Vector3


// Hardcoded copy constructors (tolua++ does not support function templates .. yet)
Vector3(const Vector3<float> & a_Rhs) : x((T) a_Rhs.x), y((T) a_Rhs.y), z((T) a_Rhs.z) {}
Vector3(const Vector3<double> & a_Rhs) : x((T) a_Rhs.x), y((T) a_Rhs.y), z((T) a_Rhs.z) {}
Vector3(const Vector3<int> & a_Rhs) : x((T) a_Rhs.x), y((T) a_Rhs.y), z((T) a_Rhs.z) {}
Vector3(const Vector3<float> & a_Rhs) : x(static_cast<T>(a_Rhs.x)), y(static_cast<T>(a_Rhs.y)), z(static_cast<T>(a_Rhs.z)) {}
Vector3(const Vector3<double> & a_Rhs) : x(static_cast<T>(a_Rhs.x)), y(static_cast<T>(a_Rhs.y)), z(static_cast<T>(a_Rhs.z)) {}
Vector3(const Vector3<int> & a_Rhs) : x(static_cast<T>(a_Rhs.x)), y(static_cast<T>(a_Rhs.y)), z(static_cast<T>(a_Rhs.z)) {}


// tolua_end
Expand All @@ -53,19 +52,19 @@ class Vector3
{
double Len = 1.0 / Length();

x = (T)(x * Len);
y = (T)(y * Len);
z = (T)(z * Len);
x = static_cast<T>(x * Len);
y = static_cast<T>(y * Len);
z = static_cast<T>(z * Len);
}

inline Vector3<T> NormalizeCopy(void) const
{
double Len = 1.0 / Length();

return Vector3<T>(
(T)(x * Len),
(T)(y * Len),
(T)(z * Len)
static_cast<T>(x * Len),
static_cast<T>(y * Len),
static_cast<T>(z * Len)
);
}

Expand All @@ -74,15 +73,15 @@ class Vector3
double Len = 1.0 / Length();

a_Rhs.Set(
(T)(x * Len),
(T)(y * Len),
(T)(z * Len)
static_cast<T>(x * Len),
static_cast<T>(y * Len),
static_cast<T>(z * Len)
);
}

inline double Length(void) const
{
return sqrt((double)(x * x + y * y + z * z));
return sqrt(static_cast<double>(x * x + y * y + z * z));
}

inline double SqrLength(void) const
Expand Down Expand Up @@ -138,9 +137,9 @@ class Vector3
inline Vector3<int> Floor(void) const
{
return Vector3<int>(
(int)floor(x),
(int)floor(y),
(int)floor(z)
static_cast<int>(floor(x)),
static_cast<int>(floor(y)),
static_cast<int>(floor(z))
);
}

Expand Down

0 comments on commit f8d1e96

Please sign in to comment.