This commit is contained in:
2024-10-24 14:03:12 +02:00
parent 8c98430b68
commit 8f33308f8d
2 changed files with 14 additions and 32 deletions

View File

@@ -56,12 +56,6 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture)
: MovingSprite(texture), : MovingSprite(texture),
current_animation_(0) {} current_animation_(0) {}
// Destructor
AnimatedSprite::~AnimatedSprite()
{
animations_.clear();
}
// Obtiene el indice de la animación a partir del nombre // Obtiene el indice de la animación a partir del nombre
int AnimatedSprite::getIndex(const std::string &name) int AnimatedSprite::getIndex(const std::string &name)
{ {
@@ -260,8 +254,8 @@ std::vector<Animation> AnimatedSprite::loadFromFile(const std::string &file_path
{ {
// Inicializa variables // Inicializa variables
std::vector<Animation> animations; std::vector<Animation> animations;
auto frame_width = 0; auto frame_width = 1;
auto frame_height = 0; auto frame_height = 1;
std::ifstream file(file_path); std::ifstream file(file_path);
std::string line; std::string line;
@@ -273,8 +267,8 @@ std::vector<Animation> AnimatedSprite::loadFromFile(const std::string &file_path
std::cout << "Animation loaded: " << getFileName(file_path) << std::endl; std::cout << "Animation loaded: " << getFileName(file_path) << std::endl;
while (std::getline(file, line)) while (std::getline(file, line))
{ {
auto max_tiles = 0; auto max_tiles = 1;
auto frames_per_row = 0; auto frames_per_row = 1;
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]") if (line == "[animation]")
{ {
@@ -333,20 +327,15 @@ std::vector<Animation> AnimatedSprite::loadFromFile(const std::string &file_path
} }
// En caso contrario se parsea el fichero para buscar las variables y los valores // En caso contrario se parsea el fichero para buscar las variables y los valores
else if (line != "[animation]")
{ {
// Encuentra la posición del caracter '=' // Encuentra la posición del caracter '='
int pos = line.find("="); size_t pos = line.find("=");
// Procesa las dos subcadenas // Procesa las dos subcadenas
if (pos != (int)line.npos) if (pos != line.npos)
{ {
if (line.substr(0, pos) == "frames_per_row") if (line.substr(0, pos) == "frame_width")
{
frames_per_row = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frame_width")
{ {
frame_width = std::stoi(line.substr(pos + 1, line.length())); frame_width = std::stoi(line.substr(pos + 1, line.length()));
} }
@@ -358,21 +347,14 @@ std::vector<Animation> AnimatedSprite::loadFromFile(const std::string &file_path
else else
{ {
std::cout << "Warning: file " << getFileName(file_path).c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; std::cout << "Warning: file " << getFileName(file_path) << "\n, unknown parameter \"" << line.substr(0, pos) << "\"" << std::endl;
} }
// Normaliza valores frames_per_row = texture_->getWidth() / frame_width;
if (frames_per_row == 0 && frame_width > 0)
{
frames_per_row = texture_->getWidth() / frame_width;
}
if (max_tiles == 0 && frame_width > 0 && frame_height > 0) const auto w = texture_->getWidth() / frame_width;
{ const auto h = texture_->getHeight() / frame_height;
const auto w = texture_->getWidth() / frame_width; max_tiles = w * h;
const auto h = texture_->getHeight() / frame_height;
max_tiles = w * h;
}
} }
} }
} }

View File

@@ -49,7 +49,7 @@ public:
explicit AnimatedSprite(std::shared_ptr<Texture> texture); explicit AnimatedSprite(std::shared_ptr<Texture> texture);
// Destructor // Destructor
virtual ~AnimatedSprite(); virtual ~AnimatedSprite() = default;
// Actualiza las variables del objeto // Actualiza las variables del objeto
void update() override; void update() override;