#include "resource.h" #include // Para SDL_LogInfo, SDL_LogCategory, SDL_LogError, SDL_SetRenderDrawColor, SDL_EventType, SDL_PollEvent, SDL_RenderFillRect, SDL_RenderRect, SDLK_ESCAPE, SDL_Event #include // Para find_if, max, find #include // Para array #include // Para exit #include // Para runtime_error #include // Para move #include "asset.h" // Para Asset #include "color.h" // Para Color #ifndef NO_AUDIO #include "external/jail_audio.h" // Para JA_LoadMusic, JA_LoadSound, JA_DeleteMusic, JA_DeleteSound #endif #include "lang.h" // Para getText #include "param.h" // Para Param, param, ParamResource, ParamGame #include "screen.h" // Para Screen #include "text.h" // Para Text struct JA_Music_t; // lines 11-11 struct JA_Sound_t; // lines 12-12 // Declaraciones de funciones que necesitas implementar en otros archivos // Singleton Resource *Resource::instance = nullptr; // Inicializa la instancia única del singleton con modo de carga void Resource::init(LoadingMode mode) { Resource::instance = new Resource(mode); } // Libera la instancia void Resource::destroy() { delete Resource::instance; Resource::instance = nullptr; } // Obtiene la instancia auto Resource::get() -> Resource * { return Resource::instance; } // Constructor con modo de carga Resource::Resource(LoadingMode mode) : loading_mode_(mode), loading_text_(nullptr) { if (loading_mode_ == LoadingMode::PRELOAD) { loading_text_ = Screen::get()->getText(); load(); } else { // En modo lazy, cargamos lo mínimo indispensable initResourceLists(); loadEssentialResources(); } } // Destructor Resource::~Resource() { clear(); } // Carga los recursos esenciales que siempre se necesitan (modo lazy) void Resource::loadEssentialResources() { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading essential resources for lazy mode"); // Cargar recursos de texto básicos que se usan para crear texturas loadTextFilesQuiet(); // <- VERSIÓN SILENCIOSA loadEssentialTextures(); // Ya es silenciosa createText(); // Crear objetos de texto createTextTextures(); // Crear texturas generadas (game_text_xxx) createPlayerTextures(); // Crea las texturas de jugadores con todas sus variantes de paleta SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Essential resources loaded"); } // Carga los ficheros de texto del juego (versión silenciosa) void Resource::loadTextFilesQuiet() { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXT FILES (quiet load)"); auto list = Asset::get()->getListByType(Asset::Type::FONT); for (const auto &l : list) { auto name = getFileName(l); // Buscar en nuestra lista y cargar directamente auto it = std::ranges::find_if(text_files_, [&name](const auto &t) { return t.name == name; }); if (it != text_files_.end()) { it->text_file = Text::loadFile(l); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Text file loaded: %s", name.c_str()); } } } // Carga solo las texturas esenciales (fuentes) void Resource::loadEssentialTextures() { const std::vector ESSENTIAL_TEXTURES = { "04b_25.png", "04b_25_2x.png", "04b_25_metal.png", "04b_25_grey.png", "04b_25_flat.png", "04b_25_reversed.png", "04b_25_flat_2x.png", "04b_25_reversed_2x.png", "8bithud.png", "aseprite.png", "smb2.png", "smb2_grad.png"}; auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP); for (const auto &file : texture_list) { auto name = getFileName(file); // Solo cargar texturas esenciales if (std::ranges::find(ESSENTIAL_TEXTURES, name) != ESSENTIAL_TEXTURES.end()) { // Buscar en nuestra lista y cargar auto it = std::ranges::find_if(textures_, [&name](const auto &t) { return t.name == name; }); if (it != textures_.end()) { it->texture = std::make_shared(Screen::get()->getRenderer(), file); } } } } // Inicializa las listas de recursos sin cargar el contenido (modo lazy) void Resource::initResourceLists() { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Initializing resource lists for lazy loading"); // Inicializa lista de sonidos auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND); sounds_.clear(); for (const auto &file : sound_list) { sounds_.emplace_back(getFileName(file)); } // Inicializa lista de músicas auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC); musics_.clear(); for (const auto &file : music_list) { musics_.emplace_back(getFileName(file)); } // Inicializa lista de texturas auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP); textures_.clear(); for (const auto &file : texture_list) { textures_.emplace_back(getFileName(file)); } // Inicializa lista de ficheros de texto auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT); text_files_.clear(); for (const auto &file : text_file_list) { text_files_.emplace_back(getFileName(file)); } // Inicializa lista de animaciones auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION); animations_.clear(); for (const auto &file : animation_list) { animations_.emplace_back(getFileName(file)); } // Los demos se cargan directamente sin mostrar progreso (son pocos y pequeños) loadDemoDataQuiet(); // Inicializa lista de objetos de texto (sin cargar el contenido) const std::vector TEXT_OBJECTS = { "04b_25", "04b_25_2x", "04b_25_metal", "04b_25_grey", "04b_25_flat", "04b_25_reversed", "04b_25_flat_2x", "04b_25_reversed_2x", "8bithud", "aseprite", "smb2", "smb2_grad"}; texts_.clear(); for (const auto &text_name : TEXT_OBJECTS) { texts_.emplace_back(text_name); // Constructor con nullptr por defecto } SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Resource lists initialized for lazy loading"); } // Obtiene el sonido a partir de un nombre (con carga perezosa) auto Resource::getSound(const std::string &name) -> JA_Sound_t * { auto it = std::ranges::find_if(sounds_, [&name](const auto &s) { return s.name == name; }); if (it != sounds_.end()) { // Si está en modo lazy y no se ha cargado aún, lo carga ahora if (loading_mode_ == LoadingMode::LAZY_LOAD && it->sound == nullptr) { it->sound = loadSoundLazy(name); } return it->sound; } SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Sonido no encontrado %s", name.c_str()); throw std::runtime_error("Sonido no encontrado: " + name); } // Obtiene la música a partir de un nombre (con carga perezosa) auto Resource::getMusic(const std::string &name) -> JA_Music_t * { auto it = std::ranges::find_if(musics_, [&name](const auto &m) { return m.name == name; }); if (it != musics_.end()) { // Si está en modo lazy y no se ha cargado aún, lo carga ahora if (loading_mode_ == LoadingMode::LAZY_LOAD && it->music == nullptr) { it->music = loadMusicLazy(name); } return it->music; } SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Música no encontrada %s", name.c_str()); throw std::runtime_error("Música no encontrada: " + name); } // Obtiene la textura a partir de un nombre (con carga perezosa) auto Resource::getTexture(const std::string &name) -> std::shared_ptr { auto it = std::ranges::find_if(textures_, [&name](const auto &t) { return t.name == name; }); if (it != textures_.end()) { // Si está en modo lazy y no se ha cargado aún, lo carga ahora if (loading_mode_ == LoadingMode::LAZY_LOAD && it->texture == nullptr) { it->texture = loadTextureLazy(name); } return it->texture; } SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Imagen no encontrada %s", name.c_str()); throw std::runtime_error("Imagen no encontrada: " + name); } // Obtiene el fichero de texto a partir de un nombre (con carga perezosa) auto Resource::getTextFile(const std::string &name) -> std::shared_ptr { auto it = std::ranges::find_if(text_files_, [&name](const auto &t) { return t.name == name; }); if (it != text_files_.end()) { // Si está en modo lazy y no se ha cargado aún, lo carga ahora if (loading_mode_ == LoadingMode::LAZY_LOAD && it->text_file == nullptr) { it->text_file = loadTextFileLazy(name); } return it->text_file; } SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: TextFile no encontrado %s", name.c_str()); throw std::runtime_error("TextFile no encontrado: " + name); } // Obtiene el objeto de texto a partir de un nombre (con carga perezosa) auto Resource::getText(const std::string &name) -> std::shared_ptr { auto it = std::ranges::find_if(texts_, [&name](const auto &t) { return t.name == name; }); if (it != texts_.end()) { // Si está en modo lazy y no se ha cargado aún, lo carga ahora if (loading_mode_ == LoadingMode::LAZY_LOAD && it->text == nullptr) { it->text = loadTextLazy(name); } return it->text; } SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Text no encontrado %s", name.c_str()); throw std::runtime_error("Text no encontrado: " + name); } // Obtiene la animación a partir de un nombre (con carga perezosa) auto Resource::getAnimation(const std::string &name) -> AnimationsFileBuffer & { auto it = std::ranges::find_if(animations_, [&name](const auto &a) { return a.name == name; }); if (it != animations_.end()) { // Si está en modo lazy y no se ha cargado aún (vector vacío), lo carga ahora if (loading_mode_ == LoadingMode::LAZY_LOAD && it->animation.empty()) { it->animation = loadAnimationLazy(name); } return it->animation; } SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Animación no encontrada %s", name.c_str()); throw std::runtime_error("Animación no encontrada: " + name); } // Obtiene el fichero con los datos para el modo demostración a partir de un índice auto Resource::getDemoData(int index) -> DemoData & { return demos_.at(index); } // --- Métodos de carga perezosa --- auto Resource::loadSoundLazy(const std::string &name) -> JA_Sound_t * { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading sound lazily: %s", name.c_str()); #ifndef NO_AUDIO auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND); for (const auto &file : sound_list) { if (getFileName(file) == name) { return JA_LoadSound(file.c_str()); } } #endif return nullptr; } auto Resource::loadMusicLazy(const std::string &name) -> JA_Music_t * { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading music lazily: %s", name.c_str()); #ifndef NO_AUDIO auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC); for (const auto &file : music_list) { if (getFileName(file) == name) { return JA_LoadMusic(file.c_str()); } } #endif return nullptr; } auto Resource::loadTextureLazy(const std::string &name) -> std::shared_ptr { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading texture lazily: %s", name.c_str()); auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP); for (const auto &file : texture_list) { if (getFileName(file) == name) { return std::make_shared(Screen::get()->getRenderer(), file); } } return nullptr; } auto Resource::loadTextFileLazy(const std::string &name) -> std::shared_ptr { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading text file lazily: %s", name.c_str()); auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT); for (const auto &file : text_file_list) { if (getFileName(file) == name) { return Text::loadFile(file); } } return nullptr; } auto Resource::loadTextLazy(const std::string &name) -> std::shared_ptr { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading text object lazily: %s", name.c_str()); // Mapeo de objetos de texto a sus recursos struct TextMapping { std::string key; std::string texture_file; std::string text_file; }; const std::vector TEXT_MAPPINGS = { {.key = "04b_25", .texture_file = "04b_25.png", .text_file = "04b_25.txt"}, {.key = "04b_25_2x", .texture_file = "04b_25_2x.png", .text_file = "04b_25_2x.txt"}, {.key = "04b_25_metal", .texture_file = "04b_25_metal.png", .text_file = "04b_25.txt"}, {.key = "04b_25_grey", .texture_file = "04b_25_grey.png", .text_file = "04b_25.txt"}, {.key = "04b_25_flat", .texture_file = "04b_25_flat.png", .text_file = "04b_25.txt"}, {.key = "04b_25_reversed", .texture_file = "04b_25_reversed.png", .text_file = "04b_25.txt"}, {.key = "04b_25_flat_2x", .texture_file = "04b_25_flat_2x.png", .text_file = "04b_25_2x.txt"}, {.key = "04b_25_reversed_2x", .texture_file = "04b_25_reversed_2x.png", .text_file = "04b_25_2x.txt"}, {.key = "8bithud", .texture_file = "8bithud.png", .text_file = "8bithud.txt"}, {.key = "aseprite", .texture_file = "aseprite.png", .text_file = "aseprite.txt"}, {.key = "smb2", .texture_file = "smb2.png", .text_file = "smb2.txt"}, {.key = "smb2_grad", .texture_file = "smb2_grad.png", .text_file = "smb2.txt"}}; for (const auto &mapping : TEXT_MAPPINGS) { if (mapping.key == name) { // Cargar las dependencias automáticamente auto texture = getTexture(mapping.texture_file); // Esto cargará la textura si no está cargada auto text_file = getTextFile(mapping.text_file); // Esto cargará el archivo de texto si no está cargado if (texture && text_file) { return std::make_shared(texture, text_file); } } } return nullptr; } auto Resource::loadAnimationLazy(const std::string &name) -> AnimationsFileBuffer { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading animation lazily: %s", name.c_str()); auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION); for (const auto &file : animation_list) { if (getFileName(file) == name) { return loadAnimationsFromFile(file); } } // Si no se encuentra, retorna vector vacío return AnimationsFileBuffer{}; } // Vacia todos los vectores de recursos void Resource::clear() { #ifndef NO_AUDIO clearSounds(); clearMusics(); #endif textures_.clear(); text_files_.clear(); texts_.clear(); animations_.clear(); demos_.clear(); } // Carga todos los recursos del juego y muestra el progreso de carga void Resource::load() { // Prepara la gestión del progreso de carga calculateTotalResources(); initProgressBar(); // Muerstra la ventana y desactiva el sincronismo vertical auto *screen = Screen::get(); auto vsync = Screen::getVSync(); screen->setVSync(false); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** LOADING RESOURCES"); #ifndef NO_AUDIO loadSounds(); // Carga sonidos loadMusics(); // Carga músicas #endif loadTextures(); // Carga texturas loadTextFiles(); // Carga ficheros de texto loadAnimations(); // Carga animaciones loadDemoData(); // Carga datos de demo createText(); // Crea objetos de texto createTextTextures(); // Crea texturas a partir de texto createPlayerTextures(); // Crea las texturas de jugadores con todas sus variantes de paleta SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** RESOURCES LOADED"); // Restablece el sincronismo vertical a su valor original screen->setVSync(vsync); } // Recarga todos los recursos (limpia y vuelve a cargar) void Resource::reload() { clear(); if (loading_mode_ == LoadingMode::PRELOAD) { load(); } else { initResourceLists(); } } // Carga los sonidos del juego void Resource::loadSounds() { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> SOUND FILES"); auto list = Asset::get()->getListByType(Asset::Type::SOUND); sounds_.clear(); for (const auto &l : list) { auto name = getFileName(l); updateLoadingProgress(name); #ifndef NO_AUDIO sounds_.emplace_back(name, JA_LoadSound(l.c_str())); #else sounds_.emplace_back(name, nullptr); #endif printWithDots("Sound : ", name, "[ LOADED ]"); } } // Carga las músicas del juego void Resource::loadMusics() { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> MUSIC FILES"); auto list = Asset::get()->getListByType(Asset::Type::MUSIC); musics_.clear(); for (const auto &l : list) { auto name = getFileName(l); updateLoadingProgress(name); #ifndef NO_AUDIO musics_.emplace_back(name, JA_LoadMusic(l.c_str())); #else musics_.emplace_back(name, nullptr); #endif printWithDots("Music : ", name, "[ LOADED ]"); } } // Carga las texturas del juego void Resource::loadTextures() { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXTURES"); auto list = Asset::get()->getListByType(Asset::Type::BITMAP); textures_.clear(); for (const auto &l : list) { auto name = getFileName(l); updateLoadingProgress(name); textures_.emplace_back(name, std::make_shared(Screen::get()->getRenderer(), l)); } } // Carga los ficheros de texto del juego void Resource::loadTextFiles() { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXT FILES"); auto list = Asset::get()->getListByType(Asset::Type::FONT); text_files_.clear(); for (const auto &l : list) { auto name = getFileName(l); updateLoadingProgress(name); text_files_.emplace_back(name, Text::loadFile(l)); } } // Carga las animaciones del juego void Resource::loadAnimations() { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> ANIMATIONS"); auto list = Asset::get()->getListByType(Asset::Type::ANIMATION); animations_.clear(); for (const auto &l : list) { auto name = getFileName(l); updateLoadingProgress(name); animations_.emplace_back(name, loadAnimationsFromFile(l)); } } // Carga los datos para el modo demostración void Resource::loadDemoData() { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> DEMO FILES"); constexpr std::array DEMO_FILES = {"demo1.bin", "demo2.bin"}; for (const auto &file : DEMO_FILES) { updateLoadingProgress(file); demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get(file))); } } // Crea las texturas de jugadores con todas sus variantes de paleta void Resource::createPlayerTextures() { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> CREATING PLAYER TEXTURES"); // Configuración de jugadores y sus paletas struct PlayerConfig { std::string base_texture; std::vector palette_files; std::string name_prefix; }; std::vector players = { {.base_texture = "player1.gif", .palette_files = {"player1_coffee1.pal", "player1_coffee2.pal", "player1_invencible.pal"}, .name_prefix = "player1"}, {.base_texture = "player2.gif", .palette_files = {"player2_coffee1.pal", "player2_coffee2.pal", "player2_invencible.pal"}, .name_prefix = "player2"}}; // Bucle principal for (size_t player_idx = 0; player_idx < players.size(); ++player_idx) { const auto &player = players[player_idx]; // Obtenemos el jugador actual // Encontrar el archivo original de la textura std::string texture_file_path; auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP); for (const auto &file : texture_list) { if (getFileName(file) == player.base_texture) { texture_file_path = file; break; } } // Crear las 4 texturas con sus respectivas paletas for (int palette_idx = 0; palette_idx < 4; ++palette_idx) { std::shared_ptr texture; if (palette_idx == 0) { // Textura 0 - usar la ya cargada y modificar solo paleta 0 (default_shirt) texture = getTexture(player.base_texture); texture->setPaletteColor(0, 16, param.player.default_shirt[player_idx].darkest.TO_UINT32()); texture->setPaletteColor(0, 17, param.player.default_shirt[player_idx].dark.TO_UINT32()); texture->setPaletteColor(0, 18, param.player.default_shirt[player_idx].base.TO_UINT32()); texture->setPaletteColor(0, 19, param.player.default_shirt[player_idx].light.TO_UINT32()); texture->setPaletteColor(0, 56, param.player.outline_color[player_idx].TO_UINT32()); } else { // Crear textura nueva desde archivo texture = std::make_shared(Screen::get()->getRenderer(), texture_file_path); // Añadir todas las paletas texture->addPaletteFromPalFile(Asset::get()->get(player.palette_files[0])); texture->addPaletteFromPalFile(Asset::get()->get(player.palette_files[1])); texture->addPaletteFromPalFile(Asset::get()->get(player.palette_files[2])); if (palette_idx == 1) { // Textura 1 - modificar solo paleta 1 (one_coffee_shirt) texture->setPaletteColor(1, 16, param.player.one_coffee_shirt[player_idx].darkest.TO_UINT32()); texture->setPaletteColor(1, 17, param.player.one_coffee_shirt[player_idx].dark.TO_UINT32()); texture->setPaletteColor(1, 18, param.player.one_coffee_shirt[player_idx].base.TO_UINT32()); texture->setPaletteColor(1, 19, param.player.one_coffee_shirt[player_idx].light.TO_UINT32()); texture->setPaletteColor(1, 56, param.player.outline_color[player_idx].TO_UINT32()); } else if (palette_idx == 2) { // Textura 2 - modificar solo paleta 2 (two_coffee_shirt) texture->setPaletteColor(2, 16, param.player.two_coffee_shirt[player_idx].darkest.TO_UINT32()); texture->setPaletteColor(2, 17, param.player.two_coffee_shirt[player_idx].dark.TO_UINT32()); texture->setPaletteColor(2, 18, param.player.two_coffee_shirt[player_idx].base.TO_UINT32()); texture->setPaletteColor(2, 19, param.player.two_coffee_shirt[player_idx].light.TO_UINT32()); texture->setPaletteColor(2, 56, param.player.outline_color[player_idx].TO_UINT32()); } // Textura 3 (palette_idx == 3) - no modificar nada, usar colores originales } // Asignar la paleta correspondiente texture->setPalette(palette_idx); // Guardar con nombre específico std::string texture_name = player.name_prefix + "_pal" + std::to_string(palette_idx); textures_.emplace_back(texture_name, texture); printWithDots("Player Texture : ", texture_name, "[ DONE ]"); } } } // Crea texturas a partir de textos para mostrar puntuaciones y mensajes void Resource::createTextTextures() { struct NameAndText { std::string name; std::string text; NameAndText(std::string name_init, std::string text_init) : name(std::move(name_init)), text(std::move(text_init)) {} }; SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> CREATING TEXTURES"); // Texturas de tamaño normal con outline std::vector strings1 = { {"game_text_1000_points", "1.000"}, {"game_text_2500_points", "2.500"}, {"game_text_5000_points", "5.000"}, {"game_text_powerup", Lang::getText("[GAME_TEXT] 4")}, {"game_text_one_hit", Lang::getText("[GAME_TEXT] 5")}, {"game_text_stop", Lang::getText("[GAME_TEXT] 6")}}; auto text1 = getText("04b_25_enhanced"); for (const auto &s : strings1) { textures_.emplace_back(s.name, text1->writeDXToTexture(Text::STROKE, s.text, -2, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color)); printWithDots("Texture : ", s.name, "[ DONE ]"); } // Texturas de tamaño normal std::vector strings2 = { {"game_text_1000000_points", Lang::getText("[GAME_TEXT] 8")}}; auto text2 = getText("04b_25"); for (const auto &s : strings2) { textures_.emplace_back(s.name, text2->writeDXToTexture(Text::STROKE, s.text, -2, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color)); printWithDots("Texture : ", s.name, "[ DONE ]"); } // Texturas de tamaño doble std::vector strings3 = { {"game_text_100000_points", "100.000"}, {"game_text_get_ready", Lang::getText("[GAME_TEXT] 7")}, {"game_text_last_stage", Lang::getText("[GAME_TEXT] 3")}, {"game_text_congratulations", Lang::getText("[GAME_TEXT] 1")}, {"game_text_game_over", "Game Over"}}; auto text3 = getText("04b_25_2x"); for (const auto &s : strings3) { textures_.emplace_back(s.name, text3->writeToTexture(s.text, 1, -4)); printWithDots("Texture : ", s.name, "[ DONE ]"); } } // Crea los objetos de texto a partir de los archivos de textura y texto void Resource::createText() { struct ResourceInfo { std::string key; std::string texture_file; std::string text_file; std::string white_texture_file; // Textura blanca opcional ResourceInfo(std::string k, std::string t_file, std::string txt_file, std::string w_file = "") : key(std::move(k)), texture_file(std::move(t_file)), text_file(std::move(txt_file)), white_texture_file(std::move(w_file)) {} }; SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> CREATING TEXT OBJECTS"); std::vector resources = { {"04b_25", "04b_25.png", "04b_25.txt"}, {"04b_25_enhanced", "04b_25.png", "04b_25.txt", "04b_25_white.png"}, // Nueva fuente con textura blanca {"04b_25_white", "04b_25_white.png", "04b_25.txt"}, {"04b_25_2x", "04b_25_2x.png", "04b_25_2x.txt"}, {"04b_25_metal", "04b_25_metal.png", "04b_25.txt"}, {"04b_25_grey", "04b_25_grey.png", "04b_25.txt"}, {"04b_25_flat", "04b_25_flat.png", "04b_25.txt"}, {"04b_25_reversed", "04b_25_reversed.png", "04b_25.txt"}, {"04b_25_flat_2x", "04b_25_flat_2x.png", "04b_25_2x.txt"}, {"04b_25_reversed_2x", "04b_25_reversed_2x.png", "04b_25_2x.txt"}, {"8bithud", "8bithud.png", "8bithud.txt"}, {"aseprite", "aseprite.png", "aseprite.txt"}, {"smb2", "smb2.png", "smb2.txt"}, {"smb2_grad", "smb2_grad.png", "smb2.txt"}}; for (const auto &resource : resources) { if (!resource.white_texture_file.empty()) { // Crear texto con textura blanca texts_.emplace_back(resource.key, std::make_shared(getTexture(resource.texture_file), getTexture(resource.white_texture_file), getTextFile(resource.text_file))); } else { // Crear texto normal texts_.emplace_back(resource.key, std::make_shared(getTexture(resource.texture_file), getTextFile(resource.text_file))); } printWithDots("Text : ", resource.key, "[ DONE ]"); } } // Vacía el vector de sonidos y libera la memoria asociada void Resource::clearSounds() { for (auto &sound : sounds_) { if (sound.sound != nullptr) { #ifndef NO_AUDIO JA_DeleteSound(sound.sound); #endif sound.sound = nullptr; } } sounds_.clear(); } // Vacía el vector de músicas y libera la memoria asociada void Resource::clearMusics() { for (auto &music : musics_) { if (music.music != nullptr) { #ifndef NO_AUDIO JA_DeleteMusic(music.music); #endif music.music = nullptr; } } musics_.clear(); } // Calcula el número total de recursos a cargar y reinicia el contador de carga void Resource::calculateTotalResources() { const std::array ASSET_TYPES = { Asset::Type::SOUND, Asset::Type::MUSIC, Asset::Type::BITMAP, Asset::Type::FONT, Asset::Type::ANIMATION, Asset::Type::DEMODATA}; size_t total = 0; for (const auto &asset_type : ASSET_TYPES) { auto list = Asset::get()->getListByType(asset_type); total += list.size(); } loading_count_ = ResourceCount(total); } // Muestra el progreso de carga en pantalla (barra y texto) void Resource::renderProgress() { // Obtiene la pantalla y el renderer auto *screen = Screen::get(); auto *renderer = screen->getRenderer(); // Actualiza la lógica principal de la pantalla (input, etc.) screen->coreUpdate(); // Inicia el frame y limpia la pantalla screen->start(); screen->clean(); auto color = param.resource.color; // Dibuja el interior de la barra de progreso SDL_SetRenderDrawColor(renderer, param.resource.color.r, param.resource.color.g, param.resource.color.b, param.resource.color.a); SDL_RenderFillRect(renderer, &loading_full_rect_); // Dibuja el marco de la barra de progreso SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); SDL_RenderRect(renderer, &loading_wired_rect_); // Escribe el texto de carga encima de la barra loading_text_->writeColored( loading_wired_rect_.x, loading_wired_rect_.y - 9, Lang::getText("[RESOURCE] LOADING") + " : " + loading_resource_name_, param.resource.color); // Renderiza el frame en pantalla screen->coreRender(); } // Comprueba los eventos durante la carga (permite salir con ESC o cerrar ventana) void Resource::checkEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_EVENT_QUIT: exit(0); break; case SDL_EVENT_KEY_DOWN: if (event.key.key == SDLK_ESCAPE) { exit(0); } break; } } } // Carga los datos para el modo demostración (sin mostrar progreso) void Resource::loadDemoDataQuiet() { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> DEMO FILES (quiet load)"); constexpr std::array DEMO_FILES = {"demo1.bin", "demo2.bin"}; for (const auto &file : DEMO_FILES) { demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get(file))); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Demo file loaded: %s", file); } } // Inicializa los rectangulos que definen la barra de progreso void Resource::initProgressBar() { constexpr float X_PADDING = 20.0F; constexpr float Y_PADDING = 20.0F; constexpr float BAR_HEIGHT = 10.0F; const float BAR_Y_POSITION = param.game.height - BAR_HEIGHT - Y_PADDING; const float WIRED_BAR_WIDTH = param.game.width - (X_PADDING * 2); loading_wired_rect_ = {.x = X_PADDING, .y = BAR_Y_POSITION, .w = WIRED_BAR_WIDTH, .h = BAR_HEIGHT}; const float FULL_BAR_WIDTH = WIRED_BAR_WIDTH * loading_count_.getPercentage(); loading_full_rect_ = {.x = X_PADDING, .y = BAR_Y_POSITION, .w = FULL_BAR_WIDTH, .h = BAR_HEIGHT}; } // Actualiza el progreso de carga, muestra la barra y procesa eventos void Resource::updateLoadingProgress(std::string name) { loading_resource_name_ = std::move(name); loading_count_.increase(); updateProgressBar(); renderProgress(); checkEvents(); } // Actualiza la barra de estado void Resource::updateProgressBar() { loading_full_rect_.w = loading_wired_rect_.w * loading_count_.getPercentage(); }