From 9d2a2f41956542107bd1b459dc0f80fb5b4185e1 Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Sat, 4 Oct 2025 05:20:50 +0100 Subject: [PATCH] [Fix] Separate single-player and multiplayer ports. --- client/src/game_state.cpp | 38 +++++++++++++++++++++++-------------- client/src/game_state.h | 2 ++ client/src/ui/main_menu.cpp | 3 +++ client/src/ui/main_menu.h | 4 ++++ common/src/net/constants.h | 5 +++++ server/src/main.cpp | 3 ++- 6 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 common/src/net/constants.h diff --git a/client/src/game_state.cpp b/client/src/game_state.cpp index 576e9d4..d54a2a2 100644 --- a/client/src/game_state.cpp +++ b/client/src/game_state.cpp @@ -5,10 +5,9 @@ #include #include "gfx/types.h" +#include "net/constants.h" #include "network_manager.h" #include "game_state.h" -#include "gfx/shape_renderer.h" -#include "gfx/txt_renderer.h" #include "terminal.h" #include "ui/desktop.h" #include "ui/i_window_content.h" @@ -29,7 +28,7 @@ void GameState::_init_desktop(void) { void GameState::_run_server(void) { try { NetworkManager server; - server.start(1337); + server.start(SINGLE_PLAYER_PORT); /* * Server's start() method is non-blocking, but NetworkManager * object must be kept alive. We'll just loop forever and let the OS @@ -46,7 +45,8 @@ void GameState::_run_server(void) { GameState::GameState(void) : _current_screen(Screen::MAIN_MENU), _screen_width(0), - _screen_height(0) {} + _screen_height(0), + _is_single_player(false) {} GameState::~GameState(void) = default; @@ -60,15 +60,16 @@ void GameState::init(int screen_width, int screen_height) { } void GameState::start_single_player_now(int screen_width, int screen_height) { - _screen_width = screen_width; - _screen_height = screen_height; + _is_single_player = true; + _screen_width = screen_width; + _screen_height = screen_height; fprintf(stdout, "Starting in single-player mode...\n"); std::thread server_thread(&GameState::_run_server, this); server_thread.detach(); std::this_thread::sleep_for(std::chrono::milliseconds(200)); _network = std::make_unique(); - if(!_network->connect("127.0.0.1", 1337)) { + if(!_network->connect("127.0.0.1", SINGLE_PLAYER_PORT)) { /* TODO: Handle connection failure. */ } _current_screen = Screen::DESKTOP; @@ -99,6 +100,10 @@ void GameState::update(void) { if(!_main_menu) break; Screen next_screen = _main_menu->update(); if(next_screen != Screen::MAIN_MENU) { + const MenuButton* clicked_button = _main_menu->get_clicked_button(); + if(clicked_button) { + _is_single_player = clicked_button->is_single_player; + } _current_screen = next_screen; _main_menu.reset(); /* Free mem. */ @@ -112,13 +117,18 @@ void GameState::update(void) { if(!_boot_sequence) break; /* Shouldn't happen. */ if(_boot_sequence->is_finished()) { /* Connect to server. */ - fprintf(stdout, "Starting in single-player mode...\n"); - std::thread server_thread(&GameState::_run_server, this); - server_thread.detach(); - std::this_thread::sleep_for(std::chrono::milliseconds(200)); - - if(!_network->connect("127.0.0.1", 1337)) { - /* TODO: Handle connection failure. */ + if(_is_single_player) { + fprintf(stdout, "Starting in single-player mode...\n"); + std::thread server_thread(&GameState::_run_server, this); + server_thread.detach(); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + if(!_network->connect("127.0.0.1", SINGLE_PLAYER_PORT)) { + /* TODO: Handle connection failure. */ + } + } else { + if(!_network->connect("127.0.0.1", MULTIPLAYER_PORT)) { + /* TODO: Handle connection failure. */ + } } _current_screen = Screen::DESKTOP; _init_desktop(); diff --git a/client/src/game_state.h b/client/src/game_state.h index cc1d633..318a133 100644 --- a/client/src/game_state.h +++ b/client/src/game_state.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "gfx/types.h" @@ -37,6 +38,7 @@ private: Screen _current_screen; int _screen_width; int _screen_height; + bool _is_single_player; void _init_desktop(void); void _run_server(void); diff --git a/client/src/ui/main_menu.cpp b/client/src/ui/main_menu.cpp index 416c4bf..1319315 100644 --- a/client/src/ui/main_menu.cpp +++ b/client/src/ui/main_menu.cpp @@ -43,12 +43,14 @@ MainMenu::MainMenu(int screen_width, int screen_height) : "Single-Player", { center_x, center_y + 30, button_width, button_height }, Screen::BOOTING, /* This will trigger the booting screen. */ + true, false }); _buttons.push_back({ "Online", { center_x, center_y - 30, button_width, button_height }, Screen::BOOTING, + false, false }); @@ -80,6 +82,7 @@ void MainMenu::handle_event(SDL_Event* event) { for(const auto& button : _buttons) { if(button.is_hovered) { _next_screen = button.action; + _clicked_button = &button; break; /* Once clicked button found, exit. */ } } diff --git a/client/src/ui/main_menu.h b/client/src/ui/main_menu.h index 487dd42..2671b3f 100644 --- a/client/src/ui/main_menu.h +++ b/client/src/ui/main_menu.h @@ -13,6 +13,7 @@ struct MenuButton { std::string label; Rect rect; Screen action; /* Change state. */ + bool is_single_player; bool is_hovered = false; }; @@ -25,6 +26,8 @@ public: Screen update(void); void render(UIRenderer* ui_renderer); + const MenuButton* get_clicked_button(void) const { return _clicked_button; } + private: void _update_background(void); void _render_background(TextRenderer* txdt_renderer); @@ -45,4 +48,5 @@ private: std::vector _buttons; std::vector _snippets; Screen _next_screen; + const MenuButton* _clicked_button = nullptr; }; diff --git a/common/src/net/constants.h b/common/src/net/constants.h new file mode 100644 index 0000000..bc5e997 --- /dev/null +++ b/common/src/net/constants.h @@ -0,0 +1,5 @@ +#pragma once +#include + +const uint16_t MULTIPLAYER_PORT = 1337; +const uint16_t SINGLE_PLAYER_PORT = 1338; diff --git a/server/src/main.cpp b/server/src/main.cpp index e4811a8..ad1788d 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -4,13 +4,14 @@ #include #include "network_manager.h" +#include "net/constants.h" int main(int argc, char** argv) { try { /* We'll keep main thread alive while server runs in background. */ NetworkManager server; - server.start(1337); + server.start(MULTIPLAYER_PORT); while(true) { std::this_thread::sleep_for(std::chrono::seconds(1)); }