From de073d7d7ad5179a53ea3020b4117fd8e86125ef Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Tue, 7 Jan 2025 13:47:50 +0000 Subject: [PATCH] Use CMake option for asset dir instead of command-line flag --- CMakeLists.txt | 3 +++ README | 21 +++++++++++---------- game/CMakeLists.txt | 1 + game/main.c | 40 +++++++++++++++++----------------------- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 08147d9..20c9c2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,9 @@ option(UBSAN "Enable undefined behaviour sanitizer" OFF) option(PERFMON "Monitor performance of game code" ON) option(HOTRELOAD "Enable hot reloading of game code" OFF) +set(ASSET_DIR "${CMAKE_SOURCE_DIR}/assets" + CACHE STRING "Directory containing asset pack") + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) macro(set_default_target_options target) diff --git a/README b/README index 653aa43..da482a9 100644 --- a/README +++ b/README @@ -5,24 +5,25 @@ Dependencies: - libxml2 :: https://gitlab.gnome.org/GNOME/libxml2/ - SDL2 :: https://www.libsdl.org/ -The build is handled with CMake (version 3.10 or later): +The build is handled with CMake (version 3.10 or later). You can +specify a path to a directory containing the assets when configuring +the build; alternatively it will default to the "assets" directory +under the root of the project. - cmake -B build + cmake -Bbuild # -DASSETS_DIR="..." cmake --build build -The path to a directory containing the assets must be passed on the -command line to run the game: +You should then be able to run the game: - build/game/game ASSETS-DIR + build/game/game Hot Reloading The engine supports hot reloading of the game code via the F5 key; set -the HOTRELOAD option on in CMake to enable it. You also have to run an -executable built from the engine rather than the game if using this -feature. +the HOTRELOAD option on in CMake to enable it. You also have to run +the engine executable rather than the game one if using this feature. - cmake -B build -D HOTRELOAD=ON + cmake -Bbuild -DHOTRELOAD=ON cmake --build build - build/engine/engine assets/ + build/engine/engine diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 8b88df6..e55e9e2 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -7,6 +7,7 @@ else() target_link_libraries(game PRIVATE engine) endif() set_default_target_options(game) +target_compile_definitions(game PRIVATE ASSET_DIR="${ASSET_DIR}") target_include_directories(game PUBLIC include) target_link_libraries(game PRIVATE diff --git a/game/main.c b/game/main.c index 88a828e..f9e49b5 100644 --- a/game/main.c +++ b/game/main.c @@ -140,7 +140,6 @@ typedef struct { typedef unsigned map_t[MAPNLAYERS][MAPWIDTH][MAPHEIGHT]; typedef struct { - const char *assetdir; map_t map; SDL_Texture *tstex, *pidle, *pwalk; input_state_t input; @@ -251,8 +250,8 @@ static unsigned load_objtype( xmlNodePtr sprites) { static char buf[MAX_PATH_LEN]; - assert(strlen(state->assetdir) + strlen(templ) + 1 < MAX_PATH_LEN); - strcpy(buf, state->assetdir); + assert(strlen(ASSET_DIR) + strlen(templ) + 1 < MAX_PATH_LEN); + strcpy(buf, ASSET_DIR); strcat(buf, "/"); strcat(buf, templ); @@ -340,9 +339,9 @@ static unsigned load_objtype( state->objtypes[id].animframes = atoi((const char *)val); } else if (xmlStrcmp(key, (const xmlChar *)"assetpath") == 0) { assert( - strlen(state->assetdir) + strlen((const char *)val) + strlen(ASSET_DIR) + strlen((const char *)val) < MAX_PATH_LEN); - strcpy(buf, state->assetdir); + strcpy(buf, ASSET_DIR); strcat(buf, (const char *)val); state->objtypes[id].tex = IMG_LoadTexture(renderer, buf); assert(NULL != state->objtypes[id].tex); @@ -362,9 +361,8 @@ static void load_objects(gamestate_t *state, xmlNodePtr node, SDL_Renderer *renderer) { char path[MAX_PATH_LEN]; - assert(strlen(state->assetdir) + strlen(OBJSPRITES) < MAX_PATH_LEN); - strcpy(path, state->assetdir); - strcat(path, OBJSPRITES); + assert(strlen(ASSET_DIR OBJSPRITES) < MAX_PATH_LEN); + strcpy(path, ASSET_DIR OBJSPRITES); xmlDocPtr spritesdoc = xmlParseFile(path); xmlNodePtr sprites = xmlDocGetRootElement(spritesdoc); @@ -496,34 +494,30 @@ void init_drawlist(gamestate_t *state) void game_init(int argc, char *argv[], void *mem, SDL_Renderer *renderer) { + (void)argc; + (void)argv; + char path[MAX_PATH_LEN]; gamestate_t *state = (gamestate_t *)mem; - assert(2 == argc); - state->assetdir = argv[1]; - // Load map - assert(strlen(state->assetdir) + strlen(MAP_ASSET) < MAX_PATH_LEN); - strcpy(path, state->assetdir); - strcat(path, MAP_ASSET); + assert(strlen(ASSET_DIR MAP_ASSET) < MAX_PATH_LEN); + strcpy(path, ASSET_DIR MAP_ASSET); load_map(state, path, renderer); // Load tileset - assert(strlen(state->assetdir) + strlen(TSASSET) < MAX_PATH_LEN); - strcpy(path, state->assetdir); - strcat(path, TSASSET); + assert(strlen(ASSET_DIR TSASSET) < MAX_PATH_LEN); + strcpy(path, ASSET_DIR TSASSET); state->tstex = IMG_LoadTexture(renderer, path); assert(NULL != state->tstex); // Load player spritesheets and initialize texture - assert(strlen(state->assetdir) + strlen(PIDLE_ASSET) < MAX_PATH_LEN); - strcpy(path, state->assetdir); - strcat(path, PIDLE_ASSET); + assert(strlen(ASSET_DIR PIDLE_ASSET) < MAX_PATH_LEN); + strcpy(path, ASSET_DIR PIDLE_ASSET); state->pidle = IMG_LoadTexture(renderer, path); assert(NULL != state->pidle); - assert(strlen(state->assetdir) + strlen(PWALK_ASSET) < MAX_PATH_LEN); - strcpy(path, state->assetdir); - strcat(path, PWALK_ASSET); + assert(strlen(ASSET_DIR PWALK_ASSET) < MAX_PATH_LEN); + strcpy(path, ASSET_DIR PWALK_ASSET); state->pwalk = IMG_LoadTexture(renderer, path); assert(NULL != state->pwalk);