Optimizada la carga de las animaciones de los globos. Cacheada

This commit is contained in:
2022-10-04 12:13:47 +02:00
parent ddb70c8c85
commit c3fd348a61
6 changed files with 45 additions and 51 deletions

View File

@@ -1,7 +1,7 @@
#include "animatedsprite.h"
// Constructor
AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::string file, std::stringstream *stream)
AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::string file, std::vector<std::string> *buffer)
{
// Copia los punteros
setTexture(texture);
@@ -13,9 +13,9 @@ AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::s
loadFromFile(file);
}
else if (stream)
else if (buffer)
{
loadFromStream(stream);
loadFromVector(buffer);
}
// Inicializa variables
@@ -310,31 +310,26 @@ bool AnimatedSprite::loadFromFile(std::string filePath)
return success;
}
// Carga la animación desde un stream
bool AnimatedSprite::loadFromStream(std::stringstream *stream)
// Carga la animación desde un vector
bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
{
// Inicializa variables
int framesPerRow = 0;
int frameWidth = 0;
int frameHeight = 0;
int maxTiles = 0;
static int number = 0;
number++;
std::cout << "Reading stream #" << number << std::endl;
// Indicador de éxito en la carga
// Indicador de éxito en el proceso
bool success = true;
std::string line;
while (std::getline(*stream, line))
// Recorre todo el vector
int index = 0;
while (index < source->size())
{
std::cout << "***: "<<line << std::endl;
}
// Lee desde el vector
line = source->at(index);
// Procesa el fichero linea a linea
while (std::getline(*stream, line))
{
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]")
{
@@ -345,7 +340,9 @@ bool AnimatedSprite::loadFromStream(std::stringstream *stream)
do
{
std::getline(*stream, line);
// Aumenta el indice para leer la siguiente linea
index++;
line = source->at(index);
// Encuentra la posición del caracter '='
int pos = line.find("=");
@@ -440,13 +437,14 @@ bool AnimatedSprite::loadFromStream(std::stringstream *stream)
}
}
}
// Una vez procesada la linea, aumenta el indice para pasar a la siguiente
index++;
}
// Pone un valor por defecto
setPos({0, 0, frameWidth, frameHeight});
std::cout << "Closing stream #" << number << std::endl;
return success;
}