ajustant el jugador
This commit is contained in:
@@ -30,7 +30,7 @@ namespace Resource {
|
||||
Cache* Cache::cache = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto cache con esta función estática
|
||||
void Cache::init() { Cache::cache = new Cache(); }
|
||||
void Cache::init(LoadingMode mode) { Cache::cache = new Cache(mode); }
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto cache con esta función estática
|
||||
void Cache::destroy() { delete Cache::cache; }
|
||||
@@ -39,8 +39,8 @@ namespace Resource {
|
||||
auto Cache::get() -> Cache* { return Cache::cache; }
|
||||
|
||||
// Constructor
|
||||
Cache::Cache()
|
||||
: loading_text_(Screen::get()->getText()) {
|
||||
Cache::Cache(LoadingMode mode)
|
||||
: loading_mode_(mode), loading_text_(Screen::get()->getText()) {
|
||||
load();
|
||||
}
|
||||
|
||||
@@ -59,18 +59,25 @@ namespace Resource {
|
||||
void Cache::load() {
|
||||
// Nota: el overlay de debug (RenderInfo) se inicializa después de esta carga,
|
||||
// por lo que updateZoomFactor() se llamará correctamente en RenderInfo::init().
|
||||
calculateTotal();
|
||||
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
std::cout << "\n** LOADING RESOURCES" << '\n';
|
||||
loadSounds();
|
||||
loadMusics();
|
||||
loadSurfaces();
|
||||
loadPalettes();
|
||||
loadTextFiles();
|
||||
loadAnimations();
|
||||
loadRooms();
|
||||
createText();
|
||||
std::cout << "\n** RESOURCES LOADED" << '\n';
|
||||
if (loading_mode_ == LoadingMode::EAGER) {
|
||||
calculateTotal();
|
||||
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
std::cout << "\n** LOADING RESOURCES" << '\n';
|
||||
loadSounds();
|
||||
loadMusics();
|
||||
loadSurfaces();
|
||||
loadPalettes();
|
||||
loadTextFiles();
|
||||
loadAnimations();
|
||||
loadRooms();
|
||||
createText();
|
||||
std::cout << "\n** RESOURCES LOADED" << '\n';
|
||||
} else {
|
||||
std::cout << "\n** LAZY LOADING MODE **\n";
|
||||
initLazyStubs();
|
||||
createText(); // Carga fuentes bajo demanda a través de los getters lazy
|
||||
std::cout << "\n** RESOURCE STUBS READY" << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
// Recarga todos los recursos
|
||||
@@ -80,10 +87,13 @@ namespace Resource {
|
||||
}
|
||||
|
||||
// Obtiene el sonido a partir de un nombre
|
||||
auto Cache::getSound(const std::string& name) -> JA_Sound_t* { // NOLINT(readability-convert-member-functions-to-static)
|
||||
auto Cache::getSound(const std::string& name) -> JA_Sound_t* {
|
||||
auto it = std::ranges::find_if(sounds_, [&name](const auto& s) -> bool { return s.name == name; });
|
||||
|
||||
if (it != sounds_.end()) {
|
||||
if (loading_mode_ == LoadingMode::LAZY && it->sound == nullptr) {
|
||||
loadSoundByName(name);
|
||||
}
|
||||
return it->sound;
|
||||
}
|
||||
|
||||
@@ -92,10 +102,13 @@ namespace Resource {
|
||||
}
|
||||
|
||||
// Obtiene la música a partir de un nombre
|
||||
auto Cache::getMusic(const std::string& name) -> JA_Music_t* { // NOLINT(readability-convert-member-functions-to-static)
|
||||
auto Cache::getMusic(const std::string& name) -> JA_Music_t* {
|
||||
auto it = std::ranges::find_if(musics_, [&name](const auto& m) -> bool { return m.name == name; });
|
||||
|
||||
if (it != musics_.end()) {
|
||||
if (loading_mode_ == LoadingMode::LAZY && it->music == nullptr) {
|
||||
loadMusicByName(name);
|
||||
}
|
||||
return it->music;
|
||||
}
|
||||
|
||||
@@ -104,10 +117,13 @@ namespace Resource {
|
||||
}
|
||||
|
||||
// Obtiene la surface a partir de un nombre
|
||||
auto Cache::getSurface(const std::string& name) -> std::shared_ptr<Surface> { // NOLINT(readability-convert-member-functions-to-static)
|
||||
auto Cache::getSurface(const std::string& name) -> std::shared_ptr<Surface> {
|
||||
auto it = std::ranges::find_if(surfaces_, [&name](const auto& t) -> bool { return t.name == name; });
|
||||
|
||||
if (it != surfaces_.end()) {
|
||||
if (loading_mode_ == LoadingMode::LAZY && it->surface == nullptr) {
|
||||
loadSurfaceByName(name);
|
||||
}
|
||||
return it->surface;
|
||||
}
|
||||
|
||||
@@ -116,10 +132,13 @@ namespace Resource {
|
||||
}
|
||||
|
||||
// Obtiene la paleta a partir de un nombre
|
||||
auto Cache::getPalette(const std::string& name) -> Palette { // NOLINT(readability-convert-member-functions-to-static)
|
||||
auto Cache::getPalette(const std::string& name) -> Palette {
|
||||
auto it = std::ranges::find_if(palettes_, [&name](const auto& t) -> bool { return t.name == name; });
|
||||
|
||||
if (it != palettes_.end()) {
|
||||
if (loading_mode_ == LoadingMode::LAZY && !it->loaded) {
|
||||
loadPaletteByName(name);
|
||||
}
|
||||
return it->palette;
|
||||
}
|
||||
|
||||
@@ -128,10 +147,13 @@ namespace Resource {
|
||||
}
|
||||
|
||||
// Obtiene el fichero de texto a partir de un nombre
|
||||
auto Cache::getTextFile(const std::string& name) -> std::shared_ptr<Text::File> { // NOLINT(readability-convert-member-functions-to-static)
|
||||
auto Cache::getTextFile(const std::string& name) -> std::shared_ptr<Text::File> {
|
||||
auto it = std::ranges::find_if(text_files_, [&name](const auto& t) -> bool { return t.name == name; });
|
||||
|
||||
if (it != text_files_.end()) {
|
||||
if (loading_mode_ == LoadingMode::LAZY && it->text_file == nullptr) {
|
||||
loadTextFileByName(name);
|
||||
}
|
||||
return it->text_file;
|
||||
}
|
||||
|
||||
@@ -152,10 +174,13 @@ namespace Resource {
|
||||
}
|
||||
|
||||
// Obtiene los datos de animación parseados a partir de un nombre
|
||||
auto Cache::getAnimationData(const std::string& name) -> const AnimationResource& { // NOLINT(readability-convert-member-functions-to-static)
|
||||
auto Cache::getAnimationData(const std::string& name) -> const AnimationResource& {
|
||||
auto it = std::ranges::find_if(animations_, [&name](const auto& a) -> bool { return a.name == name; });
|
||||
|
||||
if (it != animations_.end()) {
|
||||
if (loading_mode_ == LoadingMode::LAZY && it->yaml_data.empty()) {
|
||||
loadAnimationByName(name);
|
||||
}
|
||||
return *it;
|
||||
}
|
||||
|
||||
@@ -164,10 +189,13 @@ namespace Resource {
|
||||
}
|
||||
|
||||
// Obtiene la habitación a partir de un nombre
|
||||
auto Cache::getRoom(const std::string& name) -> std::shared_ptr<Room::Data> { // NOLINT(readability-convert-member-functions-to-static)
|
||||
auto Cache::getRoom(const std::string& name) -> std::shared_ptr<Room::Data> {
|
||||
auto it = std::ranges::find_if(rooms_, [&name](const auto& r) -> bool { return r.name == name; });
|
||||
|
||||
if (it != rooms_.end()) {
|
||||
if (loading_mode_ == LoadingMode::LAZY && it->room == nullptr) {
|
||||
loadRoomByName(name);
|
||||
}
|
||||
return it->room;
|
||||
}
|
||||
|
||||
@@ -205,6 +233,11 @@ namespace Resource {
|
||||
|
||||
// Obtiene todas las habitaciones
|
||||
auto Cache::getRooms() -> std::vector<RoomResource>& {
|
||||
if (loading_mode_ == LoadingMode::LAZY) {
|
||||
for (auto& r : rooms_) {
|
||||
if (r.room == nullptr) { loadRoomByName(r.name); }
|
||||
}
|
||||
}
|
||||
return rooms_;
|
||||
}
|
||||
|
||||
@@ -530,4 +563,144 @@ namespace Resource {
|
||||
checkEvents();
|
||||
}
|
||||
|
||||
// --- Modo lazy: stubs y cargadores bajo demanda ---------------------------
|
||||
|
||||
// Rellena los vectores de recursos con entradas name-only (sin contenido)
|
||||
void Cache::initLazyStubs() {
|
||||
sounds_.clear();
|
||||
musics_.clear();
|
||||
surfaces_.clear();
|
||||
palettes_.clear();
|
||||
text_files_.clear();
|
||||
animations_.clear();
|
||||
rooms_.clear();
|
||||
|
||||
for (const auto& l : List::get()->getListByType(List::Type::SOUND)) {
|
||||
sounds_.emplace_back(SoundResource{.name = getFileName(l), .sound = nullptr});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::MUSIC)) {
|
||||
musics_.emplace_back(MusicResource{.name = getFileName(l), .music = nullptr});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::BITMAP)) {
|
||||
surfaces_.emplace_back(SurfaceResource{.name = getFileName(l), .surface = nullptr});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::PALETTE)) {
|
||||
palettes_.emplace_back(ResourcePalette{.name = getFileName(l)});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::FONT)) {
|
||||
text_files_.emplace_back(TextFileResource{.name = getFileName(l), .text_file = nullptr});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::ANIMATION)) {
|
||||
animations_.emplace_back(AnimationResource{.name = getFileName(l), .yaml_data = {}});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::ROOM)) {
|
||||
rooms_.emplace_back(RoomResource{.name = getFileName(l), .room = nullptr});
|
||||
}
|
||||
}
|
||||
|
||||
void Cache::loadSoundByName(const std::string& name) {
|
||||
auto it = std::ranges::find_if(sounds_, [&name](const auto& s) { return s.name == name; });
|
||||
if (it == sounds_.end()) { return; }
|
||||
auto path = List::get()->get(name);
|
||||
try {
|
||||
auto bytes = Helper::loadFile(path);
|
||||
JA_Sound_t* sound = nullptr;
|
||||
if (!bytes.empty()) { sound = JA_LoadSound(bytes.data(), static_cast<Uint32>(bytes.size())); }
|
||||
if (sound == nullptr) { sound = JA_LoadSound(path.c_str()); }
|
||||
if (sound == nullptr) { throw std::runtime_error("Failed to decode audio file"); }
|
||||
it->sound = sound;
|
||||
std::cout << "[lazy] Sound loaded: " << name << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
throwLoadError("SOUND", path, e);
|
||||
}
|
||||
}
|
||||
|
||||
void Cache::loadMusicByName(const std::string& name) {
|
||||
auto it = std::ranges::find_if(musics_, [&name](const auto& m) { return m.name == name; });
|
||||
if (it == musics_.end()) { return; }
|
||||
auto path = List::get()->get(name);
|
||||
try {
|
||||
auto bytes = Helper::loadFile(path);
|
||||
JA_Music_t* music = nullptr;
|
||||
if (!bytes.empty()) { music = JA_LoadMusic(bytes.data(), static_cast<Uint32>(bytes.size())); }
|
||||
if (music == nullptr) { music = JA_LoadMusic(path.c_str()); }
|
||||
if (music == nullptr) { throw std::runtime_error("Failed to decode music file"); }
|
||||
it->music = music;
|
||||
std::cout << "[lazy] Music loaded: " << name << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
throwLoadError("MUSIC", path, e);
|
||||
}
|
||||
}
|
||||
|
||||
void Cache::loadSurfaceByName(const std::string& name) {
|
||||
auto it = std::ranges::find_if(surfaces_, [&name](const auto& s) { return s.name == name; });
|
||||
if (it == surfaces_.end()) { return; }
|
||||
auto path = List::get()->get(name);
|
||||
try {
|
||||
it->surface = std::make_shared<Surface>(path);
|
||||
it->surface->setTransparentColor(0);
|
||||
// Superficies con color transparente específico (replica el ajuste de loadSurfaces)
|
||||
if (name == "loading_screen_color.gif" || name == "ending1.gif" || name == "ending2.gif" ||
|
||||
name == "ending3.gif" || name == "ending4.gif" || name == "ending5.gif") {
|
||||
it->surface->setTransparentColor();
|
||||
} else if (name == "standard.gif") {
|
||||
it->surface->setTransparentColor(16);
|
||||
}
|
||||
std::cout << "[lazy] Surface loaded: " << name << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
throwLoadError("BITMAP", path, e);
|
||||
}
|
||||
}
|
||||
|
||||
void Cache::loadPaletteByName(const std::string& name) {
|
||||
auto it = std::ranges::find_if(palettes_, [&name](const auto& p) { return p.name == name; });
|
||||
if (it == palettes_.end()) { return; }
|
||||
auto path = List::get()->get(name);
|
||||
try {
|
||||
it->palette = readPalFile(path);
|
||||
it->loaded = true;
|
||||
std::cout << "[lazy] Palette loaded: " << name << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
throwLoadError("PALETTE", path, e);
|
||||
}
|
||||
}
|
||||
|
||||
void Cache::loadTextFileByName(const std::string& name) {
|
||||
auto it = std::ranges::find_if(text_files_, [&name](const auto& t) { return t.name == name; });
|
||||
if (it == text_files_.end()) { return; }
|
||||
auto path = List::get()->get(name);
|
||||
try {
|
||||
it->text_file = Text::loadTextFile(path);
|
||||
std::cout << "[lazy] TextFile loaded: " << name << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
throwLoadError("FONT", path, e);
|
||||
}
|
||||
}
|
||||
|
||||
void Cache::loadAnimationByName(const std::string& name) {
|
||||
auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; });
|
||||
if (it == animations_.end()) { return; }
|
||||
auto path = List::get()->get(name);
|
||||
try {
|
||||
auto bytes = Helper::loadFile(path);
|
||||
if (bytes.empty()) { throw std::runtime_error("File is empty or could not be loaded"); }
|
||||
it->yaml_data = bytes;
|
||||
std::cout << "[lazy] Animation loaded: " << name << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
throwLoadError("ANIMATION", path, e);
|
||||
}
|
||||
}
|
||||
|
||||
void Cache::loadRoomByName(const std::string& name) {
|
||||
auto it = std::ranges::find_if(rooms_, [&name](const auto& r) { return r.name == name; });
|
||||
if (it == rooms_.end()) { return; }
|
||||
auto path = List::get()->get(name);
|
||||
try {
|
||||
it->room = std::make_shared<Room::Data>(Room::loadYAML(path));
|
||||
std::cout << "[lazy] Room loaded: " << name << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
throwLoadError("ROOM", path, e);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Resource
|
||||
|
||||
@@ -11,9 +11,14 @@ namespace Resource {
|
||||
|
||||
class Cache {
|
||||
public:
|
||||
static void init(); // Inicialización singleton
|
||||
static void destroy(); // Destrucción singleton
|
||||
static auto get() -> Cache*; // Acceso al singleton
|
||||
enum class LoadingMode {
|
||||
EAGER, // Carga todos los recursos en init() (comportamiento por defecto, producción)
|
||||
LAZY // Sólo registra nombres; carga cada recurso la primera vez que se pide (desarrollo)
|
||||
};
|
||||
|
||||
static void init(LoadingMode mode = LoadingMode::EAGER); // Inicialización singleton
|
||||
static void destroy(); // Destrucción singleton
|
||||
static auto get() -> Cache*; // Acceso al singleton
|
||||
|
||||
auto getSound(const std::string& name) -> JA_Sound_t*; // Getters de recursos
|
||||
auto getMusic(const std::string& name) -> JA_Music_t*;
|
||||
@@ -57,6 +62,18 @@ namespace Resource {
|
||||
void loadRooms();
|
||||
void createText();
|
||||
|
||||
// Registro de stubs (modo lazy): sólo nombres, sin contenido
|
||||
void initLazyStubs();
|
||||
|
||||
// Cargadores bajo demanda (modo lazy): cargan un recurso individual por nombre
|
||||
void loadSoundByName(const std::string& name);
|
||||
void loadMusicByName(const std::string& name);
|
||||
void loadSurfaceByName(const std::string& name);
|
||||
void loadPaletteByName(const std::string& name);
|
||||
void loadTextFileByName(const std::string& name);
|
||||
void loadAnimationByName(const std::string& name);
|
||||
void loadRoomByName(const std::string& name);
|
||||
|
||||
// Métodos de limpieza
|
||||
void clear();
|
||||
void clearSounds();
|
||||
@@ -73,9 +90,11 @@ namespace Resource {
|
||||
[[noreturn]] static void throwLoadError(const std::string& asset_type, const std::string& file_path, const std::exception& e);
|
||||
|
||||
// Constructor y destructor
|
||||
Cache();
|
||||
explicit Cache(LoadingMode mode);
|
||||
~Cache() = default;
|
||||
|
||||
LoadingMode loading_mode_ = LoadingMode::EAGER;
|
||||
|
||||
// Singleton instance
|
||||
static Cache* cache;
|
||||
|
||||
|
||||
@@ -33,8 +33,9 @@ struct SurfaceResource {
|
||||
|
||||
// Estructura para almacenar objetos Palette y su nombre
|
||||
struct ResourcePalette {
|
||||
std::string name; // Nombre de la surface
|
||||
Palette palette{}; // Paleta
|
||||
std::string name; // Nombre de la surface
|
||||
Palette palette{}; // Paleta
|
||||
bool loaded{false}; // Usado por el modo lazy para saber si ya se ha leído del disco
|
||||
};
|
||||
|
||||
// Estructura para almacenar ficheros TextFile y su nombre
|
||||
|
||||
Reference in New Issue
Block a user