Resource: treballant en la visualització de la càrrega de recursos
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
#include "resource.h"
|
||||
#include <SDL3/SDL_log.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_LogError
|
||||
#include <algorithm> // Para find_if
|
||||
#include <stdexcept> // Para runtime_error
|
||||
#include "asset.h" // Para Asset, AssetType
|
||||
#include "jail_audio.h" // Para JA_DeleteMusic, JA_DeleteSound, JA_LoadMusic
|
||||
#include "lang.h" // Para getText
|
||||
#include "screen.h" // Para Screen
|
||||
#include "text.h" // Para Text, loadTextFile
|
||||
struct JA_Music_t; // lines 11-11
|
||||
struct JA_Sound_t; // lines 12-12
|
||||
#include <SDL3/SDL.h>
|
||||
#include <algorithm> // Para find_if
|
||||
#include <stdexcept> // Para runtime_error
|
||||
#include "asset.h" // Para Asset, AssetType
|
||||
#include "jail_audio.h" // Para JA_DeleteMusic, JA_DeleteSound, JA_LoadMusic
|
||||
#include "lang.h" // Para getText
|
||||
#include "screen.h" // Para Screen
|
||||
#include "text.h" // Para Text, loadTextFile
|
||||
struct JA_Music_t; // lines 11-11
|
||||
struct JA_Sound_t; // lines 12-12
|
||||
|
||||
// Singleton
|
||||
Resource *Resource::instance_ = nullptr;
|
||||
@@ -40,6 +41,9 @@ void Resource::clear()
|
||||
// Carga todos los recursos
|
||||
void Resource::load()
|
||||
{
|
||||
calculateTotal();
|
||||
Screen::get()->show();
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** LOADING RESOURCES");
|
||||
loadSounds();
|
||||
loadMusics();
|
||||
@@ -174,8 +178,9 @@ void Resource::loadSounds()
|
||||
for (const auto &l : list)
|
||||
{
|
||||
auto name = getFileName(l);
|
||||
sounds_.emplace_back(ResourceSound(name, JA_LoadSound(l.c_str())));
|
||||
sounds_.emplace_back(Resource::ResourceSound(name, JA_LoadSound(l.c_str())));
|
||||
printWithDots("Sound : ", name, "[ LOADED ]");
|
||||
updateLoadingProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,8 +194,9 @@ void Resource::loadMusics()
|
||||
for (const auto &l : list)
|
||||
{
|
||||
auto name = getFileName(l);
|
||||
musics_.emplace_back(ResourceMusic(name, JA_LoadMusic(l.c_str())));
|
||||
musics_.emplace_back(Resource::ResourceMusic(name, JA_LoadMusic(l.c_str())));
|
||||
printWithDots("Music : ", name, "[ LOADED ]");
|
||||
updateLoadingProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,7 +210,8 @@ void Resource::loadTextures()
|
||||
for (const auto &l : list)
|
||||
{
|
||||
auto name = getFileName(l);
|
||||
textures_.emplace_back(ResourceTexture(name, std::make_shared<Texture>(Screen::get()->getRenderer(), l)));
|
||||
textures_.emplace_back(Resource::ResourceTexture(name, std::make_shared<Texture>(Screen::get()->getRenderer(), l)));
|
||||
updateLoadingProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,7 +225,8 @@ void Resource::loadTextFiles()
|
||||
for (const auto &l : list)
|
||||
{
|
||||
auto name = getFileName(l);
|
||||
text_files_.emplace_back(ResourceTextFile(name, loadTextFile(l)));
|
||||
text_files_.emplace_back(Resource::ResourceTextFile(name, loadTextFile(l)));
|
||||
updateLoadingProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,7 +240,8 @@ void Resource::loadAnimations()
|
||||
for (const auto &l : list)
|
||||
{
|
||||
auto name = getFileName(l);
|
||||
animations_.emplace_back(ResourceAnimation(name, loadAnimationsFromFile(l)));
|
||||
animations_.emplace_back(Resource::ResourceAnimation(name, loadAnimationsFromFile(l)));
|
||||
updateLoadingProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,7 +250,9 @@ void Resource::loadDemoData()
|
||||
{
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> DEMO FILES");
|
||||
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get("demo1.bin")));
|
||||
updateLoadingProgress();
|
||||
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get("demo2.bin")));
|
||||
updateLoadingProgress();
|
||||
}
|
||||
|
||||
// Añade paletas a las texturas
|
||||
@@ -290,7 +301,7 @@ void Resource::createTextures()
|
||||
auto text = getText("04b_25");
|
||||
for (const auto &s : strings)
|
||||
{
|
||||
textures_.emplace_back(ResourceTexture(s.name, text->writeToTexture(s.text, 1, -2)));
|
||||
textures_.emplace_back(Resource::ResourceTexture(s.name, text->writeToTexture(s.text, 1, -2)));
|
||||
printWithDots("Texture : ", s.name, "[ DONE ]");
|
||||
}
|
||||
|
||||
@@ -305,7 +316,7 @@ void Resource::createTextures()
|
||||
auto text2 = getText("04b_25_2x");
|
||||
for (const auto &s : strings2X)
|
||||
{
|
||||
textures_.emplace_back(ResourceTexture(s.name, text2->writeToTexture(s.text, 1, -4)));
|
||||
textures_.emplace_back(Resource::ResourceTexture(s.name, text2->writeToTexture(s.text, 1, -4)));
|
||||
printWithDots("Texture : ", s.name, "[ DONE ]");
|
||||
}
|
||||
}
|
||||
@@ -339,7 +350,7 @@ void Resource::createText()
|
||||
|
||||
for (const auto &resource : resources)
|
||||
{
|
||||
texts_.emplace_back(ResourceText(resource.key, std::make_shared<Text>(
|
||||
texts_.emplace_back(Resource::ResourceText(resource.key, std::make_shared<Text>(
|
||||
getTexture(resource.textureFile),
|
||||
getTextFile(resource.textFile))));
|
||||
printWithDots("Text : ", resource.key, "[ DONE ]");
|
||||
@@ -372,4 +383,75 @@ void Resource::clearMusics()
|
||||
}
|
||||
}
|
||||
musics_.clear();
|
||||
}
|
||||
|
||||
// Calcula el numero de recursos para cargar
|
||||
void Resource::calculateTotal()
|
||||
{
|
||||
size_t total = 0;
|
||||
for (int i = 0; i < static_cast<int>(AssetType::COUNT); ++i)
|
||||
{
|
||||
auto assetType = static_cast<AssetType>(i);
|
||||
auto list = Asset::get()->getListByType(assetType);
|
||||
total += list.size();
|
||||
}
|
||||
|
||||
count_ = ResourceCount(total, 0);
|
||||
}
|
||||
|
||||
// Muestra el progreso de carga
|
||||
void Resource::renderProgress()
|
||||
{
|
||||
auto screen = Screen::get();
|
||||
auto renderer = screen->getRenderer();
|
||||
|
||||
constexpr float X_PADDING = 10.0f;
|
||||
constexpr float Y_PADDING = 10.0f;
|
||||
constexpr float BAR_HEIGHT = 10.0f;
|
||||
const float bar_position = param.game.height - BAR_HEIGHT - Y_PADDING;
|
||||
screen->start();
|
||||
screen->clean();
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
|
||||
const float wired_bar_width = param.game.width - (X_PADDING * 2);
|
||||
SDL_FRect rect_wired = {X_PADDING, bar_position, wired_bar_width, X_PADDING};
|
||||
SDL_RenderRect(renderer, &rect_wired);
|
||||
|
||||
const float full_bar_width = wired_bar_width * count_.getPercentage();
|
||||
SDL_FRect rect_full = {X_PADDING, bar_position, full_bar_width, X_PADDING};
|
||||
SDL_RenderFillRect(renderer, &rect_full);
|
||||
|
||||
screen->render();
|
||||
}
|
||||
|
||||
// Comprueba los eventos de la pantalla de carga
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el progreso de carga
|
||||
void Resource::updateLoadingProgress(int steps)
|
||||
{
|
||||
count_.add(1);
|
||||
if (count_.loaded % steps == 0 || count_.loaded == count_.total)
|
||||
{
|
||||
renderProgress();
|
||||
}
|
||||
checkEvents();
|
||||
}
|
||||
Reference in New Issue
Block a user