The Battle for Wesnoth  1.19.7+dev
rect.cpp
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 #include "sdl/point.hpp"
17 #include "sdl/rect.hpp"
18 
19 #include <algorithm>
20 #include <ostream>
21 
22 bool operator==(const SDL_Rect& a, const SDL_Rect& b)
23 {
24  return SDL_RectEquals(&a, &b) != SDL_FALSE;
25 }
26 
27 bool operator!=(const SDL_Rect& a, const SDL_Rect& b)
28 {
29  return !operator==(a,b);
30 }
31 
32 std::ostream& operator<<(std::ostream& s, const SDL_Rect& r)
33 {
34  s << '[' << r.x << ',' << r.y << '|' << r.w << ',' << r.h << ']';
35  return s;
36 }
37 
38 bool rect::operator==(const rect& r) const
39 {
40  return SDL_RectEquals(this, &r) != SDL_FALSE;
41 }
42 
43 bool rect::operator==(const SDL_Rect& r) const
44 {
45  return SDL_RectEquals(this, &r) != SDL_FALSE;
46 }
47 
48 bool rect::empty() const
49 {
50  return SDL_RectEmpty(this);
51 }
52 
53 bool rect::contains(int x, int y) const
54 {
55  SDL_Point p{x, y};
56  return SDL_PointInRect(&p, this) != SDL_FALSE;
57 }
58 
59 bool rect::contains(const point& point) const
60 {
61  return SDL_PointInRect(&point, this) != SDL_FALSE;
62 }
63 
64 bool rect::contains(const SDL_Rect& r) const
65 {
66  if(this->x > r.x) return false;
67  if(this->y > r.y) return false;
68  if(this->x + this->w < r.x + r.w) return false;
69  if(this->y + this->h < r.y + r.h) return false;
70  return true;
71 }
72 
73 bool rect::overlaps(const SDL_Rect& r) const
74 {
75  return SDL_HasIntersection(this, &r);
76 }
77 
78 rect rect::minimal_cover(const SDL_Rect& other) const
79 {
80  rect result;
81  SDL_UnionRect(this, &other, &result);
82  return result;
83 }
84 
85 rect& rect::expand_to_cover(const SDL_Rect& other)
86 {
87  SDL_UnionRect(this, &other, this);
88  return *this;
89 }
90 
91 rect rect::intersect(const SDL_Rect& other) const
92 {
93  rect result;
94  if(!SDL_IntersectRect(this, &other, &result)) {
95  return rect();
96  }
97  return result;
98 }
99 
100 void rect::clip(const SDL_Rect& other)
101 {
102  *this = this->intersect(other);
103 }
104 
105 void rect::shift(const point& other)
106 {
107  this->x += other.x;
108  this->y += other.y;
109 }
110 
111 rect rect::shifted_by(int x, int y) const
112 {
113  rect res = *this;
114  res.x += x;
115  res.y += y;
116  return res;
117 }
118 
119 rect rect::shifted_by(const point& other) const
120 {
121  return shifted_by(other.x, other.y);
122 }
123 
124 point rect::point_at(double x, double y) const
125 {
126  return {
127  static_cast<int>(this->x + this->w * std::clamp(x, 0.0, 1.0)),
128  static_cast<int>(this->y + this->h * std::clamp(y, 0.0, 1.0))
129  };
130 }
131 
132 rect rect::subrect(const SDL_FPoint& tl, const SDL_FPoint& br) const
133 {
134  point p1 = point_at(tl.x, tl.y);
135  point p2 = point_at(br.x, br.y);
136  return { p1, p2 - p1 };
137 }
138 
139 std::ostream& operator<<(std::ostream& s, const rect& r)
140 {
141  s << '[' << r.x << ',' << r.y << '|' << r.w << ',' << r.h << ']';
142  return s;
143 }
int w
static const std::string br
Definition: markup.hpp:33
bool operator==(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:22
std::ostream & operator<<(std::ostream &s, const SDL_Rect &r)
Definition: rect.cpp:32
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:27
Contains the SDL_Rect helper code.
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: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
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
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
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
bool operator==(const rect &r) const
Definition: rect.cpp:38
void clip(const SDL_Rect &r)
Clip this rectangle by the given rectangle.
Definition: rect.cpp:100
void shift(const point &p)
Shift the rectangle by the given relative position.
Definition: rect.cpp:105
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