Fix randomly happening floating point error during clipping.

This commit is contained in:
King_DuckZ 2016-11-09 01:46:36 +01:00
parent e65c2341c2
commit 38826b94b8
4 changed files with 21 additions and 3 deletions

View file

@ -66,6 +66,7 @@ add_executable(${PROJECT_NAME}
src/texture.cpp src/texture.cpp
src/movingobject.cpp src/movingobject.cpp
src/character.cpp src/character.cpp
src/rect_to_sdl.cpp
) )
target_include_directories(${PROJECT_NAME} SYSTEM target_include_directories(${PROJECT_NAME} SYSTEM

View file

@ -11,7 +11,6 @@
#if !defined(NDEBUG) #if !defined(NDEBUG)
# include "compatibility.h" # include "compatibility.h"
# include <cmath> # include <cmath>
# include <limits>
#endif #endif
namespace curry { namespace curry {
@ -66,7 +65,7 @@ namespace curry {
///---------------------------------------------------------------------- ///----------------------------------------------------------------------
bool clip_rect (Rect<float>& parSrc, Rect<float>& parDest, const Rect<float>& parClip) { bool clip_rect (Rect<float>& parSrc, Rect<float>& parDest, const Rect<float>& parClip) {
typedef Rect<float> RectFloat; typedef Rect<float> RectFloat;
assert(are_equal_rel(parSrc.width(), parDest.width(), std::numeric_limits<float>::epsilon())); assert(are_equal_rel(parSrc.width(), parDest.width(), 0.00001f));
assert(parSrc.is_valid()); assert(parSrc.is_valid());
assert(parDest.is_valid()); assert(parDest.is_valid());
@ -99,7 +98,7 @@ namespace curry {
assert(dst.is_valid()); assert(dst.is_valid());
assert(dst.from >= parClip.from); assert(dst.from >= parClip.from);
assert(dst.to <= parClip.to); assert(dst.to <= parClip.to);
assert(are_equal_rel(src.width(), dst.width(), std::numeric_limits<float>::epsilon())); assert(are_equal_rel(src.width(), dst.width(), 0.00001f));
} }
parDest = dst; parDest = dst;
parSrc = src; parSrc = src;

15
src/rect_to_sdl.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "rect_to_sdl.hpp"
#include <cmath>
namespace curry {
SDL_Rect make_sdlrect (const Rect<float>& parOther) {
typedef decltype(std::declval<SDL_Rect>().w) ValueType;
return SDL_Rect{
static_cast<ValueType>(std::lround(parOther.left())),
static_cast<ValueType>(std::lround(parOther.top())),
static_cast<ValueType>(std::lround(parOther.width())),
static_cast<ValueType>(std::lround(parOther.height()))
};
}
} //namespace curry

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "rect.hpp" #include "rect.hpp"
#include "compatibility.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <utility> #include <utility>
@ -16,4 +17,6 @@ namespace curry {
static_cast<ValueType>(parOther.height()) static_cast<ValueType>(parOther.height())
}; };
} }
SDL_Rect make_sdlrect (const Rect<float>& parOther) a_pure;
} //namespace curry } //namespace curry