diff --git a/source/animated_sprite.cpp b/source/animated_sprite.cpp index 268523c..8549121 100644 --- a/source/animated_sprite.cpp +++ b/source/animated_sprite.cpp @@ -23,8 +23,8 @@ AnimationsFileBuffer loadAnimationsFromFile(const std::string &file_path) std::string line; while (std::getline(file, line)) { - // if (!line.empty()) - buffer.push_back(line); + if (!line.empty()) + buffer.push_back(line); } return buffer; @@ -246,19 +246,43 @@ void AnimatedSprite::resetAnimation() } // Carga la animación desde un vector de cadenas -bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source) +void AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source) { - int frame_width = 0; - int frame_height = 0; - int frames_per_row = 0; - int max_tiles = 0; - bool success = true; - size_t index = 0; + int frame_width = 1; + int frame_height = 1; + int frames_per_row = 1; + int max_tiles = 1; + size_t index = 0; while (index < source.size()) { std::string line = source.at(index); + // Parsea el fichero para buscar variables y valores + if (line != "[animation]") + { + // Encuentra la posición del caracter '=' + size_t pos = line.find("="); + + // Procesa las dos subcadenas + if (pos != std::string::npos) + { + std::string key = line.substr(0, pos); + int value = std::stoi(line.substr(pos + 1)); + if (key == "frame_width") + frame_width = value; + else if (key == "frame_height") + frame_height = value; + else + std::cout << "Warning: unknown parameter " << key << std::endl; + + frames_per_row = texture_->getWidth() / frame_width; + const int w = texture_->getWidth() / frame_width; + const int h = texture_->getHeight() / frame_height; + max_tiles = w * h; + } + } + // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación if (line == "[animation]") { @@ -273,21 +297,13 @@ bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so { std::string key = line.substr(0, pos); std::string value = line.substr(pos + 1); + if (key == "name") - { animation.name = value; - } - else if (key == "speed") - { animation.speed = std::stoi(value); - } - else if (key == "loop") - { animation.loop = std::stoi(value); - } - else if (key == "frames") { // Se introducen los valores separados por comas en un vector @@ -297,7 +313,7 @@ bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so while (getline(ss, tmp, ',')) { // Comprueba que el tile no sea mayor que el maximo indice permitido - int num_tile = std::stoi(tmp); + const int num_tile = std::stoi(tmp); if (num_tile <= max_tiles) { rect.x = (num_tile % frames_per_row) * frame_width; @@ -308,10 +324,7 @@ bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so } else - { std::cout << "Warning: unknown parameter " << key << std::endl; - success = false; - } } } while (line != "[/animation]"); @@ -319,48 +332,6 @@ bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so animations_.emplace_back(animation); } - // En caso contrario se parsea el fichero para buscar las variables y los valores - if (line != "[animation]") - { - // Encuentra la posición del caracter '=' - size_t pos = line.find("="); - - // Procesa las dos subcadenas - if (pos != std::string::npos) - { - std::string key = line.substr(0, pos); - int value = std::stoi(line.substr(pos + 1)); - if (key == "frame_width") - { - frame_width = value; - } - - else if (key == "frame_height") - { - frame_height = value; - } - - else - { - std::cout << "Warning: unknown parameter " << key << std::endl; - success = false; - } - - // Normaliza valores - 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 int w = texture_->getWidth() / frame_width; - const int h = texture_->getHeight() / frame_height; - max_tiles = w * h; - } - } - } - // Una vez procesada la linea, aumenta el indice para pasar a la siguiente index++; } @@ -368,6 +339,4 @@ bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so // Pone un valor por defecto setWidth(frame_width); setHeight(frame_height); - - return success; } \ No newline at end of file diff --git a/source/animated_sprite.h b/source/animated_sprite.h index b18defb..1917f4c 100644 --- a/source/animated_sprite.h +++ b/source/animated_sprite.h @@ -37,7 +37,7 @@ protected: void animate(); // Carga la animación desde un vector de cadenas - bool loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source); + void loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source); public: // Constructor