From b879673bc28e5738300fdd80eb19db57c3bc919a Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 18 Oct 2024 17:10:06 +0200 Subject: [PATCH] Mes merdes que faltaven del merge --- source/animated_sprite.cpp | 151 +------------------------------------ 1 file changed, 4 insertions(+), 147 deletions(-) diff --git a/source/animated_sprite.cpp b/source/animated_sprite.cpp index 200d602..7e32ac6 100644 --- a/source/animated_sprite.cpp +++ b/source/animated_sprite.cpp @@ -176,149 +176,6 @@ SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF) return animations_[indexA].frames[indexF]; } -// Carga la animación desde un vector -bool AnimatedSprite::loadFromVector(const std::vector &source) -{ - // Inicializa variables - auto frames_per_row = 0; - auto frame_width = 0; - auto frame_height = 0; - auto max_tiles = 0; - - // Indicador de éxito en el proceso - auto success = true; - std::string line; - - // Recorre todo el vector - auto index = 0; - while (index < (int)source.size()) - { - // Lee desde el vector - line = source.at(index); - - // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación - if (line == "[animation]") - { - 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 - { - // Aumenta el indice para leer la siguiente linea - index++; - line = source.at(index); - - // Encuentra la posición del caracter '=' - int pos = line.find("="); - - // Procesa las dos subcadenas - if (pos != static_cast(line.npos)) - { - if (line.substr(0, pos) == "name") - { - animation.name = line.substr(pos + 1, line.length()); - } - - else if (line.substr(0, pos) == "speed") - { - animation.speed = std::stoi(line.substr(pos + 1, line.length())); - } - - else if (line.substr(0, pos) == "loop") - { - animation.loop = std::stoi(line.substr(pos + 1, line.length())); - } - - else if (line.substr(0, pos) == "frames") - { - // Se introducen los valores separados por comas en un vector - std::stringstream ss(line.substr(pos + 1, line.length())); - std::string tmp; - SDL_Rect rect = {0, 0, frame_width, frame_height}; - while (getline(ss, tmp, ',')) - { - // Comprueba que el tile no sea mayor que el maximo indice permitido - 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; - animation.frames.push_back(rect); - } - } - - else - { - std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl; - success = false; - } - } - } while (line != "[/animation]"); - - // Añade la animación al vector de animaciones - animations_.push_back(animation); - } - - // En caso contrario se parsea el fichero para buscar las variables y los valores - else - { - // Encuentra la posición del caracter '=' - int pos = line.find("="); - - // Procesa las dos subcadenas - if (pos != (int)line.npos) - { - if (line.substr(0, pos) == "frames_per_row") - { - 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 - { - std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << 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++; - } - - // Pone un valor por defecto - setWidth(frame_width); - setHeight(frame_height); - - return success; -} - // Establece la animacion actual void AnimatedSprite::setCurrentAnimation(const std::string &name) { @@ -515,7 +372,7 @@ std::vector AnimatedSprite::loadFromFile(const std::string &file_path } // Carga la animación desde un vector -bool AnimatedSprite::loadFromVector(std::vector *source) +bool AnimatedSprite::loadFromVector(const std::vector &source) { // Inicializa variables auto frames_per_row = 0; @@ -529,10 +386,10 @@ bool AnimatedSprite::loadFromVector(std::vector *source) // Recorre todo el vector auto index = 0; - while (index < (int)source->size()) + while (index < (int)source.size()) { // Lee desde el vector - line = source->at(index); + line = source.at(index); // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación if (line == "[animation]") @@ -543,7 +400,7 @@ bool AnimatedSprite::loadFromVector(std::vector *source) { // Aumenta el indice para leer la siguiente linea index++; - line = source->at(index); + line = source.at(index); // Encuentra la posición del caracter '=' int pos = line.find("=");