116 lines
3.7 KiB
C++
116 lines
3.7 KiB
C++
#include "core/system/director.hpp"
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <utility>
|
|
|
|
#include "core/rendering/screen.hpp"
|
|
#include "core/resources/resource_cache.hpp"
|
|
#include "core/scene/scene.hpp"
|
|
#include "core/system/event_buffer.hpp"
|
|
#include "game/options.hpp"
|
|
#include "game/scenes/game_scene.hpp"
|
|
|
|
Director::Director() {
|
|
if (!SDL_Init(0)) {
|
|
std::fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
|
|
}
|
|
|
|
// Registrem SDL_Quit ABANS de tocar cap singleton: així el handler atexit
|
|
// s'executa DESPRÉS dels destructors estàtics de Screen i Cache (atexit
|
|
// registrat abans de la construcció d'un objecte estàtic corre després
|
|
// del seu destructor). Sense açò, Screen::~Screen cridaria SDL_Destroy*
|
|
// amb SDL ja apagat → caos de destrucció clàssic.
|
|
std::atexit(+[] { SDL_Quit(); });
|
|
|
|
// Prepara la carpeta de sistema (~/.config/jailgames/aee_2026/) i carrega
|
|
// les opcions; si no existeix config.yaml el crea amb els defaults.
|
|
createSystemFolder();
|
|
Options::setConfigFile(system_folder_ + "config.yaml");
|
|
if (!Options::loadFromFile()) {
|
|
std::cerr << "Director: no s'ha pogut carregar ni desar config.yaml a " << system_folder_ << '\n';
|
|
}
|
|
|
|
// Forcem la construcció dels singletons en l'ordre desitjat: primer
|
|
// Screen (necessari per a renderitzar), després Cache. La destrucció
|
|
// estàtica anirà LIFO: Cache primer, Screen últim.
|
|
Screen::get().configure(Options::buildScreenConfig());
|
|
(void)Resource::Cache::get();
|
|
|
|
current_scene_ = std::make_unique<GameScene>();
|
|
last_ticks_ms_ = SDL_GetTicks();
|
|
}
|
|
|
|
Director::~Director() {
|
|
current_scene_.reset();
|
|
// SDL_Quit corre via atexit; els singletons via destrucció estàtica.
|
|
}
|
|
|
|
void Director::createSystemFolder() {
|
|
// Volem la config a $XDG_CONFIG_HOME/jailgames/aee_2026/ (defecte
|
|
// ~/.config/jailgames/aee_2026/). SDL_GetPrefPath apunta a XDG_DATA_HOME
|
|
// (.local/share), que és el lloc per a dades, no per a configuració.
|
|
const char* xdg = std::getenv("XDG_CONFIG_HOME");
|
|
std::string base;
|
|
if (xdg != nullptr && *xdg != '\0') {
|
|
base = xdg;
|
|
} else {
|
|
const char* home = std::getenv("HOME");
|
|
base = (home != nullptr) ? std::string(home) + "/.config" : ".";
|
|
}
|
|
system_folder_ = base + "/jailgames/aee_2026/";
|
|
|
|
std::error_code error;
|
|
std::filesystem::create_directories(system_folder_, error);
|
|
if (error) {
|
|
std::cerr << "Director: no s'ha pogut crear " << system_folder_ << ": " << error.message() << '\n';
|
|
}
|
|
}
|
|
|
|
void Director::processEvents() {
|
|
const bool QUIT_REQUESTED = std::ranges::any_of(
|
|
EventBuffer::events,
|
|
[](const SDL_Event& event) { return event.type == SDL_EVENT_QUIT; });
|
|
if (QUIT_REQUESTED) { current_scene_.reset(); }
|
|
}
|
|
|
|
void Director::switchSceneIfRequested() {
|
|
if (!current_scene_) { return; }
|
|
if (auto next = current_scene_->nextScene()) {
|
|
current_scene_ = std::move(next);
|
|
}
|
|
}
|
|
|
|
auto Director::iterate() -> SDL_AppResult {
|
|
processEvents();
|
|
|
|
if (!current_scene_) {
|
|
EventBuffer::events.clear();
|
|
return SDL_APP_SUCCESS;
|
|
}
|
|
|
|
const std::uint64_t NOW_MS = SDL_GetTicks();
|
|
int delta_ms = static_cast<int>(NOW_MS - last_ticks_ms_);
|
|
delta_ms = std::max(delta_ms, 0);
|
|
last_ticks_ms_ = NOW_MS;
|
|
|
|
current_scene_->update(delta_ms);
|
|
current_scene_->render();
|
|
|
|
if (current_scene_->wantsQuit()) {
|
|
EventBuffer::events.clear();
|
|
return SDL_APP_SUCCESS;
|
|
}
|
|
|
|
switchSceneIfRequested();
|
|
|
|
EventBuffer::events.clear();
|
|
return SDL_APP_CONTINUE;
|
|
}
|