pasaeta loca de clang-format (despres m'arrepentiré pero bueno)
This commit is contained in:
@@ -1,20 +1,21 @@
|
||||
#include "resource.h"
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_L...
|
||||
#include <stdlib.h> // Para exit
|
||||
#include <SDL3/SDL.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_L...
|
||||
#include <stdlib.h> // Para exit
|
||||
|
||||
#include <algorithm> // Para find_if
|
||||
#include <array> // Para array
|
||||
#include <stdexcept> // Para runtime_error
|
||||
|
||||
#include "asset.h" // Para Asset, AssetType
|
||||
#include "external/jail_audio.h" // Para JA_DeleteMusic, JA_DeleteSound, JA_...
|
||||
#include "lang.h" // Para getText
|
||||
#include "param.h" // Para Param, param, ParamResource, ParamGame
|
||||
#include "screen.h" // Para Screen
|
||||
#include "text.h" // Para Text, loadTextFile, TextFile (ptr o...
|
||||
#include "asset.h" // Para Asset, AssetType
|
||||
#include "external/jail_audio.h" // Para JA_DeleteMusic, JA_DeleteSound, JA_...
|
||||
#include "lang.h" // Para getText
|
||||
#include "param.h" // Para Param, param, ParamResource, ParamGame
|
||||
#include "screen.h" // Para Screen
|
||||
#include "text.h" // Para Text, loadTextFile, TextFile (ptr o...
|
||||
|
||||
struct JA_Music_t; // lines 11-11
|
||||
struct JA_Sound_t; // lines 12-12
|
||||
struct JA_Music_t; // lines 11-11
|
||||
struct JA_Sound_t; // lines 12-12
|
||||
|
||||
// Singleton
|
||||
Resource *Resource::instance_ = nullptr;
|
||||
@@ -35,8 +36,7 @@ Resource::Resource() : loading_text_(Screen::get()->getText()) { load(); }
|
||||
Resource::~Resource() { clear(); }
|
||||
|
||||
// Vacia todos los vectores de recursos
|
||||
void Resource::clear()
|
||||
{
|
||||
void Resource::clear() {
|
||||
clearSounds();
|
||||
clearMusics();
|
||||
textures_.clear();
|
||||
@@ -47,8 +47,7 @@ void Resource::clear()
|
||||
}
|
||||
|
||||
// Carga todos los recursos del juego y muestra el progreso de carga
|
||||
void Resource::load()
|
||||
{
|
||||
void Resource::load() {
|
||||
// Prepara la gestión del progreso de carga
|
||||
calculateTotalResources();
|
||||
initProgressBar();
|
||||
@@ -59,15 +58,15 @@ void Resource::load()
|
||||
screen->setVSync(false);
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** LOADING RESOURCES");
|
||||
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
|
||||
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
|
||||
@@ -75,28 +74,23 @@ void Resource::load()
|
||||
}
|
||||
|
||||
// Recarga todos los recursos (limpia y vuelve a cargar)
|
||||
void Resource::reload()
|
||||
{
|
||||
void Resource::reload() {
|
||||
clear();
|
||||
load();
|
||||
}
|
||||
|
||||
// Recarga solo las texturas y paletas
|
||||
void Resource::reloadTextures()
|
||||
{
|
||||
void Resource::reloadTextures() {
|
||||
loadTextures();
|
||||
addPalettes();
|
||||
createTextures();
|
||||
}
|
||||
|
||||
// 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)
|
||||
{ return s.name == name; });
|
||||
JA_Sound_t *Resource::getSound(const std::string &name) {
|
||||
auto it = std::find_if(sounds_.begin(), sounds_.end(), [&name](const auto &s) { return s.name == name; });
|
||||
|
||||
if (it != sounds_.end())
|
||||
{
|
||||
if (it != sounds_.end()) {
|
||||
return it->sound;
|
||||
}
|
||||
|
||||
@@ -105,13 +99,10 @@ JA_Sound_t *Resource::getSound(const std::string &name)
|
||||
}
|
||||
|
||||
// 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)
|
||||
{ return m.name == name; });
|
||||
JA_Music_t *Resource::getMusic(const std::string &name) {
|
||||
auto it = std::find_if(musics_.begin(), musics_.end(), [&name](const auto &m) { return m.name == name; });
|
||||
|
||||
if (it != musics_.end())
|
||||
{
|
||||
if (it != musics_.end()) {
|
||||
return it->music;
|
||||
}
|
||||
|
||||
@@ -120,13 +111,10 @@ JA_Music_t *Resource::getMusic(const std::string &name)
|
||||
}
|
||||
|
||||
// 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)
|
||||
{ return t.name == name; });
|
||||
std::shared_ptr<Texture> Resource::getTexture(const std::string &name) {
|
||||
auto it = std::find_if(textures_.begin(), textures_.end(), [&name](const auto &t) { return t.name == name; });
|
||||
|
||||
if (it != textures_.end())
|
||||
{
|
||||
if (it != textures_.end()) {
|
||||
return it->texture;
|
||||
}
|
||||
|
||||
@@ -135,13 +123,10 @@ std::shared_ptr<Texture> Resource::getTexture(const std::string &name)
|
||||
}
|
||||
|
||||
// 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)
|
||||
{ return t.name == name; });
|
||||
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) { return t.name == name; });
|
||||
|
||||
if (it != text_files_.end())
|
||||
{
|
||||
if (it != text_files_.end()) {
|
||||
return it->text_file;
|
||||
}
|
||||
|
||||
@@ -150,13 +135,10 @@ std::shared_ptr<TextFile> Resource::getTextFile(const std::string &name)
|
||||
}
|
||||
|
||||
// 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)
|
||||
{ return t.name == name; });
|
||||
std::shared_ptr<Text> Resource::getText(const std::string &name) {
|
||||
auto it = std::find_if(texts_.begin(), texts_.end(), [&name](const auto &t) { return t.name == name; });
|
||||
|
||||
if (it != texts_.end())
|
||||
{
|
||||
if (it != texts_.end()) {
|
||||
return it->text;
|
||||
}
|
||||
|
||||
@@ -165,13 +147,10 @@ std::shared_ptr<Text> Resource::getText(const std::string &name)
|
||||
}
|
||||
|
||||
// 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)
|
||||
{ return a.name == name; });
|
||||
AnimationsFileBuffer &Resource::getAnimation(const std::string &name) {
|
||||
auto it = std::find_if(animations_.begin(), animations_.end(), [&name](const auto &a) { return a.name == name; });
|
||||
|
||||
if (it != animations_.end())
|
||||
{
|
||||
if (it != animations_.end()) {
|
||||
return it->animation;
|
||||
}
|
||||
|
||||
@@ -180,20 +159,17 @@ AnimationsFileBuffer &Resource::getAnimation(const std::string &name)
|
||||
}
|
||||
|
||||
// Obtiene el fichero con los datos para el modo demostración a partir de un índice
|
||||
DemoData &Resource::getDemoData(int index)
|
||||
{
|
||||
DemoData &Resource::getDemoData(int index) {
|
||||
return demos_.at(index);
|
||||
}
|
||||
|
||||
// Carga los sonidos del juego
|
||||
void Resource::loadSounds()
|
||||
{
|
||||
void Resource::loadSounds() {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> SOUND FILES");
|
||||
auto list = Asset::get()->getListByType(AssetType::SOUND);
|
||||
sounds_.clear();
|
||||
|
||||
for (const auto &l : list)
|
||||
{
|
||||
for (const auto &l : list) {
|
||||
auto name = getFileName(l);
|
||||
updateLoadingProgress(name);
|
||||
sounds_.emplace_back(Resource::ResourceSound(name, JA_LoadSound(l.c_str())));
|
||||
@@ -202,14 +178,12 @@ void Resource::loadSounds()
|
||||
}
|
||||
|
||||
// Carga las músicas del juego
|
||||
void Resource::loadMusics()
|
||||
{
|
||||
void Resource::loadMusics() {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> MUSIC FILES");
|
||||
auto list = Asset::get()->getListByType(AssetType::MUSIC);
|
||||
musics_.clear();
|
||||
|
||||
for (const auto &l : list)
|
||||
{
|
||||
for (const auto &l : list) {
|
||||
auto name = getFileName(l);
|
||||
updateLoadingProgress(name);
|
||||
musics_.emplace_back(Resource::ResourceMusic(name, JA_LoadMusic(l.c_str())));
|
||||
@@ -218,14 +192,12 @@ void Resource::loadMusics()
|
||||
}
|
||||
|
||||
// Carga las texturas del juego
|
||||
void Resource::loadTextures()
|
||||
{
|
||||
void Resource::loadTextures() {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXTURES");
|
||||
auto list = Asset::get()->getListByType(AssetType::BITMAP);
|
||||
textures_.clear();
|
||||
|
||||
for (const auto &l : list)
|
||||
{
|
||||
for (const auto &l : list) {
|
||||
auto name = getFileName(l);
|
||||
updateLoadingProgress(name);
|
||||
textures_.emplace_back(Resource::ResourceTexture(name, std::make_shared<Texture>(Screen::get()->getRenderer(), l)));
|
||||
@@ -233,14 +205,12 @@ void Resource::loadTextures()
|
||||
}
|
||||
|
||||
// Carga los ficheros de texto del juego
|
||||
void Resource::loadTextFiles()
|
||||
{
|
||||
void Resource::loadTextFiles() {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXT FILES");
|
||||
auto list = Asset::get()->getListByType(AssetType::FONT);
|
||||
text_files_.clear();
|
||||
|
||||
for (const auto &l : list)
|
||||
{
|
||||
for (const auto &l : list) {
|
||||
auto name = getFileName(l);
|
||||
updateLoadingProgress(name);
|
||||
text_files_.emplace_back(Resource::ResourceTextFile(name, loadTextFile(l)));
|
||||
@@ -248,14 +218,12 @@ void Resource::loadTextFiles()
|
||||
}
|
||||
|
||||
// Carga las animaciones del juego
|
||||
void Resource::loadAnimations()
|
||||
{
|
||||
void Resource::loadAnimations() {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> ANIMATIONS");
|
||||
auto list = Asset::get()->getListByType(AssetType::ANIMATION);
|
||||
animations_.clear();
|
||||
|
||||
for (const auto &l : list)
|
||||
{
|
||||
for (const auto &l : list) {
|
||||
auto name = getFileName(l);
|
||||
updateLoadingProgress(name);
|
||||
animations_.emplace_back(Resource::ResourceAnimation(name, loadAnimationsFromFile(l)));
|
||||
@@ -263,22 +231,19 @@ void Resource::loadAnimations()
|
||||
}
|
||||
|
||||
// Carga los datos para el modo demostración
|
||||
void Resource::loadDemoData()
|
||||
{
|
||||
void Resource::loadDemoData() {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> DEMO FILES");
|
||||
|
||||
constexpr std::array<const char *, 2> demo_files = {"demo1.bin", "demo2.bin"};
|
||||
|
||||
for (const auto &file : demo_files)
|
||||
{
|
||||
for (const auto &file : demo_files) {
|
||||
updateLoadingProgress(file);
|
||||
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get(file)));
|
||||
}
|
||||
}
|
||||
|
||||
// Añade paletas de colores a las texturas principales
|
||||
void Resource::addPalettes()
|
||||
{
|
||||
void Resource::addPalettes() {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> PALETTES");
|
||||
|
||||
// Paletas para el jugador 1
|
||||
@@ -293,10 +258,8 @@ void Resource::addPalettes()
|
||||
}
|
||||
|
||||
// Crea texturas a partir de textos para mostrar puntuaciones y mensajes
|
||||
void Resource::createTextures()
|
||||
{
|
||||
struct NameAndText
|
||||
{
|
||||
void Resource::createTextures() {
|
||||
struct NameAndText {
|
||||
std::string name;
|
||||
std::string text;
|
||||
|
||||
@@ -317,8 +280,7 @@ void Resource::createTextures()
|
||||
{"game_text_1000000_points", Lang::getText("[GAME_TEXT] 8")}};
|
||||
|
||||
auto text = getText("04b_25");
|
||||
for (const auto &s : strings)
|
||||
{
|
||||
for (const auto &s : strings) {
|
||||
textures_.emplace_back(Resource::ResourceTexture(s.name, text->writeToTexture(s.text, 1, -2)));
|
||||
printWithDots("Texture : ", s.name, "[ DONE ]");
|
||||
}
|
||||
@@ -332,18 +294,15 @@ void Resource::createTextures()
|
||||
{"game_text_game_over", "Game Over"}};
|
||||
|
||||
auto text2 = getText("04b_25_2x");
|
||||
for (const auto &s : strings2X)
|
||||
{
|
||||
for (const auto &s : strings2X) {
|
||||
textures_.emplace_back(Resource::ResourceTexture(s.name, text2->writeToTexture(s.text, 1, -4)));
|
||||
printWithDots("Texture : ", s.name, "[ DONE ]");
|
||||
}
|
||||
}
|
||||
|
||||
// Crea los objetos de texto a partir de los archivos de textura y texto
|
||||
void Resource::createText()
|
||||
{
|
||||
struct ResourceInfo
|
||||
{
|
||||
void Resource::createText() {
|
||||
struct ResourceInfo {
|
||||
std::string key;
|
||||
std::string textureFile;
|
||||
std::string textFile;
|
||||
@@ -368,22 +327,16 @@ void Resource::createText()
|
||||
{"smb2", "smb2.png", "smb2.txt"},
|
||||
{"smb2_grad", "smb2_grad.png", "smb2.txt"}};
|
||||
|
||||
for (const auto &resource : resources)
|
||||
{
|
||||
texts_.emplace_back(Resource::ResourceText(resource.key, std::make_shared<Text>(
|
||||
getTexture(resource.textureFile),
|
||||
getTextFile(resource.textFile))));
|
||||
for (const auto &resource : resources) {
|
||||
texts_.emplace_back(Resource::ResourceText(resource.key, std::make_shared<Text>(getTexture(resource.textureFile), getTextFile(resource.textFile))));
|
||||
printWithDots("Text : ", resource.key, "[ DONE ]");
|
||||
}
|
||||
}
|
||||
|
||||
// Vacía el vector de sonidos y libera la memoria asociada
|
||||
void Resource::clearSounds()
|
||||
{
|
||||
for (auto &sound : sounds_)
|
||||
{
|
||||
if (sound.sound)
|
||||
{
|
||||
void Resource::clearSounds() {
|
||||
for (auto &sound : sounds_) {
|
||||
if (sound.sound) {
|
||||
JA_DeleteSound(sound.sound);
|
||||
sound.sound = nullptr;
|
||||
}
|
||||
@@ -392,12 +345,9 @@ void Resource::clearSounds()
|
||||
}
|
||||
|
||||
// Vacía el vector de músicas y libera la memoria asociada
|
||||
void Resource::clearMusics()
|
||||
{
|
||||
for (auto &music : musics_)
|
||||
{
|
||||
if (music.music)
|
||||
{
|
||||
void Resource::clearMusics() {
|
||||
for (auto &music : musics_) {
|
||||
if (music.music) {
|
||||
JA_DeleteMusic(music.music);
|
||||
music.music = nullptr;
|
||||
}
|
||||
@@ -406,8 +356,7 @@ void Resource::clearMusics()
|
||||
}
|
||||
|
||||
// Calcula el número total de recursos a cargar y reinicia el contador de carga
|
||||
void Resource::calculateTotalResources()
|
||||
{
|
||||
void Resource::calculateTotalResources() {
|
||||
const std::array<AssetType, 6> ASSET_TYPES = {
|
||||
AssetType::SOUND,
|
||||
AssetType::MUSIC,
|
||||
@@ -417,8 +366,7 @@ void Resource::calculateTotalResources()
|
||||
AssetType::DEMODATA};
|
||||
|
||||
size_t total = 0;
|
||||
for (const auto &asset_type : ASSET_TYPES)
|
||||
{
|
||||
for (const auto &asset_type : ASSET_TYPES) {
|
||||
auto list = Asset::get()->getListByType(asset_type);
|
||||
total += list.size();
|
||||
}
|
||||
@@ -427,8 +375,7 @@ void Resource::calculateTotalResources()
|
||||
}
|
||||
|
||||
// Muestra el progreso de carga en pantalla (barra y texto)
|
||||
void Resource::renderProgress()
|
||||
{
|
||||
void Resource::renderProgress() {
|
||||
// Obtiene la pantalla y el renderer
|
||||
auto screen = Screen::get();
|
||||
auto renderer = screen->getRenderer();
|
||||
@@ -462,29 +409,24 @@ void Resource::renderProgress()
|
||||
}
|
||||
|
||||
// Comprueba los eventos durante la carga (permite salir con ESC o cerrar ventana)
|
||||
void Resource::checkEvents()
|
||||
{
|
||||
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)
|
||||
{
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_EVENT_QUIT:
|
||||
exit(0);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
if (event.key.key == SDLK_ESCAPE) {
|
||||
exit(0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el progreso de carga, muestra la barra y procesa eventos
|
||||
void Resource::updateLoadingProgress(std::string name)
|
||||
{
|
||||
void Resource::updateLoadingProgress(std::string name) {
|
||||
loading_resource_name_ = name;
|
||||
loading_count_.increase();
|
||||
updateProgressBar();
|
||||
@@ -493,8 +435,7 @@ void Resource::updateLoadingProgress(std::string name)
|
||||
}
|
||||
|
||||
// Inicializa los rectangulos que definen la barra de progreso
|
||||
void Resource::initProgressBar()
|
||||
{
|
||||
void Resource::initProgressBar() {
|
||||
constexpr float X_PADDING = 20.0f;
|
||||
constexpr float Y_PADDING = 20.0f;
|
||||
constexpr float BAR_HEIGHT = 10.0f;
|
||||
@@ -508,7 +449,6 @@ void Resource::initProgressBar()
|
||||
}
|
||||
|
||||
// Actualiza la barra de estado
|
||||
void Resource::updateProgressBar()
|
||||
{
|
||||
void Resource::updateProgressBar() {
|
||||
loading_full_rect_.w = loading_wired_rect_.w * loading_count_.getPercentage();
|
||||
}
|
||||
Reference in New Issue
Block a user