RType
Drawable.cpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** RType
4 ** File description:
5 ** Drawable.cpp
6 */
7 
8 #include "Drawable.hpp"
9 
10 #include <cstring>
11 #include <utility>
12 
13 /**
14  * @brief Construct a new Drawable:: Drawable object
15  */
17 {
18  setPosition({0, 0});
19  setSize({200, 200});
20  setRect({0, 0, 200, 200});
21  _attribute = new char[64];
22  std::memset(_attribute, 0, 64);
23  _textureId = 0;
24 }
25 
26 /**
27  * @brief getAttribute, get the attribute
28  * @return attribute (char *)
29  */
30 char *Drawable::getAttribute() const
31 {
32  return _attribute;
33 }
34 
35 /**
36  * @brief setAttribute, set the attribute
37  * @param attribute
38  */
39 void Drawable::setAttribute(std::string attribute)
40 {
41  std::memset(_attribute, 0, 64);
42  std::memcpy(_attribute, attribute.c_str(), attribute.size());
43  std::cout << "attribute: " << _attribute << std::endl;
44  hasChanged = true;
45 }
46 
47 /**
48  * @brief setAttribute, set the attribute
49  * @param attribute (srd::tuple<float, float>)
50  */
51 std::tuple<float, float> Drawable::getPosition() const
52 {
53  return _position;
54 }
55 
56 /**
57  * @brief setPosition, set the position
58  * @param position
59  */
60 void Drawable::setPosition(std::tuple<float, float> position)
61 {
62  _position = position;
63  hasChanged = true;
64 }
65 
66 /**
67  * @brief getSize, get the size
68  * @return size (std::tuple<float, float>)
69  */
70 std::tuple<float, float> Drawable::getSize() const
71 {
72  return _size;
73 }
74 
75 /**
76  * @brief getScale, get the scale
77  * @return scale (float)
78  */
79 float Drawable::getScale() const
80 {
81  return _scale;
82 }
83 
84 /**
85  * @brief setSize, set the size
86  * @param size
87  */
88 void Drawable::setSize(std::tuple<float, float> size)
89 {
90  _size = size;
91  hasChanged = true;
92 }
93 
94 /**
95  * @brief setScale, set the scale
96  */
97 void Drawable::setScale(float scale)
98 {
99  _scale = scale;
100 }
101 
102 /**
103  * @brief getRect, get the rect
104  * @return rect (std::tuple<int, int, int, int>)
105  */
106 std::tuple<int, int, int, int> Drawable::getRect() const
107 {
108  return _rect;
109 }
110 
111 /**
112  * @brief setRect, set the rect
113  * @param rect
114  */
115 void Drawable::setRect(std::tuple<int, int, int, int> rect)
116 {
117  _rect = rect;
118  hasChanged = true;
119 }
120 
121 /**
122  * @brief getPacket, get the packet
123  * @return packet (std::shared_ptr<Packet>)
124  */
125 std::shared_ptr<Packet> Drawable::getPacket()
126 {
127  auto packet = std::make_shared<Packet>();
128  DrawablePacket drawablePacket;
129  packet->code = ELEMENT;
130  packet->data_size = sizeof(Drawable);
131  packet->data = std::malloc(sizeof(Drawable));
132  memset(packet->data, 0, sizeof(Drawable));
133  drawablePacket.id = _textureId;
134  drawablePacket.x = std::get<0>(_position);
135  drawablePacket.y = std::get<1>(_position);
136  drawablePacket.sizeHorizontal = std::get<0>(_size);
137  drawablePacket.sizeVertical = std::get<1>(_size);
138  drawablePacket.rectLeft = std::get<0>(_rect);
139  drawablePacket.rectTop = std::get<1>(_rect);
140  drawablePacket.rectWidth = std::get<2>(_rect);
141  drawablePacket.rectHeight = std::get<3>(_rect);
142  std::memcpy(&drawablePacket.attribute, _attribute, 8);
143  std::memcpy(&drawablePacket.attribute2, _attribute + 8, 8);
144  memcpy(packet->data, &drawablePacket, sizeof(DrawablePacket));
145  return packet;
146 }
147 
148 /**
149  * @brief setHasChanged, set if the drawable has changed
150  * @param hasChanged
151  */
152 void Drawable::setHasChanged(bool hasChanged)
153 {
154  this->hasChanged = hasChanged;
155 }
156 
157 /**
158  * @brief getHasChanged, get if the drawable has changed
159  * @return hasChanged (bool)
160  */
161 bool Drawable::getHasChanged() const
162 {
163  return hasChanged;
164 }
165 
166 /**
167  * @brief addDrawableCollision, add a drawable collision
168  * @param drawableCollision
169  */
170 void Drawable::addDrawableCollision(std::shared_ptr<Drawable> drawableCollision)
171 {
172  _drawablesCollision.push_back(std::move(drawableCollision));
173 }
174 
175 /**
176  * @brief getDrawablesCollision, get the drawables collision
177  * @return drawablesCollision (std::vector<std::shared_ptr<Drawable>>)
178  */
180 {
181  return _drawablesCollision;
182 }