[Fix] Separate single-player and multiplayer ports.

This commit is contained in:
Ritchie Cunningham 2025-10-04 05:20:50 +01:00
parent e7607e3fc0
commit 9d2a2f4195
6 changed files with 40 additions and 15 deletions

View File

@ -5,10 +5,9 @@
#include <chrono> #include <chrono>
#include "gfx/types.h" #include "gfx/types.h"
#include "net/constants.h"
#include "network_manager.h" #include "network_manager.h"
#include "game_state.h" #include "game_state.h"
#include "gfx/shape_renderer.h"
#include "gfx/txt_renderer.h"
#include "terminal.h" #include "terminal.h"
#include "ui/desktop.h" #include "ui/desktop.h"
#include "ui/i_window_content.h" #include "ui/i_window_content.h"
@ -29,7 +28,7 @@ void GameState::_init_desktop(void) {
void GameState::_run_server(void) { void GameState::_run_server(void) {
try { try {
NetworkManager server; NetworkManager server;
server.start(1337); server.start(SINGLE_PLAYER_PORT);
/* /*
* Server's start() method is non-blocking, but NetworkManager * Server's start() method is non-blocking, but NetworkManager
* object must be kept alive. We'll just loop forever and let the OS * 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) : GameState::GameState(void) :
_current_screen(Screen::MAIN_MENU), _current_screen(Screen::MAIN_MENU),
_screen_width(0), _screen_width(0),
_screen_height(0) {} _screen_height(0),
_is_single_player(false) {}
GameState::~GameState(void) = default; GameState::~GameState(void) = default;
@ -60,6 +60,7 @@ void GameState::init(int screen_width, int screen_height) {
} }
void GameState::start_single_player_now(int screen_width, int screen_height) { void GameState::start_single_player_now(int screen_width, int screen_height) {
_is_single_player = true;
_screen_width = screen_width; _screen_width = screen_width;
_screen_height = screen_height; _screen_height = screen_height;
fprintf(stdout, "Starting in single-player mode...\n"); fprintf(stdout, "Starting in single-player mode...\n");
@ -68,7 +69,7 @@ void GameState::start_single_player_now(int screen_width, int screen_height) {
std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::this_thread::sleep_for(std::chrono::milliseconds(200));
_network = std::make_unique<ClientNetwork>(); _network = std::make_unique<ClientNetwork>();
if(!_network->connect("127.0.0.1", 1337)) { if(!_network->connect("127.0.0.1", SINGLE_PLAYER_PORT)) {
/* TODO: Handle connection failure. */ /* TODO: Handle connection failure. */
} }
_current_screen = Screen::DESKTOP; _current_screen = Screen::DESKTOP;
@ -99,6 +100,10 @@ void GameState::update(void) {
if(!_main_menu) break; if(!_main_menu) break;
Screen next_screen = _main_menu->update(); Screen next_screen = _main_menu->update();
if(next_screen != Screen::MAIN_MENU) { 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; _current_screen = next_screen;
_main_menu.reset(); /* Free mem. */ _main_menu.reset(); /* Free mem. */
@ -112,14 +117,19 @@ void GameState::update(void) {
if(!_boot_sequence) break; /* Shouldn't happen. */ if(!_boot_sequence) break; /* Shouldn't happen. */
if(_boot_sequence->is_finished()) { if(_boot_sequence->is_finished()) {
/* Connect to server. */ /* Connect to server. */
if(_is_single_player) {
fprintf(stdout, "Starting in single-player mode...\n"); fprintf(stdout, "Starting in single-player mode...\n");
std::thread server_thread(&GameState::_run_server, this); std::thread server_thread(&GameState::_run_server, this);
server_thread.detach(); server_thread.detach();
std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::this_thread::sleep_for(std::chrono::milliseconds(200));
if(!_network->connect("127.0.0.1", SINGLE_PLAYER_PORT)) {
if(!_network->connect("127.0.0.1", 1337)) {
/* TODO: Handle connection failure. */ /* TODO: Handle connection failure. */
} }
} else {
if(!_network->connect("127.0.0.1", MULTIPLAYER_PORT)) {
/* TODO: Handle connection failure. */
}
}
_current_screen = Screen::DESKTOP; _current_screen = Screen::DESKTOP;
_init_desktop(); _init_desktop();
_boot_sequence.reset(); /* Free mem. */ _boot_sequence.reset(); /* Free mem. */

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <string>
#include "gfx/types.h" #include "gfx/types.h"
@ -37,6 +38,7 @@ private:
Screen _current_screen; Screen _current_screen;
int _screen_width; int _screen_width;
int _screen_height; int _screen_height;
bool _is_single_player;
void _init_desktop(void); void _init_desktop(void);
void _run_server(void); void _run_server(void);

View File

@ -43,12 +43,14 @@ MainMenu::MainMenu(int screen_width, int screen_height) :
"Single-Player", "Single-Player",
{ center_x, center_y + 30, button_width, button_height }, { center_x, center_y + 30, button_width, button_height },
Screen::BOOTING, /* This will trigger the booting screen. */ Screen::BOOTING, /* This will trigger the booting screen. */
true,
false false
}); });
_buttons.push_back({ _buttons.push_back({
"Online", "Online",
{ center_x, center_y - 30, button_width, button_height }, { center_x, center_y - 30, button_width, button_height },
Screen::BOOTING, Screen::BOOTING,
false,
false false
}); });
@ -80,6 +82,7 @@ void MainMenu::handle_event(SDL_Event* event) {
for(const auto& button : _buttons) { for(const auto& button : _buttons) {
if(button.is_hovered) { if(button.is_hovered) {
_next_screen = button.action; _next_screen = button.action;
_clicked_button = &button;
break; /* Once clicked button found, exit. */ break; /* Once clicked button found, exit. */
} }
} }

View File

@ -13,6 +13,7 @@ struct MenuButton {
std::string label; std::string label;
Rect rect; Rect rect;
Screen action; /* Change state. */ Screen action; /* Change state. */
bool is_single_player;
bool is_hovered = false; bool is_hovered = false;
}; };
@ -25,6 +26,8 @@ public:
Screen update(void); Screen update(void);
void render(UIRenderer* ui_renderer); void render(UIRenderer* ui_renderer);
const MenuButton* get_clicked_button(void) const { return _clicked_button; }
private: private:
void _update_background(void); void _update_background(void);
void _render_background(TextRenderer* txdt_renderer); void _render_background(TextRenderer* txdt_renderer);
@ -45,4 +48,5 @@ private:
std::vector<MenuButton> _buttons; std::vector<MenuButton> _buttons;
std::vector<std::string> _snippets; std::vector<std::string> _snippets;
Screen _next_screen; Screen _next_screen;
const MenuButton* _clicked_button = nullptr;
}; };

View File

@ -0,0 +1,5 @@
#pragma once
#include <cstdint>
const uint16_t MULTIPLAYER_PORT = 1337;
const uint16_t SINGLE_PLAYER_PORT = 1338;

View File

@ -4,13 +4,14 @@
#include <chrono> #include <chrono>
#include "network_manager.h" #include "network_manager.h"
#include "net/constants.h"
int main(int argc, char** argv) { int main(int argc, char** argv) {
try { try {
/* We'll keep main thread alive while server runs in background. */ /* We'll keep main thread alive while server runs in background. */
NetworkManager server; NetworkManager server;
server.start(1337); server.start(MULTIPLAYER_PORT);
while(true) { while(true) {
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
} }