[Fix] Separate single-player and multiplayer ports.
This commit is contained in:
parent
e7607e3fc0
commit
9d2a2f4195
@ -5,10 +5,9 @@
|
||||
#include <chrono>
|
||||
|
||||
#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<ClientNetwork>();
|
||||
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();
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#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);
|
||||
|
||||
@ -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. */
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<MenuButton> _buttons;
|
||||
std::vector<std::string> _snippets;
|
||||
Screen _next_screen;
|
||||
const MenuButton* _clicked_button = nullptr;
|
||||
};
|
||||
|
||||
5
common/src/net/constants.h
Normal file
5
common/src/net/constants.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
const uint16_t MULTIPLAYER_PORT = 1337;
|
||||
const uint16_t SINGLE_PLAYER_PORT = 1338;
|
||||
@ -4,13 +4,14 @@
|
||||
#include <chrono>
|
||||
|
||||
#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));
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user