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