R-Type  2
Doom but in better
Loading...
Searching...
No Matches
TextureComponent.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** RenderComponent.cpp
6*/
7
15
20
22 : EntityNode(0), _collisionInfo(0)
23{
24 update(other);
25};
26
28 : EntityNode(0), _collisionInfo(0)
29{
30 setFilePath(filePath);
31 setCollisionInfo(collisionInfo);
32};
33
35 : EntityNode(0), _collisionInfo(0)
36{
37 setTexture(texture);
38 setCollisionInfo(collisionInfo);
39}
40
42 : EntityNode(entityId), _collisionInfo(entityId)
43{
44};
45
47 : EntityNode(entityId), _collisionInfo(entityId)
48{
49 update(other);
50};
51
52GUI::ECS::Components::TextureComponent::TextureComponent(const std::uint32_t entityId, const std::string &filePath, const GUI::ECS::Systems::Collision &collisionInfo)
53 : EntityNode(entityId), _collisionInfo(entityId)
54{
55 setFilePath(filePath);
56 setCollisionInfo(collisionInfo);
57};
58
59GUI::ECS::Components::TextureComponent::TextureComponent(const std::uint32_t entityId, const std::any &texture, const GUI::ECS::Systems::Collision &collisionInfo)
60 : EntityNode(entityId), _collisionInfo(entityId)
61{
62 setTexture(texture);
63 setCollisionInfo(collisionInfo);
64}
65
67
69{
70 _visible = visible;
71}
72
74{
75 _textureSet = false;
76 _texture = std::make_shared<sf::Texture>();
77 const bool loadStatus = _texture->loadFromFile(filePath);
78 if (loadStatus != true) {
79 PRETTY_CRITICAL << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' " << "Texture material not found in filepath: '" << filePath << "'." << std::endl;
80 throw CustomExceptions::FileNotFound(filePath);
81 }
82 _textureSet = true;
83 PRETTY_SUCCESS << "The texture is set" << std::endl;
84 sf::Vector2u node = _texture->getSize();
85 _collisionInfo.setDimension({ node.x, node.y });
86}
87
89{
90 _textureSet = false;
91 PRETTY_INFO << "Setting the texture" << std::endl;
92 if (!texture.has_value()) {
93 PRETTY_WARNING << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' " << "There is no texture to be processed" << std::endl;
94 return;
95 }
96 if (texture.type() == typeid(std::shared_ptr<sf::Texture>)) {
97 std::optional<std::shared_ptr<sf::Texture>> textureCapsule = Utilities::unCast<std::shared_ptr<sf::Texture>, CustomExceptions::NoTexture>(texture, true, "<There is no std::shared_ptr<sf::Texture> to be extracted from the std::any cast.>, system error: ");
98 if (!textureCapsule.has_value()) {
99 PRETTY_ERROR << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' " << "There is no std::shared_ptr<sf::Texture> to be processed" << std::endl;
100 throw CustomExceptions::NoTexture("<There is no std::shared_ptr<sf::Texture> to be extracted from the std::any cast.>");
101 }
102 PRETTY_INFO << "Updating texture with the new one as a copy." << std::endl;
103 _texture = std::make_shared<sf::Texture>(*(textureCapsule.value()));
104 _textureSet = true;
105 PRECISE_SUCCESS << "Updated the texture with the new one as a copy." << std::endl;
106 } else if (texture.type() == typeid(sf::Texture)) {
107 std::optional<sf::Texture> textCapsule = Utilities::unCast<sf::Texture, CustomExceptions::NoTexture>(texture, true, "<There is no sf::Texture to be extracted from the std::any cast.>, system error: ");
108 if (!textCapsule.has_value()) {
109 PRETTY_ERROR << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' " << "There is no sf::Texture to be processed" << std::endl;
110 throw CustomExceptions::NoTexture("<There is no sf::Texture to be extracted from the std::any cast.>");
111 }
112 PRETTY_INFO << "Updating texture with the new one using the '=' operator and not the .update function" << std::endl;
113 _texture = std::make_shared<sf::Texture>(textCapsule.value());
114 _textureSet = true;
115 PRETTY_SUCCESS << "Updated the texture with a copy of the passed structure." << std::endl;
116 } else {
117 PRETTY_CRITICAL << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' " << "No texture (sf::Texture or std::shared_ptr<sf::Texture>) found" << std::endl;
118 throw CustomExceptions::NoTexture("<No texture (sf::Texture or std::shared_ptr<sf::Texture>) found>");
119 }
120 PRETTY_INFO << "Getting the texture size" << std::endl;
121 sf::Vector2u node = _texture->getSize();
122 PRETTY_INFO << "Setting the collision info with the new texture size" << std::endl;
123 _collisionInfo.setDimension({ node.x, node.y });
124 PRETTY_SUCCESS << "Dimensions for the collisionInfo is set." << std::endl;
125}
126
128{
129 _collisionInfo.update(collisionInfo);
130}
131
132void GUI::ECS::Components::TextureComponent::setPosition(const std::pair<int, int> &position)
133{
134 _collisionInfo.setPosition(position);
135}
136
137void GUI::ECS::Components::TextureComponent::setSize(const std::pair<float, float> &size)
138{
139 if (!_textureSet) {
140 PRETTY_WARNING << "There are no textures set from wich to calculate the rescale factor." << std::endl;
141 _collisionInfo.setDimension(size);
142 return;
143 }
144 std::pair<float, float> result;
145 sf::Vector2u originalSize = _texture->getSize();
146
147 PRETTY_DEBUG << "Converting the texture width and height to floats" << std::endl;
148 const float originalWidth = static_cast<float>(originalSize.x);
149 const float originalHeight = static_cast<float>(originalSize.y);
150 PRETTY_DEBUG << "The values of the converted texture are: width: '" << originalWidth << "', height: '" << originalHeight << "'" << std::endl;
151
152 PRETTY_DEBUG << "Calculating the scale factors for the resulting image." << std::endl;
153 result.first = size.first / originalWidth;
154 result.second = size.second / originalHeight;
155 PRETTY_SUCCESS << "The calculated scale is: " << result << std::endl;
156
157
158 PRETTY_DEBUG << "Setting the scale factors for the resulting image." << std::endl;
159 _collisionInfo.setDimension(result);
160 PRETTY_SUCCESS << "The dimensions for the collisionInfo is set." << std::endl;
161}
162
164{
165 PRETTY_INFO << "Updating texture component" << std::endl;
166 PRETTY_DEBUG << "Updating Visibility" << std::endl;
167 setVisible(copy.getVisible());
168 PRETTY_DEBUG << "Updated Visibility" << std::endl;
169 PRETTY_DEBUG << "Updating Texture" << std::endl;
170 setTexture(copy.getTexture());
171 PRETTY_DEBUG << "Updated Texture" << std::endl;
172 PRETTY_DEBUG << "Updating Collision Info" << std::endl;
173 setCollisionInfo(copy.getCollisionInfo());
174 PRETTY_DEBUG << "Updated Collision Info" << std::endl;
175 PRETTY_SUCCESS << "Out of updating texture component " << std::endl;
176}
177
179{
180 PRETTY_INFO << "Creating an any pointer from the std::shared_ptr<sf::Texture>" << std::endl;
181 if (!_textureSet) {
182 PRETTY_CRITICAL << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' " << "The texture is not set, texture collisions '" << _collisionInfo << "'" << std::endl;
183 throw CustomExceptions::NoTexture("<There is no std::shared_ptr<sf::Texture> in the class>");
184 }
185 return std::make_any<std::shared_ptr<sf::Texture>>(_texture);
186}
187
189{
190 return _visible;
191}
192
197
198const std::string GUI::ECS::Components::TextureComponent::getInfo(const unsigned int indent) const
199{
200 std::string indentation = "";
201 for (unsigned int i = 0; i < indent; ++i) {
202 indentation += "\t";
203 }
204 std::string result = indentation + "Texture:\n";
205 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
206 result += indentation + "- Visible: " + Recoded::myToString(_visible) + "\n";
207 result += indentation + "- Collision Info: {\n" + _collisionInfo.getInfo(indent + 1) + indentation + "}\n";
208 return result;
209}
210
212{
213 if (this != &copy) {
214 update(copy);
215 }
216 return *this;
217};
218
220{
221 os << item.getInfo();
222 return os;
223}
#define PRETTY_ERROR
Error log with details and colour.
#define PRETTY_DEBUG
Debug log with details and colour.
#define PRETTY_INFO
Info log with details and colour.
#define PRETTY_CRITICAL
Critical log with details and colour.
#define PRETTY_WARNING
Warning log with details and colour.
#define PRECISE_SUCCESS
Success log with precise details.
#define PRETTY_SUCCESS
Success log with details and colour.
This file contains the declaration of the TextureComponent class which manages textures.
This is the class in charge of informing the user that the provided file path could not be found.
Definition NotFound.hpp:27
This is the class in charge of informing the user that they tried to access a non-existant texture in...
Definition No.hpp:446
Represents a texture component used in an entity component system.
const std::string getInfo(const unsigned int indent=0) const
This is a function meant for debugging purposes It will dump the current state of the variables upon ...
TextureComponent & operator=(const GUI::ECS::Components::TextureComponent &copy)
Assignment operator for copying data from another TextureComponent.
~TextureComponent()
Destructor. Cleans up resources when the component is destroyed.
void setTexture(const std::any &texture)
Sets the texture using a std::any object.
void setCollisionInfo(const GUI::ECS::Systems::Collision &collisionInfo)
Sets the collision information for the texture.
void setPosition(const std::pair< int, int > &position)
Sets the position of the texture.
const std::any getTexture() const
Retrieves the texture as a std::any object.
const bool getVisible() const
Retrieves the visibility status of the texture.
TextureComponent()
Default constructor. Initializes the component with default values.
void update(const TextureComponent &copy)
Updates the texture component by copying data from another component.
void setVisible(const bool visible)
Sets the visibility of the texture.
void setFilePath(const std::string &filePath)
Sets the file path for the texture.
void setSize(const std::pair< float, float > &size)
Sets the size of the texture.
const GUI::ECS::Systems::Collision getCollisionInfo() const
Retrieves the collision information for the texture.
Represents a rectangular component that can detect collisions and mouse interactions,...
Definition Collision.hpp:39
const std::string getInfo(const unsigned int indent=0) const
This is a function meant for debugging purposes It will dump the current state of the variables upon ...
std::ostream & operator<<(std::ostream &os, const AnimationComponent &item)
Outputs the animation's info to a stream.
const std::string myToString(const Rect< RectType > &rectangle)
Converts a Rect<T> object to its string representation.
Definition Rect.hpp:223
std::optional< T > unCast(const std::any &classNode, const bool raiseOnError=true, const std::string customErrorMessage="")
Casts the content of a std::any back to its original type.
Definition UnCast.hpp:65