Skip to content

Commit

Permalink
[GameState|InterfaceState] Now closing the game correctly on SDL_QUIT…
Browse files Browse the repository at this point in the history
… event.
  • Loading branch information
Unarelith committed Feb 25, 2020
1 parent fabefd8 commit 14bfabe
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
6 changes: 4 additions & 2 deletions client/source/states/ChatState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}

Expand Down
9 changes: 6 additions & 3 deletions client/source/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down
13 changes: 10 additions & 3 deletions client/source/states/InterfaceState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
#include <glm/gtc/matrix_transform.hpp>

#include <gk/core/ApplicationStateStack.hpp>

#include "Config.hpp"
#include "InterfaceState.hpp"

Expand All @@ -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;
}
Expand Down
7 changes: 3 additions & 4 deletions client/source/states/PauseMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,19 @@ 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();
});
}

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) {
Expand Down
2 changes: 1 addition & 1 deletion client/source/states/SettingsMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 14bfabe

Please sign in to comment.