From 14bfabee0de422f0206054c10daa744e5fec4384 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Tue, 25 Feb 2020 22:11:02 +0900 Subject: [PATCH] [GameState|InterfaceState] Now closing the game correctly on SDL_QUIT event. --- client/source/states/ChatState.cpp | 6 ++++-- client/source/states/GameState.cpp | 9 ++++++--- client/source/states/InterfaceState.cpp | 13 ++++++++++--- client/source/states/PauseMenuState.cpp | 7 +++---- client/source/states/SettingsMenuState.cpp | 2 +- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/client/source/states/ChatState.cpp b/client/source/states/ChatState.cpp index 8e81da520..66f684101 100644 --- a/client/source/states/ChatState.cpp +++ b/client/source/states/ChatState.cpp @@ -74,7 +74,8 @@ void ChatState::onEvent(const SDL_Event &event) { m_chat.setMessageVisibility(false); - m_stateStack->pop(); + if (!m_stateStack->empty()) + m_stateStack->pop(); } if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN) { @@ -87,7 +88,8 @@ void ChatState::onEvent(const SDL_Event &event) { m_chat.setMessageVisibility(false); - m_stateStack->pop(); + if (!m_stateStack->empty()) + m_stateStack->pop(); } } diff --git a/client/source/states/GameState.cpp b/client/source/states/GameState.cpp index e7dfe37f2..2844c5a7c 100644 --- a/client/source/states/GameState.cpp +++ b/client/source/states/GameState.cpp @@ -71,10 +71,13 @@ GameState::GameState(const std::string &host, int port) { } void GameState::onEvent(const SDL_Event &event) { - if (event.type == SDL_QUIT) + if (event.type == SDL_QUIT) { m_client.disconnect(); - if (&m_stateStack->top() == this) { + m_stateStack->clear(); + } + + if (!m_stateStack->empty() && &m_stateStack->top() == this) { gk::KeyboardHandler *keyboardHandler = (gk::KeyboardHandler *)gk::GamePad::getInputHandler(); if (event.type == SDL_MOUSEMOTION) { @@ -130,7 +133,7 @@ void GameState::update() { // FIXME: Registry init and TextureAtlas building should be done during loading phase if (m_clientCommandHandler.isRegistryInitialized()) { if (m_textureAtlas->isReady()) { - if (&m_stateStack->top() == this) { + if (!m_stateStack->empty() && &m_stateStack->top() == this) { m_player.processInputs(); if (gk::GamePad::isKeyPressedOnce(GameKey::Inventory)) { diff --git a/client/source/states/InterfaceState.cpp b/client/source/states/InterfaceState.cpp index 78c0aa641..1682ab5c7 100644 --- a/client/source/states/InterfaceState.cpp +++ b/client/source/states/InterfaceState.cpp @@ -26,6 +26,8 @@ */ #include +#include + #include "Config.hpp" #include "InterfaceState.hpp" @@ -50,10 +52,15 @@ void InterfaceState::setup() { } void InterfaceState::onEvent(const SDL_Event &event) { + if (m_parent) { + m_parent->onEvent(event); + } + else if (event.type == SDL_QUIT) { + m_stateStack->clear(); + } + if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - if (m_parent) - m_parent->onEvent(event); - else { + if (!m_parent) { Config::screenWidth = event.window.data1; Config::screenHeight = event.window.data2; } diff --git a/client/source/states/PauseMenuState.cpp b/client/source/states/PauseMenuState.cpp index 6f4f99ef8..af0b84d7a 100644 --- a/client/source/states/PauseMenuState.cpp +++ b/client/source/states/PauseMenuState.cpp @@ -65,8 +65,7 @@ PauseMenuState::PauseMenuState(Client &client, gk::ApplicationState *parent) m_menuWidget.addButton("Exit", [this] (TextButton &) { m_client.disconnect(); - while(!m_stateStack->empty()) - m_stateStack->pop(); + m_stateStack->clear(); }); } @@ -74,11 +73,11 @@ void PauseMenuState::onEvent(const SDL_Event &event) { InterfaceState::onEvent(event); if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - if (&m_stateStack->top() != this) + if (!m_stateStack->empty() && &m_stateStack->top() != this) m_menuWidget.onEvent(event); } - if (&m_stateStack->top() == this) { + if (!m_stateStack->empty() && &m_stateStack->top() == this) { m_menuWidget.onEvent(event); if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) { diff --git a/client/source/states/SettingsMenuState.cpp b/client/source/states/SettingsMenuState.cpp index c250f0a6a..fc79cf0fe 100644 --- a/client/source/states/SettingsMenuState.cpp +++ b/client/source/states/SettingsMenuState.cpp @@ -61,7 +61,7 @@ void SettingsMenuState::onEvent(const SDL_Event &event) { m_menuWidget.onEvent(event); } - if (&m_stateStack->top() == this) { + if (!m_stateStack->empty() && &m_stateStack->top() == this) { m_menuWidget.onEvent(event); m_doneButton.onEvent(event);