Afegida barra de progres en la càrrega de recursos
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<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();
|
||||
}
|
||||
Reference in New Issue
Block a user