refactor: migració a l'arquitectura SDL3 Callback API
Substitueix el bucle blocant main() → Director::run() → escena::run() per SDL_AppInit/Iterate/Event/Quit. Cada escena implementa ara iterate() (un frame) i handleEvent() (un event) sota una interfície base Scene. - Director gestiona l'escena activa i les transicions via switchToActiveScene() - Setup/cleanup que estava al voltant del while de run() mogut a ctor/dtor (música de Game/Ending/Ending2, volum de LoadingScreen) - GlobalEvents ja no processa SDL_EVENT_QUIT (ho fa Director::handleEvent) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -163,9 +163,19 @@ Game::Game(Mode mode)
|
||||
|
||||
SceneManager::current = (mode_ == Mode::GAME) ? SceneManager::Scene::GAME : SceneManager::Scene::DEMO;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
|
||||
// Arranca la música del juego (abans a run())
|
||||
keepMusicPlaying();
|
||||
if (!scoreboard_data_->music && mode_ == Mode::GAME) {
|
||||
Audio::get()->pauseMusic();
|
||||
}
|
||||
}
|
||||
|
||||
Game::~Game() {
|
||||
if (mode_ == Mode::GAME) {
|
||||
Audio::get()->stopMusic();
|
||||
}
|
||||
|
||||
ItemTracker::destroy();
|
||||
|
||||
GameControl::change_player_skin = nullptr;
|
||||
@@ -188,38 +198,35 @@ Game::~Game() {
|
||||
#endif
|
||||
}
|
||||
|
||||
// Comprueba los eventos de la cola
|
||||
void Game::handleEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
GlobalEvents::handle(event);
|
||||
// Despatx d'un event (SDL3 Callback API)
|
||||
void Game::handleEvent(const SDL_Event& event) {
|
||||
GlobalEvents::handle(event);
|
||||
#ifdef _DEBUG
|
||||
// En modo editor: click del ratón cierra la consola
|
||||
if (Console::get()->isActive() && MapEditor::get()->isActive() &&
|
||||
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
Console::get()->toggle();
|
||||
}
|
||||
|
||||
if (!Console::get()->isActive()) {
|
||||
// Tecla 9: toggle editor (funciona tanto dentro como fuera del editor)
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_9 && static_cast<int>(event.key.repeat) == 0) {
|
||||
if (MapEditor::get()->isActive()) {
|
||||
GameControl::exit_editor();
|
||||
Notifier::get()->show({Locale::get()->get("game.editor_disabled")}); // NOLINT(readability-static-accessed-through-instance)
|
||||
} else {
|
||||
GameControl::enter_editor();
|
||||
Notifier::get()->show({Locale::get()->get("game.editor_enabled")}); // NOLINT(readability-static-accessed-through-instance)
|
||||
}
|
||||
} else if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_8 && static_cast<int>(event.key.repeat) == 0 && MapEditor::get()->isActive()) {
|
||||
MapEditor::get()->showGrid(!MapEditor::get()->isGridEnabled());
|
||||
} else if (MapEditor::get()->isActive()) {
|
||||
MapEditor::get()->handleEvent(event);
|
||||
} else {
|
||||
handleDebugEvents(event);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
// En modo editor: click del ratón cierra la consola
|
||||
if (Console::get()->isActive() && MapEditor::get()->isActive() &&
|
||||
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
Console::get()->toggle();
|
||||
}
|
||||
|
||||
if (!Console::get()->isActive()) {
|
||||
// Tecla 9: toggle editor (funciona tanto dentro como fuera del editor)
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_9 && static_cast<int>(event.key.repeat) == 0) {
|
||||
if (MapEditor::get()->isActive()) {
|
||||
GameControl::exit_editor();
|
||||
Notifier::get()->show({Locale::get()->get("game.editor_disabled")}); // NOLINT(readability-static-accessed-through-instance)
|
||||
} else {
|
||||
GameControl::enter_editor();
|
||||
Notifier::get()->show({Locale::get()->get("game.editor_enabled")}); // NOLINT(readability-static-accessed-through-instance)
|
||||
}
|
||||
} else if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_8 && static_cast<int>(event.key.repeat) == 0 && MapEditor::get()->isActive()) {
|
||||
MapEditor::get()->showGrid(!MapEditor::get()->isGridEnabled());
|
||||
} else if (MapEditor::get()->isActive()) {
|
||||
MapEditor::get()->handleEvent(event);
|
||||
} else {
|
||||
handleDebugEvents(event);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Comprueba el teclado
|
||||
@@ -262,29 +269,17 @@ void Game::handleInput() {
|
||||
GlobalInputs::handle();
|
||||
}
|
||||
|
||||
// Bucle para el juego
|
||||
void Game::run() {
|
||||
keepMusicPlaying();
|
||||
if (!scoreboard_data_->music && mode_ == Mode::GAME) {
|
||||
Audio::get()->pauseMusic();
|
||||
}
|
||||
|
||||
while (SceneManager::current == SceneManager::Scene::GAME || SceneManager::current == SceneManager::Scene::DEMO) {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
|
||||
if (mode_ == Mode::GAME) {
|
||||
Audio::get()->stopMusic();
|
||||
}
|
||||
// Un frame de l'escena (SDL3 Callback API)
|
||||
void Game::iterate() {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
|
||||
// Actualiza el juego, las variables, comprueba la entrada, etc.
|
||||
void Game::update() {
|
||||
const float DELTA_TIME = delta_timer_.tick();
|
||||
|
||||
handleEvents(); // Comprueba los eventos
|
||||
handleInput(); // Comprueba las entradas
|
||||
handleInput(); // Comprueba las entradas
|
||||
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->clear();
|
||||
|
||||
Reference in New Issue
Block a user