Afegida barra de progres en la càrrega de recursos

This commit is contained in:
2025-02-26 10:05:52 +01:00
parent 64880a427e
commit 2457517f2b
4 changed files with 175 additions and 26 deletions

View File

@@ -1,14 +1,16 @@
#include "resource.h"
#include <algorithm> // for find_if
#include <iostream> // for basic_ostream, operator<<, endl, cout, cerr
#include <stdexcept> // 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 <algorithm> // for find_if
#include <iostream> // for basic_ostream, operator<<, endl, cout, cerr
#include <stdexcept> // 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 <SDL2/SDL_events.h> // 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<Texture>(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<room_t>(loadRoomFile(l))));
printWithDots("Room : ", name, "[ LOADED ]");
updateLoadingProgress();
}
}
@@ -358,3 +370,76 @@ 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<AssetType> 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();
}

View File

@@ -1,14 +1,14 @@
#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
#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
@@ -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<float>(loaded) / static_cast<float>(total);
}
};
class Resource
{
private:
@@ -113,6 +148,8 @@ private:
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();
@@ -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

View File

@@ -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)
{

View File

@@ -1,12 +1,12 @@
#pragma once
#include <SDL2/SDL_blendmode.h> // for SDL_BlendMode
#include <SDL2/SDL_rect.h> // for SDL_Rect
#include <SDL2/SDL_render.h> // for SDL_Renderer, SDL_Texture
#include <SDL2/SDL_stdinc.h> // for Uint32
#include <SDL2/SDL_video.h> // for SDL_Window
#include <vector> // for vector
#include "utils.h" // for Color
#include <SDL2/SDL_blendmode.h> // for SDL_BlendMode
#include <SDL2/SDL_rect.h> // for SDL_Rect
#include <SDL2/SDL_render.h> // for SDL_Renderer, SDL_Texture
#include <SDL2/SDL_stdinc.h> // for Uint32
#include <SDL2/SDL_video.h> // for SDL_Window
#include <vector> // 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();