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(PERFMON "Monitor performance of game code" ON)
option(HOTRELOAD "Enable hot reloading of game code" OFF) 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) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
macro(set_default_target_options target) macro(set_default_target_options target)

19
README
View File

@ -5,24 +5,25 @@ Dependencies:
- libxml2 :: https://gitlab.gnome.org/GNOME/libxml2/ - libxml2 :: https://gitlab.gnome.org/GNOME/libxml2/
- SDL2 :: https://www.libsdl.org/ - 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 cmake --build build
The path to a directory containing the assets must be passed on the You should then be able to run the game:
command line to run the game:
build/game/game ASSETS-DIR build/game/game
Hot Reloading Hot Reloading
The engine supports hot reloading of the game code via the F5 key; set 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 the HOTRELOAD option on in CMake to enable it. You also have to run
executable built from the engine rather than the game if using this the engine executable rather than the game one if using this feature.
feature.
cmake -Bbuild -DHOTRELOAD=ON cmake -Bbuild -DHOTRELOAD=ON
cmake --build build cmake --build build
build/engine/engine assets/ build/engine/engine

View File

@ -7,6 +7,7 @@ else()
target_link_libraries(game PRIVATE engine) target_link_libraries(game PRIVATE engine)
endif() endif()
set_default_target_options(game) set_default_target_options(game)
target_compile_definitions(game PRIVATE ASSET_DIR="${ASSET_DIR}")
target_include_directories(game PUBLIC include) target_include_directories(game PUBLIC include)
target_link_libraries(game PRIVATE target_link_libraries(game PRIVATE

View File

@ -140,7 +140,6 @@ typedef struct {
typedef unsigned map_t[MAPNLAYERS][MAPWIDTH][MAPHEIGHT]; typedef unsigned map_t[MAPNLAYERS][MAPWIDTH][MAPHEIGHT];
typedef struct { typedef struct {
const char *assetdir;
map_t map; map_t map;
SDL_Texture *tstex, *pidle, *pwalk; SDL_Texture *tstex, *pidle, *pwalk;
input_state_t input; input_state_t input;
@ -251,8 +250,8 @@ static unsigned load_objtype(
xmlNodePtr sprites) xmlNodePtr sprites)
{ {
static char buf[MAX_PATH_LEN]; static char buf[MAX_PATH_LEN];
assert(strlen(state->assetdir) + strlen(templ) + 1 < MAX_PATH_LEN); assert(strlen(ASSET_DIR) + strlen(templ) + 1 < MAX_PATH_LEN);
strcpy(buf, state->assetdir); strcpy(buf, ASSET_DIR);
strcat(buf, "/"); strcat(buf, "/");
strcat(buf, templ); strcat(buf, templ);
@ -340,9 +339,9 @@ static unsigned load_objtype(
state->objtypes[id].animframes = atoi((const char *)val); state->objtypes[id].animframes = atoi((const char *)val);
} else if (xmlStrcmp(key, (const xmlChar *)"assetpath") == 0) { } else if (xmlStrcmp(key, (const xmlChar *)"assetpath") == 0) {
assert( assert(
strlen(state->assetdir) + strlen((const char *)val) strlen(ASSET_DIR) + strlen((const char *)val)
< MAX_PATH_LEN); < MAX_PATH_LEN);
strcpy(buf, state->assetdir); strcpy(buf, ASSET_DIR);
strcat(buf, (const char *)val); strcat(buf, (const char *)val);
state->objtypes[id].tex = IMG_LoadTexture(renderer, buf); state->objtypes[id].tex = IMG_LoadTexture(renderer, buf);
assert(NULL != state->objtypes[id].tex); assert(NULL != state->objtypes[id].tex);
@ -362,9 +361,8 @@ static void
load_objects(gamestate_t *state, xmlNodePtr node, SDL_Renderer *renderer) load_objects(gamestate_t *state, xmlNodePtr node, SDL_Renderer *renderer)
{ {
char path[MAX_PATH_LEN]; char path[MAX_PATH_LEN];
assert(strlen(state->assetdir) + strlen(OBJSPRITES) < MAX_PATH_LEN); assert(strlen(ASSET_DIR OBJSPRITES) < MAX_PATH_LEN);
strcpy(path, state->assetdir); strcpy(path, ASSET_DIR OBJSPRITES);
strcat(path, OBJSPRITES);
xmlDocPtr spritesdoc = xmlParseFile(path); xmlDocPtr spritesdoc = xmlParseFile(path);
xmlNodePtr sprites = xmlDocGetRootElement(spritesdoc); 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 game_init(int argc, char *argv[], void *mem, SDL_Renderer *renderer)
{ {
(void)argc;
(void)argv;
char path[MAX_PATH_LEN]; char path[MAX_PATH_LEN];
gamestate_t *state = (gamestate_t *)mem; gamestate_t *state = (gamestate_t *)mem;
assert(2 == argc);
state->assetdir = argv[1];
// Load map // Load map
assert(strlen(state->assetdir) + strlen(MAP_ASSET) < MAX_PATH_LEN); assert(strlen(ASSET_DIR MAP_ASSET) < MAX_PATH_LEN);
strcpy(path, state->assetdir); strcpy(path, ASSET_DIR MAP_ASSET);
strcat(path, MAP_ASSET);
load_map(state, path, renderer); load_map(state, path, renderer);
// Load tileset // Load tileset
assert(strlen(state->assetdir) + strlen(TSASSET) < MAX_PATH_LEN); assert(strlen(ASSET_DIR TSASSET) < MAX_PATH_LEN);
strcpy(path, state->assetdir); strcpy(path, ASSET_DIR TSASSET);
strcat(path, TSASSET);
state->tstex = IMG_LoadTexture(renderer, path); state->tstex = IMG_LoadTexture(renderer, path);
assert(NULL != state->tstex); assert(NULL != state->tstex);
// Load player spritesheets and initialize texture // Load player spritesheets and initialize texture
assert(strlen(state->assetdir) + strlen(PIDLE_ASSET) < MAX_PATH_LEN); assert(strlen(ASSET_DIR PIDLE_ASSET) < MAX_PATH_LEN);
strcpy(path, state->assetdir); strcpy(path, ASSET_DIR PIDLE_ASSET);
strcat(path, PIDLE_ASSET);
state->pidle = IMG_LoadTexture(renderer, path); state->pidle = IMG_LoadTexture(renderer, path);
assert(NULL != state->pidle); assert(NULL != state->pidle);
assert(strlen(state->assetdir) + strlen(PWALK_ASSET) < MAX_PATH_LEN); assert(strlen(ASSET_DIR PWALK_ASSET) < MAX_PATH_LEN);
strcpy(path, state->assetdir); strcpy(path, ASSET_DIR PWALK_ASSET);
strcat(path, PWALK_ASSET);
state->pwalk = IMG_LoadTexture(renderer, path); state->pwalk = IMG_LoadTexture(renderer, path);
assert(NULL != state->pwalk); assert(NULL != state->pwalk);