Precàrrega de tots els recursos al inici del joc

8.000.000 de cherrypickings que he anat fent pel codi
This commit is contained in:
2024-10-20 11:06:10 +02:00
parent f23dcae5b6
commit a4b4e188cd
32 changed files with 532 additions and 364 deletions

View File

@@ -1,5 +1,7 @@
#include <iostream>
#include "resource.h"
#include "asset.h"
#include "screen.h"
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Resource *Resource::resource_ = nullptr;
@@ -25,8 +27,13 @@ Resource *Resource::get()
// Constructor
Resource::Resource()
{
std::cout << "** LOADING RESOURCES" << std::endl;
loadSounds();
loadMusics();
loadTextures();
loadTextFiles();
loadAnimations();
std::cout << "\n** RESOURCES LOADED" << std::endl;
}
// Destructor
@@ -34,9 +41,12 @@ Resource::~Resource()
{
sounds_.clear();
musics_.clear();
textures_.clear();
text_files_.clear();
animations_.clear();
}
// Obtiene el fichero de sonido a partir de un nombre
// Obtiene el sonido a partir de un nombre
JA_Sound_t *Resource::getSound(const std::string &name)
{
for (const auto &s : sounds_)
@@ -46,10 +56,11 @@ JA_Sound_t *Resource::getSound(const std::string &name)
return s.sound;
}
}
return nullptr;
std::cerr << "Error: Sonido no encontrado " << name << std::endl;
throw std::runtime_error("Sonido no encontrado: " + name);
}
// Obtiene el fichero de música a partir de un nombre
// Obtiene la música a partir de un nombre
JA_Music_t *Resource::getMusic(const std::string &name)
{
for (const auto &m : musics_)
@@ -59,12 +70,57 @@ JA_Music_t *Resource::getMusic(const std::string &name)
return m.music;
}
}
return nullptr;
std::cerr << "Error: Música no encontrada " << name << std::endl;
throw std::runtime_error("Música no encontrada: " + name);
}
// Carga los sonidos del juego
// Obtiene la textura a partir de un nombre
std::shared_ptr<Texture> Resource::getTexture(const std::string &name)
{
for (const auto &t : textures_)
{
if (t.name == name)
{
return t.texture;
}
}
std::cerr << "Error: Imagen no encontrada " << name << std::endl;
throw std::runtime_error("Imagen no encontrada: " + name);
}
// Obtiene el fichero de texto a partir de un nombre
std::shared_ptr<TextFile> Resource::getTextFile(const std::string &name)
{
for (const auto &t : text_files_)
{
if (t.name == name)
{
return t.text_file;
}
}
std::cerr << "Error: TextFile no encontrado " << name << std::endl;
throw std::runtime_error("TextFile no encontrado: " + name);
}
// Obtiene la animación a partir de un nombre
Animations &Resource::getAnimation(const std::string &name)
{
for (auto &a : animations_)
{
if (a.name == name)
{
return a.animation;
}
}
std::cerr << "Error: Animación no encontrada " << name << std::endl;
throw std::runtime_error("Animación no encontrada: " + name);
}
// Carga los sonidos
void Resource::loadSounds()
{
std::cout << "\n>> SOUND FILES" << std::endl;
// Obtiene la lista con las rutas a los ficheros de sonidos
auto list = Asset::get()->getListByType(AssetType::SOUND);
sounds_.clear();
@@ -77,13 +133,16 @@ void Resource::loadSounds()
// Obtiene la subcadena desde el último '/'
auto name = l.substr(last_index);
sounds_.emplace_back(ResourceSound{name, JA_LoadSound(l.c_str())});
sounds_.emplace_back(ResourceSound(name, JA_LoadSound(l.c_str())));
printWithDots("Sound : ", name, "[ LOADED ]");
}
}
// Carga las musicas del juego
// Carga las musicas
void Resource::loadMusics()
{
std::cout << "\n>> MUSIC FILES" << std::endl;
// Obtiene la lista con las rutas a los ficheros musicales
auto list = Asset::get()->getListByType(AssetType::MUSIC);
musics_.clear();
@@ -96,6 +155,68 @@ void Resource::loadMusics()
// Obtiene la subcadena desde el último '/'
auto name = l.substr(last_index);
musics_.emplace_back(ResourceMusic{name, JA_LoadMusic(l.c_str())});
musics_.emplace_back(ResourceMusic(name, JA_LoadMusic(l.c_str())));
printWithDots("Music : ", name, "[ LOADED ]");
}
}
// Carga las texturas
void Resource::loadTextures()
{
std::cout << "\n>> TEXTURES" << std::endl;
// Obtiene la lista con las rutas a los ficheros png
auto list = Asset::get()->getListByType(AssetType::BITMAP);
textures_.clear();
for (const auto &l : list)
{
// Encuentra el último índice de '/'
auto last_index = l.find_last_of('/') + 1;
// Obtiene la subcadena desde el último '/'
auto name = l.substr(last_index);
textures_.emplace_back(ResourceTexture(name, std::make_shared<Texture>(Screen::get()->getRenderer(), l)));
}
}
// Carga los ficheros de texto
void Resource::loadTextFiles()
{
std::cout << "\n>> TEXT FILES" << std::endl;
// Obtiene la lista con las rutas a los ficheros png
auto list = Asset::get()->getListByType(AssetType::FONT);
text_files_.clear();
for (const auto &l : list)
{
// Encuentra el último índice de '/'
auto last_index = l.find_last_of('/') + 1;
// Obtiene la subcadena desde el último '/'
auto name = l.substr(last_index);
text_files_.emplace_back(ResourceTextFile(name, loadTextFile(l)));
}
}
// Carga las animaciones
void Resource::loadAnimations()
{
std::cout << "\n>> ANIMATIONS" << std::endl;
// Obtiene la lista con las rutas a los ficheros ani
auto list = Asset::get()->getListByType(AssetType::ANIMATION);
animations_.clear();
for (const auto &l : list)
{
// Encuentra el último índice de '/'
auto last_index = l.find_last_of('/') + 1;
// Obtiene la subcadena desde el último '/'
auto name = l.substr(last_index);
animations_.emplace_back(ResourceAnimation(name, loadAnimationsFromFile(l)));
}
}