792 lines
28 KiB
C++
792 lines
28 KiB
C++
#include "resource.h"
|
|
|
|
#include <SDL3/SDL.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_LogError, SDL_SetRenderDrawColor, SDL_EventType, SDL_PollEvent, SDL_RenderFillRect, SDL_RenderRect, SDLK_ESCAPE, SDL_Event
|
|
|
|
#include <algorithm> // Para find_if, max, find
|
|
#include <array> // Para array
|
|
#include <cstdlib> // Para exit
|
|
#include <stdexcept> // Para runtime_error
|
|
#include <utility> // Para move
|
|
|
|
#include "asset.h" // Para Asset
|
|
#include "color.h" // Para Color
|
|
#ifndef NO_AUDIO
|
|
#include "external/jail_audio.h" // Para JA_LoadMusic, JA_LoadSound, JA_DeleteMusic, JA_DeleteSound
|
|
#endif
|
|
#include "lang.h" // Para getText
|
|
#include "param.h" // Para Param, param, ParamResource, ParamGame
|
|
#include "screen.h" // Para Screen
|
|
#include "text.h" // Para Text
|
|
|
|
struct JA_Music_t; // lines 11-11
|
|
struct JA_Sound_t; // lines 12-12
|
|
|
|
// Declaraciones de funciones que necesitas implementar en otros archivos
|
|
|
|
// Singleton
|
|
Resource *Resource::instance = nullptr;
|
|
|
|
// Inicializa la instancia única del singleton con modo de carga
|
|
void Resource::init(LoadingMode mode) {
|
|
Resource::instance = new Resource(mode);
|
|
}
|
|
|
|
// Libera la instancia
|
|
void Resource::destroy() {
|
|
delete Resource::instance;
|
|
Resource::instance = nullptr;
|
|
}
|
|
|
|
// Obtiene la instancia
|
|
auto Resource::get() -> Resource * { return Resource::instance; }
|
|
|
|
// Constructor con modo de carga
|
|
Resource::Resource(LoadingMode mode)
|
|
: loading_mode_(mode), loading_text_(nullptr) {
|
|
if (loading_mode_ == LoadingMode::PRELOAD) {
|
|
loading_text_ = Screen::get()->getText();
|
|
load();
|
|
} else {
|
|
// En modo lazy, cargamos lo mínimo indispensable
|
|
initResourceLists();
|
|
loadEssentialResources();
|
|
}
|
|
}
|
|
|
|
// Destructor
|
|
Resource::~Resource() {
|
|
clear();
|
|
}
|
|
|
|
// Carga los recursos esenciales que siempre se necesitan (modo lazy)
|
|
void Resource::loadEssentialResources() {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading essential resources for lazy mode");
|
|
|
|
// Cargar recursos de texto básicos que se usan para crear texturas
|
|
loadTextFilesQuiet(); // <- VERSIÓN SILENCIOSA
|
|
loadEssentialTextures(); // Ya es silenciosa
|
|
createText(); // Crear objetos de texto
|
|
createTextTextures(); // Crear texturas generadas (game_text_xxx)
|
|
createPlayerTextures(); // Crea las texturas de jugadores con todas sus variantes de paleta
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Essential resources loaded");
|
|
}
|
|
|
|
// Carga los ficheros de texto del juego (versión silenciosa)
|
|
void Resource::loadTextFilesQuiet() {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXT FILES (quiet load)");
|
|
auto list = Asset::get()->getListByType(Asset::Type::FONT);
|
|
|
|
for (const auto &l : list) {
|
|
auto name = getFileName(l);
|
|
// Buscar en nuestra lista y cargar directamente
|
|
auto it = std::find_if(text_files_.begin(), text_files_.end(), [&name](const auto &t) { return t.name == name; });
|
|
if (it != text_files_.end()) {
|
|
it->text_file = Text::loadFile(l);
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Text file loaded: %s", name.c_str());
|
|
}
|
|
}
|
|
}
|
|
|
|
// Carga solo las texturas esenciales (fuentes)
|
|
void Resource::loadEssentialTextures() {
|
|
const std::vector<std::string> ESSENTIAL_TEXTURES = {
|
|
"04b_25.png",
|
|
"04b_25_2x.png",
|
|
"04b_25_metal.png",
|
|
"04b_25_grey.png",
|
|
"04b_25_flat.png",
|
|
"04b_25_reversed.png",
|
|
"04b_25_flat_2x.png",
|
|
"04b_25_reversed_2x.png",
|
|
"8bithud.png",
|
|
"aseprite.png",
|
|
"smb2.png",
|
|
"smb2_grad.png"};
|
|
|
|
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
|
|
|
for (const auto &file : texture_list) {
|
|
auto name = getFileName(file);
|
|
// Solo cargar texturas esenciales
|
|
if (std::find(ESSENTIAL_TEXTURES.begin(), ESSENTIAL_TEXTURES.end(), name) != ESSENTIAL_TEXTURES.end()) {
|
|
// Buscar en nuestra lista y cargar
|
|
auto it = std::find_if(textures_.begin(), textures_.end(), [&name](const auto &t) { return t.name == name; });
|
|
if (it != textures_.end()) {
|
|
it->texture = std::make_shared<Texture>(Screen::get()->getRenderer(), file);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Inicializa las listas de recursos sin cargar el contenido (modo lazy)
|
|
void Resource::initResourceLists() {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Initializing resource lists for lazy loading");
|
|
|
|
// Inicializa lista de sonidos
|
|
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
|
|
sounds_.clear();
|
|
for (const auto &file : sound_list) {
|
|
sounds_.emplace_back(getFileName(file));
|
|
}
|
|
|
|
// Inicializa lista de músicas
|
|
auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
|
|
musics_.clear();
|
|
for (const auto &file : music_list) {
|
|
musics_.emplace_back(getFileName(file));
|
|
}
|
|
|
|
// Inicializa lista de texturas
|
|
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
|
textures_.clear();
|
|
for (const auto &file : texture_list) {
|
|
textures_.emplace_back(getFileName(file));
|
|
}
|
|
|
|
// Inicializa lista de ficheros de texto
|
|
auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
|
|
text_files_.clear();
|
|
for (const auto &file : text_file_list) {
|
|
text_files_.emplace_back(getFileName(file));
|
|
}
|
|
|
|
// Inicializa lista de animaciones
|
|
auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
|
|
animations_.clear();
|
|
for (const auto &file : animation_list) {
|
|
animations_.emplace_back(getFileName(file));
|
|
}
|
|
|
|
// Los demos se cargan directamente sin mostrar progreso (son pocos y pequeños)
|
|
loadDemoDataQuiet();
|
|
|
|
// Inicializa lista de objetos de texto (sin cargar el contenido)
|
|
const std::vector<std::string> TEXT_OBJECTS = {
|
|
"04b_25",
|
|
"04b_25_2x",
|
|
"04b_25_metal",
|
|
"04b_25_grey",
|
|
"04b_25_flat",
|
|
"04b_25_reversed",
|
|
"04b_25_flat_2x",
|
|
"04b_25_reversed_2x",
|
|
"8bithud",
|
|
"aseprite",
|
|
"smb2",
|
|
"smb2_grad"};
|
|
|
|
texts_.clear();
|
|
for (const auto &text_name : TEXT_OBJECTS) {
|
|
texts_.emplace_back(text_name); // Constructor con nullptr por defecto
|
|
}
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Resource lists initialized for lazy loading");
|
|
}
|
|
|
|
// Obtiene el sonido a partir de un nombre (con carga perezosa)
|
|
auto Resource::getSound(const std::string &name) -> JA_Sound_t * {
|
|
auto it = std::find_if(sounds_.begin(), sounds_.end(), [&name](const auto &s) { return s.name == name; });
|
|
|
|
if (it != sounds_.end()) {
|
|
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
|
|
if (loading_mode_ == LoadingMode::LAZY_LOAD && it->sound == nullptr) {
|
|
it->sound = loadSoundLazy(name);
|
|
}
|
|
return it->sound;
|
|
}
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Sonido no encontrado %s", name.c_str());
|
|
throw std::runtime_error("Sonido no encontrado: " + name);
|
|
}
|
|
|
|
// Obtiene la música a partir de un nombre (con carga perezosa)
|
|
auto Resource::getMusic(const std::string &name) -> JA_Music_t * {
|
|
auto it = std::find_if(musics_.begin(), musics_.end(), [&name](const auto &m) { return m.name == name; });
|
|
|
|
if (it != musics_.end()) {
|
|
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
|
|
if (loading_mode_ == LoadingMode::LAZY_LOAD && it->music == nullptr) {
|
|
it->music = loadMusicLazy(name);
|
|
}
|
|
return it->music;
|
|
}
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Música no encontrada %s", name.c_str());
|
|
throw std::runtime_error("Música no encontrada: " + name);
|
|
}
|
|
|
|
// Obtiene la textura a partir de un nombre (con carga perezosa)
|
|
auto Resource::getTexture(const std::string &name) -> std::shared_ptr<Texture> {
|
|
auto it = std::find_if(textures_.begin(), textures_.end(), [&name](const auto &t) { return t.name == name; });
|
|
|
|
if (it != textures_.end()) {
|
|
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
|
|
if (loading_mode_ == LoadingMode::LAZY_LOAD && it->texture == nullptr) {
|
|
it->texture = loadTextureLazy(name);
|
|
}
|
|
return it->texture;
|
|
}
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Imagen no encontrada %s", name.c_str());
|
|
throw std::runtime_error("Imagen no encontrada: " + name);
|
|
}
|
|
|
|
// Obtiene el fichero de texto a partir de un nombre (con carga perezosa)
|
|
auto Resource::getTextFile(const std::string &name) -> std::shared_ptr<Text::File> {
|
|
auto it = std::find_if(text_files_.begin(), text_files_.end(), [&name](const auto &t) { return t.name == name; });
|
|
|
|
if (it != text_files_.end()) {
|
|
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
|
|
if (loading_mode_ == LoadingMode::LAZY_LOAD && it->text_file == nullptr) {
|
|
it->text_file = loadTextFileLazy(name);
|
|
}
|
|
return it->text_file;
|
|
}
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: TextFile no encontrado %s", name.c_str());
|
|
throw std::runtime_error("TextFile no encontrado: " + name);
|
|
}
|
|
|
|
// Obtiene el objeto de texto a partir de un nombre (con carga perezosa)
|
|
auto Resource::getText(const std::string &name) -> std::shared_ptr<Text> {
|
|
auto it = std::find_if(texts_.begin(), texts_.end(), [&name](const auto &t) { return t.name == name; });
|
|
|
|
if (it != texts_.end()) {
|
|
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
|
|
if (loading_mode_ == LoadingMode::LAZY_LOAD && it->text == nullptr) {
|
|
it->text = loadTextLazy(name);
|
|
}
|
|
return it->text;
|
|
}
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Text no encontrado %s", name.c_str());
|
|
throw std::runtime_error("Text no encontrado: " + name);
|
|
}
|
|
|
|
// Obtiene la animación a partir de un nombre (con carga perezosa)
|
|
auto Resource::getAnimation(const std::string &name) -> AnimationsFileBuffer & {
|
|
auto it = std::find_if(animations_.begin(), animations_.end(), [&name](const auto &a) { return a.name == name; });
|
|
|
|
if (it != animations_.end()) {
|
|
// Si está en modo lazy y no se ha cargado aún (vector vacío), lo carga ahora
|
|
if (loading_mode_ == LoadingMode::LAZY_LOAD && it->animation.empty()) {
|
|
it->animation = loadAnimationLazy(name);
|
|
}
|
|
return it->animation;
|
|
}
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Animación no encontrada %s", name.c_str());
|
|
throw std::runtime_error("Animación no encontrada: " + name);
|
|
}
|
|
|
|
// Obtiene el fichero con los datos para el modo demostración a partir de un índice
|
|
auto Resource::getDemoData(int index) -> DemoData & {
|
|
return demos_.at(index);
|
|
}
|
|
|
|
// --- Métodos de carga perezosa ---
|
|
|
|
auto Resource::loadSoundLazy(const std::string &name) -> JA_Sound_t * {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading sound lazily: %s", name.c_str());
|
|
#ifndef NO_AUDIO
|
|
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
|
|
for (const auto &file : sound_list) {
|
|
if (getFileName(file) == name) {
|
|
return JA_LoadSound(file.c_str());
|
|
}
|
|
}
|
|
#endif
|
|
return nullptr;
|
|
}
|
|
|
|
auto Resource::loadMusicLazy(const std::string &name) -> JA_Music_t * {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading music lazily: %s", name.c_str());
|
|
#ifndef NO_AUDIO
|
|
auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
|
|
for (const auto &file : music_list) {
|
|
if (getFileName(file) == name) {
|
|
return JA_LoadMusic(file.c_str());
|
|
}
|
|
}
|
|
#endif
|
|
return nullptr;
|
|
}
|
|
|
|
auto Resource::loadTextureLazy(const std::string &name) -> std::shared_ptr<Texture> {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading texture lazily: %s", name.c_str());
|
|
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
|
for (const auto &file : texture_list) {
|
|
if (getFileName(file) == name) {
|
|
return std::make_shared<Texture>(Screen::get()->getRenderer(), file);
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
auto Resource::loadTextFileLazy(const std::string &name) -> std::shared_ptr<Text::File> {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading text file lazily: %s", name.c_str());
|
|
auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
|
|
for (const auto &file : text_file_list) {
|
|
if (getFileName(file) == name) {
|
|
return Text::loadFile(file);
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
auto Resource::loadTextLazy(const std::string &name) -> std::shared_ptr<Text> {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading text object lazily: %s", name.c_str());
|
|
|
|
// Mapeo de objetos de texto a sus recursos
|
|
struct TextMapping {
|
|
std::string key;
|
|
std::string texture_file;
|
|
std::string text_file;
|
|
};
|
|
|
|
const std::vector<TextMapping> TEXT_MAPPINGS = {
|
|
{"04b_25", "04b_25.png", "04b_25.txt"},
|
|
{"04b_25_2x", "04b_25_2x.png", "04b_25_2x.txt"},
|
|
{"04b_25_metal", "04b_25_metal.png", "04b_25.txt"},
|
|
{"04b_25_grey", "04b_25_grey.png", "04b_25.txt"},
|
|
{"04b_25_flat", "04b_25_flat.png", "04b_25.txt"},
|
|
{"04b_25_reversed", "04b_25_reversed.png", "04b_25.txt"},
|
|
{"04b_25_flat_2x", "04b_25_flat_2x.png", "04b_25_2x.txt"},
|
|
{"04b_25_reversed_2x", "04b_25_reversed_2x.png", "04b_25_2x.txt"},
|
|
{"8bithud", "8bithud.png", "8bithud.txt"},
|
|
{"aseprite", "aseprite.png", "aseprite.txt"},
|
|
{"smb2", "smb2.png", "smb2.txt"},
|
|
{"smb2_grad", "smb2_grad.png", "smb2.txt"}};
|
|
|
|
for (const auto &mapping : TEXT_MAPPINGS) {
|
|
if (mapping.key == name) {
|
|
// Cargar las dependencias automáticamente
|
|
auto texture = getTexture(mapping.texture_file); // Esto cargará la textura si no está cargada
|
|
auto text_file = getTextFile(mapping.text_file); // Esto cargará el archivo de texto si no está cargado
|
|
|
|
if (texture && text_file) {
|
|
return std::make_shared<Text>(texture, text_file);
|
|
}
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
auto Resource::loadAnimationLazy(const std::string &name) -> AnimationsFileBuffer {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading animation lazily: %s", name.c_str());
|
|
auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
|
|
for (const auto &file : animation_list) {
|
|
if (getFileName(file) == name) {
|
|
return loadAnimationsFromFile(file);
|
|
}
|
|
}
|
|
// Si no se encuentra, retorna vector vacío
|
|
return AnimationsFileBuffer{};
|
|
}
|
|
|
|
// Vacia todos los vectores de recursos
|
|
void Resource::clear() {
|
|
#ifndef NO_AUDIO
|
|
clearSounds();
|
|
clearMusics();
|
|
#endif
|
|
textures_.clear();
|
|
text_files_.clear();
|
|
texts_.clear();
|
|
animations_.clear();
|
|
demos_.clear();
|
|
}
|
|
|
|
// Carga todos los recursos del juego y muestra el progreso de carga
|
|
void Resource::load() {
|
|
// Prepara la gestión del progreso de carga
|
|
calculateTotalResources();
|
|
initProgressBar();
|
|
|
|
// Muerstra la ventana y desactiva el sincronismo vertical
|
|
auto *screen = Screen::get();
|
|
auto vsync = Screen::getVSync();
|
|
screen->setVSync(false);
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** LOADING RESOURCES");
|
|
#ifndef NO_AUDIO
|
|
loadSounds(); // Carga sonidos
|
|
loadMusics(); // Carga músicas
|
|
#endif
|
|
loadTextures(); // Carga texturas
|
|
loadTextFiles(); // Carga ficheros de texto
|
|
loadAnimations(); // Carga animaciones
|
|
loadDemoData(); // Carga datos de demo
|
|
createText(); // Crea objetos de texto
|
|
createTextTextures(); // Crea texturas a partir de texto
|
|
createPlayerTextures(); // Crea las texturas de jugadores con todas sus variantes de paleta
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** RESOURCES LOADED");
|
|
|
|
// Restablece el sincronismo vertical a su valor original
|
|
screen->setVSync(vsync);
|
|
}
|
|
|
|
// Recarga todos los recursos (limpia y vuelve a cargar)
|
|
void Resource::reload() {
|
|
clear();
|
|
if (loading_mode_ == LoadingMode::PRELOAD) {
|
|
load();
|
|
} else {
|
|
initResourceLists();
|
|
}
|
|
}
|
|
|
|
// Carga los sonidos del juego
|
|
void Resource::loadSounds() {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> SOUND FILES");
|
|
auto list = Asset::get()->getListByType(Asset::Type::SOUND);
|
|
sounds_.clear();
|
|
|
|
for (const auto &l : list) {
|
|
auto name = getFileName(l);
|
|
updateLoadingProgress(name);
|
|
#ifndef NO_AUDIO
|
|
sounds_.emplace_back(name, JA_LoadSound(l.c_str()));
|
|
#else
|
|
sounds_.emplace_back(name, nullptr);
|
|
#endif
|
|
printWithDots("Sound : ", name, "[ LOADED ]");
|
|
}
|
|
}
|
|
|
|
// Carga las músicas del juego
|
|
void Resource::loadMusics() {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> MUSIC FILES");
|
|
auto list = Asset::get()->getListByType(Asset::Type::MUSIC);
|
|
musics_.clear();
|
|
|
|
for (const auto &l : list) {
|
|
auto name = getFileName(l);
|
|
updateLoadingProgress(name);
|
|
#ifndef NO_AUDIO
|
|
musics_.emplace_back(name, JA_LoadMusic(l.c_str()));
|
|
#else
|
|
musics_.emplace_back(name, nullptr);
|
|
#endif
|
|
printWithDots("Music : ", name, "[ LOADED ]");
|
|
}
|
|
}
|
|
|
|
// Carga las texturas del juego
|
|
void Resource::loadTextures() {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXTURES");
|
|
auto list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
|
textures_.clear();
|
|
|
|
for (const auto &l : list) {
|
|
auto name = getFileName(l);
|
|
updateLoadingProgress(name);
|
|
textures_.emplace_back(name, std::make_shared<Texture>(Screen::get()->getRenderer(), l));
|
|
}
|
|
}
|
|
|
|
// Carga los ficheros de texto del juego
|
|
void Resource::loadTextFiles() {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXT FILES");
|
|
auto list = Asset::get()->getListByType(Asset::Type::FONT);
|
|
text_files_.clear();
|
|
|
|
for (const auto &l : list) {
|
|
auto name = getFileName(l);
|
|
updateLoadingProgress(name);
|
|
text_files_.emplace_back(name, Text::loadFile(l));
|
|
}
|
|
}
|
|
|
|
// Carga las animaciones del juego
|
|
void Resource::loadAnimations() {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> ANIMATIONS");
|
|
auto list = Asset::get()->getListByType(Asset::Type::ANIMATION);
|
|
animations_.clear();
|
|
|
|
for (const auto &l : list) {
|
|
auto name = getFileName(l);
|
|
updateLoadingProgress(name);
|
|
animations_.emplace_back(name, loadAnimationsFromFile(l));
|
|
}
|
|
}
|
|
|
|
// Carga los datos para el modo demostración
|
|
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) {
|
|
updateLoadingProgress(file);
|
|
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get(file)));
|
|
}
|
|
}
|
|
|
|
// Crea las texturas de jugadores con todas sus variantes de paleta
|
|
void Resource::createPlayerTextures() {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> CREATING PLAYER TEXTURES");
|
|
|
|
// Configuración de jugadores y sus paletas
|
|
struct PlayerConfig {
|
|
std::string base_texture;
|
|
std::vector<std::string> palette_files;
|
|
std::string name_prefix;
|
|
};
|
|
|
|
std::vector<PlayerConfig> players = {
|
|
{"player1.gif", {"player1_coffee1.pal", "player1_coffee2.pal", "player1_invencible.pal"}, "player1"},
|
|
{"player2.gif", {"player2_coffee1.pal", "player2_coffee2.pal", "player2_invencible.pal"}, "player2"}};
|
|
|
|
for (const auto &player : players) {
|
|
// Encontrar el archivo original de la textura
|
|
std::string texture_file_path;
|
|
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
|
for (const auto &file : texture_list) {
|
|
if (getFileName(file) == player.base_texture) {
|
|
texture_file_path = file;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Crear variante con paleta original (pal0) - usar la textura ya cargada
|
|
auto base_texture = getTexture(player.base_texture);
|
|
std::string pal0_name = player.name_prefix + "_pal0";
|
|
textures_.emplace_back(pal0_name, base_texture);
|
|
printWithDots("Player Texture : ", pal0_name, "[ DONE ]");
|
|
|
|
// Crear variantes con paletas adicionales - CADA UNA DESDE EL ARCHIVO
|
|
for (size_t i = 0; i < player.palette_files.size(); ++i) {
|
|
// Crear textura completamente nueva desde el archivo
|
|
auto texture_copy = std::make_shared<Texture>(Screen::get()->getRenderer(), texture_file_path);
|
|
|
|
// Añadir todas las paletas
|
|
texture_copy->addPaletteFromPalFile(Asset::get()->get(player.palette_files[0]));
|
|
texture_copy->addPaletteFromPalFile(Asset::get()->get(player.palette_files[1]));
|
|
texture_copy->addPaletteFromPalFile(Asset::get()->get(player.palette_files[2]));
|
|
|
|
// Cambiar a la paleta específica (índice i+1 porque 0 es la original)
|
|
texture_copy->setPalette(i + 1);
|
|
|
|
// Guardar con nombre específico
|
|
std::string variant_name = player.name_prefix + "_pal" + std::to_string(i + 1);
|
|
textures_.emplace_back(variant_name, texture_copy);
|
|
printWithDots("Player Texture : ", variant_name, "[ DONE ]");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Crea texturas a partir de textos para mostrar puntuaciones y mensajes
|
|
void Resource::createTextTextures() {
|
|
struct NameAndText {
|
|
std::string name;
|
|
std::string text;
|
|
|
|
NameAndText(std::string name_init, std::string text_init)
|
|
: name(std::move(name_init)), text(std::move(text_init)) {}
|
|
};
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> CREATING TEXTURES");
|
|
|
|
// Texturas de tamaño normal
|
|
std::vector<NameAndText> strings = {
|
|
{"game_text_1000_points", "1.000"},
|
|
{"game_text_2500_points", "2.500"},
|
|
{"game_text_5000_points", "5.000"},
|
|
{"game_text_powerup", Lang::getText("[GAME_TEXT] 4")},
|
|
{"game_text_one_hit", Lang::getText("[GAME_TEXT] 5")},
|
|
{"game_text_stop", Lang::getText("[GAME_TEXT] 6")},
|
|
{"game_text_1000000_points", Lang::getText("[GAME_TEXT] 8")}};
|
|
|
|
auto text = getText("04b_25");
|
|
for (const auto &s : strings) {
|
|
textures_.emplace_back(s.name, text->writeToTexture(s.text, 1, -2));
|
|
printWithDots("Texture : ", s.name, "[ DONE ]");
|
|
}
|
|
|
|
// Texturas de tamaño doble
|
|
std::vector<NameAndText> strings2_x = {
|
|
{"game_text_100000_points", "100.000"},
|
|
{"game_text_get_ready", Lang::getText("[GAME_TEXT] 7")},
|
|
{"game_text_last_stage", Lang::getText("[GAME_TEXT] 3")},
|
|
{"game_text_congratulations", Lang::getText("[GAME_TEXT] 1")},
|
|
{"game_text_game_over", "Game Over"}};
|
|
|
|
auto text2 = getText("04b_25_2x");
|
|
for (const auto &s : strings2_x) {
|
|
textures_.emplace_back(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 {
|
|
std::string key;
|
|
std::string texture_file;
|
|
std::string text_file;
|
|
|
|
ResourceInfo(std::string k, std::string t_file, std::string txt_file)
|
|
: key(std::move(k)), texture_file(std::move(t_file)), text_file(std::move(txt_file)) {}
|
|
};
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> CREATING TEXT OBJECTS");
|
|
|
|
std::vector<ResourceInfo> resources = {
|
|
{"04b_25", "04b_25.png", "04b_25.txt"},
|
|
{"04b_25_2x", "04b_25_2x.png", "04b_25_2x.txt"},
|
|
{"04b_25_metal", "04b_25_metal.png", "04b_25.txt"},
|
|
{"04b_25_grey", "04b_25_grey.png", "04b_25.txt"},
|
|
{"04b_25_flat", "04b_25_flat.png", "04b_25.txt"},
|
|
{"04b_25_reversed", "04b_25_reversed.png", "04b_25.txt"},
|
|
{"04b_25_flat_2x", "04b_25_flat_2x.png", "04b_25_2x.txt"},
|
|
{"04b_25_reversed_2x", "04b_25_reversed_2x.png", "04b_25_2x.txt"},
|
|
{"8bithud", "8bithud.png", "8bithud.txt"},
|
|
{"aseprite", "aseprite.png", "aseprite.txt"},
|
|
{"smb2", "smb2.png", "smb2.txt"},
|
|
{"smb2_grad", "smb2_grad.png", "smb2.txt"}};
|
|
|
|
for (const auto &resource : resources) {
|
|
texts_.emplace_back(resource.key, std::make_shared<Text>(getTexture(resource.texture_file), getTextFile(resource.text_file)));
|
|
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 != nullptr) {
|
|
#ifndef NO_AUDIO
|
|
JA_DeleteSound(sound.sound);
|
|
#endif
|
|
sound.sound = nullptr;
|
|
}
|
|
}
|
|
sounds_.clear();
|
|
}
|
|
|
|
// Vacía el vector de músicas y libera la memoria asociada
|
|
void Resource::clearMusics() {
|
|
for (auto &music : musics_) {
|
|
if (music.music != nullptr) {
|
|
#ifndef NO_AUDIO
|
|
JA_DeleteMusic(music.music);
|
|
#endif
|
|
music.music = nullptr;
|
|
}
|
|
}
|
|
musics_.clear();
|
|
}
|
|
|
|
// Calcula el número total de recursos a cargar y reinicia el contador de carga
|
|
void Resource::calculateTotalResources() {
|
|
const std::array<Asset::Type, 6> ASSET_TYPES = {
|
|
Asset::Type::SOUND,
|
|
Asset::Type::MUSIC,
|
|
Asset::Type::BITMAP,
|
|
Asset::Type::FONT,
|
|
Asset::Type::ANIMATION,
|
|
Asset::Type::DEMODATA};
|
|
|
|
size_t total = 0;
|
|
for (const auto &asset_type : ASSET_TYPES) {
|
|
auto list = Asset::get()->getListByType(asset_type);
|
|
total += list.size();
|
|
}
|
|
|
|
loading_count_ = ResourceCount(total);
|
|
}
|
|
|
|
// Muestra el progreso de carga en pantalla (barra y texto)
|
|
void Resource::renderProgress() {
|
|
// Obtiene la pantalla y el renderer
|
|
auto *screen = Screen::get();
|
|
auto *renderer = screen->getRenderer();
|
|
|
|
// Actualiza la lógica principal de la pantalla (input, etc.)
|
|
screen->coreUpdate();
|
|
|
|
// Inicia el frame y limpia la pantalla
|
|
screen->start();
|
|
screen->clean();
|
|
|
|
auto color = param.resource.color;
|
|
|
|
// Dibuja el interior de la barra de progreso
|
|
SDL_SetRenderDrawColor(renderer, param.resource.color.r, param.resource.color.g, param.resource.color.b, param.resource.color.a);
|
|
SDL_RenderFillRect(renderer, &loading_full_rect_);
|
|
|
|
// Dibuja el marco de la barra de progreso
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
|
|
SDL_RenderRect(renderer, &loading_wired_rect_);
|
|
|
|
// Escribe el texto de carga encima de la barra
|
|
loading_text_->writeColored(
|
|
loading_wired_rect_.x,
|
|
loading_wired_rect_.y - 9,
|
|
Lang::getText("[RESOURCE] LOADING") + " : " + loading_resource_name_,
|
|
param.resource.color);
|
|
|
|
// Renderiza el frame en pantalla
|
|
screen->coreRender();
|
|
}
|
|
|
|
// Comprueba los eventos durante la carga (permite salir con ESC o cerrar ventana)
|
|
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) {
|
|
exit(0);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Carga los datos para el modo demostración (sin mostrar progreso)
|
|
void Resource::loadDemoDataQuiet() {
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> DEMO FILES (quiet load)");
|
|
|
|
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)));
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Demo file loaded: %s", file);
|
|
}
|
|
}
|
|
|
|
// Inicializa los rectangulos que definen la barra de progreso
|
|
void Resource::initProgressBar() {
|
|
constexpr float X_PADDING = 20.0F;
|
|
constexpr float Y_PADDING = 20.0F;
|
|
constexpr float BAR_HEIGHT = 10.0F;
|
|
const float BAR_Y_POSITION = param.game.height - BAR_HEIGHT - Y_PADDING;
|
|
|
|
const float WIRED_BAR_WIDTH = param.game.width - (X_PADDING * 2);
|
|
loading_wired_rect_ = {X_PADDING, BAR_Y_POSITION, WIRED_BAR_WIDTH, BAR_HEIGHT};
|
|
|
|
const float FULL_BAR_WIDTH = WIRED_BAR_WIDTH * loading_count_.getPercentage();
|
|
loading_full_rect_ = {X_PADDING, BAR_Y_POSITION, FULL_BAR_WIDTH, BAR_HEIGHT};
|
|
}
|
|
|
|
// Actualiza el progreso de carga, muestra la barra y procesa eventos
|
|
void Resource::updateLoadingProgress(std::string name) {
|
|
loading_resource_name_ = name;
|
|
loading_count_.increase();
|
|
updateProgressBar();
|
|
renderProgress();
|
|
checkEvents();
|
|
}
|
|
|
|
// Actualiza la barra de estado
|
|
void Resource::updateProgressBar() {
|
|
loading_full_rect_.w = loading_wired_rect_.w * loading_count_.getPercentage();
|
|
}
|