The Battle for Wesnoth  1.19.0+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 pos() 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  /** False if both w and h are > 0, true otherwise. */
104  bool empty() const;
105 
106  /** Whether the given point lies within the rectangle. */
107  bool contains(int x, int y) const;
108  bool contains(const point& p) const;
109 
110  /** Whether the given rectangle is completely contained by this one. */
111  bool contains(const SDL_Rect& r) const;
112 
113  /** Whether the given rectangle and this rectangle overlap. */
114  bool overlaps(const SDL_Rect& r) const;
115 
116  /**
117  * Calculates the minimal rectangle that completely contains both
118  * this rectangle and the given rectangle.
119  */
120  rect minimal_cover(const SDL_Rect& r) const;
121 
122  /** Minimally expand this rect to fully contain another. */
123  rect& expand_to_cover(const SDL_Rect& r);
124 
125  /**
126  * Calculates the intersection of this rectangle and another;
127  * that is, the maximal rectangle that is contained by both.
128  */
129  rect intersect(const SDL_Rect& r) const;
130 
131  /**
132  * Clip this rectangle by the given rectangle.
133  *
134  * This rectangle will be reduced to the intersection of both rectangles.
135  */
136  void clip(const SDL_Rect& r);
137 
138  /**
139  * Shift the rectangle by the given relative position.
140  *
141  * The point's X and Y coordinates will be added to the rectangle's.
142  */
143  void shift(const point& p);
144 };
145 
146 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:21
std::ostream & operator<<(std::ostream &s, const SDL_Rect &rect)
Definition: rect.cpp:31
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:26
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
bool empty() const
False if both w and h are > 0, true otherwise.
Definition: rect.cpp:47
rect & expand_to_cover(const SDL_Rect &r)
Minimally expand this rect to fully contain another.
Definition: rect.cpp:84
constexpr rect(const SDL_Rect &r)
There's nothing extra when converting an SDL_Rect.
Definition: rect.hpp:53
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:77
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
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:52
bool operator==(const rect &r) const
Definition: rect.cpp:37
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:99
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:104
constexpr rect operator*(int s) const
Definition: rect.hpp:72
constexpr point pos() const
Definition: rect.hpp:64
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:90
bool overlaps(const SDL_Rect &r) const
Whether the given rectangle and this rectangle overlap.
Definition: rect.cpp:72
mock_party p
static map_location::DIRECTION s
#define h
#define a
#define b