The Battle for Wesnoth  1.19.9+dev
rect.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2025
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 <cmath>
20 #include <algorithm>
21 #include <ostream>
22 
23 bool operator==(const SDL_Rect& a, const SDL_Rect& b)
24 {
25  return SDL_RectEquals(&a, &b) != SDL_FALSE;
26 }
27 
28 bool operator!=(const SDL_Rect& a, const SDL_Rect& b)
29 {
30  return !operator==(a,b);
31 }
32 
33 std::ostream& operator<<(std::ostream& s, const SDL_Rect& r)
34 {
35  s << '[' << r.x << ',' << r.y << '|' << r.w << ',' << r.h << ']';
36  return s;
37 }
38 
39 bool rect::operator==(const rect& r) const
40 {
41  return SDL_RectEquals(this, &r) != SDL_FALSE;
42 }
43 
44 bool rect::operator==(const SDL_Rect& r) const
45 {
46  return SDL_RectEquals(this, &r) != SDL_FALSE;
47 }
48 
49 bool rect::empty() const
50 {
51  return SDL_RectEmpty(this);
52 }
53 
54 bool rect::contains(int x, int y) const
55 {
56  SDL_Point p{x, y};
57  return SDL_PointInRect(&p, this) != SDL_FALSE;
58 }
59 
60 bool rect::contains(const point& point) const
61 {
62  return SDL_PointInRect(&point, this) != SDL_FALSE;
63 }
64 
65 bool rect::contains(const SDL_Rect& r) const
66 {
67  if(this->x > r.x) return false;
68  if(this->y > r.y) return false;
69  if(this->x + this->w < r.x + r.w) return false;
70  if(this->y + this->h < r.y + r.h) return false;
71  return true;
72 }
73 
74 bool rect::overlaps(const SDL_Rect& r) const
75 {
76  return SDL_HasIntersection(this, &r);
77 }
78 
79 rect rect::minimal_cover(const SDL_Rect& other) const
80 {
81  rect result;
82  SDL_UnionRect(this, &other, &result);
83  return result;
84 }
85 
86 rect& rect::expand_to_cover(const SDL_Rect& other)
87 {
88  SDL_UnionRect(this, &other, this);
89  return *this;
90 }
91 
92 rect rect::intersect(const SDL_Rect& other) const
93 {
94  rect result;
95  if(!SDL_IntersectRect(this, &other, &result)) {
96  return rect();
97  }
98  return result;
99 }
100 
101 void rect::clip(const SDL_Rect& other)
102 {
103  *this = this->intersect(other);
104 }
105 
106 void rect::shift(const point& other)
107 {
108  this->x += other.x;
109  this->y += other.y;
110 }
111 
112 rect rect::shifted_by(int x, int y) const
113 {
114  rect res = *this;
115  res.x += x;
116  res.y += y;
117  return res;
118 }
119 
120 rect rect::shifted_by(const point& other) const
121 {
122  return shifted_by(other.x, other.y);
123 }
124 
125 point rect::point_at(double x, double y) const
126 {
127  return {
128  static_cast<int>(this->x + std::round(this->w * std::clamp(x, 0.0, 1.0))),
129  static_cast<int>(this->y + std::round(this->h * std::clamp(y, 0.0, 1.0)))
130  };
131 }
132 
133 std::ostream& operator<<(std::ostream& s, const rect& r)
134 {
135  s << '[' << r.x << ',' << r.y << '|' << r.w << ',' << r.h << ']';
136  return s;
137 }
int w
bool operator==(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:23
std::ostream & operator<<(std::ostream &s, const SDL_Rect &r)
Definition: rect.cpp:33
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:28
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:49
bool empty() const
False if both w and h are > 0, true otherwise.
Definition: rect.cpp:49
rect & expand_to_cover(const SDL_Rect &r)
Minimally expand this rect to fully contain another.
Definition: rect.cpp:86
rect shifted_by(int x, int y) const
Returns a new rectangle shifted by the given relative position.
Definition: rect.cpp:112
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:125
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:79
constexpr rect()
Explicitly initialize rects to 0.
Definition: rect.hpp:52
bool contains(int x, int y) const
Whether the given point lies within the rectangle.
Definition: rect.cpp:54
bool operator==(const rect &r) const
Definition: rect.cpp:39
void clip(const SDL_Rect &r)
Clip this rectangle by the given rectangle.
Definition: rect.cpp:101
void shift(const point &p)
Shift the rectangle by the given relative position.
Definition: rect.cpp:106
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:92
bool overlaps(const SDL_Rect &r) const
Whether the given rectangle and this rectangle overlap.
Definition: rect.cpp:74
mock_party p
static map_location::direction s
#define h
#define b