Passant std::cout a SDL_Log

This commit is contained in:
2025-03-27 19:57:30 +01:00
parent c6288918b2
commit 8afca398e9
27 changed files with 588 additions and 764 deletions

View File

@@ -1,14 +1,14 @@
#include "resource.h"
#include <algorithm> // Para find_if
#include <iostream> // Para basic_ostream, operator<<, endl, cout, cerr
#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_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
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Resource *Resource::resource_ = nullptr;
@@ -52,7 +52,7 @@ void Resource::clear()
// Carga todos los recursos
void Resource::load()
{
std::cout << "** LOADING RESOURCES" << std::endl;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "** LOADING RESOURCES");
loadSounds();
loadMusics();
loadTextures();
@@ -62,7 +62,7 @@ void Resource::load()
addPalettes();
createText();
createTextures();
std::cout << "\n** RESOURCES LOADED" << std::endl;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "** RESOURCES LOADED");
}
// Recarga todos los recursos
@@ -91,7 +91,7 @@ JA_Sound_t *Resource::getSound(const std::string &name)
return it->sound;
}
std::cerr << "Error: Sonido no encontrado " << name << std::endl;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Sonido no encontrado %s", name.c_str());
throw std::runtime_error("Sonido no encontrado: " + name);
}
@@ -106,7 +106,7 @@ JA_Music_t *Resource::getMusic(const std::string &name)
return it->music;
}
std::cerr << "Error: Música no encontrada " << name << std::endl;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Música no encontrada %s", name.c_str());
throw std::runtime_error("Música no encontrada: " + name);
}
@@ -121,7 +121,7 @@ std::shared_ptr<Texture> Resource::getTexture(const std::string &name)
return it->texture;
}
std::cerr << "Error: Imagen no encontrada " << name << std::endl;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Imagen no encontrada %s", name.c_str());
throw std::runtime_error("Imagen no encontrada: " + name);
}
@@ -136,7 +136,7 @@ std::shared_ptr<TextFile> Resource::getTextFile(const std::string &name)
return it->text_file;
}
std::cerr << "Error: TextFile no encontrado " << name << std::endl;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: TextFile no encontrado %s", name.c_str());
throw std::runtime_error("TextFile no encontrado: " + name);
}
@@ -151,7 +151,7 @@ std::shared_ptr<Text> Resource::getText(const std::string &name)
return it->text;
}
std::cerr << "Error: Text no encontrado " << name << std::endl;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Text no encontrado %s", name.c_str());
throw std::runtime_error("Text no encontrado: " + name);
}
@@ -166,7 +166,7 @@ AnimationsFileBuffer &Resource::getAnimation(const std::string &name)
return it->animation;
}
std::cerr << "Error: Animación no encontrada " << name << std::endl;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Animación no encontrada %s", name.c_str());
throw std::runtime_error("Animación no encontrada: " + name);
}
@@ -179,7 +179,7 @@ DemoData &Resource::getDemoData(int index)
// Carga los sonidos
void Resource::loadSounds()
{
std::cout << "\n>> SOUND FILES" << std::endl;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, ">> SOUND FILES");
auto list = Asset::get()->getListByType(AssetType::SOUND);
sounds_.clear();
@@ -187,14 +187,14 @@ void Resource::loadSounds()
{
auto name = getFileName(l);
sounds_.emplace_back(ResourceSound(name, JA_LoadSound(l.c_str())));
printWithDots("Sound : ", name, "[ LOADED ]");
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Sound : %s [ LOADED ]", name.c_str());
}
}
// Carga las musicas
// Carga las músicas
void Resource::loadMusics()
{
std::cout << "\n>> MUSIC FILES" << std::endl;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, ">> MUSIC FILES");
auto list = Asset::get()->getListByType(AssetType::MUSIC);
musics_.clear();
@@ -202,14 +202,14 @@ void Resource::loadMusics()
{
auto name = getFileName(l);
musics_.emplace_back(ResourceMusic(name, JA_LoadMusic(l.c_str())));
printWithDots("Music : ", name, "[ LOADED ]");
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Music : %s [ LOADED ]", name.c_str());
}
}
// Carga las texturas
void Resource::loadTextures()
{
std::cout << "\n>> TEXTURES" << std::endl;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, ">> TEXTURES");
auto list = Asset::get()->getListByType(AssetType::BITMAP);
textures_.clear();
@@ -217,13 +217,14 @@ void Resource::loadTextures()
{
auto name = getFileName(l);
textures_.emplace_back(ResourceTexture(name, std::make_shared<Texture>(Screen::get()->getRenderer(), l)));
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Texture : %s [ LOADED ]", name.c_str());
}
}
// Carga los ficheros de texto
void Resource::loadTextFiles()
{
std::cout << "\n>> TEXT FILES" << std::endl;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, ">> TEXT FILES");
auto list = Asset::get()->getListByType(AssetType::FONT);
text_files_.clear();
@@ -231,13 +232,14 @@ void Resource::loadTextFiles()
{
auto name = getFileName(l);
text_files_.emplace_back(ResourceTextFile(name, loadTextFile(l)));
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "TextFile : %s [ LOADED ]", name.c_str());
}
}
// Carga las animaciones
void Resource::loadAnimations()
{
std::cout << "\n>> ANIMATIONS" << std::endl;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, ">> ANIMATIONS");
auto list = Asset::get()->getListByType(AssetType::ANIMATION);
animations_.clear();
@@ -245,22 +247,25 @@ void Resource::loadAnimations()
{
auto name = getFileName(l);
animations_.emplace_back(ResourceAnimation(name, loadAnimationsFromFile(l)));
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Animation : %s [ LOADED ]", name.c_str());
}
}
// Carga los datos para el modo demostración
void Resource::loadDemoData()
{
std::cout << "\n>> DEMO_FILES" << std::endl;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, ">> DEMO FILES");
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get("demo1.bin")));
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get("demo2.bin")));
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Demo data loaded.");
}
// Añade paletas a las texturas
void Resource::addPalettes()
{
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, ">> PALETTES");
// Jugador 1
std::cout << "\n>> PALETTES" << std::endl;
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"));
@@ -282,43 +287,42 @@ void Resource::createTextures()
std::string name;
std::string text;
// Constructor
NameAndText(const std::string &name_init, const std::string &text_init)
: name(name_init), text(text_init) {}
};
std::cout << "\n>> CREATING TEXTURES" << std::endl;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, ">> CREATING TEXTURES");
// Tamaño normal
std::vector<NameAndText> strings = {
NameAndText("game_text_1000_points", "1.000"),
NameAndText("game_text_2500_points", "2.500"),
NameAndText("game_text_5000_points", "5.000"),
NameAndText("game_text_powerup", lang::getText(117)),
NameAndText("game_text_one_hit", lang::getText(118)),
NameAndText("game_text_stop", lang::getText(119)),
NameAndText("game_text_1000000_points", lang::getText(76))};
{"game_text_1000_points", "1.000"},
{"game_text_2500_points", "2.500"},
{"game_text_5000_points", "5.000"},
{"game_text_powerup", lang::getText(117)},
{"game_text_one_hit", lang::getText(118)},
{"game_text_stop", lang::getText(119)},
{"game_text_1000000_points", lang::getText(76)}};
auto text = getText("04b_25");
for (const auto &s : strings)
{
textures_.emplace_back(ResourceTexture(s.name, text->writeToTexture(s.text, 1, -2)));
printWithDots("Texture : ", s.name, "[ DONE ]");
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Texture : %s [ DONE ]", s.name.c_str());
}
// Tamaño doble
std::vector<NameAndText> strings2X = {
NameAndText("game_text_100000_points", "100.000"),
NameAndText("game_text_get_ready", lang::getText(75)),
NameAndText("game_text_last_stage", lang::getText(79)),
NameAndText("game_text_congratulations", lang::getText(50)),
NameAndText("game_text_game_over", "Game Over")};
{"game_text_100000_points", "100.000"},
{"game_text_get_ready", lang::getText(75)},
{"game_text_last_stage", lang::getText(79)},
{"game_text_congratulations", lang::getText(50)},
{"game_text_game_over", "Game Over"}};
auto text2 = getText("04b_25_2x");
for (const auto &s : strings2X)
{
textures_.emplace_back(ResourceTexture(s.name, text2->writeToTexture(s.text, 1, -4)));
printWithDots("Texture : ", s.name, "[ DONE ]");
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Texture : %s [ DONE ]", s.name.c_str());
}
}
@@ -326,16 +330,15 @@ void Resource::createText()
{
struct ResourceInfo
{
std::string key; // Identificador del recurso
std::string textureFile; // Nombre del archivo de textura
std::string textFile; // Nombre del archivo de texto
std::string key;
std::string textureFile;
std::string textFile;
// Constructor para facilitar la creación de objetos ResourceInfo
ResourceInfo(const std::string &k, const std::string &tFile, const std::string &txtFile)
: key(k), textureFile(tFile), textFile(txtFile) {}
};
std::cout << "\n>> CREATING TEXT_OBJECTS" << std::endl;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, ">> CREATING TEXT OBJECTS");
std::vector<ResourceInfo> resources = {
{"04b_25", "04b_25.png", "04b_25.txt"},
@@ -350,14 +353,13 @@ void Resource::createText()
texts_.emplace_back(ResourceText(resource.key, std::make_shared<Text>(
getTexture(resource.textureFile),
getTextFile(resource.textFile))));
printWithDots("Text : ", resource.key, "[ DONE ]");
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Text : %s [ DONE ]", resource.key.c_str());
}
}
// Vacía el vector de sonidos
void Resource::clearSounds()
{
// Itera sobre el vector y libera los recursos asociados a cada JA_Sound_t
for (auto &sound : sounds_)
{
if (sound.sound)
@@ -366,13 +368,12 @@ void Resource::clearSounds()
sound.sound = nullptr;
}
}
sounds_.clear(); // Limpia el vector después de liberar todos los recursos
sounds_.clear();
}
// Vacía el vector de musicas
// Vacía el vector de músicas
void Resource::clearMusics()
{
// Itera sobre el vector y libera los recursos asociados a cada JA_Music_t
for (auto &music : musics_)
{
if (music.music)
@@ -381,5 +382,5 @@ void Resource::clearMusics()
music.music = nullptr;
}
}
musics_.clear(); // Limpia el vector después de liberar todos los recursos
musics_.clear();
}