From 8b04672d34428f5dca22f3ef7040d714c76a4bd5 Mon Sep 17 00:00:00 2001 From: snit Date: Wed, 2 Oct 2024 15:43:44 -0500 Subject: [PATCH] prepare for daemonisation by splitting into a client/server/common design --- CMakeLists.txt | 36 ++++++--------------------- TODO.gmi | 13 +++++++--- build.sh | 25 +++---------------- common/CMakeLists.txt | 16 ++++++++++++ {src => common/src}/entity/entity.c | 0 {src => common/src}/entity/entity.h | 0 {src => common/src}/entity/registry.c | 0 {src => common/src}/entity/registry.h | 0 {src => common/src}/error.c | 0 {src => common/src}/error.h | 0 {src => common/src}/world.c | 0 {src => common/src}/world.h | 0 server/CMakeLists.txt | 10 ++++++++ {src => server/src}/main.c | 2 +- {src => server/src}/render/render.c | 0 {src => server/src}/render/render.h | 2 +- 16 files changed, 50 insertions(+), 54 deletions(-) create mode 100644 common/CMakeLists.txt rename {src => common/src}/entity/entity.c (100%) rename {src => common/src}/entity/entity.h (100%) rename {src => common/src}/entity/registry.c (100%) rename {src => common/src}/entity/registry.h (100%) rename {src => common/src}/error.c (100%) rename {src => common/src}/error.h (100%) rename {src => common/src}/world.c (100%) rename {src => common/src}/world.h (100%) create mode 100644 server/CMakeLists.txt rename {src => server/src}/main.c (100%) rename {src => server/src}/render/render.c (100%) rename {src => server/src}/render/render.h (79%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f04588..3841672 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,32 +1,12 @@ +# CmakeLists.txt + cmake_minimum_required(VERSION 3.10) +project(simworld VERSION 0.0.1 LANGUAGES C) -# Set the project name and specify the languages -project(simworld LANGUAGES C) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat=2 -Wshadow -Wwrite-strings -Wstrict-prototypes -g") -# Set compiler flags -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat=2 -Wshadow -Wwrite-strings -Wstrict-prototypes") +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -# Specify the source directory -set(SOURCE_DIR ${CMAKE_SOURCE_DIR}/src) - -# Create a list of source files -file(GLOB_RECURSE SOURCES ${SOURCE_DIR}/*.c) - -# Create the executable from the source files -add_executable(${PROJECT_NAME} ${SOURCES}) - -# Dependencies -find_package(PkgConfig REQUIRED) -pkg_check_modules(LUA REQUIRED lua) - -target_link_libraries(${PROJECT_NAME} ${LUA_LIBRARIES}) -target_include_directories(${PROJECT_NAME} PRIVATE ${LUA_INCLUDE_DIRS}) - -# `#define DEBUG` if `cmake -DCMAKE_BUILD_TYPE=debug` -if(CMAKE_BUILD_TYPE STREQUAL "debug") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g") - target_compile_definitions(${PROJECT_NAME} PRIVATE BUILD_DEBUG) -else() - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2") - target_compile_definitions(${PROJECT_NAME} PRIVATE BUILD_RELEASE) -endif() +add_subdirectory(common) +add_subdirectory(server) +# add_subdirectory(client) diff --git a/TODO.gmi b/TODO.gmi index 55080a0..9327e18 100644 --- a/TODO.gmi +++ b/TODO.gmi @@ -1,11 +1,18 @@ # TODO A list of things I'd like to accomplish -* Game loop -* Create and load worlds +## In Progress * Daemonise the serverside * Expose a socket or pipe on the daemon for the client -* Client connection to daemon +* Create client to connect to daemon + +## Completed +* Split into client and server (and common library?) +* Write Makefile to automate compilation + +## Planned +* Game loop +* Create and load worlds * Display environment and pan camera * Time controls (play/pause/speed up) * See creature stats diff --git a/build.sh b/build.sh index 01f931d..d523c5b 100755 --- a/build.sh +++ b/build.sh @@ -4,23 +4,11 @@ set -e run_exe=0 -## Ensure first argument is correct -if [ "$1" = "debug" ]; then - debug=1 -elif [ "$1" = "release" ]; then - debug=0 -else - echo "Run as '$0 debug' or '$0 release'" - exit 1 -fi - -## Ensure second argument is correct -if [ "$2" = "run" ]; then +if [ "$1" = "run" ]; then run_exe=1 fi -## Ensure its run in the right place -if ! [ -f "CMakeLists.txt" ]; then +if ! [ -f ".gitignore" ]; then echo "Run in the project root" exit 1 fi @@ -28,16 +16,11 @@ fi mkdir -p build cd build || exit -if [ ${debug} -eq 1 ]; then - cmake -DCMAKE_BUILD_TYPE=debug .. -else - cmake -DCMAKE_BUILD_TYPE=release .. -fi - +cmake .. make if [ ${run_exe} -eq 1 ]; then - ./simworld + ./server/simworld-daemon fi cd .. diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt new file mode 100644 index 0000000..dd9064a --- /dev/null +++ b/common/CMakeLists.txt @@ -0,0 +1,16 @@ +# common/CMakeLists.txt + +cmake_minimum_required(VERSION 3.10) +project(libsimworld VERSION 0.0.1 LANGUAGES C) + +set(SOURCE_DIR ${CMAKE_SOURCE_DIR}/common/src) +file(GLOB_RECURSE SOURCES ${SOURCE_DIR}/*.c) + +add_library(${PROJECT_NAME} SHARED ${SOURCES}) +target_include_directories(${PROJECT_NAME} PUBLIC ${SOURCE_DIR}) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(LUA REQUIRED lua) + +target_link_libraries(${PROJECT_NAME} ${LUA_LIBRARIES}) +target_include_directories(${PROJECT_NAME} PRIVATE ${LUA_INCLUDE_DIRS}) diff --git a/src/entity/entity.c b/common/src/entity/entity.c similarity index 100% rename from src/entity/entity.c rename to common/src/entity/entity.c diff --git a/src/entity/entity.h b/common/src/entity/entity.h similarity index 100% rename from src/entity/entity.h rename to common/src/entity/entity.h diff --git a/src/entity/registry.c b/common/src/entity/registry.c similarity index 100% rename from src/entity/registry.c rename to common/src/entity/registry.c diff --git a/src/entity/registry.h b/common/src/entity/registry.h similarity index 100% rename from src/entity/registry.h rename to common/src/entity/registry.h diff --git a/src/error.c b/common/src/error.c similarity index 100% rename from src/error.c rename to common/src/error.c diff --git a/src/error.h b/common/src/error.h similarity index 100% rename from src/error.h rename to common/src/error.h diff --git a/src/world.c b/common/src/world.c similarity index 100% rename from src/world.c rename to common/src/world.c diff --git a/src/world.h b/common/src/world.h similarity index 100% rename from src/world.h rename to common/src/world.h diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt new file mode 100644 index 0000000..004cdd0 --- /dev/null +++ b/server/CMakeLists.txt @@ -0,0 +1,10 @@ +# server/CMakeLists.txt + +cmake_minimum_required(VERSION 3.10) +project(simworld-daemon VERSION 0.0.1 LANGUAGES C) + +set(SOURCE_DIR ${CMAKE_SOURCE_DIR}/server/src) +file(GLOB_RECURSE SOURCES ${SOURCE_DIR}/*.c) + +add_executable(simworld-daemon ${SOURCES}) +target_link_libraries(${PROJECT_NAME} PRIVATE libsimworld) diff --git a/src/main.c b/server/src/main.c similarity index 100% rename from src/main.c rename to server/src/main.c index c72b73b..37eaf55 100644 --- a/src/main.c +++ b/server/src/main.c @@ -2,8 +2,8 @@ #include -#include "world.h" #include "render/render.h" +#include "world.h" int main(void) { enum error_t err = ERR_OK; diff --git a/src/render/render.c b/server/src/render/render.c similarity index 100% rename from src/render/render.c rename to server/src/render/render.c diff --git a/src/render/render.h b/server/src/render/render.h similarity index 79% rename from src/render/render.h rename to server/src/render/render.h index 57e638e..78738f5 100644 --- a/src/render/render.h +++ b/server/src/render/render.h @@ -1,7 +1,7 @@ #ifndef RENDER_H #define RENDER_H -#include "../world.h" +#include "world.h" void render_world(struct world_t const *);