Canviat el final de linea als scripts de linux

This commit is contained in:
2024-10-24 18:14:33 +02:00
parent 018bb68f9a
commit f26ecbd969
7 changed files with 80 additions and 216 deletions

View File

@@ -5,6 +5,6 @@ SOURCEPATH=../source/
for i in "$SOURCEPATH"/*.cpp for i in "$SOURCEPATH"/*.cpp
do do
include-what-you-use -D DEBUG -D VERBOSE -std=c++20 -Wall "$i" 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 clear
done done

View File

@@ -1,8 +1,10 @@
#!/bin/bash
# warning,style,performance # warning,style,performance
#cppcheck --force --enable=warning,style,performance --std=c++20 \ #cppcheck --force --enable=warning,style,performance --std=c++20 \
--suppressions-list=/home/sergio/gitea/coffee_crisis_arcade_edition/linux-utils/cppcheck_suppressions \ # --suppressions-list=/home/sergio/gitea/coffee_crisis_arcade_edition/linux-utils/cppcheck_suppressions \
/home/sergio/gitea/coffee_crisis_arcade_edition/source/ \ # /home/sergio/gitea/coffee_crisis_arcade_edition/source/ \
2>/home/sergio/cppcheck-result-warning-style-performance # 2>/home/sergio/cppcheck-result-warning-style-performance
# all # all
cppcheck --force --enable=all -I /usr/include --std=c++20 \ cppcheck --force --enable=all -I /usr/include --std=c++20 \

View File

@@ -1,3 +1,6 @@
#!/bin/bash #!/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 valgrind --suppressions=valgrind_exceptions \
--leak-check=full \
~/coffee_crisis_arcade_edition/coffee_crisis_arcade_edition_debug \
> ~/coffee_crisis_arcade_edition/debug.txt 2>&1

View File

@@ -8,9 +8,8 @@
#include "utils.h" #include "utils.h"
// Carga las animaciones en un vector(Animations) desde un fichero // 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<std::string> buffer;
std::ifstream file(file_path); std::ifstream file(file_path);
if (!file) if (!file)
{ {
@@ -18,11 +17,13 @@ Animations loadAnimationsFromFile(const std::string &file_path)
throw std::runtime_error("Fichero no encontrado: " + 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 ]"); printWithDots("Animation : ", file_path.substr(file_path.find_last_of("\\/") + 1), "[ LOADED ]");
std::vector<std::string> buffer;
std::string line;
while (std::getline(file, line)) while (std::getline(file, line))
{ {
// if (!line.empty())
buffer.push_back(line); buffer.push_back(line);
} }
@@ -31,30 +32,25 @@ Animations loadAnimationsFromFile(const std::string &file_path)
// Constructor // Constructor
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const std::string &file_path) AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const std::string &file_path)
: MovingSprite(texture), : MovingSprite(texture)
current_animation_(0)
{ {
// Carga las animaciones // Carga las animaciones
if (!file_path.empty()) if (!file_path.empty())
{ {
animations_ = loadFromFile(file_path); AnimationsFileBuffer v = loadAnimationsFromFile(file_path);
} loadFromAnimationsFileBuffer(v);
}
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const Animations &animations)
: MovingSprite(texture),
current_animation_(0)
{
if (!animations.empty())
{
loadFromVector(animations);
} }
} }
// Constructor // Constructor
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture) AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const AnimationsFileBuffer &animations)
: MovingSprite(texture), : MovingSprite(texture)
current_animation_(0) {} {
if (!animations.empty())
{
loadFromAnimationsFileBuffer(animations);
}
}
// Obtiene el indice de la animación a partir del nombre // Obtiene el indice de la animación a partir del nombre
int AnimatedSprite::getIndex(const std::string &name) int AnimatedSprite::getIndex(const std::string &name)
@@ -249,81 +245,78 @@ void AnimatedSprite::resetAnimation()
animations_[current_animation_].completed = false; animations_[current_animation_].completed = false;
} }
// Carga la animación desde un fichero // Carga la animación desde un vector de cadenas
std::vector<Animation> AnimatedSprite::loadFromFile(const std::string &file_path) bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source)
{ {
// Inicializa variables int frame_width = 0;
std::vector<Animation> animations; int frame_height = 0;
auto frame_width = 1; int frames_per_row = 0;
auto frame_height = 1; int max_tiles = 0;
bool success = true;
size_t index = 0;
std::ifstream file(file_path); while (index < source.size())
std::string line; {
std::string line = source.at(index);
// El fichero se puede abrir
if (file.good())
{
// 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 // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]") if (line == "[animation]")
{ {
Animation animation = Animation(); Animation animation;
do do
{ {
std::getline(file, line); index++;
line = source.at(index);
size_t pos = line.find("=");
// Encuentra la posición del caracter '=' if (pos != std::string::npos)
int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != static_cast<int>(line.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 // 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; std::string tmp;
SDL_Rect rect = {0, 0, frame_width, frame_height}; SDL_Rect rect = {0, 0, frame_width, frame_height};
while (getline(ss, tmp, ',')) while (getline(ss, tmp, ','))
{ {
// Comprueba que el tile no sea mayor que el maximo indice permitido // 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); int num_tile = std::stoi(tmp);
if (num_tile <= max_tiles)
{
rect.x = (num_tile % frames_per_row) * frame_width; rect.x = (num_tile % frames_per_row) * frame_width;
rect.y = (num_tile / frames_per_row) * frame_height; rect.y = (num_tile / frames_per_row) * frame_height;
animation.frames.push_back(rect); animation.frames.emplace_back(rect);
}
} }
} }
else else
{ {
std::cout << "Warning: file " << getFileName(file_path).c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; std::cout << "Warning: unknown parameter " << key << std::endl;
success = false;
} }
} }
} while (line != "[/animation]"); } while (line != "[/animation]");
// Añade la animación al vector de animaciones // 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 // En caso contrario se parsea el fichero para buscar las variables y los valores
@@ -333,155 +326,23 @@ std::vector<Animation> AnimatedSprite::loadFromFile(const std::string &file_path
size_t pos = line.find("="); size_t pos = line.find("=");
// Procesa las dos subcadenas // Procesa las dos subcadenas
if (pos != line.npos) if (pos != std::string::npos)
{ {
if (line.substr(0, pos) == "frame_width") std::string key = line.substr(0, pos);
int value = std::stoi(line.substr(pos + 1));
if (key == "frame_width")
{ {
frame_width = std::stoi(line.substr(pos + 1, line.length())); frame_width = value;
} }
else if (line.substr(0, pos) == "frame_height") else if (key == "frame_height")
{ {
frame_height = std::stoi(line.substr(pos + 1, line.length())); frame_height = value;
} }
else else
{ {
std::cout << "Warning: file " << getFileName(file_path) << "\n, unknown parameter \"" << line.substr(0, pos) << "\"" << std::endl; std::cout << "Warning: unknown parameter " << key << 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);
// 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<int>(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; success = false;
} }

View File

@@ -21,32 +21,30 @@ struct Animation
Animation() : name(std::string()), speed(5), loop(0), completed(false), current_frame(0), counter(0) {} Animation() : name(std::string()), speed(5), loop(0), completed(false), current_frame(0), counter(0) {}
}; };
using Animations = std::vector<std::string>; using AnimationsFileBuffer = std::vector<std::string>;
// Carga las animaciones en un vector(Animations) desde un fichero // 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 class AnimatedSprite : public MovingSprite
{ {
protected: protected:
// Variables // Variables
std::vector<Animation> animations_; // Vector con las diferentes animaciones std::vector<Animation> 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 // Calcula el frame correspondiente a la animación actual
void animate(); void animate();
// Carga la animación desde un fichero // Carga la animación desde un vector de cadenas
std::vector<Animation> loadFromFile(const std::string &file_path); bool loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source);
// Carga la animación desde un vector
bool loadFromVector(const Animations &source);
public: public:
// Constructor // Constructor
AnimatedSprite(std::shared_ptr<Texture> texture, const std::string &file_path); AnimatedSprite(std::shared_ptr<Texture> texture, const std::string &file_path);
AnimatedSprite(std::shared_ptr<Texture> texture, const Animations &animations); AnimatedSprite(std::shared_ptr<Texture> texture, const AnimationsFileBuffer &animations);
explicit AnimatedSprite(std::shared_ptr<Texture> texture); explicit AnimatedSprite(std::shared_ptr<Texture> texture)
: MovingSprite(texture) {}
// Destructor // Destructor
virtual ~AnimatedSprite() = default; virtual ~AnimatedSprite() = default;

View File

@@ -111,7 +111,7 @@ std::shared_ptr<TextFile> Resource::getTextFile(const std::string &name)
} }
// Obtiene la animación a partir de un nombre // 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) auto it = std::find_if(animations_.begin(), animations_.end(), [&name](auto &a)
{ return a.name == name; }); { return a.name == name; });

View File

@@ -58,10 +58,10 @@ struct ResourceTextFile
struct ResourceAnimation struct ResourceAnimation
{ {
std::string name; // Nombre del fichero std::string name; // Nombre del fichero
Animations animation; // Objeto con las animaciones AnimationsFileBuffer animation; // Objeto con las animaciones
// Constructor // Constructor
ResourceAnimation(const std::string &name, const Animations &animation) ResourceAnimation(const std::string &name, const AnimationsFileBuffer &animation)
: name(name), animation(animation) {} : name(name), animation(animation) {}
}; };
@@ -130,7 +130,7 @@ public:
std::shared_ptr<TextFile> getTextFile(const std::string &name); std::shared_ptr<TextFile> getTextFile(const std::string &name);
// Obtiene la animación a partir de un nombre // 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 // Obtiene el fichero con los datos para el modo demostración a partir de un çindice
DemoData &getDemoData(int index); DemoData &getDemoData(int index);