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, height} {}
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  Rectangle& operator=(const Rectangle& rect) {
60  set(rect);
61  return *this;
62  }
63 
64  inline ::Vector4 ToVector4() {
65  return {x, y, width, height};
66  }
67 
68  operator ::Vector4() const {
69  return {x, y, width, height};
70  }
71 
75  inline Rectangle& Draw(::Color color) {
76  ::DrawRectangle(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
77  static_cast<int>(height), color);
78  return *this;
79  }
80 
81  inline Rectangle& Draw(::Vector2 origin, float rotation, ::Color color) {
82  ::DrawRectanglePro(*this, origin, rotation, color);
83  return *this;
84  }
85 
86  inline Rectangle& DrawGradientV(::Color color1, ::Color color2) {
87  ::DrawRectangleGradientV(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
88  static_cast<int>(height), color1, color2);
89  return *this;
90  }
91 
92  inline Rectangle& DrawGradientH(::Color color1, ::Color color2) {
93  ::DrawRectangleGradientH(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
94  static_cast<int>(height), color1, color2);
95  return *this;
96  }
97 
98  inline Rectangle& DrawGradient(::Color col1, ::Color col2, ::Color col3, ::Color col4) {
99  ::DrawRectangleGradientEx(*this, col1, col2, col3, col4);
100  return *this;
101  }
102 
103  inline Rectangle& DrawLines(::Color color) {
104  ::DrawRectangleLines(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
105  static_cast<int>(height), color);
106  return *this;
107  }
108 
109  inline Rectangle& DrawLines(::Color color, int lineThick) {
110  ::DrawRectangleLinesEx(*this, lineThick, color);
111  return *this;
112  }
113 
114  inline Rectangle& DrawRounded(float roundness, int segments, ::Color color) {
115  ::DrawRectangleRounded(*this, roundness, segments, color);
116  return *this;
117  }
118 
119  inline Rectangle& DrawRoundedLines(float roundness, int segments, int lineThick,
120  ::Color color) {
121  ::DrawRectangleRoundedLines(*this, roundness, segments, lineThick, color);
122  return *this;
123  }
124 
128  inline bool CheckCollision(::Rectangle rec2) const {
129  return ::CheckCollisionRecs(*this, rec2);
130  }
131 
135  inline ::Rectangle GetCollision(::Rectangle rec2) const {
136  return ::GetCollisionRec(*this, rec2);
137  }
138 
142  inline bool CheckCollision(::Vector2 point) const {
143  return ::CheckCollisionPointRec(point, *this);
144  }
145 
149  inline bool CheckCollision(::Vector2 center, float radius) {
150  return ::CheckCollisionCircleRec(center, radius, *this);
151  }
152 
153  inline ::Vector2 GetSize() {
154  return {width, height};
155  }
156 
157  inline Rectangle& SetSize(float newWidth, float newHeight) {
158  width = newWidth;
159  height = newHeight;
160  return *this;
161  }
162 
163  inline Rectangle& SetSize(const ::Vector2& size) {
164  return SetSize(size.x, size.y);
165  }
166 
167  inline ::Vector2 GetPosition() {
168  return {x, y};
169  }
170 
171  inline Rectangle& SetPosition(float newX, float newY) {
172  x = newX;
173  y = newY;
174  return *this;
175  }
176 
177  inline Rectangle& SetPosition(const ::Vector2& position) {
178  return SetPosition(position.x, position.y);
179  }
180 
181  protected:
182  inline void set(const ::Rectangle& rect) {
183  x = rect.x;
184  y = rect.y;
185  width = rect.width;
186  height = rect.height;
187  }
188 };
189 } // namespace raylib
190 
191 #endif // RAYLIB_CPP_INCLUDE_RECTANGLE_HPP_
raylib::Rectangle::GetPosition
inline ::Vector2 GetPosition()
Definition: Rectangle.hpp:167
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:157
raylib::Rectangle::Rectangle
Rectangle(float x)
Definition: Rectangle.hpp:42
raylib::Rectangle::DrawRounded
Rectangle & DrawRounded(float roundness, int segments, ::Color color)
Definition: Rectangle.hpp:114
raylib::Rectangle::DrawGradientH
Rectangle & DrawGradientH(::Color color1, ::Color color2)
Definition: Rectangle.hpp:92
raylib::Rectangle::Rectangle
Rectangle(::Vector2 size)
Definition: Rectangle.hpp:47
raylib::Rectangle::Draw
Rectangle & Draw(::Vector2 origin, float rotation, ::Color color)
Definition: Rectangle.hpp:81
raylib::Rectangle::GetSize
inline ::Vector2 GetSize()
Definition: Rectangle.hpp:153
raylib::Rectangle::Rectangle
Rectangle(::Vector2 position, ::Vector2 size)
Definition: Rectangle.hpp:45
raylib::Rectangle::SetPosition
Rectangle & SetPosition(const ::Vector2 &position)
Definition: Rectangle.hpp:177
raylib::Rectangle::DrawGradientV
Rectangle & DrawGradientV(::Color color1, ::Color color2)
Definition: Rectangle.hpp:86
raylib::Rectangle::DrawLines
Rectangle & DrawLines(::Color color)
Definition: Rectangle.hpp:103
raylib::Rectangle::CheckCollision
bool CheckCollision(::Vector2 point) const
Check if point is inside rectangle.
Definition: Rectangle.hpp:142
raylib::Rectangle::ToVector4
inline ::Vector4 ToVector4()
Definition: Rectangle.hpp:64
raylib::Rectangle::CheckCollision
bool CheckCollision(::Vector2 center, float radius)
Check collision between circle and rectangle.
Definition: Rectangle.hpp:149
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:135
raylib::Rectangle::SetSize
Rectangle & SetSize(const ::Vector2 &size)
Definition: Rectangle.hpp:163
raylib::Rectangle::SetPosition
Rectangle & SetPosition(float newX, float newY)
Definition: Rectangle.hpp:171
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:98
raylib::Rectangle::CheckCollision
bool CheckCollision(::Rectangle rec2) const
Check collision between two rectangles.
Definition: Rectangle.hpp:128
raylib::Rectangle::Rectangle
Rectangle()
Definition: Rectangle.hpp:43
raylib::Rectangle::DrawLines
Rectangle & DrawLines(::Color color, int lineThick)
Definition: Rectangle.hpp:109
raylib::Rectangle::set
void set(const ::Rectangle &rect)
Definition: Rectangle.hpp:182
raylib::Rectangle::Draw
Rectangle & Draw(::Color color)
Draw a color-filled rectangle.
Definition: Rectangle.hpp:75
raylib::Rectangle::DrawRoundedLines
Rectangle & DrawRoundedLines(float roundness, int segments, int lineThick, ::Color color)
Definition: Rectangle.hpp:119
raylib::Rectangle::operator=
Rectangle & operator=(const Rectangle &rect)
Definition: Rectangle.hpp:59