From 2457517f2b10f467167eb451a7ff972cc91a9911 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Wed, 26 Feb 2025 10:05:52 +0100 Subject: [PATCH] =?UTF-8?q?Afegida=20barra=20de=20progres=20en=20la=20c?= =?UTF-8?q?=C3=A0rrega=20de=20recursos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/resource.cpp | 105 +++++++++++++++++++++++++++++++++++++++----- source/resource.h | 67 ++++++++++++++++++++++++---- source/screen.cpp | 14 ++++++ source/screen.h | 15 ++++--- 4 files changed, 175 insertions(+), 26 deletions(-) diff --git a/source/resource.cpp b/source/resource.cpp index 8910b50f..c60a0b07 100644 --- a/source/resource.cpp +++ b/source/resource.cpp @@ -1,14 +1,16 @@ #include "resource.h" -#include // for find_if -#include // for basic_ostream, operator<<, endl, cout, cerr -#include // for runtime_error -#include "asset.h" // for Asset, AssetType -#include "jail_audio.h" // for JA_DeleteMusic, JA_DeleteSound, JA_LoadMusic -#include "screen.h" // for Screen -#include "text.h" // for Text, loadTextFile -#include "utils.h" // for getFileName, printWithDots -struct JA_Music_t; // lines 10-10 -struct JA_Sound_t; // lines 11-11 +#include // for find_if +#include // for basic_ostream, operator<<, endl, cout, cerr +#include // for runtime_error +#include "asset.h" // for Asset, AssetType +#include "jail_audio.h" // for JA_DeleteMusic, JA_DeleteSound, JA_LoadMusic +#include "screen.h" // for Screen +#include "text.h" // for Text, loadTextFile +#include "utils.h" // for getFileName, printWithDots +#include "options.h" // for getFileName, printWithDots +#include // for SDL_PollEvent +struct JA_Music_t; // lines 10-10 +struct JA_Sound_t; // lines 11-11 // [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado Resource *Resource::resource_ = nullptr; @@ -51,6 +53,9 @@ void Resource::clear() // Carga todos los recursos void Resource::load() { + calculateTotal(); + Screen::get()->show(); + Screen::get()->setBorderColor(Color(0, 0, 0)); std::cout << "** LOADING RESOURCES" << std::endl; loadSounds(); loadMusics(); @@ -208,6 +213,7 @@ void Resource::loadSounds() auto name = getFileName(l); sounds_.emplace_back(ResourceSound(name, JA_LoadSound(l.c_str()))); printWithDots("Sound : ", name, "[ LOADED ]"); + updateLoadingProgress(); } } @@ -223,6 +229,7 @@ void Resource::loadMusics() auto name = getFileName(l); musics_.emplace_back(ResourceMusic(name, JA_LoadMusic(l.c_str()))); printWithDots("Music : ", name, "[ LOADED ]"); + updateLoadingProgress(); } } @@ -237,6 +244,7 @@ void Resource::loadTextures() { auto name = getFileName(l); textures_.emplace_back(ResourceTexture(name, std::make_shared(Screen::get()->getRenderer(), l))); + updateLoadingProgress(); } } @@ -251,6 +259,7 @@ void Resource::loadTextFiles() { auto name = getFileName(l); text_files_.emplace_back(ResourceTextFile(name, loadTextFile(l))); + updateLoadingProgress(); } } @@ -265,6 +274,7 @@ void Resource::loadAnimations() { auto name = getFileName(l); animations_.emplace_back(ResourceAnimation(name, loadAnimationsFromFile(l))); + updateLoadingProgress(); } } @@ -280,6 +290,7 @@ void Resource::loadTileMaps() auto name = getFileName(l); tile_maps_.emplace_back(ResourceTileMap(name, loadRoomTileFile(l))); printWithDots("TileMap : ", name, "[ LOADED ]"); + updateLoadingProgress(); } } @@ -295,6 +306,7 @@ void Resource::loadRooms() auto name = getFileName(l); rooms_.emplace_back(ResourceRoom(name, std::make_shared(loadRoomFile(l)))); printWithDots("Room : ", name, "[ LOADED ]"); + updateLoadingProgress(); } } @@ -357,4 +369,77 @@ void Resource::clearMusics() } } musics_.clear(); // Limpia el vector después de liberar todos los recursos +} + +// Calcula el numero de recursos para cargar +void Resource::calculateTotal() +{ + std::vector assetTypes = { + AssetType::SOUND, + AssetType::MUSIC, + AssetType::BITMAP, + AssetType::FONT, + AssetType::ANIMATION, + AssetType::TILEMAP, + AssetType::ROOM}; + + size_t total = 0; + for (const auto &assetType : assetTypes) + { + auto list = Asset::get()->getListByType(assetType); + total += list.size(); + } + + count_ = ResourceCount(total, 0); +} + +// Muestra el progreso de carga +void Resource::renderProgress() +{ + constexpr int X_PADDING = 10; + constexpr int Y_PADDING = 10; + constexpr int BAR_HEIGHT = 10; + const int bar_position = options.game.height - BAR_HEIGHT - Y_PADDING; + Screen::get()->start(); + Screen::get()->clean(); + SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 255, 255, 255, 255); + + const int wired_bar_width = options.game.width - (X_PADDING * 2); + SDL_Rect rect_wired = {X_PADDING, bar_position, wired_bar_width, X_PADDING}; + SDL_RenderDrawRect(Screen::get()->getRenderer(), &rect_wired); + + const int full_bar_width = wired_bar_width * count_.getPercentage(); + SDL_Rect rect_full = {X_PADDING, bar_position, full_bar_width, X_PADDING}; + SDL_RenderFillRect(Screen::get()->getRenderer(), &rect_full); + + Screen::get()->renderWithoutNotifier(); +} + +// Comprueba los eventos de la pantalla de carga +void Resource::checkEvents() +{ + SDL_Event event; + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: + exit(0); + break; + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_ESCAPE) + { + exit(0); + } + break; + } + } +} + +// Actualiza el progreso de carga +void Resource::updateLoadingProgress() +{ + count_.add(1); + renderProgress(); + checkEvents(); } \ No newline at end of file diff --git a/source/resource.h b/source/resource.h index 38a5a885..8ab83d73 100644 --- a/source/resource.h +++ b/source/resource.h @@ -1,14 +1,14 @@ #pragma once -#include // for shared_ptr -#include // for string -#include // 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 +#include // for shared_ptr +#include // for string +#include // 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 @@ -98,6 +98,41 @@ struct ResourceRoom : 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(loaded) / static_cast(total); + } +}; + class Resource { private: @@ -113,6 +148,8 @@ private: std::vector tile_maps_; // Vector con los mapas de tiles std::vector rooms_; // Vector con las habitaciones + ResourceCount count_; // Contador de recursos + // Carga los sonidos void loadSounds(); @@ -149,6 +186,18 @@ private: // 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 diff --git a/source/screen.cpp b/source/screen.cpp index f1a0be94..1fc96960 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -121,6 +121,20 @@ void Screen::render() renderPresent(); } + +// Vuelca el contenido del renderizador en pantalla +void Screen::renderWithoutNotifier() +{ + // Si está el borde activo, vuelca gameCanvas sobre borderCanvas + if (options.video.border.enabled) + { + gameCanvasToBorderCanvas(); + } + + // Muestra el contenido por pantalla + renderPresent(); +} + // Establece el modo de video void Screen::setVideoMode(int videoMode) { diff --git a/source/screen.h b/source/screen.h index 8e2c6035..1a91c3a9 100644 --- a/source/screen.h +++ b/source/screen.h @@ -1,12 +1,12 @@ #pragma once -#include // for SDL_BlendMode -#include // for SDL_Rect -#include // for SDL_Renderer, SDL_Texture -#include // for Uint32 -#include // for SDL_Window -#include // for vector -#include "utils.h" // for Color +#include // for SDL_BlendMode +#include // for SDL_Rect +#include // for SDL_Renderer, SDL_Texture +#include // for Uint32 +#include // for SDL_Window +#include // for vector +#include "utils.h" // for Color // Tipos de filtro enum class ScreenFilter : Uint32 @@ -100,6 +100,7 @@ public: // Vuelca el contenido del renderizador en pantalla void render(); + void renderWithoutNotifier(); // Actualiza la lógica de la clase void update();