diff --git a/source/animated_sprite.cpp b/source/animated_sprite.cpp index da0c483..8f7f668 100644 --- a/source/animated_sprite.cpp +++ b/source/animated_sprite.cpp @@ -16,7 +16,7 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr texture, const std::stri { animations_ = loadFromFile(file_path); } - //setSpriteClip(animations_[current_animation_].frames[animations_[current_animation_].current_frame]); + // setSpriteClip(animations_[current_animation_].frames[animations_[current_animation_].current_frame]); } AnimatedSprite::AnimatedSprite(std::shared_ptr texture, std::vector *animations) @@ -27,7 +27,7 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr texture, 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) { @@ -401,14 +258,7 @@ std::vector AnimatedSprite::loadFromFile(std::string file_path) // 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(); + Animation animation = Animation(); do { @@ -521,4 +371,140 @@ std::vector AnimatedSprite::loadFromFile(std::string file_path) setHeight(frame_height); return animations; +} + +// Carga la animación desde un vector +bool AnimatedSprite::loadFromVector(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(); + + 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; } \ No newline at end of file diff --git a/source/animated_sprite.h b/source/animated_sprite.h index 8aafd98..19c9c17 100644 --- a/source/animated_sprite.h +++ b/source/animated_sprite.h @@ -17,6 +17,8 @@ struct Animation bool completed; // Indica si ha finalizado la animación int current_frame; // Frame actual int counter; // Contador para las animaciones + + Animation() : name(std::string()), speed(5), loop(0), completed(false), current_frame(0), counter(0) {} }; class AnimatedSprite : public MovingSprite