diff --git a/CMakeLists.txt b/CMakeLists.txt index e374de9..e26d76b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,6 +108,7 @@ target_compile_features(${PROJECT_NAME} target_compile_definitions(${PROJECT_NAME} PRIVATE ${PNG_DEFINITIONS} + PRIVATE VWR_WITH_IMPLICIT_CONVERSIONS=1 ) configure_file(src/${PROJECT_NAME}Config.h.in ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.h) diff --git a/lib/vectorwrapper b/lib/vectorwrapper index 736e838..2f4d319 160000 --- a/lib/vectorwrapper +++ b/lib/vectorwrapper @@ -1 +1 @@ -Subproject commit 736e83893a645cce2cc0767237f538785cf8651d +Subproject commit 2f4d31967541c9c4a87b2511fb0c6d3792c72c2d diff --git a/src/gamescenebase.cpp b/src/gamescenebase.cpp index df01248..471a79d 100644 --- a/src/gamescenebase.cpp +++ b/src/gamescenebase.cpp @@ -8,9 +8,23 @@ #include #include #include +#if !defined(NDEBUG) +# include "compatibility.h" +# include +# include +#endif namespace curry { namespace { +#if !defined(NDEBUG) + bool are_equal_rel (float parA, float parB, float parEpsilon) a_pure; + + //see: http://stackoverflow.com/questions/4548004/how-to-correctly-and-standardly-compare-floats + bool are_equal_rel (float parA, float parB, float parEpsilon) { + return (std::fabs(parA - parB) <= parEpsilon * std::max(std::fabs(parA), std::fabs(parB))); + } +#endif + ///--------------------------------------------------------------------- ///--------------------------------------------------------------------- bool DoEvents (cloonel::InputBag& parInput, cloonel::SDLMain* parSdlMain) { @@ -52,6 +66,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(parSrc.is_valid()); assert(parDest.is_valid()); @@ -60,29 +75,34 @@ namespace curry { if (parDest.to <= parClip.from or parDest.from >= parClip.to) return false; - { - const RectFloat clip(vec2f(0.0f), vec2f(1.0f)); - const vec2f srcWidthHeight(parSrc.width_height()); - const vec2f scaledOffs((parDest.from - parClip.from) / parClip.width_height()); - parSrc.from -= vec2f(std::min(0.0f, scaledOffs.x() * srcWidthHeight.x()), std::min(0.0f, scaledOffs.y() * srcWidthHeight.y())); - const vec2f scaledCrop((parClip.to - parDest.to) / parClip.width_height()); - parSrc.to += vec2f(std::min(0.0f, scaledCrop.x() * srcWidthHeight.x()), std::min(0.0f, scaledCrop.y() * srcWidthHeight.y())); - assert(parSrc.is_valid()); - } - - RectFloat dst(parDest.from - parClip.from, parDest.to - parClip.from); - dst.from.x() = std::max(dst.from.x(), parClip.from.x()); - dst.from.y() = std::max(dst.from.y(), parClip.from.y()); - dst.to.x() = std::min(dst.to.x(), parClip.to.x()); - dst.to.y() = std::min(dst.to.y(), parClip.to.y()); + RectFloat dst; + dst.from.x() = std::max(parDest.from.x(), parClip.from.x()); + dst.from.y() = std::max(parDest.from.y(), parClip.from.y()); + dst.to.x() = std::min(parDest.to.x(), parClip.to.x()); + dst.to.y() = std::min(parDest.to.y(), parClip.to.y()); if (not dst.is_valid()) return false; - parDest.from += parClip.from; - parDest.to += parClip.from; + assert(parDest.from <= dst.from); + assert(parDest.to >= dst.to); - assert(parDest.is_valid()); + RectFloat src; + { + const vec2f srcWidthHeight(parSrc.width_height()); + const vec2f scaledOffs((dst.from - parDest.from) / parDest.width_height()); + src.from = parSrc.from + scaledOffs * srcWidthHeight; + const vec2f scaledCrop((parDest.to - dst.to) / parDest.width_height()); + src.to = parSrc.to - scaledCrop * srcWidthHeight; + assert(src.is_valid()); + + 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())); + } + parDest = dst; + parSrc = src; return true; } } //unnamed namespace @@ -149,6 +169,8 @@ namespace curry { auto src = make_sdlrect(parSrc); auto dst = make_sdlrect(parDest); + assert(src.w == dst.w); + assert(src.h == dst.h); SDL_RenderCopy(m_sdlmain->GetRenderer(), parTexture.texture(), &src, &dst); } } //namespace curry diff --git a/src/rect.hpp b/src/rect.hpp index 126c5aa..66e949d 100644 --- a/src/rect.hpp +++ b/src/rect.hpp @@ -11,6 +11,7 @@ namespace curry { public: typedef vwr::Vec> VecType; + Rect() = default; Rect (T parLeft, T parTop, T parRight, T parBottom); template Rect (const vwr::Vec& parLeftTop, const vwr::Vec& parBottomRight);