101 lines
2.3 KiB
C++
101 lines
2.3 KiB
C++
#include "resource.h"
|
|
#include "asset.h"
|
|
|
|
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
|
|
Resource *Resource::resource_ = nullptr;
|
|
|
|
// [SINGLETON] Crearemos el objeto screen con esta función estática
|
|
void Resource::init()
|
|
{
|
|
Resource::resource_ = new Resource();
|
|
}
|
|
|
|
// [SINGLETON] Destruiremos el objeto screen con esta función estática
|
|
void Resource::destroy()
|
|
{
|
|
delete Resource::resource_;
|
|
}
|
|
|
|
// [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él
|
|
Resource *Resource::get()
|
|
{
|
|
return Resource::resource_;
|
|
}
|
|
|
|
// Constructor
|
|
Resource::Resource()
|
|
{
|
|
loadSounds();
|
|
loadMusics();
|
|
}
|
|
|
|
// Destructor
|
|
Resource::~Resource()
|
|
{
|
|
sounds_.clear();
|
|
musics_.clear();
|
|
}
|
|
|
|
// Obtiene el fichero de sonido a partir de un nombre
|
|
JA_Sound_t *Resource::getSound(const std::string &name)
|
|
{
|
|
for (const auto &s : sounds_)
|
|
{
|
|
if (s.name == name)
|
|
{
|
|
return s.sound;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
// Obtiene el fichero de música a partir de un nombre
|
|
JA_Music_t *Resource::getMusic(const std::string &name)
|
|
{
|
|
for (const auto &m : musics_)
|
|
{
|
|
if (m.name == name)
|
|
{
|
|
return m.music;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
// Carga los sonidos del juego
|
|
void Resource::loadSounds()
|
|
{
|
|
// Obtiene la lista con las rutas a los ficheros de sonidos
|
|
auto list = Asset::get()->getListByType(AssetType::SOUND);
|
|
sounds_.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);
|
|
|
|
sounds_.emplace_back(ResourceSound{name, JA_LoadSound(l.c_str())});
|
|
}
|
|
}
|
|
|
|
// Carga las musicas del juego
|
|
void Resource::loadMusics()
|
|
{
|
|
// Obtiene la lista con las rutas a los ficheros musicales
|
|
auto list = Asset::get()->getListByType(AssetType::MUSIC);
|
|
musics_.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);
|
|
|
|
musics_.emplace_back(ResourceMusic{name, JA_LoadMusic(l.c_str())});
|
|
}
|
|
} |