Commit de vesprà tirada a la brossa

This commit is contained in:
2024-10-16 22:35:19 +02:00
parent 5585f996cb
commit db884cb422
32 changed files with 450 additions and 463 deletions

View File

@@ -7,11 +7,10 @@
#include "texture.h" // for Texture
// Carga la animación desde un fichero
AnimatedFile loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string file_path)
std::vector<Animation> loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string file_path)
{
// Inicializa variables
AnimatedFile af;
af.texture = texture;
std::vector<Animation> animations;
auto frames_per_row = 0;
auto frame_width = 0;
auto frame_height = 0;
@@ -35,14 +34,14 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]")
{
Animation buffer;
buffer.counter = 0;
buffer.current_frame = 0;
buffer.completed = false;
buffer.name.clear();
buffer.speed = 5;
buffer.loop = 0;
buffer.frames.clear();
Animation animation;
animation.counter = 0;
animation.current_frame = 0;
animation.completed = false;
animation.name.clear();
animation.speed = 5;
animation.loop = 0;
animation.frames.clear();
do
{
@@ -56,17 +55,17 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string
{
if (line.substr(0, pos) == "name")
{
buffer.name = line.substr(pos + 1, line.length());
animation.name = line.substr(pos + 1, line.length());
}
else if (line.substr(0, pos) == "speed")
{
buffer.speed = std::stoi(line.substr(pos + 1, line.length()));
animation.speed = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "loop")
{
buffer.loop = std::stoi(line.substr(pos + 1, line.length()));
animation.loop = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frames")
@@ -81,7 +80,7 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string
const auto num_tile = std::stoi(tmp) > max_tiles ? 0 : std::stoi(tmp);
rect.x = (num_tile % frames_per_row) * frame_width;
rect.y = (num_tile / frames_per_row) * frame_height;
buffer.frames.push_back(rect);
animation.frames.push_back(rect);
}
}
@@ -95,7 +94,7 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string
} while (line != "[/animation]");
// Añade la animación al vector de animaciones
af.animations.push_back(buffer);
animations.push_back(animation);
}
// En caso contrario se parsea el fichero para buscar las variables y los valores
@@ -156,37 +155,35 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string
#endif
}
return af;
return animations;
}
// Constructor
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const std::string &file, std::vector<std::string> *buffer)
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const std::string &file_path)
: MovingSprite(texture),
current_animation_(0)
{
// Carga las animaciones
if (!file.empty())
if (!file_path.empty())
{
AnimatedFile as = loadAnimationFromFile(texture, file);
// Copia los datos de las animaciones
std::copy(as.animations.begin(), as.animations.end(), std::back_inserter(animations_));
animations_ = loadAnimationFromFile(texture, file_path);
}
}
else if (buffer)
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, std::vector<std::string> *animations)
: MovingSprite(texture),
current_animation_(0)
{
if (animations)
{
loadFromVector(buffer);
loadFromVector(animations);
}
}
// Constructor
AnimatedSprite::AnimatedSprite(const AnimatedFile *animation)
: MovingSprite(animation->texture),
current_animation_(0)
{
// Copia los datos de las animaciones
std::copy(animation->animations.begin(), animation->animations.end(), std::back_inserter(animations_));
}
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture)
: MovingSprite(texture),
current_animation_(0) {}
// Destructor
AnimatedSprite::~AnimatedSprite()
@@ -356,14 +353,14 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]")
{
Animation buffer;
buffer.counter = 0;
buffer.current_frame = 0;
buffer.completed = false;
buffer.name.clear();
buffer.speed = 5;
buffer.loop = 0;
buffer.frames.clear();
Animation animation;
animation.counter = 0;
animation.current_frame = 0;
animation.completed = false;
animation.name.clear();
animation.speed = 5;
animation.loop = 0;
animation.frames.clear();
do
{
@@ -379,17 +376,17 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
{
if (line.substr(0, pos) == "name")
{
buffer.name = line.substr(pos + 1, line.length());
animation.name = line.substr(pos + 1, line.length());
}
else if (line.substr(0, pos) == "speed")
{
buffer.speed = std::stoi(line.substr(pos + 1, line.length()));
animation.speed = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "loop")
{
buffer.loop = std::stoi(line.substr(pos + 1, line.length()));
animation.loop = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frames")
@@ -404,7 +401,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
const int num_tile = std::stoi(tmp) > max_tiles ? 0 : std::stoi(tmp);
rect.x = (num_tile % frames_per_row) * frame_width;
rect.y = (num_tile / frames_per_row) * frame_height;
buffer.frames.push_back(rect);
animation.frames.push_back(rect);
}
}
@@ -419,7 +416,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
} while (line != "[/animation]");
// Añade la animación al vector de animaciones
animations_.push_back(buffer);
animations_.push_back(animation);
}
// En caso contrario se parsea el fichero para buscar las variables y los valores