From 03fedbe3b07418db28a0b3674e746889fd41b56b Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 20 Sep 2022 22:16:16 +0200 Subject: [PATCH] Arreglos y mejoras en la clase animatedsprite --- source/animatedsprite.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index dc5d18b2..77995910 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -91,6 +91,13 @@ void AnimatedSprite::animate() // Establece el frame actual de la animación void AnimatedSprite::setCurrentFrame(int num) { + // Descarta valores fuera de rango + if (num >= animation[currentAnimation].frames.size()) + { + num = 0; + } + + // Cambia el valor de la variable animation[currentAnimation].currentFrame = num; // Escoge el frame correspondiente de la animación @@ -154,7 +161,7 @@ bool AnimatedSprite::load(std::string filePath) printf("Reading file %s\n", filename.c_str()); while (std::getline(file, line)) { - // Si la linea contiene el texto [enemy] se realiza el proceso de carga de un enemigo + // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación if (line == "[animation]") { t_animation buffer; @@ -206,7 +213,7 @@ bool AnimatedSprite::load(std::string filePath) } } while (line != "[/animation]"); - // Añade el enemigo al vector de enemigos + // Añade la animación al vector de animaciones animation.push_back(buffer); } @@ -223,14 +230,17 @@ bool AnimatedSprite::load(std::string filePath) { 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())); } + else if (line.substr(0, pos) == "frame_height") { frame_height = std::stoi(line.substr(pos + 1, line.length())); } + else { printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str()); @@ -251,6 +261,12 @@ bool AnimatedSprite::load(std::string filePath) success = false; } + // Normaliza valores + if (frames_per_row == 0) + { + frames_per_row = texture->getWidth() / frame_width; + } + // Pone un valor por defecto setPos({0, 0, frame_width, frame_height});