Resource: afegida pantalla de progres de càrrega
Screen: Afegit objecte de text propi per a la clase
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
#include "resource.h"
|
||||
#include <SDL3/SDL_log.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_LogError
|
||||
#include <SDL3/SDL.h>
|
||||
#include <algorithm> // Para find_if
|
||||
#include <algorithm> // Para find_if
|
||||
#include <array>
|
||||
#include <stdexcept> // Para runtime_error
|
||||
#include "asset.h" // Para Asset, AssetType
|
||||
#include "jail_audio.h" // Para JA_DeleteMusic, JA_DeleteSound, JA_LoadMusic
|
||||
@@ -24,7 +25,7 @@ void Resource::destroy() { delete Resource::instance_; }
|
||||
Resource *Resource::get() { return Resource::instance_; }
|
||||
|
||||
// Constructor
|
||||
Resource::Resource() { load(); }
|
||||
Resource::Resource() : loading_text_(Screen::get()->getText()) { load(); }
|
||||
|
||||
// Vacia todos los vectores de recursos
|
||||
void Resource::clear()
|
||||
@@ -38,33 +39,43 @@ void Resource::clear()
|
||||
demos_.clear();
|
||||
}
|
||||
|
||||
// Carga todos los recursos
|
||||
// Carga todos los recursos del juego y muestra el progreso de carga
|
||||
void Resource::load()
|
||||
{
|
||||
// Calcula el total de recursos a cargar
|
||||
calculateTotal();
|
||||
Screen::get()->show();
|
||||
|
||||
// Muerstra la ventana y desactiva el sincronismo vertical
|
||||
auto screen = Screen::get();
|
||||
auto vsync = screen->getVSync();
|
||||
screen->show();
|
||||
screen->setVSync(false);
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** LOADING RESOURCES");
|
||||
loadSounds();
|
||||
loadMusics();
|
||||
loadTextures();
|
||||
loadTextFiles();
|
||||
loadAnimations();
|
||||
loadDemoData();
|
||||
addPalettes();
|
||||
createText();
|
||||
createTextures();
|
||||
loadSounds(); // Carga sonidos
|
||||
loadMusics(); // Carga músicas
|
||||
loadTextures(); // Carga texturas
|
||||
loadTextFiles(); // Carga ficheros de texto
|
||||
loadAnimations(); // Carga animaciones
|
||||
loadDemoData(); // Carga datos de demo
|
||||
addPalettes(); // Añade paletas a las texturas
|
||||
createText(); // Crea objetos de texto
|
||||
createTextures(); // Crea texturas a partir de texto
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** RESOURCES LOADED");
|
||||
|
||||
// Restablece el sincronismo vertical a su valor original
|
||||
screen->setVSync(vsync);
|
||||
SDL_Delay(1000);
|
||||
}
|
||||
|
||||
// Recarga todos los recursos
|
||||
// Recarga todos los recursos (limpia y vuelve a cargar)
|
||||
void Resource::reload()
|
||||
{
|
||||
clear();
|
||||
load();
|
||||
}
|
||||
|
||||
// Recarga las texturas
|
||||
// Recarga solo las texturas y paletas
|
||||
void Resource::reloadTextures()
|
||||
{
|
||||
loadTextures();
|
||||
@@ -72,7 +83,7 @@ void Resource::reloadTextures()
|
||||
createTextures();
|
||||
}
|
||||
|
||||
// Obtiene el sonido a partir de un nombre
|
||||
// Obtiene el sonido a partir de un nombre. Lanza excepción si no existe.
|
||||
JA_Sound_t *Resource::getSound(const std::string &name)
|
||||
{
|
||||
auto it = std::find_if(sounds_.begin(), sounds_.end(), [&name](const auto &s)
|
||||
@@ -87,7 +98,7 @@ JA_Sound_t *Resource::getSound(const std::string &name)
|
||||
throw std::runtime_error("Sonido no encontrado: " + name);
|
||||
}
|
||||
|
||||
// Obtiene la música a partir de un nombre
|
||||
// Obtiene la música a partir de un nombre. Lanza excepción si no existe.
|
||||
JA_Music_t *Resource::getMusic(const std::string &name)
|
||||
{
|
||||
auto it = std::find_if(musics_.begin(), musics_.end(), [&name](const auto &m)
|
||||
@@ -102,7 +113,7 @@ JA_Music_t *Resource::getMusic(const std::string &name)
|
||||
throw std::runtime_error("Música no encontrada: " + name);
|
||||
}
|
||||
|
||||
// Obtiene la textura a partir de un nombre
|
||||
// Obtiene la textura a partir de un nombre. Lanza excepción si no existe.
|
||||
std::shared_ptr<Texture> Resource::getTexture(const std::string &name)
|
||||
{
|
||||
auto it = std::find_if(textures_.begin(), textures_.end(), [&name](const auto &t)
|
||||
@@ -117,7 +128,7 @@ std::shared_ptr<Texture> Resource::getTexture(const std::string &name)
|
||||
throw std::runtime_error("Imagen no encontrada: " + name);
|
||||
}
|
||||
|
||||
// Obtiene el fichero de texto a partir de un nombre
|
||||
// Obtiene el fichero de texto a partir de un nombre. Lanza excepción si no existe.
|
||||
std::shared_ptr<TextFile> Resource::getTextFile(const std::string &name)
|
||||
{
|
||||
auto it = std::find_if(text_files_.begin(), text_files_.end(), [&name](const auto &t)
|
||||
@@ -132,7 +143,7 @@ std::shared_ptr<TextFile> Resource::getTextFile(const std::string &name)
|
||||
throw std::runtime_error("TextFile no encontrado: " + name);
|
||||
}
|
||||
|
||||
// Obtiene el objeto de texto a partir de un nombre
|
||||
// Obtiene el objeto de texto a partir de un nombre. Lanza excepción si no existe.
|
||||
std::shared_ptr<Text> Resource::getText(const std::string &name)
|
||||
{
|
||||
auto it = std::find_if(texts_.begin(), texts_.end(), [&name](const auto &t)
|
||||
@@ -147,7 +158,7 @@ std::shared_ptr<Text> Resource::getText(const std::string &name)
|
||||
throw std::runtime_error("Text no encontrado: " + name);
|
||||
}
|
||||
|
||||
// Obtiene la animación a partir de un nombre
|
||||
// Obtiene la animación a partir de un nombre. Lanza excepción si no existe.
|
||||
AnimationsFileBuffer &Resource::getAnimation(const std::string &name)
|
||||
{
|
||||
auto it = std::find_if(animations_.begin(), animations_.end(), [&name](const auto &a)
|
||||
@@ -162,13 +173,13 @@ AnimationsFileBuffer &Resource::getAnimation(const std::string &name)
|
||||
throw std::runtime_error("Animación no encontrada: " + name);
|
||||
}
|
||||
|
||||
// Obtiene el fichero con los datos para el modo demostración a partir de un çindice
|
||||
// Obtiene el fichero con los datos para el modo demostración a partir de un índice
|
||||
DemoData &Resource::getDemoData(int index)
|
||||
{
|
||||
return demos_.at(index);
|
||||
}
|
||||
|
||||
// Carga los sonidos
|
||||
// Carga los sonidos del juego
|
||||
void Resource::loadSounds()
|
||||
{
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> SOUND FILES");
|
||||
@@ -180,11 +191,11 @@ void Resource::loadSounds()
|
||||
auto name = getFileName(l);
|
||||
sounds_.emplace_back(Resource::ResourceSound(name, JA_LoadSound(l.c_str())));
|
||||
printWithDots("Sound : ", name, "[ LOADED ]");
|
||||
updateLoadingProgress();
|
||||
updateLoadingProgress(name);
|
||||
}
|
||||
}
|
||||
|
||||
// Carga las músicas
|
||||
// Carga las músicas del juego
|
||||
void Resource::loadMusics()
|
||||
{
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> MUSIC FILES");
|
||||
@@ -196,11 +207,11 @@ void Resource::loadMusics()
|
||||
auto name = getFileName(l);
|
||||
musics_.emplace_back(Resource::ResourceMusic(name, JA_LoadMusic(l.c_str())));
|
||||
printWithDots("Music : ", name, "[ LOADED ]");
|
||||
updateLoadingProgress();
|
||||
updateLoadingProgress(name);
|
||||
}
|
||||
}
|
||||
|
||||
// Carga las texturas
|
||||
// Carga las texturas del juego
|
||||
void Resource::loadTextures()
|
||||
{
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXTURES");
|
||||
@@ -211,11 +222,11 @@ void Resource::loadTextures()
|
||||
{
|
||||
auto name = getFileName(l);
|
||||
textures_.emplace_back(Resource::ResourceTexture(name, std::make_shared<Texture>(Screen::get()->getRenderer(), l)));
|
||||
updateLoadingProgress();
|
||||
updateLoadingProgress(name);
|
||||
}
|
||||
}
|
||||
|
||||
// Carga los ficheros de texto
|
||||
// Carga los ficheros de texto del juego
|
||||
void Resource::loadTextFiles()
|
||||
{
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXT FILES");
|
||||
@@ -226,11 +237,11 @@ void Resource::loadTextFiles()
|
||||
{
|
||||
auto name = getFileName(l);
|
||||
text_files_.emplace_back(Resource::ResourceTextFile(name, loadTextFile(l)));
|
||||
updateLoadingProgress();
|
||||
updateLoadingProgress(name);
|
||||
}
|
||||
}
|
||||
|
||||
// Carga las animaciones
|
||||
// Carga las animaciones del juego
|
||||
void Resource::loadAnimations()
|
||||
{
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> ANIMATIONS");
|
||||
@@ -241,7 +252,7 @@ void Resource::loadAnimations()
|
||||
{
|
||||
auto name = getFileName(l);
|
||||
animations_.emplace_back(Resource::ResourceAnimation(name, loadAnimationsFromFile(l)));
|
||||
updateLoadingProgress();
|
||||
updateLoadingProgress(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,32 +260,36 @@ void Resource::loadAnimations()
|
||||
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();
|
||||
|
||||
constexpr std::array<const char *, 2> demo_files = {"demo1.bin", "demo2.bin"};
|
||||
|
||||
for (const auto &file : demo_files)
|
||||
{
|
||||
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get(file)));
|
||||
updateLoadingProgress(file);
|
||||
}
|
||||
}
|
||||
|
||||
// Añade paletas a las texturas
|
||||
// Añade paletas de colores a las texturas principales
|
||||
void Resource::addPalettes()
|
||||
{
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> PALETTES");
|
||||
|
||||
// Jugador 1
|
||||
// Paletas para el jugador 1
|
||||
getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_1_coffee_palette.gif"));
|
||||
getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_2_coffee_palette.gif"));
|
||||
getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_invencible_palette.gif"));
|
||||
|
||||
// Jugador 2
|
||||
// Paletas para el jugador 2
|
||||
getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_1_coffee_palette.gif"));
|
||||
getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_2_coffee_palette.gif"));
|
||||
getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_invencible_palette.gif"));
|
||||
|
||||
// Fuentes
|
||||
// Paleta para la fuente SMB2
|
||||
getTexture("smb2.gif")->addPaletteFromFile(Asset::get()->get("smb2_palette1.pal"));
|
||||
}
|
||||
|
||||
// Crea texturas
|
||||
// Crea texturas a partir de textos para mostrar puntuaciones y mensajes
|
||||
void Resource::createTextures()
|
||||
{
|
||||
struct NameAndText
|
||||
@@ -288,7 +303,7 @@ void Resource::createTextures()
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> CREATING TEXTURES");
|
||||
|
||||
// Tamaño normal
|
||||
// Texturas de tamaño normal
|
||||
std::vector<NameAndText> strings = {
|
||||
{"game_text_1000_points", "1.000"},
|
||||
{"game_text_2500_points", "2.500"},
|
||||
@@ -305,7 +320,7 @@ void Resource::createTextures()
|
||||
printWithDots("Texture : ", s.name, "[ DONE ]");
|
||||
}
|
||||
|
||||
// Tamaño doble
|
||||
// Texturas de tamaño doble
|
||||
std::vector<NameAndText> strings2X = {
|
||||
{"game_text_100000_points", "100.000"},
|
||||
{"game_text_get_ready", lang::getText("[GAME_TEXT] 7")},
|
||||
@@ -321,6 +336,7 @@ void Resource::createTextures()
|
||||
}
|
||||
}
|
||||
|
||||
// Crea los objetos de texto a partir de los archivos de textura y texto
|
||||
void Resource::createText()
|
||||
{
|
||||
struct ResourceInfo
|
||||
@@ -351,13 +367,13 @@ void Resource::createText()
|
||||
for (const auto &resource : resources)
|
||||
{
|
||||
texts_.emplace_back(Resource::ResourceText(resource.key, std::make_shared<Text>(
|
||||
getTexture(resource.textureFile),
|
||||
getTextFile(resource.textFile))));
|
||||
getTexture(resource.textureFile),
|
||||
getTextFile(resource.textFile))));
|
||||
printWithDots("Text : ", resource.key, "[ DONE ]");
|
||||
}
|
||||
}
|
||||
|
||||
// Vacía el vector de sonidos
|
||||
// Vacía el vector de sonidos y libera la memoria asociada
|
||||
void Resource::clearSounds()
|
||||
{
|
||||
for (auto &sound : sounds_)
|
||||
@@ -371,7 +387,7 @@ void Resource::clearSounds()
|
||||
sounds_.clear();
|
||||
}
|
||||
|
||||
// Vacía el vector de músicas
|
||||
// Vacía el vector de músicas y libera la memoria asociada
|
||||
void Resource::clearMusics()
|
||||
{
|
||||
for (auto &music : musics_)
|
||||
@@ -385,46 +401,60 @@ void Resource::clearMusics()
|
||||
musics_.clear();
|
||||
}
|
||||
|
||||
// Calcula el numero de recursos para cargar
|
||||
// Calcula el número total de recursos a cargar y reinicia el contador de carga
|
||||
void Resource::calculateTotal()
|
||||
{
|
||||
const std::array<AssetType, 6> ASSET_TYPES = {
|
||||
AssetType::SOUND,
|
||||
AssetType::MUSIC,
|
||||
AssetType::BITMAP,
|
||||
AssetType::FONT,
|
||||
AssetType::ANIMATION,
|
||||
AssetType::DEMODATA};
|
||||
|
||||
size_t total = 0;
|
||||
for (int i = 0; i < static_cast<int>(AssetType::COUNT); ++i)
|
||||
for (const auto &asset_type : ASSET_TYPES)
|
||||
{
|
||||
auto assetType = static_cast<AssetType>(i);
|
||||
auto list = Asset::get()->getListByType(assetType);
|
||||
auto list = Asset::get()->getListByType(asset_type);
|
||||
total += list.size();
|
||||
std::string log = std::to_string(list.size()) + " - " + std::to_string(total);
|
||||
SDL_Log("%s", log.c_str());
|
||||
}
|
||||
|
||||
count_ = ResourceCount(total, 0);
|
||||
loading_count_ = ResourceCount(total);
|
||||
}
|
||||
|
||||
// Muestra el progreso de carga
|
||||
// Muestra el progreso de carga en pantalla (barra y texto)
|
||||
void Resource::renderProgress()
|
||||
{
|
||||
auto screen = Screen::get();
|
||||
auto renderer = screen->getRenderer();
|
||||
|
||||
screen->update();
|
||||
|
||||
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;
|
||||
const float BAR_Y_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_FRect rect_wired = {X_PADDING, BAR_Y_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};
|
||||
const float full_bar_width = wired_bar_width * loading_count_.getPercentage();
|
||||
SDL_FRect rect_full = {X_PADDING, BAR_Y_POSITION, full_bar_width, X_PADDING};
|
||||
SDL_RenderFillRect(renderer, &rect_full);
|
||||
|
||||
loading_text_->write(X_PADDING, BAR_Y_POSITION - 9, "Loading : " + loading_resource_name_);
|
||||
|
||||
screen->render();
|
||||
}
|
||||
|
||||
// Comprueba los eventos de la pantalla de carga
|
||||
// Comprueba los eventos durante la carga (permite salir con ESC o cerrar ventana)
|
||||
void Resource::checkEvents()
|
||||
{
|
||||
SDL_Event event;
|
||||
@@ -445,13 +475,11 @@ void Resource::checkEvents()
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el progreso de carga
|
||||
void Resource::updateLoadingProgress(int steps)
|
||||
// Actualiza el progreso de carga, muestra la barra y procesa eventos
|
||||
void Resource::updateLoadingProgress(std::string name)
|
||||
{
|
||||
count_.add(1);
|
||||
if (count_.loaded % steps == 0 || count_.loaded == count_.total)
|
||||
{
|
||||
renderProgress();
|
||||
}
|
||||
loading_resource_name_ = name;
|
||||
loading_count_.add(1);
|
||||
renderProgress();
|
||||
checkEvents();
|
||||
}
|
||||
Reference in New Issue
Block a user