raylib-cpp
C++ object-oriented wrapper library for raylib.
Rectangle.hpp
1 #ifndef RAYLIB_CPP_INCLUDE_RECTANGLE_HPP_
2 #define RAYLIB_CPP_INCLUDE_RECTANGLE_HPP_
3 
4 #include "./raylib.hpp"
5 #include "./raylib-cpp-utils.hpp"
6 #include "./Vector2.hpp"
7 
8 namespace raylib {
12 class Rectangle : public ::Rectangle {
13  public:
14  Rectangle(const ::Rectangle& vec) {
15  set(vec);
16  }
17 
18  Rectangle(float x, float y, float width, float height) : ::Rectangle{x, y, width, height} {}
19  Rectangle(float x, float y, float width) : ::Rectangle{x, y, width, 0} {}
20  Rectangle(float x, float y) : ::Rectangle{x, y, 0, 0} {}
21  Rectangle(float x) : ::Rectangle{x, 0, 0, 0} {}
22  Rectangle() : ::Rectangle{0, 0, 0, 0} {}
23 
24  Rectangle(::Vector2 position, ::Vector2 size)
25  : ::Rectangle{position.x, position.y, size.x, size.y} {}
26  Rectangle(::Vector2 size) : ::Rectangle{0, 0, size.x, size.y} {}
27  Rectangle(::Vector4 rect) : ::Rectangle{rect.x, rect.y, rect.z, rect.w} {}
28 
29  GETTERSETTER(float, X, x)
30  GETTERSETTER(float, Y, y)
31  GETTERSETTER(float, Width, width)
32  GETTERSETTER(float, Height, height)
33 
34  Rectangle& operator=(const ::Rectangle& rect) {
35  set(rect);
36  return *this;
37  }
38 
39  inline ::Vector4 ToVector4() {
40  return {x, y, width, height};
41  }
42 
43  operator ::Vector4() const {
44  return {x, y, width, height};
45  }
46 
50  inline Rectangle& Draw(::Color color) {
51  ::DrawRectangle(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
52  static_cast<int>(height), color);
53  return *this;
54  }
55 
56  inline Rectangle& Draw(::Vector2 origin, float rotation, ::Color color) {
57  ::DrawRectanglePro(*this, origin, rotation, color);
58  return *this;
59  }
60 
61  inline Rectangle& DrawGradientV(::Color color1, ::Color color2) {
62  ::DrawRectangleGradientV(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
63  static_cast<int>(height), color1, color2);
64  return *this;
65  }
66 
67  inline Rectangle& DrawGradientH(::Color color1, ::Color color2) {
68  ::DrawRectangleGradientH(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
69  static_cast<int>(height), color1, color2);
70  return *this;
71  }
72 
73  inline Rectangle& DrawGradient(::Color col1, ::Color col2, ::Color col3, ::Color col4) {
74  ::DrawRectangleGradientEx(*this, col1, col2, col3, col4);
75  return *this;
76  }
77 
78  inline Rectangle& DrawLines(::Color color) {
79  ::DrawRectangleLines(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
80  static_cast<int>(height), color);
81  return *this;
82  }
83 
84  inline Rectangle& DrawLines(::Color color, float lineThick) {
85  ::DrawRectangleLinesEx(*this, lineThick, color);
86  return *this;
87  }
88 
89  inline Rectangle& DrawRounded(float roundness, int segments, ::Color color) {
90  ::DrawRectangleRounded(*this, roundness, segments, color);
91  return *this;
92  }
93 
94  inline Rectangle& DrawRoundedLines(float roundness, int segments,
95  float lineThick, ::Color color) {
96  ::DrawRectangleRoundedLines(*this, roundness, segments, lineThick, color);
97  return *this;
98  }
99 
103  inline bool CheckCollision(::Rectangle rec2) const {
104  return ::CheckCollisionRecs(*this, rec2);
105  }
106 
110  inline ::Rectangle GetCollision(::Rectangle rec2) const {
111  return ::GetCollisionRec(*this, rec2);
112  }
113 
117  inline bool CheckCollision(::Vector2 point) const {
118  return ::CheckCollisionPointRec(point, *this);
119  }
120 
124  inline bool CheckCollision(::Vector2 center, float radius) {
125  return ::CheckCollisionCircleRec(center, radius, *this);
126  }
127 
128  inline ::Vector2 GetSize() {
129  return {width, height};
130  }
131 
132  inline Rectangle& SetSize(float newWidth, float newHeight) {
133  width = newWidth;
134  height = newHeight;
135  return *this;
136  }
137 
138  inline Rectangle& SetSize(const ::Vector2& size) {
139  return SetSize(size.x, size.y);
140  }
141 
142  inline Rectangle& SetShapesTexture(const ::Texture2D& texture) {
143  ::SetShapesTexture(texture, *this);
144  return *this;
145  }
146 
147  inline ::Vector2 GetPosition() {
148  return {x, y};
149  }
150 
151  inline Rectangle& SetPosition(float newX, float newY) {
152  x = newX;
153  y = newY;
154  return *this;
155  }
156 
157  inline Rectangle& SetPosition(const ::Vector2& position) {
158  return SetPosition(position.x, position.y);
159  }
160 
161  private:
162  inline void set(const ::Rectangle& rect) {
163  x = rect.x;
164  y = rect.y;
165  width = rect.width;
166  height = rect.height;
167  }
168 };
169 } // namespace raylib
170 
171 #endif // RAYLIB_CPP_INCLUDE_RECTANGLE_HPP_
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
raylib::Vector4
Vector4 type.
Definition: Vector4.hpp:17
raylib::Rectangle::CheckCollision
bool CheckCollision(::Vector2 point) const
Check if point is inside rectangle.
Definition: Rectangle.hpp:117
raylib::Rectangle::CheckCollision
bool CheckCollision(::Vector2 center, float radius)
Check collision between circle and rectangle.
Definition: Rectangle.hpp:124
raylib::Rectangle
Rectangle type.
Definition: Rectangle.hpp:12
raylib::Vector2
Vector2 type.
Definition: Vector2.hpp:16
raylib::Color
Color type, RGBA (32bit)
Definition: Color.hpp:14
raylib::Rectangle::GetCollision
inline ::Rectangle GetCollision(::Rectangle rec2) const
Get collision rectangle for two rectangles collision.
Definition: Rectangle.hpp:110
raylib::Rectangle::CheckCollision
bool CheckCollision(::Rectangle rec2) const
Check collision between two rectangles.
Definition: Rectangle.hpp:103
raylib::Rectangle::Draw
Rectangle & Draw(::Color color)
Draw a color-filled rectangle.
Definition: Rectangle.hpp:50