forked from jaildesigner-jailgames/jaildoctors_dilemma
248 lines
7.4 KiB
C++
248 lines
7.4 KiB
C++
#pragma once
|
|
|
|
#include <memory> // for shared_ptr
|
|
#include <string> // for string
|
|
#include <vector> // for vector
|
|
#include "animated_sprite.h" // for AnimationsFileBuffer
|
|
#include "room.h" // for room_t
|
|
#include "text.h" // for Text, TextFile
|
|
#include "texture.h" // for Texture
|
|
struct JA_Music_t; // lines 11-11
|
|
struct JA_Sound_t; // lines 12-12
|
|
|
|
// Estructura para almacenar ficheros de sonido y su nombre
|
|
struct ResourceSound
|
|
{
|
|
std::string name; // Nombre del sonido
|
|
JA_Sound_t *sound; // Objeto con el sonido
|
|
|
|
// Constructor
|
|
ResourceSound(const std::string &name, JA_Sound_t *sound)
|
|
: name(name), sound(sound) {}
|
|
};
|
|
|
|
// Estructura para almacenar ficheros musicales y su nombre
|
|
struct ResourceMusic
|
|
{
|
|
std::string name; // Nombre de la musica
|
|
JA_Music_t *music; // Objeto con la música
|
|
|
|
// Constructor
|
|
ResourceMusic(const std::string &name, JA_Music_t *music)
|
|
: name(name), music(music) {}
|
|
};
|
|
|
|
// Estructura para almacenar objetos Texture y su nombre
|
|
struct ResourceTexture
|
|
{
|
|
std::string name; // Nombre de la textura
|
|
std::shared_ptr<Texture> texture; // Objeto con la textura
|
|
|
|
// Constructor
|
|
ResourceTexture(const std::string &name, std::shared_ptr<Texture> texture)
|
|
: name(name), texture(texture) {}
|
|
};
|
|
|
|
// Estructura para almacenar ficheros TextFile y su nombre
|
|
struct ResourceTextFile
|
|
{
|
|
std::string name; // Nombre del fichero
|
|
std::shared_ptr<TextFile> text_file; // Objeto con los descriptores de la fuente de texto
|
|
|
|
// Constructor
|
|
ResourceTextFile(const std::string &name, std::shared_ptr<TextFile> text_file)
|
|
: name(name), text_file(text_file) {}
|
|
};
|
|
|
|
// Estructura para almacenar objetos Text y su nombre
|
|
struct ResourceText
|
|
{
|
|
std::string name; // Nombre del objeto
|
|
std::shared_ptr<Text> text; // Objeto
|
|
|
|
// Constructor
|
|
ResourceText(const std::string &name, std::shared_ptr<Text> text)
|
|
: name(name), text(text) {}
|
|
};
|
|
|
|
// Estructura para almacenar ficheros animaciones y su nombre
|
|
struct ResourceAnimation
|
|
{
|
|
std::string name; // Nombre del fichero
|
|
AnimationsFileBuffer animation; // Objeto con las animaciones
|
|
|
|
// Constructor
|
|
ResourceAnimation(const std::string &name, const AnimationsFileBuffer &animation)
|
|
: name(name), animation(animation) {}
|
|
};
|
|
|
|
// Estructura para almacenar ficheros con el mapa de tiles de una habitación y su nombre
|
|
struct ResourceTileMap
|
|
{
|
|
std::string name; // Nombre del mapa de tiles
|
|
std::vector<int> tileMap; // Vector con los indices del mapa de tiles
|
|
|
|
// Constructor
|
|
ResourceTileMap(const std::string &name, const std::vector<int> &tileMap)
|
|
: name(name), tileMap(tileMap) {}
|
|
};
|
|
|
|
// Estructura para almacenar habitaciones y su nombre
|
|
struct ResourceRoom
|
|
{
|
|
std::string name; // Nombre de la habitación
|
|
std::shared_ptr<room_t> room; // Habitación
|
|
|
|
// Constructor
|
|
ResourceRoom(const std::string &name, std::shared_ptr<room_t> room)
|
|
: name(name), room(room) {}
|
|
};
|
|
|
|
// Estructura para llevar la cuenta de los recursos cargados
|
|
struct ResourceCount
|
|
{
|
|
// int sounds; // Número de sonidos cargados
|
|
// int musics; // Número de musicas cargadas
|
|
// int textures; // Número de texturas cargadas
|
|
// int text_files; // Número de ficheros de texto cargados
|
|
// int texts; // Número de objetos de texto cargados
|
|
// int animations; // Número de animaciones cargadas
|
|
// int tile_maps; // Número de mapas de tiles cargados
|
|
// int rooms; // Número de habitaciones cargadas
|
|
int total; // Número total de recursos
|
|
int loaded; // Número de recursos cargados
|
|
|
|
// Constructor
|
|
ResourceCount()
|
|
: total(0), loaded(0) {}
|
|
|
|
// Constructor
|
|
ResourceCount(int total, int loaded)
|
|
: total(total), loaded(loaded) {}
|
|
|
|
// Añade una cantidad a los recursos cargados
|
|
void add(int amount)
|
|
{
|
|
loaded += amount;
|
|
}
|
|
|
|
// Obtiene el porcentaje de recursos cargados
|
|
float getPercentage()
|
|
{
|
|
return static_cast<float>(loaded) / static_cast<float>(total);
|
|
}
|
|
};
|
|
|
|
class Resource
|
|
{
|
|
private:
|
|
// [SINGLETON] Objeto resource privado para Don Melitón
|
|
static Resource *resource_;
|
|
|
|
std::vector<ResourceSound> sounds_; // Vector con los sonidos
|
|
std::vector<ResourceMusic> musics_; // Vector con las musicas
|
|
std::vector<ResourceTexture> textures_; // Vector con las musicas
|
|
std::vector<ResourceTextFile> text_files_; // Vector con los ficheros de texto
|
|
std::vector<ResourceText> texts_; // Vector con los objetos de texto
|
|
std::vector<ResourceAnimation> animations_; // Vector con las animaciones
|
|
std::vector<ResourceTileMap> tile_maps_; // Vector con los mapas de tiles
|
|
std::vector<ResourceRoom> rooms_; // Vector con las habitaciones
|
|
|
|
ResourceCount count_; // Contador de recursos
|
|
|
|
// Carga los sonidos
|
|
void loadSounds();
|
|
|
|
// Carga las musicas
|
|
void loadMusics();
|
|
|
|
// Carga las texturas
|
|
void loadTextures();
|
|
|
|
// Carga los ficheros de texto
|
|
void loadTextFiles();
|
|
|
|
// Carga las animaciones
|
|
void loadAnimations();
|
|
|
|
// Carga los mapas de tiles
|
|
void loadTileMaps();
|
|
|
|
// Carga las habitaciones
|
|
void loadRooms();
|
|
|
|
// Crea los objetos de texto
|
|
void createText();
|
|
|
|
// Vacia todos los vectores de recursos
|
|
void clear();
|
|
|
|
// Carga todos los recursos
|
|
void load();
|
|
|
|
// Vacía el vector de sonidos
|
|
void clearSounds();
|
|
|
|
// Vacía el vector de musicas
|
|
void clearMusics();
|
|
|
|
// Calcula el numero de recursos para cargar
|
|
void calculateTotal();
|
|
|
|
// Muestra el progreso de carga
|
|
void renderProgress();
|
|
|
|
// Comprueba los eventos
|
|
void checkEvents();
|
|
|
|
// Actualiza el progreso de carga
|
|
void updateLoadingProgress();
|
|
|
|
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos resource desde fuera
|
|
|
|
// Constructor
|
|
Resource();
|
|
|
|
// Destructor
|
|
~Resource() = default;
|
|
|
|
public:
|
|
// [SINGLETON] Crearemos el objeto resource con esta función estática
|
|
static void init();
|
|
|
|
// [SINGLETON] Destruiremos el objeto resource con esta función estática
|
|
static void destroy();
|
|
|
|
// [SINGLETON] Con este método obtenemos el objeto resource y podemos trabajar con él
|
|
static Resource *get();
|
|
|
|
// Obtiene el sonido a partir de un nombre
|
|
JA_Sound_t *getSound(const std::string &name);
|
|
|
|
// Obtiene la música a partir de un nombre
|
|
JA_Music_t *getMusic(const std::string &name);
|
|
|
|
// Obtiene la textura a partir de un nombre
|
|
std::shared_ptr<Texture> getTexture(const std::string &name);
|
|
|
|
// Obtiene el fichero de texto a partir de un nombre
|
|
std::shared_ptr<TextFile> getTextFile(const std::string &name);
|
|
|
|
// Obtiene el objeto de texto a partir de un nombre
|
|
std::shared_ptr<Text> getText(const std::string &name);
|
|
|
|
// Obtiene la animación a partir de un nombre
|
|
AnimationsFileBuffer &getAnimation(const std::string &name);
|
|
|
|
// Obtiene el mapa de tiles a partir de un nombre
|
|
std::vector<int> &getTileMap(const std::string &name);
|
|
|
|
// Obtiene la habitación a partir de un nombre
|
|
std::shared_ptr<room_t> getRoom(const std::string &name);
|
|
|
|
// Obtiene todas las habitaciones
|
|
std::vector<ResourceRoom> &getRooms();
|
|
|
|
// Recarga todos los recursos
|
|
void reload();
|
|
}; |