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 
28  GETTERSETTER(float, X, x)
29  GETTERSETTER(float, Y, y)
30  GETTERSETTER(float, Width, width)
31  GETTERSETTER(float, Height, height)
32 
33  Rectangle& operator=(const ::Rectangle& rect) {
34  set(rect);
35  return *this;
36  }
37 
38  inline ::Vector4 ToVector4() {
39  return {x, y, width, height};
40  }
41 
42  operator ::Vector4() const {
43  return {x, y, width, height};
44  }
45 
49  inline Rectangle& Draw(::Color color) {
50  ::DrawRectangle(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
51  static_cast<int>(height), color);
52  return *this;
53  }
54 
55  inline Rectangle& Draw(::Vector2 origin, float rotation, ::Color color) {
56  ::DrawRectanglePro(*this, origin, rotation, color);
57  return *this;
58  }
59 
60  inline Rectangle& DrawGradientV(::Color color1, ::Color color2) {
61  ::DrawRectangleGradientV(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
62  static_cast<int>(height), color1, color2);
63  return *this;
64  }
65 
66  inline Rectangle& DrawGradientH(::Color color1, ::Color color2) {
67  ::DrawRectangleGradientH(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
68  static_cast<int>(height), color1, color2);
69  return *this;
70  }
71 
72  inline Rectangle& DrawGradient(::Color col1, ::Color col2, ::Color col3, ::Color col4) {
73  ::DrawRectangleGradientEx(*this, col1, col2, col3, col4);
74  return *this;
75  }
76 
77  inline Rectangle& DrawLines(::Color color) {
78  ::DrawRectangleLines(static_cast<int>(x), static_cast<int>(y), static_cast<int>(width),
79  static_cast<int>(height), color);
80  return *this;
81  }
82 
83  inline Rectangle& DrawLines(::Color color, int lineThick) {
84  ::DrawRectangleLinesEx(*this, lineThick, color);
85  return *this;
86  }
87 
88  inline Rectangle& DrawRounded(float roundness, int segments, ::Color color) {
89  ::DrawRectangleRounded(*this, roundness, segments, color);
90  return *this;
91  }
92 
93  inline Rectangle& DrawRoundedLines(float roundness, int segments, int lineThick,
94  ::Color color) {
95  ::DrawRectangleRoundedLines(*this, roundness, segments, lineThick, color);
96  return *this;
97  }
98 
102  inline bool CheckCollision(::Rectangle rec2) const {
103  return ::CheckCollisionRecs(*this, rec2);
104  }
105 
109  inline ::Rectangle GetCollision(::Rectangle rec2) const {
110  return ::GetCollisionRec(*this, rec2);
111  }
112 
116  inline bool CheckCollision(::Vector2 point) const {
117  return ::CheckCollisionPointRec(point, *this);
118  }
119 
123  inline bool CheckCollision(::Vector2 center, float radius) {
124  return ::CheckCollisionCircleRec(center, radius, *this);
125  }
126 
127  inline ::Vector2 GetSize() {
128  return {width, height};
129  }
130 
131  inline Rectangle& SetSize(float newWidth, float newHeight) {
132  width = newWidth;
133  height = newHeight;
134  return *this;
135  }
136 
137  inline Rectangle& SetSize(const ::Vector2& size) {
138  return SetSize(size.x, size.y);
139  }
140 
141  inline ::Vector2 GetPosition() {
142  return {x, y};
143  }
144 
145  inline Rectangle& SetPosition(float newX, float newY) {
146  x = newX;
147  y = newY;
148  return *this;
149  }
150 
151  inline Rectangle& SetPosition(const ::Vector2& position) {
152  return SetPosition(position.x, position.y);
153  }
154 
155  private:
156  inline void set(const ::Rectangle& rect) {
157  x = rect.x;
158  y = rect.y;
159  width = rect.width;
160  height = rect.height;
161  }
162 };
163 } // namespace raylib
164 
165 #endif // RAYLIB_CPP_INCLUDE_RECTANGLE_HPP_
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:7
raylib::Rectangle::CheckCollision
bool CheckCollision(::Vector2 point) const
Check if point is inside rectangle.
Definition: Rectangle.hpp:116
raylib::Rectangle::CheckCollision
bool CheckCollision(::Vector2 center, float radius)
Check collision between circle and rectangle.
Definition: Rectangle.hpp:123
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:109
raylib::Rectangle::CheckCollision
bool CheckCollision(::Rectangle rec2) const
Check collision between two rectangles.
Definition: Rectangle.hpp:102
raylib::Rectangle::Draw
Rectangle & Draw(::Color color)
Draw a color-filled rectangle.
Definition: Rectangle.hpp:49