From 78221efae3c038e2c21cb553891d9de8c37cf809 Mon Sep 17 00:00:00 2001 From: Elisey Puzko Date: Sun, 25 Feb 2018 14:49:36 +0300 Subject: min/max functions --- src/GameState.cpp | 30 ++++++---------------- src/Utility.cpp | 76 +++++++++++++++++++++++++++---------------------------- src/Utility.hpp | 56 +++++++++++++++++++++++++++------------- src/main.cpp | 2 +- 4 files changed, 85 insertions(+), 79 deletions(-) diff --git a/src/GameState.cpp b/src/GameState.cpp index 192a0c5..6c2ad42 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -594,28 +594,14 @@ BlockFacing detectHitFace(VectorF raycastHit, Vector selectedBlock) { static const auto vecRight = VectorF(1, 0, 0); static const auto vecForward = VectorF(0, 0, -1); - double up = vec.cosBetween(vecUp); - double down = -up; - double right = vec.cosBetween(vecRight); - double left = -right; - double forward = vec.cosBetween(vecForward); - double backward = -forward; - - // TODO: create a min/max function for the variable number of arguments - // NOTE: function names are surrounded by parentheses to avoid conflict of - // `std::min` and a `min` macro from `windows.h`. If there will be more uses - // of `std::min`, a macro `NOMINMAX` should be defined because these hacks can - // have the real impact on the performance. - double min_cos = (std::min)( - (std::min)( - (std::min)( - (std::min)( - (std::min)(up, down), - right), - left), - forward), - backward); - + const double up = vec.cosBetween(vecUp); + const double down = -up; + const double right = vec.cosBetween(vecRight); + const double left = -right; + const double forward = vec.cosBetween(vecForward); + const double backward = -forward; + + const double min_cos = _min(up, down, right, left, forward, backward); if (min_cos == down) return BlockFacing::Bottom; else if (min_cos == up) diff --git a/src/Utility.cpp b/src/Utility.cpp index c498fa3..848ee02 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -5,41 +5,41 @@ #include GLenum glCheckError_(const char *file, int line) { - GLenum errorCode; - while ((errorCode = glGetError()) != GL_NO_ERROR) { - std::string error; - switch (errorCode) { - case GL_INVALID_ENUM: - error = "INVALID_ENUM"; - break; - case GL_INVALID_VALUE: - error = "INVALID_VALUE"; - break; - case GL_INVALID_OPERATION: - error = "INVALID_OPERATION"; - break; - case GL_STACK_OVERFLOW: - error = "STACK_OVERFLOW"; - break; - case GL_STACK_UNDERFLOW: - error = "STACK_UNDERFLOW"; - break; - case GL_OUT_OF_MEMORY: - error = "OUT_OF_MEMORY"; - break; - case GL_INVALID_FRAMEBUFFER_OPERATION: - error = "INVALID_FRAMEBUFFER_OPERATION"; - break; - } - static int t = 0; - LOG(ERROR) << "OpenGL error: " << error << " at " << file << ":" << line; - } - return errorCode; + GLenum errorCode; + while ((errorCode = glGetError()) != GL_NO_ERROR) { + std::string error; + switch (errorCode) { + case GL_INVALID_ENUM: + error = "INVALID_ENUM"; + break; + case GL_INVALID_VALUE: + error = "INVALID_VALUE"; + break; + case GL_INVALID_OPERATION: + error = "INVALID_OPERATION"; + break; + case GL_STACK_OVERFLOW: + error = "STACK_OVERFLOW"; + break; + case GL_STACK_UNDERFLOW: + error = "STACK_UNDERFLOW"; + break; + case GL_OUT_OF_MEMORY: + error = "OUT_OF_MEMORY"; + break; + case GL_INVALID_FRAMEBUFFER_OPERATION: + error = "INVALID_FRAMEBUFFER_OPERATION"; + break; + } + static int t = 0; + LOG(ERROR) << "OpenGL error: " << error << " at " << file << ":" << line; + } + return errorCode; } LoopExecutionTimeController::LoopExecutionTimeController(duration delayLength) - : delayLength(delayLength) { - previousUpdate = clock::now(); + : delayLength(delayLength) { + previousUpdate = clock::now(); } LoopExecutionTimeController::~LoopExecutionTimeController() { @@ -47,16 +47,16 @@ LoopExecutionTimeController::~LoopExecutionTimeController() { } void LoopExecutionTimeController::SetDelayLength(duration length) { - delayLength = length; + delayLength = length; } unsigned long long LoopExecutionTimeController::GetIterations() { - return iterations; + return iterations; } void LoopExecutionTimeController::Update() { - iterations++; - auto timeToSleep = delayLength - GetDelta(); + iterations++; + auto timeToSleep = delayLength - GetDelta(); if (timeToSleep.count() > 0) std::this_thread::sleep_for(timeToSleep); previousPreviousUpdate = previousUpdate; @@ -69,8 +69,8 @@ double LoopExecutionTimeController::GetDeltaMs() { } LoopExecutionTimeController::duration LoopExecutionTimeController::GetDelta() { - auto now = clock::now(); - return duration(now-previousUpdate); + auto now = clock::now(); + return duration(now-previousUpdate); } double LoopExecutionTimeController::GetDeltaS() { diff --git a/src/Utility.hpp b/src/Utility.hpp index e8c508d..9b90cb9 100644 --- a/src/Utility.hpp +++ b/src/Utility.hpp @@ -11,18 +11,38 @@ using Uuid = std::vector; template void endswap(T *objp) { - unsigned char *memp = reinterpret_cast(objp); - std::reverse(memp, memp + sizeof(T)); + unsigned char *memp = reinterpret_cast(objp); + std::reverse(memp, memp + sizeof(T)); } template void endswap(T &obj) { - unsigned char *raw = reinterpret_cast(&obj); - std::reverse(raw, raw + sizeof(T)); + unsigned char *raw = reinterpret_cast(&obj); + std::reverse(raw, raw + sizeof(T)); +} + +template +T _min(T a, T b) { + return (a > b) ? b : a; +} + +template +T _min(T a, T b, Args... args) { + return _min(a > b ? b : a, args...); +} + +template +T _max(T a, T b) { + return (a > b) ? a : b; +} + +template +T _max(T a, T b, Args... args) { + return _max(a > b ? a : b, args...); } inline void endswap(unsigned char *arr, size_t arrLen) { - std::reverse(arr, arr + arrLen); + std::reverse(arr, arr + arrLen); } GLenum glCheckError_(const char *file, int line); @@ -30,27 +50,27 @@ GLenum glCheckError_(const char *file, int line); class LoopExecutionTimeController { - using clock = std::chrono::steady_clock ; - using timePoint = std::chrono::time_point; - using duration = std::chrono::duration; - timePoint previousUpdate; + using clock = std::chrono::steady_clock ; + using timePoint = std::chrono::time_point; + using duration = std::chrono::duration; + timePoint previousUpdate; timePoint previousPreviousUpdate; - duration delayLength; - unsigned long long iterations=0; + duration delayLength; + unsigned long long iterations=0; public: - LoopExecutionTimeController(duration delayLength); + LoopExecutionTimeController(duration delayLength); - ~LoopExecutionTimeController(); + ~LoopExecutionTimeController(); - void SetDelayLength(duration length); + void SetDelayLength(duration length); - unsigned long long GetIterations(); + unsigned long long GetIterations(); - void Update(); + void Update(); - double GetDeltaMs(); + double GetDeltaMs(); - duration GetDelta(); + duration GetDelta(); double GetDeltaS(); diff --git a/src/main.cpp b/src/main.cpp index eac2417..cb2daa8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,7 +47,7 @@ int main(int argc, char** argv) { LOG(ERROR) << e.what(); return -1; } - + GlobalState::Exec(); return 0; -- cgit v1.2.3