The Battle for Wesnoth  1.19.0-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 <ostream>
20 
21 bool operator==(const SDL_Rect& a, const SDL_Rect& b)
22 {
23  return SDL_RectEquals(&a, &b) != SDL_FALSE;
24 }
25 
26 bool operator!=(const SDL_Rect& a, const SDL_Rect& b)
27 {
28  return !operator==(a,b);
29 }
30 
31 std::ostream& operator<<(std::ostream& s, const SDL_Rect& r)
32 {
33  s << '[' << r.x << ',' << r.y << '|' << r.w << ',' << r.h << ']';
34  return s;
35 }
36 
37 bool rect::operator==(const rect& r) const
38 {
39  return SDL_RectEquals(this, &r) != SDL_FALSE;
40 }
41 
42 bool rect::operator==(const SDL_Rect& r) const
43 {
44  return SDL_RectEquals(this, &r) != SDL_FALSE;
45 }
46 
47 bool rect::empty() const
48 {
49  return SDL_RectEmpty(this);
50 }
51 
52 bool rect::contains(int x, int y) const
53 {
54  SDL_Point p{x, y};
55  return SDL_PointInRect(&p, this) != SDL_FALSE;
56 }
57 
58 bool rect::contains(const point& point) const
59 {
60  return SDL_PointInRect(&point, this) != SDL_FALSE;
61 }
62 
63 bool rect::contains(const SDL_Rect& r) const
64 {
65  if(this->x > r.x) return false;
66  if(this->y > r.y) return false;
67  if(this->x + this->w < r.x + r.w) return false;
68  if(this->y + this->h < r.y + r.h) return false;
69  return true;
70 }
71 
72 bool rect::overlaps(const SDL_Rect& r) const
73 {
74  return SDL_HasIntersection(this, &r);
75 }
76 
77 rect rect::minimal_cover(const SDL_Rect& other) const
78 {
79  rect result;
80  SDL_UnionRect(this, &other, &result);
81  return result;
82 }
83 
84 rect& rect::expand_to_cover(const SDL_Rect& other)
85 {
86  SDL_UnionRect(this, &other, this);
87  return *this;
88 }
89 
90 rect rect::intersect(const SDL_Rect& other) const
91 {
92  rect result;
93  if(!SDL_IntersectRect(this, &other, &result)) {
94  return rect();
95  }
96  return result;
97 }
98 
99 void rect::clip(const SDL_Rect& other)
100 {
101  *this = this->intersect(other);
102 }
103 
104 void rect::shift(const point& other)
105 {
106  this->x += other.x;
107  this->y += other.y;
108 }
109 
110 std::ostream& operator<<(std::ostream& s, const rect& r)
111 {
112  s << '[' << r.x << ',' << r.y << '|' << r.w << ',' << r.h << ']';
113  return s;
114 }
int w
bool operator==(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:21
std::ostream & operator<<(std::ostream &s, const SDL_Rect &r)
Definition: rect.cpp:31
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:26
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:47
rect & expand_to_cover(const SDL_Rect &r)
Minimally expand this rect to fully contain another.
Definition: rect.cpp:84
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()
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
void clip(const SDL_Rect &r)
Clip this rectangle by the given rectangle.
Definition: rect.cpp:99
void shift(const point &p)
Shift the rectangle by the given relative position.
Definition: rect.cpp:104
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