From e4ca82489c1fbbf95bb80f33066d3317682a7942 Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Tue, 10 Aug 2010 12:08:19 +0000 Subject: [PATCH] Replaced usage of get_union_rect by intersect_rects, ...as the function was actually computing the intersection of two rectangles. Added a proper function for computing unions. --- src/gui/widgets/scrollbar_container.cpp | 3 +- src/gui/widgets/widget.cpp | 2 +- src/sdl_utils.cpp | 38 +++++++++++-------------- src/sdl_utils.hpp | 12 +------- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/src/gui/widgets/scrollbar_container.cpp b/src/gui/widgets/scrollbar_container.cpp index e1c7701ca9e..2257c3924f1 100644 --- a/src/gui/widgets/scrollbar_container.cpp +++ b/src/gui/widgets/scrollbar_container.cpp @@ -390,8 +390,7 @@ void tscrollbar_container::set_visible_area(const SDL_Rect& area) tcontainer_::set_visible_area(area); // Now get the visible part of the content. - content_visible_area_ = - get_rect_union(area, content_->get_rect()); + content_visible_area_ = intersect_rects(area, content_->get_rect()); content_grid_->set_visible_area(content_visible_area_); } diff --git a/src/gui/widgets/widget.cpp b/src/gui/widgets/widget.cpp index 1fb0efbca46..0aa3b782ec5 100644 --- a/src/gui/widgets/widget.cpp +++ b/src/gui/widgets/widget.cpp @@ -238,7 +238,7 @@ twidget::tdrawing_action twidget::get_drawing_action() const void twidget::set_visible_area(const SDL_Rect& area) { - clip_rect_ = get_rect_union(area, get_rect()); + clip_rect_ = intersect_rects(area, get_rect()); if(clip_rect_ == get_rect()) { drawing_action_ = DRAWN; diff --git a/src/sdl_utils.cpp b/src/sdl_utils.cpp index a4ff1a6dcd8..a1d6c4a2b4f 100644 --- a/src/sdl_utils.cpp +++ b/src/sdl_utils.cpp @@ -106,32 +106,26 @@ SDL_Rect intersect_rects(SDL_Rect const &rect1, SDL_Rect const &rect2) SDL_Rect res; res.x = std::max(rect1.x, rect2.x); res.y = std::max(rect1.y, rect2.y); - res.w = std::max(std::min(rect1.x + rect1.w, rect2.x + rect2.w) - res.x, 0); - res.h = std::max(std::min(rect1.y + rect1.h, rect2.y + rect2.h) - res.y, 0); + int w = std::min(rect1.x + rect1.w, rect2.x + rect2.w) - res.x; + int h = std::min(rect1.y + rect1.h, rect2.y + rect2.h) - res.y; + if (w <= 0 || h <= 0) return empty_rect; + res.w = w; + res.h = h; return res; } -SDL_Rect get_rect_union(SDL_Rect const &rect1, SDL_Rect const& rect2) { - const int left_side = std::max(rect1.x, rect2.x); - const int right_side = std::min(rect1.x + rect1.w, rect2.x + rect2.w); - if(left_side > right_side) { - return empty_rect; - } - - const int top_side = std::max(rect1.y, rect2.y); - const int bottom_side = std::min(rect1.y + rect1.h, rect2.y + rect2.h); - if(top_side > bottom_side) { - return empty_rect; - } - - SDL_Rect result = create_rect( - left_side, - top_side, - right_side - left_side, - bottom_side - top_side); - - return result; +SDL_Rect union_rects(SDL_Rect const &rect1, SDL_Rect const &rect2) +{ + if (rect1.w == 0 || rect1.h == 0) return rect2; + if (rect2.w == 0 || rect2.h == 0) return rect1; + SDL_Rect res; + res.x = std::min(rect1.x, rect2.x); + res.y = std::min(rect1.y, rect2.y); + res.w = std::max(rect1.x + rect1.w, rect2.x + rect2.w) - res.x; + res.h = std::max(rect1.y + rect1.h, rect2.y + rect2.h) - res.y; + return res; } + SDL_Rect create_rect(const int x, const int y, const int w, const int h) { SDL_Rect rect; diff --git a/src/sdl_utils.hpp b/src/sdl_utils.hpp index 86ca3c4e3ca..f61482633a8 100644 --- a/src/sdl_utils.hpp +++ b/src/sdl_utils.hpp @@ -56,18 +56,8 @@ SDLKey sdl_keysym_from_name(std::string const &keyname); bool point_in_rect(int x, int y, const SDL_Rect& rect); bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2); SDL_Rect intersect_rects(SDL_Rect const &rect1, SDL_Rect const &rect2); +SDL_Rect union_rects(const SDL_Rect &rect1, const SDL_Rect &rect2); -/** - * Returns the union of two rectangles. - * - * @param rect1 The first rectangle. - * @param rect2 The second rectangle. - * - * @returns The union of the two rectangles. If the - * rectangles don't intersect and empty - * rectangle is returned. - */ -SDL_Rect get_rect_union(const SDL_Rect& rect1, const SDL_Rect& rect2); /** * Creates an empty SDL_Rect. *