Skip to content

Commit

Permalink
Fixed crash related to singleplayer games. (see #75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Mar 11, 2020
1 parent 2de04da commit c5510ee
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
6 changes: 5 additions & 1 deletion client/source/network/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ void Client::connect(sf::IpAddress serverAddress, u16 serverPort) {
if (command == Network::Command::ClientRefused)
throw EXCEPTION("Server error: Connection refused. Server probably reached max player amount.");

bool isSingleplayer;
if (command != Network::Command::ClientOk)
throw EXCEPTION("Network error: Expected 'ClientOk' packet.");
answer >> m_id;

answer >> m_id >> isSingleplayer;
if (m_isSingleplayer != isSingleplayer)
throw EXCEPTION("Client error: The server is not valid");

m_tcpSocket->setBlocking(false);
m_socket.setBlocking(false);
Expand Down
2 changes: 2 additions & 0 deletions client/source/network/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ class Client {
void update();

void setCommandCallback(Network::Command command, const CommandCallback &callback) { m_commands[command] = callback; }
void setSingleplayer(bool isSingleplayer) { m_isSingleplayer = isSingleplayer; }

bool isConnected() const { return m_isConnected; }

u16 id() const { return m_id; }

private:
bool m_isConnected = false;
bool m_isSingleplayer = false;

u16 m_id;

Expand Down
22 changes: 9 additions & 13 deletions client/source/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,27 @@
#include "Registry.hpp"
#include "TextureAtlas.hpp"

GameState::GameState(const std::string &host, int port)
GameState::GameState()
: m_textureAtlas(gk::ResourceHandler::getInstance().get<TextureAtlas>("atlas-blocks"))
{
// Set clear color to skyblue
glClearColor(0.196078, 0.6, 0.8, 1.0);

m_camera.setAspectRatio((float)Config::screenWidth / Config::screenHeight);

initShaders();

gk::Mouse::setCursorVisible(false);
gk::Mouse::setCursorGrabbed(true);

// m_playerBoxes.emplace(0, PlayerBox{});
// m_playerBoxes.at(0).setPosition(0, 22, 35);

m_client.connect(host, port);
initShaders();

m_clientCommandHandler.setupCallbacks();

m_camera.setAspectRatio((float)Config::screenWidth / Config::screenHeight);

m_world.setClient(m_clientCommandHandler);
m_player.setClientID(m_client.id());
m_world.setCamera(m_player.camera());
}

void GameState::connect(const std::string &host, int port) {
m_client.connect(host, port);
m_player.setClientID(m_client.id());
}

void GameState::onEvent(const SDL_Event &event) {
if (event.type == SDL_QUIT) {
m_client.disconnect();
Expand Down
9 changes: 7 additions & 2 deletions client/source/states/GameState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class TextureAtlas;

class GameState : public gk::ApplicationState {
public:
GameState(const std::string &host, int port);
GameState();

void connect(const std::string &host, int port);

void onEvent(const SDL_Event &event) override;

Expand All @@ -56,7 +58,10 @@ class GameState : public gk::ApplicationState {
ClientCommandHandler &clientCommandHandler() { return m_clientCommandHandler; }
TextureAtlas &textureAtlas() { return m_textureAtlas; }

void setSingleplayer(bool isSingleplayer) { m_clientCommandHandler.setSingleplayer(isSingleplayer); }
void setSingleplayer(bool isSingleplayer) {
m_clientCommandHandler.setSingleplayer(isSingleplayer);
m_client.setSingleplayer(isSingleplayer);
}

private:
void initShaders();
Expand Down
4 changes: 3 additions & 1 deletion client/source/states/ServerConnectState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ ServerConnectState::ServerConnectState(gk::ApplicationState *parent) : Interface
std::cerr << "Error: Invalid server address." << std::endl;
}

auto &game = m_stateStack->push<GameState>(host, port);
auto &game = m_stateStack->push<GameState>();
game.connect(host, port);

m_stateStack->push<ServerLoadingState>(game, true, this);
});

Expand Down
7 changes: 5 additions & 2 deletions client/source/states/TitleScreenState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,17 @@ void TitleScreenState::startSingleplayer(bool showLoadingState) {

std::this_thread::sleep_for(std::chrono::milliseconds(200));

auto &game = m_stateStack->push<GameState>("localhost", m_port);
auto &game = m_stateStack->push<GameState>();
game.setSingleplayer(true);
game.connect("localhost", m_port);

m_stateStack->push<ServerLoadingState>(game, showLoadingState, this);
}

void TitleScreenState::startMultiplayer(const std::string &host) {
auto &game = m_stateStack->push<GameState>(host, m_port);
auto &game = m_stateStack->push<GameState>();
game.connect(host, m_port);

m_stateStack->push<ServerLoadingState>(game, false, this);
}

Expand Down
2 changes: 1 addition & 1 deletion common/source/network/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Network {
// Client commands
ClientConnect = 0, // <TCP> [NetworkCommand][u16 udp port] (from Client only)
ClientDisconnect = 1, // <TCP> [NetworkCommand] (from Client only)
ClientOk = 2, // <TCP> [NetworkCommand][u16 client id] (from Server only)
ClientOk = 2, // <TCP> [NetworkCommand][u16 client id][bool isSingleplayer] (from Server only)
ClientRefused = 3, // <TCP> [NetworkCommand] (from Server only)

// Input commands
Expand Down
2 changes: 1 addition & 1 deletion server/source/network/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void Server::handleNewConnections() {
m_selector.add(*client.tcpSocket);

sf::Packet outPacket;
outPacket << Network::Command::ClientOk << client.id;
outPacket << Network::Command::ClientOk << client.id << m_isSingleplayer;
client.tcpSocket->send(outPacket);
// client.tcpSocket->setBlocking(false);

Expand Down

0 comments on commit c5510ee

Please sign in to comment.