The Battle for Wesnoth  1.17.17+dev
rect.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2023
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 #include "sdl/window.hpp"
19 #include "video.hpp"
20 
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 std::ostream& operator<<(std::ostream& s, const rect& r)
113 {
114  s << '[' << r.x << ',' << r.y << '|' << r.w << ',' << r.h << ']';
115  return s;
116 }
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.
Contains a wrapper class for the SDL_Window class.
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:49
rect & expand_to_cover(const SDL_Rect &r)
Minimally expand this rect to fully contain another.
Definition: rect.cpp:86
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:50
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 a
#define b