[Client] Handles disconnection correctly now.

This commit is contained in:
Quentin Bazin 2020-02-22 02:56:55 +09:00
parent 88f3fc1475
commit 884562cb36
4 changed files with 14 additions and 7 deletions

View File

@ -32,7 +32,7 @@ class Client;
class PauseMenuState : public InterfaceState { class PauseMenuState : public InterfaceState {
public: public:
PauseMenuState(gk::ApplicationState *parent = nullptr); PauseMenuState(Client &client, gk::ApplicationState *parent = nullptr);
void onEvent(const SDL_Event &event) override; void onEvent(const SDL_Event &event) override;
@ -42,6 +42,8 @@ class PauseMenuState : public InterfaceState {
void draw(gk::RenderTarget &target, gk::RenderStates states) const override; void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
MenuWidget m_menuWidget{1, 4}; MenuWidget m_menuWidget{1, 4};
Client &m_client;
}; };
#endif // PAUSEMENUSTATE_HPP_ #endif // PAUSEMENUSTATE_HPP_

View File

@ -64,6 +64,8 @@ void Client::disconnect() {
sf::Packet packet; sf::Packet packet;
packet << Network::Command::ClientDisconnect; packet << Network::Command::ClientDisconnect;
m_tcpSocket->send(packet); m_tcpSocket->send(packet);
m_tcpSocket->disconnect();
} }
void Client::send(sf::Packet &packet) { void Client::send(sf::Packet &packet) {

View File

@ -68,6 +68,9 @@ GameState::GameState(const std::string &host, int port) {
} }
void GameState::onEvent(const SDL_Event &event) { void GameState::onEvent(const SDL_Event &event) {
if (event.type == SDL_QUIT)
m_client.disconnect();
if (&m_stateStack->top() == this) { if (&m_stateStack->top() == this) {
gk::KeyboardHandler *keyboardHandler = (gk::KeyboardHandler *)gk::GamePad::getInputHandler(); gk::KeyboardHandler *keyboardHandler = (gk::KeyboardHandler *)gk::GamePad::getInputHandler();
@ -80,7 +83,7 @@ void GameState::onEvent(const SDL_Event &event) {
} }
} }
else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) { else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
m_stateStack->push<PauseMenuState>(this); m_stateStack->push<PauseMenuState>(m_client, this);
} }
else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == keyboardHandler->getKeyCode(GameKey::Chat)) { else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == keyboardHandler->getKeyCode(GameKey::Chat)) {
m_stateStack->push<ChatState>(m_clientCommandHandler, m_hud.chat(), this); m_stateStack->push<ChatState>(m_clientCommandHandler, m_hud.chat(), this);
@ -88,7 +91,7 @@ void GameState::onEvent(const SDL_Event &event) {
else if (event.type == SDL_WINDOWEVENT) { else if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
// FIXME // FIXME
// m_stateStack->push<PauseMenuState>(this); // m_stateStack->push<PauseMenuState>(m_client, this);
gk::Mouse::setCursorGrabbed(false); gk::Mouse::setCursorGrabbed(false);
gk::Mouse::setCursorVisible(true); gk::Mouse::setCursorVisible(true);

View File

@ -31,8 +31,8 @@
#include "SettingsMenuState.hpp" #include "SettingsMenuState.hpp"
#include "TitleScreenState.hpp" #include "TitleScreenState.hpp"
PauseMenuState::PauseMenuState(gk::ApplicationState *parent) PauseMenuState::PauseMenuState(Client &client, gk::ApplicationState *parent)
: InterfaceState(parent) : InterfaceState(parent), m_client(client)
{ {
gk::Mouse::setCursorGrabbed(false); gk::Mouse::setCursorGrabbed(false);
gk::Mouse::setCursorVisible(true); gk::Mouse::setCursorVisible(true);
@ -52,14 +52,14 @@ PauseMenuState::PauseMenuState(gk::ApplicationState *parent)
}); });
m_menuWidget.addButton("Title Screen", [this] (TextButton &) { m_menuWidget.addButton("Title Screen", [this] (TextButton &) {
// m_client.disconnect(); m_client.disconnect();
m_stateStack->clear(); m_stateStack->clear();
m_stateStack->push<TitleScreenState>(); m_stateStack->push<TitleScreenState>();
}); });
m_menuWidget.addButton("Exit", [this] (TextButton &) { m_menuWidget.addButton("Exit", [this] (TextButton &) {
// m_client.disconnect(); m_client.disconnect();
while(!m_stateStack->empty()) while(!m_stateStack->empty())
m_stateStack->pop(); m_stateStack->pop();