From f26ecbd969c654136bfad7f145d5d105a449db54 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 24 Oct 2024 18:14:33 +0200 Subject: [PATCH] Canviat el final de linea als scripts de linux --- linux_utils/check_includes.sh | 2 +- linux_utils/cppcheck_run.sh | 8 +- linux_utils/run_valgrind.sh | 5 +- source/animated_sprite.cpp | 253 ++++++++-------------------------- source/animated_sprite.h | 18 ++- source/resource.cpp | 2 +- source/resource.h | 8 +- 7 files changed, 80 insertions(+), 216 deletions(-) diff --git a/linux_utils/check_includes.sh b/linux_utils/check_includes.sh index efc06f3..aee7ad2 100644 --- a/linux_utils/check_includes.sh +++ b/linux_utils/check_includes.sh @@ -5,6 +5,6 @@ SOURCEPATH=../source/ for i in "$SOURCEPATH"/*.cpp do include-what-you-use -D DEBUG -D VERBOSE -std=c++20 -Wall "$i" - read -p "Presiona cualquier tecla para continuar..." + read -r -p "Presiona cualquier tecla para continuar..." clear done diff --git a/linux_utils/cppcheck_run.sh b/linux_utils/cppcheck_run.sh index fbddecc..606ed3d 100644 --- a/linux_utils/cppcheck_run.sh +++ b/linux_utils/cppcheck_run.sh @@ -1,8 +1,10 @@ +#!/bin/bash + # warning,style,performance #cppcheck --force --enable=warning,style,performance --std=c++20 \ - --suppressions-list=/home/sergio/gitea/coffee_crisis_arcade_edition/linux-utils/cppcheck_suppressions \ - /home/sergio/gitea/coffee_crisis_arcade_edition/source/ \ - 2>/home/sergio/cppcheck-result-warning-style-performance +# --suppressions-list=/home/sergio/gitea/coffee_crisis_arcade_edition/linux-utils/cppcheck_suppressions \ +# /home/sergio/gitea/coffee_crisis_arcade_edition/source/ \ +# 2>/home/sergio/cppcheck-result-warning-style-performance # all cppcheck --force --enable=all -I /usr/include --std=c++20 \ diff --git a/linux_utils/run_valgrind.sh b/linux_utils/run_valgrind.sh index eee874b..757dafb 100644 --- a/linux_utils/run_valgrind.sh +++ b/linux_utils/run_valgrind.sh @@ -1,3 +1,6 @@ #!/bin/bash -valgrind --suppressions=valgrind_exceptions --leak-check=full ~/coffee_crisis_arcade_edition/coffee_crisis_arcade_edition_debug > ~/coffee_crisis_arcade_edition/debug.txt 2>&1 \ No newline at end of file +valgrind --suppressions=valgrind_exceptions \ +--leak-check=full \ +~/coffee_crisis_arcade_edition/coffee_crisis_arcade_edition_debug \ +> ~/coffee_crisis_arcade_edition/debug.txt 2>&1 \ No newline at end of file diff --git a/source/animated_sprite.cpp b/source/animated_sprite.cpp index 9140911..268523c 100644 --- a/source/animated_sprite.cpp +++ b/source/animated_sprite.cpp @@ -8,9 +8,8 @@ #include "utils.h" // Carga las animaciones en un vector(Animations) desde un fichero -Animations loadAnimationsFromFile(const std::string &file_path) +AnimationsFileBuffer loadAnimationsFromFile(const std::string &file_path) { - std::vector buffer; std::ifstream file(file_path); if (!file) { @@ -18,11 +17,13 @@ Animations loadAnimationsFromFile(const std::string &file_path) throw std::runtime_error("Fichero no encontrado: " + file_path); } - std::string line; printWithDots("Animation : ", file_path.substr(file_path.find_last_of("\\/") + 1), "[ LOADED ]"); + std::vector buffer; + std::string line; while (std::getline(file, line)) { + // if (!line.empty()) buffer.push_back(line); } @@ -31,30 +32,25 @@ Animations loadAnimationsFromFile(const std::string &file_path) // Constructor AnimatedSprite::AnimatedSprite(std::shared_ptr texture, const std::string &file_path) - : MovingSprite(texture), - current_animation_(0) + : MovingSprite(texture) { // Carga las animaciones if (!file_path.empty()) { - animations_ = loadFromFile(file_path); - } -} - -AnimatedSprite::AnimatedSprite(std::shared_ptr texture, const Animations &animations) - : MovingSprite(texture), - current_animation_(0) -{ - if (!animations.empty()) - { - loadFromVector(animations); + AnimationsFileBuffer v = loadAnimationsFromFile(file_path); + loadFromAnimationsFileBuffer(v); } } // Constructor -AnimatedSprite::AnimatedSprite(std::shared_ptr texture) - : MovingSprite(texture), - current_animation_(0) {} +AnimatedSprite::AnimatedSprite(std::shared_ptr texture, const AnimationsFileBuffer &animations) + : MovingSprite(texture) +{ + if (!animations.empty()) + { + loadFromAnimationsFileBuffer(animations); + } +} // Obtiene el indice de la animación a partir del nombre int AnimatedSprite::getIndex(const std::string &name) @@ -249,239 +245,104 @@ void AnimatedSprite::resetAnimation() animations_[current_animation_].completed = false; } -// Carga la animación desde un fichero -std::vector AnimatedSprite::loadFromFile(const std::string &file_path) +// Carga la animación desde un vector de cadenas +bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source) { - // Inicializa variables - std::vector animations; - auto frame_width = 1; - auto frame_height = 1; + int frame_width = 0; + int frame_height = 0; + int frames_per_row = 0; + int max_tiles = 0; + bool success = true; + size_t index = 0; - std::ifstream file(file_path); - std::string line; - - // El fichero se puede abrir - if (file.good()) + while (index < source.size()) { - // Procesa el fichero linea a linea - std::cout << "Animation loaded: " << getFileName(file_path) << std::endl; - while (std::getline(file, line)) - { - auto max_tiles = 1; - auto frames_per_row = 1; - // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación - if (line == "[animation]") - { - Animation animation = Animation(); - - do - { - std::getline(file, line); - - // 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 auto 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: file " << getFileName(file_path).c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; - } - } - } 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 - if (line != "[animation]") - { - // Encuentra la posición del caracter '=' - size_t pos = line.find("="); - - // Procesa las dos subcadenas - if (pos != line.npos) - { - 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: file " << getFileName(file_path) << "\n, unknown parameter \"" << line.substr(0, pos) << "\"" << std::endl; - } - - frames_per_row = texture_->getWidth() / frame_width; - - const auto w = texture_->getWidth() / frame_width; - const auto h = texture_->getHeight() / frame_height; - max_tiles = w * h; - } - } - } - - // Cierra el fichero - file.close(); - } - // El fichero no se puede abrir - else - { - std::cout << "Warning: Unable to open " << getFileName(file_path).c_str() << " file" << std::endl; - } - - // Pone un valor por defecto - setWidth(frame_width); - setHeight(frame_height); - - return animations; -} - -// Carga la animación desde un vector -bool AnimatedSprite::loadFromVector(const Animations &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); + std::string 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(); - + Animation animation; do { - // Aumenta el indice para leer la siguiente linea index++; line = source.at(index); + size_t pos = line.find("="); - // Encuentra la posición del caracter '=' - int pos = line.find("="); - - // Procesa las dos subcadenas - if (pos != static_cast(line.npos)) + if (pos != std::string::npos) { - if (line.substr(0, pos) == "name") + std::string key = line.substr(0, pos); + std::string value = line.substr(pos + 1); + if (key == "name") { - animation.name = line.substr(pos + 1, line.length()); + animation.name = value; } - else if (line.substr(0, pos) == "speed") + else if (key == "speed") { - animation.speed = std::stoi(line.substr(pos + 1, line.length())); + animation.speed = std::stoi(value); } - else if (line.substr(0, pos) == "loop") + else if (key == "loop") { - animation.loop = std::stoi(line.substr(pos + 1, line.length())); + animation.loop = std::stoi(value); } - else if (line.substr(0, pos) == "frames") + else if (key == "frames") { // Se introducen los valores separados por comas en un vector - std::stringstream ss(line.substr(pos + 1, line.length())); + std::stringstream ss(value); 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); + int num_tile = std::stoi(tmp); + if (num_tile <= max_tiles) + { + rect.x = (num_tile % frames_per_row) * frame_width; + rect.y = (num_tile / frames_per_row) * frame_height; + animation.frames.emplace_back(rect); + } } } else { - std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl; + std::cout << "Warning: unknown parameter " << key << std::endl; success = false; } } } while (line != "[/animation]"); // Añade la animación al vector de animaciones - animations_.push_back(animation); + animations_.emplace_back(animation); } // En caso contrario se parsea el fichero para buscar las variables y los valores - else + if (line != "[animation]") { // Encuentra la posición del caracter '=' - int pos = line.find("="); + size_t pos = line.find("="); // Procesa las dos subcadenas - if (pos != (int)line.npos) + if (pos != std::string::npos) { - if (line.substr(0, pos) == "frames_per_row") + std::string key = line.substr(0, pos); + int value = std::stoi(line.substr(pos + 1)); + if (key == "frame_width") { - frames_per_row = std::stoi(line.substr(pos + 1, line.length())); + frame_width = value; } - else if (line.substr(0, pos) == "frame_width") + else if (key == "frame_height") { - 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())); + frame_height = value; } else { - std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl; + std::cout << "Warning: unknown parameter " << key << std::endl; success = false; } diff --git a/source/animated_sprite.h b/source/animated_sprite.h index 3b55a51..b18defb 100644 --- a/source/animated_sprite.h +++ b/source/animated_sprite.h @@ -21,32 +21,30 @@ struct Animation Animation() : name(std::string()), speed(5), loop(0), completed(false), current_frame(0), counter(0) {} }; -using Animations = std::vector; +using AnimationsFileBuffer = std::vector; // Carga las animaciones en un vector(Animations) desde un fichero -Animations loadAnimationsFromFile(const std::string &file_path); +AnimationsFileBuffer loadAnimationsFromFile(const std::string &file_path); class AnimatedSprite : public MovingSprite { protected: // Variables std::vector animations_; // Vector con las diferentes animaciones - int current_animation_; // Animacion activa + int current_animation_ = 0; // Animacion activa // Calcula el frame correspondiente a la animación actual void animate(); - // Carga la animación desde un fichero - std::vector loadFromFile(const std::string &file_path); - - // Carga la animación desde un vector - bool loadFromVector(const Animations &source); + // Carga la animación desde un vector de cadenas + bool loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source); public: // Constructor AnimatedSprite(std::shared_ptr texture, const std::string &file_path); - AnimatedSprite(std::shared_ptr texture, const Animations &animations); - explicit AnimatedSprite(std::shared_ptr texture); + AnimatedSprite(std::shared_ptr texture, const AnimationsFileBuffer &animations); + explicit AnimatedSprite(std::shared_ptr texture) + : MovingSprite(texture) {} // Destructor virtual ~AnimatedSprite() = default; diff --git a/source/resource.cpp b/source/resource.cpp index 51746ce..f311691 100644 --- a/source/resource.cpp +++ b/source/resource.cpp @@ -111,7 +111,7 @@ std::shared_ptr Resource::getTextFile(const std::string &name) } // Obtiene la animación a partir de un nombre -Animations &Resource::getAnimation(const std::string &name) +AnimationsFileBuffer &Resource::getAnimation(const std::string &name) { auto it = std::find_if(animations_.begin(), animations_.end(), [&name](auto &a) { return a.name == name; }); diff --git a/source/resource.h b/source/resource.h index f46ce17..999d754 100644 --- a/source/resource.h +++ b/source/resource.h @@ -57,11 +57,11 @@ struct ResourceTextFile // Estructura para almacenar ficheros animaciones y su nombre struct ResourceAnimation { - std::string name; // Nombre del fichero - Animations animation; // Objeto con las animaciones + std::string name; // Nombre del fichero + AnimationsFileBuffer animation; // Objeto con las animaciones // Constructor - ResourceAnimation(const std::string &name, const Animations &animation) + ResourceAnimation(const std::string &name, const AnimationsFileBuffer &animation) : name(name), animation(animation) {} }; @@ -130,7 +130,7 @@ public: std::shared_ptr getTextFile(const std::string &name); // Obtiene la animación a partir de un nombre - Animations &getAnimation(const std::string &name); + AnimationsFileBuffer &getAnimation(const std::string &name); // Obtiene el fichero con los datos para el modo demostración a partir de un çindice DemoData &getDemoData(int index);