The Battle for Wesnoth  1.17.0-dev
rect.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 /**
19  * @file
20  * Contains the SDL_Rect helper code.
21  */
22 
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  */
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  */
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);
SDL_Rect union_rects(const SDL_Rect &rect1, const SDL_Rect &rect2)
Calculates the union of two rectangles.
Definition: rect.cpp:50
bool rects_overlap(const SDL_Rect &rect1, const SDL_Rect &rect2)
Tests whether two rectangles overlap.
Definition: rect.cpp:34
#define a
void fill_surface_rect(surface &dst, SDL_Rect *dst_rect, const uint32_t color)
Fill a rectangle on a given surface.
Definition: rect.hpp:115
SDL_Rect intersect_rects(const SDL_Rect &rect1, const SDL_Rect &rect2)
Calculates the intersection of two rectangles.
Definition: rect.cpp:40
#define h
#define b
bool operator==(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:76
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:81
void draw_rectangle(const SDL_Rect &rect, const color_t &color)
Draw a rectangle outline.
Definition: rect.cpp:58
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:23
static map_location::DIRECTION s
Holds a 2D point.
Definition: point.hpp:24
int w
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an SDL_Rect with the given dimensions.
Definition: rect.hpp:40
constexpr const SDL_Rect empty_rect
Definition: rect.hpp:32
void fill_rectangle(const SDL_Rect &rect, const color_t &color)
Draws a filled rectangle.
Definition: rect.cpp:66
std::ostream & operator<<(std::ostream &s, const SDL_Rect &rect)
Definition: rect.cpp:86