[GameState|InterfaceState] Now closing the game correctly on SDL_QUIT event.

This commit is contained in:
Quentin Bazin 2020-02-25 22:11:02 +09:00
parent fabefd81b4
commit 14bfabee0d
5 changed files with 24 additions and 13 deletions

View File

@ -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();
}
}

View File

@ -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)) {

View File

@ -26,6 +26,8 @@
*/
#include <glm/gtc/matrix_transform.hpp>
#include <gk/core/ApplicationStateStack.hpp>
#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;
}

View File

@ -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) {

View File

@ -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);