raylib-cpp
C++ object-oriented wrapper library for raylib.
Rectangle.hpp
1 /*
2 * LICENSE: zlib/libpng
3 *
4 * raylib-cpp is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
5 * BSD-like license that allows static linking with closed source software:
6 *
7 * Copyright (c) 2020 Rob Loach (@RobLoach)
8 *
9 * This software is provided "as-is", without any express or implied warranty. In no event
10 * will the authors be held liable for any damages arising from the use of this software.
11 *
12 * Permission is granted to anyone to use this software for any purpose, including commercial
13 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not claim that you
16 * wrote the original software. If you use this software in a product, an acknowledgment
17 * in the product documentation would be appreciated but is not required.
18 *
19 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
20 * as being the original software.
21 *
22 * 3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 #ifndef RAYLIB_CPP_INCLUDE_RECTANGLE_HPP_
26 #define RAYLIB_CPP_INCLUDE_RECTANGLE_HPP_
27 
28 #include "./raylib.hpp"
29 #include "./raylib-cpp-utils.hpp"
30 #include "./Vector2.hpp"
31 
32 namespace raylib {
33 class Rectangle : public ::Rectangle {
34  public:
35  Rectangle(const ::Rectangle& vec) {
36  set(vec);
37  }
38 
39  Rectangle(float x, float y, float width, float height) : ::Rectangle{x, y, width, height} {}
40  Rectangle(float x, float y, float width) : ::Rectangle{x, y, width, 0} {}
41  Rectangle(float x, float y) : ::Rectangle{x, y, 0, 0} {}
42  Rectangle(float x) : ::Rectangle{x, 0, 0, 0} {}
43  Rectangle() : ::Rectangle{0, 0, 0, 0} {}
44 
45  Rectangle(::Vector2 position, ::Vector2 size)
46  : ::Rectangle{position.x, position.y, size.x, size.y} {}
47  Rectangle(::Vector2 size) : ::Rectangle{0, 0, size.x, size.y} {}
48 
49  GETTERSETTER(float, X, x)
50  GETTERSETTER(float, Y, y)
51  GETTERSETTER(float, Width, width)
52  GETTERSETTER(float, Height, height)
53 
54  Rectangle& operator=(const ::Rectangle& rect) {
55  set(rect);
56  return *this;
57  }
58 
59  inline ::Vector4 ToVector4() {
60  return {x, y, width, height};
61  }
62 
63  operator ::Vector4() const {
64  return {x, y, width, height};
65  }
66 
70  inline Rectangle& Draw(::Color color) {
71  ::DrawRectangle(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
72  static_cast<int>(height), color);
73  return *this;
74  }
75 
76  inline Rectangle& Draw(::Vector2 origin, float rotation, ::Color color) {
77  ::DrawRectanglePro(*this, origin, rotation, color);
78  return *this;
79  }
80 
81  inline Rectangle& DrawGradientV(::Color color1, ::Color color2) {
82  ::DrawRectangleGradientV(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
83  static_cast<int>(height), color1, color2);
84  return *this;
85  }
86 
87  inline Rectangle& DrawGradientH(::Color color1, ::Color color2) {
88  ::DrawRectangleGradientH(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
89  static_cast<int>(height), color1, color2);
90  return *this;
91  }
92 
93  inline Rectangle& DrawGradient(::Color col1, ::Color col2, ::Color col3, ::Color col4) {
94  ::DrawRectangleGradientEx(*this, col1, col2, col3, col4);
95  return *this;
96  }
97 
98  inline Rectangle& DrawLines(::Color color) {
99  ::DrawRectangleLines(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
100  static_cast<int>(height), color);
101  return *this;
102  }
103 
104  inline Rectangle& DrawLines(::Color color, int lineThick) {
105  ::DrawRectangleLinesEx(*this, lineThick, color);
106  return *this;
107  }
108 
109  inline Rectangle& DrawRounded(float roundness, int segments, ::Color color) {
110  ::DrawRectangleRounded(*this, roundness, segments, color);
111  return *this;
112  }
113 
114  inline Rectangle& DrawRoundedLines(float roundness, int segments, int lineThick,
115  ::Color color) {
116  ::DrawRectangleRoundedLines(*this, roundness, segments, lineThick, color);
117  return *this;
118  }
119 
123  inline bool CheckCollision(::Rectangle rec2) const {
124  return ::CheckCollisionRecs(*this, rec2);
125  }
126 
130  inline ::Rectangle GetCollision(::Rectangle rec2) const {
131  return ::GetCollisionRec(*this, rec2);
132  }
133 
137  inline bool CheckCollision(::Vector2 point) const {
138  return ::CheckCollisionPointRec(point, *this);
139  }
140 
144  inline bool CheckCollision(::Vector2 center, float radius) {
145  return ::CheckCollisionCircleRec(center, radius, *this);
146  }
147 
148  inline ::Vector2 GetSize() {
149  return {width, height};
150  }
151 
152  inline Rectangle& SetSize(float newWidth, float newHeight) {
153  width = newWidth;
154  height = newHeight;
155  return *this;
156  }
157 
158  inline Rectangle& SetSize(const ::Vector2& size) {
159  return SetSize(size.x, size.y);
160  }
161 
162  inline ::Vector2 GetPosition() {
163  return {x, y};
164  }
165 
166  inline Rectangle& SetPosition(float newX, float newY) {
167  x = newX;
168  y = newY;
169  return *this;
170  }
171 
172  inline Rectangle& SetPosition(const ::Vector2& position) {
173  return SetPosition(position.x, position.y);
174  }
175 
176  protected:
177  inline void set(const ::Rectangle& rect) {
178  x = rect.x;
179  y = rect.y;
180  width = rect.width;
181  height = rect.height;
182  }
183 };
184 } // namespace raylib
185 
186 #endif // RAYLIB_CPP_INCLUDE_RECTANGLE_HPP_
raylib::Rectangle::GetPosition
inline ::Vector2 GetPosition()
Definition: Rectangle.hpp:162
raylib
Provides all the classes associated with raylib-cpp.
Definition: AudioDevice.hpp:31
raylib::Rectangle::SetSize
Rectangle & SetSize(float newWidth, float newHeight)
Definition: Rectangle.hpp:152
raylib::Rectangle::Rectangle
Rectangle(float x)
Definition: Rectangle.hpp:42
raylib::Rectangle::DrawRounded
Rectangle & DrawRounded(float roundness, int segments, ::Color color)
Definition: Rectangle.hpp:109
raylib::Rectangle::DrawGradientH
Rectangle & DrawGradientH(::Color color1, ::Color color2)
Definition: Rectangle.hpp:87
raylib::Rectangle::Rectangle
Rectangle(::Vector2 size)
Definition: Rectangle.hpp:47
raylib::Rectangle::Draw
Rectangle & Draw(::Vector2 origin, float rotation, ::Color color)
Definition: Rectangle.hpp:76
raylib::Rectangle::GetSize
inline ::Vector2 GetSize()
Definition: Rectangle.hpp:148
raylib::Rectangle::Rectangle
Rectangle(::Vector2 position, ::Vector2 size)
Definition: Rectangle.hpp:45
raylib::Rectangle::SetPosition
Rectangle & SetPosition(const ::Vector2 &position)
Definition: Rectangle.hpp:172
raylib::Rectangle::DrawGradientV
Rectangle & DrawGradientV(::Color color1, ::Color color2)
Definition: Rectangle.hpp:81
raylib::Rectangle::DrawLines
Rectangle & DrawLines(::Color color)
Definition: Rectangle.hpp:98
raylib::Rectangle::CheckCollision
bool CheckCollision(::Vector2 point) const
Check if point is inside rectangle.
Definition: Rectangle.hpp:137
raylib::Rectangle::ToVector4
inline ::Vector4 ToVector4()
Definition: Rectangle.hpp:59
raylib::Rectangle::CheckCollision
bool CheckCollision(::Vector2 center, float radius)
Check collision between circle and rectangle.
Definition: Rectangle.hpp:144
raylib::Rectangle::Rectangle
Rectangle(const ::Rectangle &vec)
Definition: Rectangle.hpp:35
raylib::Rectangle
Definition: Rectangle.hpp:33
raylib::Vector2
Definition: Vector2.hpp:37
raylib::Color
Definition: Color.hpp:35
raylib::Rectangle::Rectangle
Rectangle(float x, float y, float width)
Definition: Rectangle.hpp:40
raylib::Rectangle::GetCollision
inline ::Rectangle GetCollision(::Rectangle rec2) const
Get collision rectangle for two rectangles collision.
Definition: Rectangle.hpp:130
raylib::Rectangle::SetSize
Rectangle & SetSize(const ::Vector2 &size)
Definition: Rectangle.hpp:158
raylib::Rectangle::SetPosition
Rectangle & SetPosition(float newX, float newY)
Definition: Rectangle.hpp:166
raylib::Rectangle::Rectangle
Rectangle(float x, float y)
Definition: Rectangle.hpp:41
raylib::Rectangle::Rectangle
Rectangle(float x, float y, float width, float height)
Definition: Rectangle.hpp:39
raylib::Rectangle::DrawGradient
Rectangle & DrawGradient(::Color col1, ::Color col2, ::Color col3, ::Color col4)
Definition: Rectangle.hpp:93
raylib::Rectangle::CheckCollision
bool CheckCollision(::Rectangle rec2) const
Check collision between two rectangles.
Definition: Rectangle.hpp:123
raylib::Rectangle::Rectangle
Rectangle()
Definition: Rectangle.hpp:43
raylib::Rectangle::DrawLines
Rectangle & DrawLines(::Color color, int lineThick)
Definition: Rectangle.hpp:104
raylib::Rectangle::set
void set(const ::Rectangle &rect)
Definition: Rectangle.hpp:177
raylib::Rectangle::Draw
Rectangle & Draw(::Color color)
Draw a color-filled rectangle.
Definition: Rectangle.hpp:70
raylib::Rectangle::DrawRoundedLines
Rectangle & DrawRoundedLines(float roundness, int segments, int lineThick, ::Color color)
Definition: Rectangle.hpp:114