Empezando a trabajar en la precarga de recursos para el juego

This commit is contained in:
2022-10-26 14:13:33 +02:00
parent db5d2901ee
commit c6e8050f95
4 changed files with 158 additions and 23 deletions

View File

@@ -7,10 +7,17 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st
setTexture(texture);
setRenderer(renderer);
animatedSprite_t as;
// Carga las animaciones
if (file != "")
{
loadFromFile(file);
as = loadFromFile(file);
// Copia los datos de las animaciones
for (auto animation : as.animations)
{
this->animation.push_back(animation);
}
}
else if (buffer)
@@ -22,6 +29,23 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st
currentAnimation = 0;
}
// Constructor
AnimatedSprite::AnimatedSprite(animatedSprite_t as)
{
// Copia los punteros
setTexture(as.texture);
setRenderer(as.renderer);
// Inicializa variables
currentAnimation = 0;
// Copia los datos de las animaciones
for (auto animation : as.animations)
{
this->animation.push_back(animation);
}
}
// Destructor
AnimatedSprite::~AnimatedSprite()
{
@@ -172,16 +196,15 @@ SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF)
}
// Carga la animación desde un fichero
bool AnimatedSprite::loadFromFile(std::string filePath)
animatedSprite_t AnimatedSprite::loadFromFile(std::string filePath)
{
// Inicializa variables
animatedSprite_t as;
int framesPerRow = 0;
int frameWidth = 0;
int frameHeight = 0;
int maxTiles = 0;
// Indicador de éxito en la carga
bool success = true;
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
std::ifstream file(filePath);
std::string line;
@@ -245,13 +268,13 @@ bool AnimatedSprite::loadFromFile(std::string filePath)
else
{
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
}
}
} while (line != "[/animation]");
// Añade la animación al vector de animaciones
animation.push_back(buffer);
as.animations.push_back(buffer);
// animation.push_back(buffer);
}
// En caso contrario se parsea el fichero para buscar las variables y los valores
@@ -281,7 +304,6 @@ bool AnimatedSprite::loadFromFile(std::string filePath)
else
{
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
}
// Normaliza valores
@@ -307,13 +329,16 @@ bool AnimatedSprite::loadFromFile(std::string filePath)
else
{
printf("Warning: Unable to open %s file\n", filename.c_str());
success = false;
}
// Pone un valor por defecto
setRect({0, 0, frameWidth, frameHeight});
return success;
// Añade los punteros
as.texture = texture;
as.renderer = renderer;
return as;
}
// Carga la animación desde un vector

View File

@@ -11,21 +11,27 @@
#ifndef ANIMATEDSPRITE_H
#define ANIMATEDSPRITE_H
// Clase AnimatedSprite
struct t_animation
{
std::string name; // Nombre de la animacion
std::vector<SDL_Rect> frames; // Cada uno de los frames que componen la animación
int speed; // Velocidad de la animación
int loop; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva
bool completed; // Indica si ha finalizado la animación
int currentFrame; // Frame actual
int counter; // Contador para las animaciones
};
struct animatedSprite_t
{
std::vector<t_animation> animations; // Vector con las diferentes animaciones
Texture *texture; // Textura con los graficos para el sprite
SDL_Renderer *renderer; // Renderizador para el sprite
};
class AnimatedSprite : public MovingSprite
{
private:
struct t_animation
{
std::string name; // Nombre de la animacion
std::vector<SDL_Rect> frames; // Cada uno de los frames que componen la animación
int speed; // Velocidad de la animación
int loop; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva
bool completed; // Indica si ha finalizado la animación
int currentFrame; // Frame actual
int counter; // Contador para las animaciones
};
// Variables
std::vector<t_animation> animation; // Vector con las diferentes animaciones
int currentAnimation; // Animacion activa
@@ -33,6 +39,7 @@ private:
public:
// Constructor
AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector<std::string> *buffer = nullptr);
AnimatedSprite(animatedSprite_t as);
// Destructor
~AnimatedSprite();
@@ -72,7 +79,7 @@ public:
int getIndex(std::string name);
// Carga la animación desde un fichero
bool loadFromFile(std::string filePath);
animatedSprite_t loadFromFile(std::string filePath);
// Carga la animación desde un vector
bool loadFromVector(std::vector<std::string> *source);