#pragma once #include #include #include #include "animated_sprite.h" #include "text.h" #include "texture.h" #include "utils.h" #include "external/jail_audio.h" // --- Clase Resource: gestiona todos los recursos del juego (singleton) --- class Resource { public: // --- Métodos de singleton --- static void init(); // Inicializa el objeto Resource static void destroy(); // Libera el objeto Resource static Resource *get(); // Obtiene el puntero al objeto Resource // --- Métodos de acceso a recursos --- JA_Sound_t *getSound(const std::string &name); // Obtiene el sonido por nombre JA_Music_t *getMusic(const std::string &name); // Obtiene la música por nombre std::shared_ptr getTexture(const std::string &name); // Obtiene la textura por nombre std::shared_ptr getTextFile(const std::string &name); // Obtiene el fichero de texto por nombre std::shared_ptr getText(const std::string &name); // Obtiene el objeto de texto por nombre AnimationsFileBuffer &getAnimation(const std::string &name); // Obtiene la animación por nombre DemoData &getDemoData(int index); // Obtiene los datos de demo por índice // --- Métodos de recarga de recursos --- void reload(); // Recarga todos los recursos void reloadTextures(); // Recarga solo las texturas private: // --- Estructuras para recursos individuales --- struct ResourceSound { std::string name; // Nombre del sonido JA_Sound_t *sound; // Objeto con el sonido ResourceSound(const std::string &name, JA_Sound_t *sound) : name(name), sound(sound) {} }; struct ResourceMusic { std::string name; // Nombre de la música JA_Music_t *music; // Objeto con la música ResourceMusic(const std::string &name, JA_Music_t *music) : name(name), music(music) {} }; struct ResourceTexture { std::string name; // Nombre de la textura std::shared_ptr texture; // Objeto con la textura ResourceTexture(const std::string &name, std::shared_ptr texture) : name(name), texture(texture) {} }; struct ResourceTextFile { std::string name; // Nombre del fichero std::shared_ptr text_file; // Objeto con los descriptores de la fuente de texto ResourceTextFile(const std::string &name, std::shared_ptr text_file) : name(name), text_file(text_file) {} }; struct ResourceText { std::string name; // Nombre del objeto std::shared_ptr text; // Objeto de texto ResourceText(const std::string &name, std::shared_ptr text) : name(name), text(text) {} }; struct ResourceAnimation { std::string name; // Nombre de la animación AnimationsFileBuffer animation; // Objeto con las animaciones ResourceAnimation(const std::string &name, const AnimationsFileBuffer &animation) : name(name), animation(animation) {} }; // --- Estructura para el progreso de carga --- struct ResourceCount { size_t total; // Número total de recursos size_t loaded; // Número de recursos cargados ResourceCount() : total(0), loaded(0) {} ResourceCount(size_t total) : total(total), loaded(0) {} void add(size_t amount) { loaded += amount; } void increase() { loaded++; } float getPercentage() const { return total > 0 ? static_cast(loaded) / static_cast(total) : 0.0f; } }; // --- Instancia singleton --- static Resource *instance_; // Instancia única de Resource // --- Vectores de recursos --- std::vector sounds_; // Vector con los sonidos std::vector musics_; // Vector con las músicas std::vector textures_; // Vector con las texturas std::vector text_files_; // Vector con los ficheros de texto std::vector texts_; // Vector con los objetos de texto std::vector animations_; // Vector con las animaciones std::vector demos_; // Vector con los ficheros de datos para el modo demostración // --- Progreso de carga --- ResourceCount loading_count_; // Contador de recursos cargados std::shared_ptr loading_text_; // Texto para escribir en pantalla std::string loading_resource_name_; // Nombre del recurso que se está cargando SDL_FRect loading_wired_rect_; SDL_FRect loading_full_rect_; // --- Métodos internos de carga y gestión --- void loadSounds(); // Carga los sonidos void loadMusics(); // Carga las músicas void loadTextures(); // Carga las texturas void loadTextFiles(); // Carga los ficheros de texto void loadAnimations(); // Carga las animaciones void loadDemoData(); // Carga los datos para el modo demostración void addPalettes(); // Añade paletas a las texturas void createTextures(); // Crea las texturas a partir de los datos cargados void createText(); // Crea los objetos de texto void clear(); // Vacía todos los vectores de recursos void load(); // Carga todos los recursos void clearSounds(); // Vacía el vector de sonidos void clearMusics(); // Vacía el vector de músicas // --- Métodos internos para gestionar el progreso --- void calculateTotalResources(); // Calcula el número de recursos para cargar void renderProgress(); // Muestra el progreso de carga void checkEvents(); // Comprueba los eventos durante la carga void updateLoadingProgress(std::string name); // Actualiza el progreso de carga void initProgressBar(); // Inicializa los rectangulos que definen la barra de progreso void updateProgressBar(); // Actualiza la barra de estado // --- Constructores y destructor privados (singleton) --- Resource(); // Constructor privado ~Resource() = default; // Destructor privado };