From a99cb340d8901e6a04db3fa4c844da49051e53b9 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Tue, 30 Jul 2024 01:20:30 +0300 Subject: [PATCH] application separation --- .gitignore | 8 ++++ CMakeLists.txt | 52 ++++++++------------- apps/template/CMakeLists.txt | 23 +++++++++ apps/template/build.sh | 3 ++ apps/template/game.c | 26 +++++++++++ apps/template/game.h | 13 ++++++ apps/template/state.h | 12 +++++ apps/testgame/CMakeLists.txt | 31 ++++++++++++ apps/testgame/build.sh | 3 ++ {src/game => apps/testgame}/game.c | 0 {src/game => apps/testgame}/game.h | 0 {src/game => apps/testgame}/player.c | 0 {src/game => apps/testgame}/player.h | 0 {src/game => apps/testgame}/scenes/ingame.c | 4 +- {src/game => apps/testgame}/scenes/ingame.h | 0 {src/game => apps/testgame}/scenes/scene.c | 0 {src/game => apps/testgame}/scenes/scene.h | 0 {src/game => apps/testgame}/scenes/title.c | 0 {src/game => apps/testgame}/scenes/title.h | 0 {src/game => apps/testgame}/state.h | 0 {src/game => apps/testgame}/world.c | 0 {src/game => apps/testgame}/world.h | 0 src/main.c | 6 ++- 23 files changed, 147 insertions(+), 34 deletions(-) create mode 100644 apps/template/CMakeLists.txt create mode 100755 apps/template/build.sh create mode 100644 apps/template/game.c create mode 100644 apps/template/game.h create mode 100644 apps/template/state.h create mode 100644 apps/testgame/CMakeLists.txt create mode 100755 apps/testgame/build.sh rename {src/game => apps/testgame}/game.c (100%) rename {src/game => apps/testgame}/game.h (100%) rename {src/game => apps/testgame}/player.c (100%) rename {src/game => apps/testgame}/player.h (100%) rename {src/game => apps/testgame}/scenes/ingame.c (96%) rename {src/game => apps/testgame}/scenes/ingame.h (100%) rename {src/game => apps/testgame}/scenes/scene.c (100%) rename {src/game => apps/testgame}/scenes/scene.h (100%) rename {src/game => apps/testgame}/scenes/title.c (100%) rename {src/game => apps/testgame}/scenes/title.h (100%) rename {src/game => apps/testgame}/state.h (100%) rename {src/game => apps/testgame}/world.c (100%) rename {src/game => apps/testgame}/world.h (100%) diff --git a/.gitignore b/.gitignore index cfb2988..fd8715a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,11 @@ +# ignore executables +* +!*.* +!*/ +!Makefile + +**/*.exe + .vs/ .vscode/ .idea/ diff --git a/CMakeLists.txt b/CMakeLists.txt index de7567e..c4ca5ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,14 +7,16 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # dependencies -find_package(SDL2 REQUIRED) -find_package(SDL2_image REQUIRED) -find_package(SDL2_ttf REQUIRED) +find_package(SDL2 REQUIRED GLOBAL) +find_package(SDL2_image REQUIRED GLOBAL) +find_package(SDL2_ttf REQUIRED GLOBAL) if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Debug) endif () +set(TOWNENGINE_TARGET townengine CACHE INTERNAL "") +set(TOWNENGINE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "") set(PHYSFS_BUILD_SHARED FALSE) set(PHYSFS_DISABLE_INSTALL TRUE) @@ -49,29 +51,16 @@ set(TOWNENGINE_SOURCE_FILES ${SYSTEM_SOURCE_FILES} ) - -set(GAME_SOURCE_FILES - src/game_api.h - - src/game/game.c src/game/game.h - src/game/state.h - - src/game/player.c src/game/player.h - src/game/world.c src/game/world.h - - src/game/scenes/scene.c src/game/scenes/scene.h - src/game/scenes/title.c src/game/scenes/title.h - src/game/scenes/ingame.c src/game/scenes/ingame.h -) +list(TRANSFORM TOWNENGINE_SOURCE_FILES PREPEND ${TOWNENGINE_DIR}/) # base engine code, reused for games and tools -add_library(${PROJECT_NAME} ${TOWNENGINE_SOURCE_FILES}) -source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${TOWNENGINE_SOURCE_FILES}) +add_library(${TOWNENGINE_TARGET} ${TOWNENGINE_SOURCE_FILES}) +source_group(TREE ${TOWNENGINE_DIR} FILES ${TOWNENGINE_SOURCE_FILES}) -set_target_properties(${PROJECT_NAME} PROPERTIES - C_STANDARD 11 - C_STANDARD_REQUIRED ON - C_EXTENSIONS ON) # extensions are required by stb_ds.h +set_target_properties(${TOWNENGINE_TARGET} PROPERTIES + C_STANDARD 11 + C_STANDARD_REQUIRED ON + C_EXTENSIONS ON) # extensions are required by stb_ds.h # distribution definitions set(ORGANIZATION_NAME "wanp" CACHE STRING @@ -161,18 +150,18 @@ endfunction() function(link_and_include_deps target) # header-only libraries should be marked as "system includes" # to suppress compiler warnings in their code (it's not my problem after all) - target_include_directories(${target} - SYSTEM - PRIVATE + set(THIRD_PARTY_INCLUDES third-party/physfs/src third-party/physfs/extras third-party/libxm/include third-party/glad/include third-party/stb ) + list(TRANSFORM THIRD_PARTY_INCLUDES PREPEND ${TOWNENGINE_DIR}/) + target_include_directories(${target} SYSTEM PRIVATE ${THIRD_PARTY_INCLUDES}) # allow access to headers from any point in source tree - target_include_directories(${target} PRIVATE ./) + target_include_directories(${target} PRIVATE ${TOWNENGINE_DIR}) target_link_libraries(${target} PUBLIC SDL2::SDL2 @@ -197,7 +186,7 @@ function(use_townengine target) # third-party libraries target_link_libraries(${target} PUBLIC - ${PROJECT_NAME} + ${TOWNENGINE_TARGET} SDL2::SDL2 SDL2::SDL2main SDL2_image::SDL2_image @@ -214,11 +203,10 @@ function(use_townengine target) ) endfunction() -give_options(${PROJECT_NAME}) -link_and_include_deps(${PROJECT_NAME}) +give_options(${TOWNENGINE_TARGET}) +link_and_include_deps(${TOWNENGINE_TARGET}) -add_executable(game ${GAME_SOURCE_FILES}) -use_townengine(game) +add_subdirectory(apps/testgame) # zip up assets # currently, you must run cmake from the project root dir for this to work correctly diff --git a/apps/template/CMakeLists.txt b/apps/template/CMakeLists.txt new file mode 100644 index 0000000..7ed15ed --- /dev/null +++ b/apps/template/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.21) + +project(template LANGUAGES C) + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Debug) +endif() + +# add root townengine cmake file +if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) + add_subdirectory(../../ ../../../.build) +endif() + +set(SOURCE_FILES + game.c game.h +) + +add_executable(${PROJECT_NAME} ${SOURCE_FILES}) +use_townengine(${PROJECT_NAME}) + +set_target_properties(${PROJECT_NAME} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) diff --git a/apps/template/build.sh b/apps/template/build.sh new file mode 100755 index 0000000..45ab8a7 --- /dev/null +++ b/apps/template/build.sh @@ -0,0 +1,3 @@ +#!/bin/env sh + +cmake -B .build "$@" && cmake --build .build diff --git a/apps/template/game.c b/apps/template/game.c new file mode 100644 index 0000000..56024ba --- /dev/null +++ b/apps/template/game.c @@ -0,0 +1,26 @@ +#include "townengine/game_api.h" +#include "game.h" +#include "state.h" + +#include + + +void game_tick(void) { + /* do your initialization on first tick */ + if (ctx.tick_count == 0) { + /* application data could be stored in ctx.udata and retrieved anywhere */ + ctx.udata = ccalloc(1, sizeof (struct state)); + } + + /* a lot of data is accessible from `ctx`, look into `townengine/context.h` for more */ + + struct state *state = ctx.udata; + ++state->counter; +} + + +void game_end(void) { + /* do your deinitialization here */ + struct state *state = ctx.udata; + free(state); +} diff --git a/apps/template/game.h b/apps/template/game.h new file mode 100644 index 0000000..fde6738 --- /dev/null +++ b/apps/template/game.h @@ -0,0 +1,13 @@ +#ifndef GAME_H +#define GAME_H + +#include "townengine/game_api.h" + +#include + + +void game_tick(void); +void game_end(void); + + +#endif diff --git a/apps/template/state.h b/apps/template/state.h new file mode 100644 index 0000000..dd2ce30 --- /dev/null +++ b/apps/template/state.h @@ -0,0 +1,12 @@ +#ifndef STATE_H +#define STATE_H + +#include "townengine/game_api.h" + +/* populate it with state information */ +struct state { + uint64_t counter; +}; + + +#endif diff --git a/apps/testgame/CMakeLists.txt b/apps/testgame/CMakeLists.txt new file mode 100644 index 0000000..545dba9 --- /dev/null +++ b/apps/testgame/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 3.21) + +project(testgame LANGUAGES C) + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Debug) +endif() + +# add root townengine cmake file +if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) + add_subdirectory(../../ ../../../.build) +endif() + +set(SOURCE_FILES + game.c game.h + state.h + + player.c player.h + world.c world.h + + scenes/scene.c scenes/scene.h + scenes/title.c scenes/title.h + scenes/ingame.c scenes/ingame.h +) + +add_executable(${PROJECT_NAME} ${SOURCE_FILES}) +use_townengine(${PROJECT_NAME}) + +set_target_properties(${PROJECT_NAME} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) diff --git a/apps/testgame/build.sh b/apps/testgame/build.sh new file mode 100755 index 0000000..45ab8a7 --- /dev/null +++ b/apps/testgame/build.sh @@ -0,0 +1,3 @@ +#!/bin/env sh + +cmake -B .build "$@" && cmake --build .build diff --git a/src/game/game.c b/apps/testgame/game.c similarity index 100% rename from src/game/game.c rename to apps/testgame/game.c diff --git a/src/game/game.h b/apps/testgame/game.h similarity index 100% rename from src/game/game.h rename to apps/testgame/game.h diff --git a/src/game/player.c b/apps/testgame/player.c similarity index 100% rename from src/game/player.c rename to apps/testgame/player.c diff --git a/src/game/player.h b/apps/testgame/player.h similarity index 100% rename from src/game/player.h rename to apps/testgame/player.h diff --git a/src/game/scenes/ingame.c b/apps/testgame/scenes/ingame.c similarity index 96% rename from src/game/scenes/ingame.c rename to apps/testgame/scenes/ingame.c index 98a21ab..149b276 100644 --- a/src/game/scenes/ingame.c +++ b/apps/testgame/scenes/ingame.c @@ -11,12 +11,14 @@ static void ingame_tick(struct state *state) { world_drawdef(scn->world); player_calc(scn->player); - static t_camera cam = { .pos = { 0 }, .target = { 0, 0, -1 }, .up = { 0, -1, 0 } }; + static t_camera cam = { .pos = { 0 }, .target = { 0, 0, -1 }, .up = { 0, -1, 0 }, .fov = (float)M_PI_2 }; if (input_is_action_pressed(&ctx.input, "player_left")) cam.pos.x -= 0.01f; if (input_is_action_pressed(&ctx.input, "player_right")) cam.pos.x += 0.01f; + if (input_is_action_pressed(&ctx.input, "player_jump")) + cam.pos.z -= 0.01f; push_camera(&cam); diff --git a/src/game/scenes/ingame.h b/apps/testgame/scenes/ingame.h similarity index 100% rename from src/game/scenes/ingame.h rename to apps/testgame/scenes/ingame.h diff --git a/src/game/scenes/scene.c b/apps/testgame/scenes/scene.c similarity index 100% rename from src/game/scenes/scene.c rename to apps/testgame/scenes/scene.c diff --git a/src/game/scenes/scene.h b/apps/testgame/scenes/scene.h similarity index 100% rename from src/game/scenes/scene.h rename to apps/testgame/scenes/scene.h diff --git a/src/game/scenes/title.c b/apps/testgame/scenes/title.c similarity index 100% rename from src/game/scenes/title.c rename to apps/testgame/scenes/title.c diff --git a/src/game/scenes/title.h b/apps/testgame/scenes/title.h similarity index 100% rename from src/game/scenes/title.h rename to apps/testgame/scenes/title.h diff --git a/src/game/state.h b/apps/testgame/state.h similarity index 100% rename from src/game/state.h rename to apps/testgame/state.h diff --git a/src/game/world.c b/apps/testgame/world.c similarity index 100% rename from src/game/world.c rename to apps/testgame/world.c diff --git a/src/game/world.h b/apps/testgame/world.h similarity index 100% rename from src/game/world.h rename to apps/testgame/world.h diff --git a/src/main.c b/src/main.c index 749e551..25c9b06 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,6 @@ #include "input.h" #include "util.h" #include "textures.h" -#include "game/game.h" #include "private/audio.h" #include @@ -20,6 +19,11 @@ #include +/* application provided */ +extern void game_tick(void); +extern void game_end(void); + + static void poll_events(void) { SDL_Event e;