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); setTexture(texture);
setRenderer(renderer); setRenderer(renderer);
animatedSprite_t as;
// Carga las animaciones // Carga las animaciones
if (file != "") 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) else if (buffer)
@@ -22,6 +29,23 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st
currentAnimation = 0; 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 // Destructor
AnimatedSprite::~AnimatedSprite() AnimatedSprite::~AnimatedSprite()
{ {
@@ -172,16 +196,15 @@ SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF)
} }
// Carga la animación desde un fichero // 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 framesPerRow = 0;
int frameWidth = 0; int frameWidth = 0;
int frameHeight = 0; int frameHeight = 0;
int maxTiles = 0; int maxTiles = 0;
// Indicador de éxito en la carga
bool success = true;
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1); const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
std::ifstream file(filePath); std::ifstream file(filePath);
std::string line; std::string line;
@@ -245,13 +268,13 @@ bool AnimatedSprite::loadFromFile(std::string filePath)
else else
{ {
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str()); printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
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
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 // 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 else
{ {
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str()); printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
} }
// Normaliza valores // Normaliza valores
@@ -307,13 +329,16 @@ bool AnimatedSprite::loadFromFile(std::string filePath)
else else
{ {
printf("Warning: Unable to open %s file\n", filename.c_str()); printf("Warning: Unable to open %s file\n", filename.c_str());
success = false;
} }
// Pone un valor por defecto // Pone un valor por defecto
setRect({0, 0, frameWidth, frameHeight}); 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 // Carga la animación desde un vector

View File

@@ -11,21 +11,27 @@
#ifndef ANIMATEDSPRITE_H #ifndef ANIMATEDSPRITE_H
#define 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 class AnimatedSprite : public MovingSprite
{ {
private: 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 // Variables
std::vector<t_animation> animation; // Vector con las diferentes animaciones std::vector<t_animation> animation; // Vector con las diferentes animaciones
int currentAnimation; // Animacion activa int currentAnimation; // Animacion activa
@@ -33,6 +39,7 @@ private:
public: public:
// Constructor // Constructor
AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector<std::string> *buffer = nullptr); AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector<std::string> *buffer = nullptr);
AnimatedSprite(animatedSprite_t as);
// Destructor // Destructor
~AnimatedSprite(); ~AnimatedSprite();
@@ -72,7 +79,7 @@ public:
int getIndex(std::string name); int getIndex(std::string name);
// Carga la animación desde un fichero // 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 // Carga la animación desde un vector
bool loadFromVector(std::vector<std::string> *source); bool loadFromVector(std::vector<std::string> *source);

View File

@@ -24,6 +24,9 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *opti
spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
// **** // ****
// Crea la cache de recursos
cacheResources();
// Crea los objetos // Crea los objetos
scoreboard = new ScoreBoard(renderer, asset, options, &board); scoreboard = new ScoreBoard(renderer, asset, options, &board);
itemTracker = new ItemTracker(); itemTracker = new ItemTracker();
@@ -58,6 +61,9 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *opti
Game::~Game() Game::~Game()
{ {
// Libera los recursos
freeCache();
// Libera la memoria de los objetos // Libera la memoria de los objetos
delete scoreboard; delete scoreboard;
delete itemTracker; delete itemTracker;
@@ -453,4 +459,87 @@ void Game::renderBlackScreen()
screen->clean(); screen->clean();
screen->setBorderColor(stringToColor(options->palette, "black")); screen->setBorderColor(stringToColor(options->palette, "black"));
} }
}
// Carga todos los recursos necesarios
void Game::cacheResources()
{
// Carga las texturas
std::vector<std::string> textureList;
// Jugador
textureList.push_back("player.png");
// Tilesets
textureList.push_back("standard.png");
textureList.push_back("standard_zxarne.png");
// Enemigos
textureList.push_back("paco.png");
textureList.push_back("chip.png");
textureList.push_back("wave.png");
textureList.push_back("wave_v.png");
textureList.push_back("sigmasua.png");
textureList.push_back("diskette.png");
textureList.push_back("bird.png");
textureList.push_back("bin.png");
textureList.push_back("qvoid.png");
textureList.push_back("batman.png");
textureList.push_back("tuno.png");
textureList.push_back("matatunos.png");
textureList.push_back("abad.png");
textureList.push_back("jailbattle_human.png");
textureList.push_back("jailbattle_alien.png");
textureList.push_back("jailer.png");
textureList.push_back("jailer2.png");
textureList.push_back("jailer3.png");
textureList.push_back("printer.png");
textureList.push_back("code.png");
textureList.push_back("demon.png");
textureList.push_back("dimallas.png");
textureList.push_back("dimallas_v.png");
textureList.push_back("heavy.png");
textureList.push_back("spider.png");
textureList.push_back("macaronni_ted.png");
textureList.push_back("mummy.png");
textureList.push_back("sam.png");
textureList.push_back("amstrad_character_set.png");
textureList.push_back("breakout.png");
textureList.push_back("lamp.png");
textureList.push_back("bry.png");
textureList.push_back("tv.png");
textureList.push_back("tv_panel.png");
textureList.push_back("arounders_door.png");
textureList.push_back("arounders_machine.png");
textureList.push_back("arounder_walk.png");
textureList.push_back("arounder_stop.png");
textureList.push_back("arounder_fly.png");
textureList.push_back("bat.png");
// Items
textureList.push_back("items.png");
// Texto
textureList.push_back("smb2.png");
textureList.push_back("debug.png");
for (auto list : textureList)
{
texture_t t;
t.name = list;
t.texture = new Texture(renderer, asset->get(t.name));
textures.push_back(t);
}
}
// Libera los recursos
void Game::freeCache()
{
// Libera las texturas
for (auto texture : textures)
{
texture.texture->unload();
delete texture.texture;
}
textures.clear();
} }

View File

@@ -24,6 +24,12 @@
class Game class Game
{ {
private: private:
struct texture_t
{
std::string name; // Nombre de la textura
Texture *texture; // La textura
};
// Objetos y punteros // Objetos y punteros
SDL_Renderer *renderer; // El renderizador de la ventana SDL_Renderer *renderer; // El renderizador de la ventana
SDL_Event *eventHandler; // Manejador de eventos SDL_Event *eventHandler; // Manejador de eventos
@@ -40,6 +46,8 @@ private:
options_t *options; // Puntero a las opciones del juego options_t *options; // Puntero a las opciones del juego
Test *test; Test *test;
std::vector<texture_t> textures;
// Variables // Variables
JA_Music music; // Musica que suena durante el juego JA_Music music; // Musica que suena durante el juego
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
@@ -107,6 +115,12 @@ private:
// Dibuja la pantalla negra // Dibuja la pantalla negra
void renderBlackScreen(); void renderBlackScreen();
// Carga todos los recursos necesarios
void cacheResources();
// Libera los recursos
void freeCache();
public: public:
// Constructor // Constructor
Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options, Input *input, Debug *debug); Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options, Input *input, Debug *debug);