Use CMake option for asset dir instead of command-line flag

This commit is contained in:
Camden Dixie O'Brien 2025-01-07 13:47:50 +00:00
parent 0b9fd92d63
commit de073d7d7a
4 changed files with 32 additions and 33 deletions

View File

@ -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)

21
README
View File

@ -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

View File

@ -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

View File

@ -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);