[Add] Add single-player mode.

This commit is contained in:
Ritchie Cunningham 2025-09-27 00:34:38 +01:00
parent fde6879720
commit 2c01d53b9d
3 changed files with 46 additions and 6 deletions

View File

@ -1,16 +1,25 @@
file(GLOB_RECURSE CLIENT_SOURCES "src/*.cpp") file(GLOB_RECURSE CLIENT_SOURCES "src/*.cpp")
# Explicitly list server sources needed for single-player to avoid main() conflict.
set(SERVER_SOURCES
"../server/src/network_manager.cpp"
"../server/src/player.cpp"
)
add_executable(bettolac add_executable(bettolac
${CLIENT_SOURCES} ${CLIENT_SOURCES}
${SERVER_SOURCES}
) )
find_package(SDL3 REQUIRED) find_package(SDL3 REQUIRED)
find_package(GLEW REQUIRED) find_package(GLEW REQUIRED)
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
find_package(Freetype REQUIRED) find_package(Freetype REQUIRED)
find_package(Threads REQUIRED)
target_link_libraries(bettolac PRIVATE bettola SDL3::SDL3 ${GLEW_LIBRARIES} target_link_libraries(bettolac PRIVATE bettola SDL3::SDL3 ${GLEW_LIBRARIES}
${OPENGL_LIBRARIES} ${FREETYPE_LIBRARIES}) ${OPENGL_LIBRARIES} ${FREETYPE_LIBRARIES} Threads::Threads)
target_include_directories(bettolac PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src target_include_directories(bettolac PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src
# Add server include dir for single player mode.
${CMAKE_SOURCE_DIR}/server/src
${GLEW_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIR} ${FREETYPE_INCLUDE_DIRS}) ${GLEW_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIR} ${FREETYPE_INCLUDE_DIRS})

View File

@ -4,14 +4,46 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_keyboard.h> #include <SDL3/SDL_keyboard.h>
/* For single player mode. */
#include <random>
#include <string>
#include <thread>
#include <chrono>
#include "../../server/src/network_manager.h"
#include "terminal.h" #include "terminal.h"
#include "gfx/shape_renderer.h" #include "gfx/shape_renderer.h"
#include "ui/desktop.h" #include "ui/desktop.h"
#include "ui/ui_window.h" #include "ui/ui_window.h"
void run_server(void) {
try {
NetworkManager server;
server.start(1337);
/*
* Server's start() method is non-blocking, but NetworkManager
* object must be kept alive. We'll just loop forever and let the OS
* clean up the thread when main exits ;)
*/
while(true) {
std::this_thread::sleep_for(std::chrono::seconds(5));
}
} catch(const std::exception& e) {
fprintf(stderr, "Single-player server thread exception: %s\n", e.what());
}
}
const int SCREEN_WIDTH = 1280; const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 720; const int SCREEN_HEIGHT = 720;
int main(int argc, char** argv) { int main(int argc, char** argv) {
if(argc > 1 && std::string(argv[1]) == "-sp") {
fprintf(stdout, "Starting in single-player mode...\n");
std::thread server_thread(run_server);
server_thread.detach();
fprintf(stdout, "Waiting for server initialise...\n");
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
/* Init SDL. */ /* Init SDL. */
if(!SDL_Init(SDL_INIT_VIDEO)) { if(!SDL_Init(SDL_INIT_VIDEO)) {
printf("SDL could not initialise! SDL_ERROR: %s\n", SDL_GetError()); printf("SDL could not initialise! SDL_ERROR: %s\n", SDL_GetError());

View File

@ -4,7 +4,6 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <SDL3/SDL_events.h> #include <SDL3/SDL_events.h>
#include <sstream> #include <sstream>
#include <algorithm>
#include "terminal.h" #include "terminal.h"
#include "client_network.h" #include "client_network.h"