From 38826b94b877ae2c19df89423c776e90d773a484 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 9 Nov 2016 01:46:36 +0100 Subject: [PATCH] Fix randomly happening floating point error during clipping. --- CMakeLists.txt | 1 + src/gamescenebase.cpp | 5 ++--- src/rect_to_sdl.cpp | 15 +++++++++++++++ src/rect_to_sdl.hpp | 3 +++ 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 src/rect_to_sdl.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e04676..59596cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ add_executable(${PROJECT_NAME} src/texture.cpp src/movingobject.cpp src/character.cpp + src/rect_to_sdl.cpp ) target_include_directories(${PROJECT_NAME} SYSTEM diff --git a/src/gamescenebase.cpp b/src/gamescenebase.cpp index 471a79d..6b3c945 100644 --- a/src/gamescenebase.cpp +++ b/src/gamescenebase.cpp @@ -11,7 +11,6 @@ #if !defined(NDEBUG) # include "compatibility.h" # include -# include #endif namespace curry { @@ -66,7 +65,7 @@ namespace curry { ///---------------------------------------------------------------------- bool clip_rect (Rect& parSrc, Rect& parDest, const Rect& parClip) { typedef Rect RectFloat; - assert(are_equal_rel(parSrc.width(), parDest.width(), std::numeric_limits::epsilon())); + assert(are_equal_rel(parSrc.width(), parDest.width(), 0.00001f)); assert(parSrc.is_valid()); assert(parDest.is_valid()); @@ -99,7 +98,7 @@ namespace curry { assert(dst.is_valid()); assert(dst.from >= parClip.from); assert(dst.to <= parClip.to); - assert(are_equal_rel(src.width(), dst.width(), std::numeric_limits::epsilon())); + assert(are_equal_rel(src.width(), dst.width(), 0.00001f)); } parDest = dst; parSrc = src; diff --git a/src/rect_to_sdl.cpp b/src/rect_to_sdl.cpp new file mode 100644 index 0000000..dd5d24f --- /dev/null +++ b/src/rect_to_sdl.cpp @@ -0,0 +1,15 @@ +#include "rect_to_sdl.hpp" +#include + +namespace curry { + SDL_Rect make_sdlrect (const Rect& parOther) { + typedef decltype(std::declval().w) ValueType; + + return SDL_Rect{ + static_cast(std::lround(parOther.left())), + static_cast(std::lround(parOther.top())), + static_cast(std::lround(parOther.width())), + static_cast(std::lround(parOther.height())) + }; + } +} //namespace curry diff --git a/src/rect_to_sdl.hpp b/src/rect_to_sdl.hpp index 88bf9bb..552fd3d 100644 --- a/src/rect_to_sdl.hpp +++ b/src/rect_to_sdl.hpp @@ -1,6 +1,7 @@ #pragma once #include "rect.hpp" +#include "compatibility.h" #include #include @@ -16,4 +17,6 @@ namespace curry { static_cast(parOther.height()) }; } + + SDL_Rect make_sdlrect (const Rect& parOther) a_pure; } //namespace curry