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

@@ -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<std::string> 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<std::string> 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> 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> 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> texture)
: MovingSprite(texture),
current_animation_(0) {}
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> 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<Animation> 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<Animation> 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<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 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<int>(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;
}