Collons, que m'havien stagejat no se que.. ara si, arreglada la carrega d'animacions

This commit is contained in:
2024-10-24 19:58:45 +02:00
parent ca464b2e81
commit a5c72a0f65
2 changed files with 36 additions and 67 deletions

View File

@@ -23,8 +23,8 @@ AnimationsFileBuffer loadAnimationsFromFile(const std::string &file_path)
std::string line; std::string line;
while (std::getline(file, line)) while (std::getline(file, line))
{ {
// if (!line.empty()) if (!line.empty())
buffer.push_back(line); buffer.push_back(line);
} }
return buffer; return buffer;
@@ -246,19 +246,43 @@ void AnimatedSprite::resetAnimation()
} }
// Carga la animación desde un vector de cadenas // Carga la animación desde un vector de cadenas
bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source) void AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source)
{ {
int frame_width = 0; int frame_width = 1;
int frame_height = 0; int frame_height = 1;
int frames_per_row = 0; int frames_per_row = 1;
int max_tiles = 0; int max_tiles = 1;
bool success = true;
size_t index = 0;
size_t index = 0;
while (index < source.size()) while (index < source.size())
{ {
std::string line = source.at(index); std::string line = source.at(index);
// Parsea el fichero para buscar variables y valores
if (line != "[animation]")
{
// Encuentra la posición del caracter '='
size_t pos = line.find("=");
// Procesa las dos subcadenas
if (pos != std::string::npos)
{
std::string key = line.substr(0, pos);
int value = std::stoi(line.substr(pos + 1));
if (key == "frame_width")
frame_width = value;
else if (key == "frame_height")
frame_height = value;
else
std::cout << "Warning: unknown parameter " << key << std::endl;
frames_per_row = texture_->getWidth() / frame_width;
const int w = texture_->getWidth() / frame_width;
const int h = texture_->getHeight() / frame_height;
max_tiles = w * h;
}
}
// 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]")
{ {
@@ -273,21 +297,13 @@ bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so
{ {
std::string key = line.substr(0, pos); std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1); std::string value = line.substr(pos + 1);
if (key == "name") if (key == "name")
{
animation.name = value; animation.name = value;
}
else if (key == "speed") else if (key == "speed")
{
animation.speed = std::stoi(value); animation.speed = std::stoi(value);
}
else if (key == "loop") else if (key == "loop")
{
animation.loop = std::stoi(value); animation.loop = std::stoi(value);
}
else if (key == "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
@@ -297,7 +313,7 @@ bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so
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
int num_tile = std::stoi(tmp); const int num_tile = std::stoi(tmp);
if (num_tile <= max_tiles) if (num_tile <= max_tiles)
{ {
rect.x = (num_tile % frames_per_row) * frame_width; rect.x = (num_tile % frames_per_row) * frame_width;
@@ -308,10 +324,7 @@ bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so
} }
else else
{
std::cout << "Warning: unknown parameter " << key << std::endl; std::cout << "Warning: unknown parameter " << key << std::endl;
success = false;
}
} }
} while (line != "[/animation]"); } while (line != "[/animation]");
@@ -319,48 +332,6 @@ bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so
animations_.emplace_back(animation); animations_.emplace_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 != std::string::npos)
{
std::string key = line.substr(0, pos);
int value = std::stoi(line.substr(pos + 1));
if (key == "frame_width")
{
frame_width = value;
}
else if (key == "frame_height")
{
frame_height = value;
}
else
{
std::cout << "Warning: unknown parameter " << key << 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 // Una vez procesada la linea, aumenta el indice para pasar a la siguiente
index++; index++;
} }
@@ -368,6 +339,4 @@ bool AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so
// Pone un valor por defecto // Pone un valor por defecto
setWidth(frame_width); setWidth(frame_width);
setHeight(frame_height); setHeight(frame_height);
return success;
} }

View File

@@ -37,7 +37,7 @@ protected:
void animate(); void animate();
// Carga la animación desde un vector de cadenas // Carga la animación desde un vector de cadenas
bool loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source); void loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source);
public: public:
// Constructor // Constructor