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 #pragma once
16 
17 /**
18  * @file
19  * Contains the SDL_Rect helper code.
20  */
21 
22 #include "global.hpp"
23 #include "utils.hpp"
24 
25 #include <SDL2/SDL_rect.h>
26 
27 struct point;
28 
29 namespace sdl
30 {
31 
32 CONSTEXPR const SDL_Rect empty_rect { 0, 0, 0, 0 };
33 
34 /**
35  * Creates an SDL_Rect with the given dimensions.
36  *
37  * This is a simple wrapper in order to avoid the narrowing conversion warnings
38  * that occur when using aggregate initialization and non-int values.
39  */
create_rect(const int x,const int y,const int w,const int h)40 inline SDL_Rect create_rect(const int x, const int y, const int w, const int h)
41 {
42 	return {x, y, w, h};
43 }
44 
45 /**
46  * Tests whether a point is inside a rectangle.
47  *
48  * @param x                       The x coordinate of the point.
49  * @param y                       The y coordinate of the point.
50  * @param rect                    The rectangle.
51  *
52  * @return                        True if point (x;y) is inside or on the border
53  *                                of rect, false otherwise
54  */
55 bool point_in_rect(int x, int y, const SDL_Rect& rect);
56 
57 bool point_in_rect(const point& point, const SDL_Rect& rect);
58 
59 /**
60  * Tests whether two rectangles overlap.
61  *
62  * @param rect1                   One rectangle.
63  * @param rect2                   Another rectangle.
64  *
65  * @return                        True if rect1 and rect2 intersect, false if
66  *                                not. Touching borders don't overlap.
67  */
68 bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2);
69 
70 /**
71  * Calculates the intersection of two rectangles.
72  *
73  * @param rect1                   One rectangle.
74  * @param rect2                   Another rectangle
75  * @return                        The intersection of rect1 and rect2, or
76  *                                empty_rect if they don't overlap.
77  */
78 SDL_Rect intersect_rects(const SDL_Rect& rect1, const SDL_Rect& rect2);
79 
80 /**
81  * Calculates the union of two rectangles. Note: "union" here doesn't mean the
82  * union of the sets of points of the two polygons, but rather the minimal
83  * rectangle that supersets both rectangles.
84  *
85  * @param rect1                   One rectangle.
86  * @param rect2                   Another rectangle.
87  *
88  * @return                        The union of rect1 and rect2.
89  */
90 SDL_Rect union_rects(const SDL_Rect &rect1, const SDL_Rect &rect2);
91 
92 /**
93  * Draw a rectangle outline.
94  *
95  * @param rect                    The dimensions of the rectangle.
96  * @param color                   The color of the rectangle.
97  */
98 void draw_rectangle(const SDL_Rect& rect, const color_t& color);
99 
100 /**
101  * Draws a filled rectangle.
102  *
103  * @param rect                    The dimensions of the rectangle.
104  * @param color                   The color of the rectangle.
105  */
106 void fill_rectangle(const SDL_Rect& rect, const color_t& color);
107 
108 /**
109  * Fill a rectangle on a given surface. Alias for SDL_FillRect.
110  *
111  * @param dst                     The surface to operate on.
112  * @param dst_rect                The rectangle to fill.
113  * @param color                   Color of the rectangle.
114  */
fill_surface_rect(surface & dst,SDL_Rect * dst_rect,const uint32_t color)115 inline void fill_surface_rect(surface& dst, SDL_Rect* dst_rect, const uint32_t color)
116 {
117 	SDL_FillRect(dst, dst_rect, color);
118 }
119 
120 } // namespace sdl
121 
122 bool operator==(const SDL_Rect& a, const SDL_Rect& b);
123 bool operator!=(const SDL_Rect& a, const SDL_Rect& b);
124 
125 std::ostream& operator<<(std::ostream& s, const SDL_Rect& rect);
126