1 /*
2 Copyright (C) 2014 - 2018 by Mark de Wever <koraq@xs4all.nl>
3 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY.
11
12 See the COPYING file for more details.
13 */
14
15 #include "sdl/point.hpp"
16 #include "sdl/rect.hpp"
17 #include "sdl/window.hpp"
18 #include "video.hpp"
19
20 namespace sdl
21 {
point_in_rect(int x,int y,const SDL_Rect & rect)22 bool point_in_rect(int x, int y, const SDL_Rect& rect)
23 {
24 SDL_Point p {x, y};
25 return SDL_PointInRect(&p, &rect) != SDL_FALSE;
26 }
27
point_in_rect(const point & point,const SDL_Rect & rect)28 bool point_in_rect(const point& point, const SDL_Rect& rect)
29 {
30 return point_in_rect(point.x, point.y, rect);
31 }
32
rects_overlap(const SDL_Rect & rect1,const SDL_Rect & rect2)33 bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2)
34 {
35 return (rect1.x < rect2.x+rect2.w && rect2.x < rect1.x+rect1.w &&
36 rect1.y < rect2.y+rect2.h && rect2.y < rect1.y+rect1.h);
37 }
38
intersect_rects(const SDL_Rect & rect1,const SDL_Rect & rect2)39 SDL_Rect intersect_rects(const SDL_Rect& rect1, const SDL_Rect& rect2)
40 {
41 SDL_Rect res;
42 if(!SDL_IntersectRect(&rect1, &rect2, &res)) {
43 return empty_rect;
44 }
45
46 return res;
47 }
48
union_rects(const SDL_Rect & rect1,const SDL_Rect & rect2)49 SDL_Rect union_rects(const SDL_Rect& rect1, const SDL_Rect& rect2)
50 {
51 SDL_Rect res;
52 SDL_UnionRect(&rect1, &rect2, &res);
53
54 return res;
55 }
56
draw_rectangle(const SDL_Rect & rect,const color_t & color)57 void draw_rectangle(const SDL_Rect& rect, const color_t& color)
58 {
59 SDL_Renderer* renderer = *CVideo::get_singleton().get_window();
60
61 SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
62 SDL_RenderDrawRect(renderer, &rect);
63 }
64
fill_rectangle(const SDL_Rect & rect,const color_t & color)65 void fill_rectangle(const SDL_Rect& rect, const color_t& color)
66 {
67 SDL_Renderer* renderer = *CVideo::get_singleton().get_window();
68
69 SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
70 SDL_RenderFillRect(renderer, &rect);
71 }
72
73 } // namespace sdl
74
operator ==(const SDL_Rect & a,const SDL_Rect & b)75 bool operator==(const SDL_Rect& a, const SDL_Rect& b)
76 {
77 return SDL_RectEquals(&a, &b) != SDL_FALSE;
78 }
79
operator !=(const SDL_Rect & a,const SDL_Rect & b)80 bool operator!=(const SDL_Rect& a, const SDL_Rect& b)
81 {
82 return !operator==(a,b);
83 }
84
operator <<(std::ostream & s,const SDL_Rect & rect)85 std::ostream& operator<<(std::ostream& s, const SDL_Rect& rect)
86 {
87 s << "x: " << rect.x << ", y: " << rect.y << ", w: " << rect.w << ", h: " << rect.h;
88 return s;
89 }
90