The Battle for Wesnoth  1.19.5+dev
rect.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
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 "sdl/point.hpp"
24 
25 #include <SDL2/SDL_rect.h>
26 
27 namespace sdl
28 {
29 
30 constexpr const SDL_Rect empty_rect { 0, 0, 0, 0 };
31 
32 } // namespace sdl
33 
34 bool operator==(const SDL_Rect& a, const SDL_Rect& b);
35 bool operator!=(const SDL_Rect& a, const SDL_Rect& b);
36 
37 std::ostream& operator<<(std::ostream& s, const SDL_Rect& rect);
38 
39 /**
40  * An abstract description of a rectangle with integer coordinates.
41  *
42  * This is a thin wrapper over SDL_Rect, furnished with utility functions.
43  *
44  * As for SDL_Rect, member variables x, y, w and h are public.
45  */
46 struct rect : SDL_Rect
47 {
48 public:
49  /** Explicitly initialize rects to 0. */
50  constexpr rect() : SDL_Rect{0, 0, 0, 0} {}
51 
52  /** There's nothing extra when converting an SDL_Rect. */
53  constexpr rect(const SDL_Rect& r) : SDL_Rect{r} {}
54 
55  /** Specify via (x, y, w, h). */
56  constexpr rect(int x, int y, int w, int h) : SDL_Rect{x, y, w, h} {}
57 
58  /** Specify via top-left corner position and size. */
59  constexpr rect(const point& pos, const point& size)
60  : SDL_Rect{pos.x, pos.y, size.x, size.y}
61  {}
62 
63  // subcomponent access
64  constexpr point origin() const { return {x, y}; }
65  constexpr point size() const { return {w, h}; }
66 
67  // Comparisons
68  bool operator==(const rect& r) const;
69  bool operator==(const SDL_Rect& r) const;
70 
71  // Scalar multiplication and division
72  constexpr rect operator*(int s) const
73  {
74  return {x * s, y * s, w * s, h * s};
75  }
76 
77  constexpr rect& operator*=(int s)
78  {
79  x *= s;
80  y *= s;
81  w *= s;
82  h *= s;
83  return *this;
84  }
85 
86  constexpr rect operator/(int s) const
87  {
88  return {x / s, y / s, w / s, h / s};
89  }
90 
91  constexpr rect& operator/=(int s)
92  {
93  x /= s;
94  y /= s;
95  w /= s;
96  h /= s;
97  return *this;
98  }
99 
100  /** The area of this rectangle, in square pixels. */
101  constexpr int area() const { return w * h; }
102 
103  /** The center point of the rectangle, accounting for origin. */
104  constexpr point center() const
105  {
106  return {x + w / 2, y + h / 2};
107  }
108 
109  /** False if both w and h are > 0, true otherwise. */
110  bool empty() const;
111 
112  /** Whether the given point lies within the rectangle. */
113  bool contains(int x, int y) const;
114  bool contains(const point& p) const;
115 
116  /** Whether the given rectangle is completely contained by this one. */
117  bool contains(const SDL_Rect& r) const;
118 
119  /** Whether the given rectangle and this rectangle overlap. */
120  bool overlaps(const SDL_Rect& r) const;
121 
122  /**
123  * Calculates the minimal rectangle that completely contains both
124  * this rectangle and the given rectangle.
125  */
126  rect minimal_cover(const SDL_Rect& r) const;
127 
128  /** Minimally expand this rect to fully contain another. */
129  rect& expand_to_cover(const SDL_Rect& r);
130 
131  /**
132  * Calculates the intersection of this rectangle and another;
133  * that is, the maximal rectangle that is contained by both.
134  */
135  rect intersect(const SDL_Rect& r) const;
136 
137  /**
138  * Clip this rectangle by the given rectangle.
139  *
140  * This rectangle will be reduced to the intersection of both rectangles.
141  */
142  void clip(const SDL_Rect& r);
143 
144  /**
145  * Shift the rectangle by the given relative position.
146  *
147  * The point's X and Y coordinates will be added to the rectangle's.
148  */
149  void shift(const point& p);
150 
151  /** Returns a new rectangle shifted by the given relative position. */
152  rect shifted_by(int x, int y) const;
153  rect shifted_by(const point& p) const;
154 
155  /** Returns a new rectangle with @a dx horizontal padding and @a dy vertical padding. */
156  constexpr rect padded_by(int dx, int dy) const
157  {
158  return { x - dx, y - dy, w + dx * 2, h + dy * 2 };
159  }
160 
161  /** Returns a new rectangle with equal @a amount horizontal and vertical padding. */
162  constexpr rect padded_by(int amount) const
163  {
164  return padded_by(amount, amount);
165  }
166 
167  /** Returns the proper point that corresponds to the given [0.0, 1.0] coordinates. */
168  point point_at(double x, double y) const;
169 
170  /** Returns the sub-rect bounded to the top left and bottom right by the given [0.0, 1.0] coordinates. */
171  rect subrect(const SDL_FPoint& top_left, const SDL_FPoint& bottom_right) const;
172 };
173 
174 std::ostream& operator<<(std::ostream&, const rect&);
int w
constexpr const SDL_Rect empty_rect
Definition: rect.hpp:30
bool operator==(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:22
std::ostream & operator<<(std::ostream &s, const SDL_Rect &rect)
Definition: rect.cpp:32
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:27
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
constexpr point center() const
The center point of the rectangle, accounting for origin.
Definition: rect.hpp:104
bool empty() const
False if both w and h are > 0, true otherwise.
Definition: rect.cpp:48
rect & expand_to_cover(const SDL_Rect &r)
Minimally expand this rect to fully contain another.
Definition: rect.cpp:85
rect shifted_by(int x, int y) const
Returns a new rectangle shifted by the given relative position.
Definition: rect.cpp:111
constexpr point origin() const
Definition: rect.hpp:64
constexpr rect(const SDL_Rect &r)
There's nothing extra when converting an SDL_Rect.
Definition: rect.hpp:53
point point_at(double x, double y) const
Returns the proper point that corresponds to the given [0.0, 1.0] coordinates.
Definition: rect.cpp:124
constexpr rect & operator*=(int s)
Definition: rect.hpp:77
rect minimal_cover(const SDL_Rect &r) const
Calculates the minimal rectangle that completely contains both this rectangle and the given rectangle...
Definition: rect.cpp:78
constexpr rect(int x, int y, int w, int h)
Specify via (x, y, w, h).
Definition: rect.hpp:56
constexpr rect operator/(int s) const
Definition: rect.hpp:86
rect subrect(const SDL_FPoint &top_left, const SDL_FPoint &bottom_right) const
Returns the sub-rect bounded to the top left and bottom right by the given [0.0, 1....
Definition: rect.cpp:132
constexpr rect()
Explicitly initialize rects to 0.
Definition: rect.hpp:50
bool contains(int x, int y) const
Whether the given point lies within the rectangle.
Definition: rect.cpp:53
constexpr rect padded_by(int amount) const
Returns a new rectangle with equal amount horizontal and vertical padding.
Definition: rect.hpp:162
bool operator==(const rect &r) const
Definition: rect.cpp:38
constexpr rect padded_by(int dx, int dy) const
Returns a new rectangle with dx horizontal padding and dy vertical padding.
Definition: rect.hpp:156
constexpr rect(const point &pos, const point &size)
Specify via top-left corner position and size.
Definition: rect.hpp:59
void clip(const SDL_Rect &r)
Clip this rectangle by the given rectangle.
Definition: rect.cpp:100
constexpr int area() const
The area of this rectangle, in square pixels.
Definition: rect.hpp:101
constexpr rect & operator/=(int s)
Definition: rect.hpp:91
constexpr point size() const
Definition: rect.hpp:65
void shift(const point &p)
Shift the rectangle by the given relative position.
Definition: rect.cpp:105
constexpr rect operator*(int s) const
Definition: rect.hpp:72
rect intersect(const SDL_Rect &r) const
Calculates the intersection of this rectangle and another; that is, the maximal rectangle that is con...
Definition: rect.cpp:91
bool overlaps(const SDL_Rect &r) const
Whether the given rectangle and this rectangle overlap.
Definition: rect.cpp:73
mock_party p
static map_location::direction s
#define h
#define b