application separation
This commit is contained in:
		
							
								
								
									
										8
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										8
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,3 +1,11 @@ | |||||||
|  | # ignore executables | ||||||
|  | * | ||||||
|  | !*.* | ||||||
|  | !*/ | ||||||
|  | !Makefile | ||||||
|  |  | ||||||
|  | **/*.exe | ||||||
|  |  | ||||||
| .vs/ | .vs/ | ||||||
| .vscode/ | .vscode/ | ||||||
| .idea/ | .idea/ | ||||||
|   | |||||||
| @@ -7,14 +7,16 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) | |||||||
| set(CMAKE_EXPORT_COMPILE_COMMANDS ON) | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) | ||||||
|  |  | ||||||
| # dependencies | # dependencies | ||||||
| find_package(SDL2 REQUIRED) | find_package(SDL2 REQUIRED GLOBAL) | ||||||
| find_package(SDL2_image REQUIRED) | find_package(SDL2_image REQUIRED GLOBAL) | ||||||
| find_package(SDL2_ttf REQUIRED) | find_package(SDL2_ttf REQUIRED GLOBAL) | ||||||
|  |  | ||||||
| if (NOT CMAKE_BUILD_TYPE) | if (NOT CMAKE_BUILD_TYPE) | ||||||
|         set(CMAKE_BUILD_TYPE Debug) |         set(CMAKE_BUILD_TYPE Debug) | ||||||
| endif () | endif () | ||||||
|  |  | ||||||
|  | set(TOWNENGINE_TARGET townengine CACHE INTERNAL "") | ||||||
|  | set(TOWNENGINE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "") | ||||||
|  |  | ||||||
| set(PHYSFS_BUILD_SHARED FALSE) | set(PHYSFS_BUILD_SHARED FALSE) | ||||||
| set(PHYSFS_DISABLE_INSTALL TRUE) | set(PHYSFS_DISABLE_INSTALL TRUE) | ||||||
| @@ -49,29 +51,16 @@ set(TOWNENGINE_SOURCE_FILES | |||||||
|  |  | ||||||
|         ${SYSTEM_SOURCE_FILES} |         ${SYSTEM_SOURCE_FILES} | ||||||
| ) | ) | ||||||
|  | list(TRANSFORM TOWNENGINE_SOURCE_FILES PREPEND ${TOWNENGINE_DIR}/) | ||||||
| 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 |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| # base engine code, reused for games and tools | # base engine code, reused for games and tools | ||||||
| add_library(${PROJECT_NAME} ${TOWNENGINE_SOURCE_FILES}) | add_library(${TOWNENGINE_TARGET} ${TOWNENGINE_SOURCE_FILES}) | ||||||
| source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${TOWNENGINE_SOURCE_FILES}) | source_group(TREE ${TOWNENGINE_DIR} FILES ${TOWNENGINE_SOURCE_FILES}) | ||||||
|  |  | ||||||
| set_target_properties(${PROJECT_NAME} PROPERTIES | set_target_properties(${TOWNENGINE_TARGET} PROPERTIES | ||||||
|                                       C_STANDARD 11 |                                            C_STANDARD 11 | ||||||
|                                       C_STANDARD_REQUIRED ON |                                            C_STANDARD_REQUIRED ON | ||||||
|                                       C_EXTENSIONS ON) # extensions are required by stb_ds.h |                                            C_EXTENSIONS ON) # extensions are required by stb_ds.h | ||||||
|  |  | ||||||
| # distribution definitions | # distribution definitions | ||||||
| set(ORGANIZATION_NAME "wanp" CACHE STRING | set(ORGANIZATION_NAME "wanp" CACHE STRING | ||||||
| @@ -161,18 +150,18 @@ endfunction() | |||||||
| function(link_and_include_deps target) | function(link_and_include_deps target) | ||||||
|         # header-only libraries should be marked as "system includes" |         # header-only libraries should be marked as "system includes" | ||||||
|         # to suppress compiler warnings in their code (it's not my problem after all) |         # to suppress compiler warnings in their code (it's not my problem after all) | ||||||
|         target_include_directories(${target} |         set(THIRD_PARTY_INCLUDES | ||||||
|                 SYSTEM |  | ||||||
|                 PRIVATE |  | ||||||
|                 third-party/physfs/src |                 third-party/physfs/src | ||||||
|                 third-party/physfs/extras |                 third-party/physfs/extras | ||||||
|                 third-party/libxm/include |                 third-party/libxm/include | ||||||
|                 third-party/glad/include |                 third-party/glad/include | ||||||
|                 third-party/stb |                 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 |         # 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 |         target_link_libraries(${target} PUBLIC | ||||||
|                                         SDL2::SDL2 |                                         SDL2::SDL2 | ||||||
| @@ -197,7 +186,7 @@ function(use_townengine target) | |||||||
|  |  | ||||||
|         # third-party libraries |         # third-party libraries | ||||||
|         target_link_libraries(${target} PUBLIC |         target_link_libraries(${target} PUBLIC | ||||||
|                                         ${PROJECT_NAME} |                                         ${TOWNENGINE_TARGET} | ||||||
|                                         SDL2::SDL2 |                                         SDL2::SDL2 | ||||||
|                                         SDL2::SDL2main |                                         SDL2::SDL2main | ||||||
|                                         SDL2_image::SDL2_image |                                         SDL2_image::SDL2_image | ||||||
| @@ -214,11 +203,10 @@ function(use_townengine target) | |||||||
|         ) |         ) | ||||||
| endfunction() | endfunction() | ||||||
|  |  | ||||||
| give_options(${PROJECT_NAME}) | give_options(${TOWNENGINE_TARGET}) | ||||||
| link_and_include_deps(${PROJECT_NAME}) | link_and_include_deps(${TOWNENGINE_TARGET}) | ||||||
|  |  | ||||||
| add_executable(game ${GAME_SOURCE_FILES}) | add_subdirectory(apps/testgame) | ||||||
| use_townengine(game) |  | ||||||
|  |  | ||||||
| # zip up assets | # zip up assets | ||||||
| # currently, you must run cmake from the project root dir for this to work correctly | # currently, you must run cmake from the project root dir for this to work correctly | ||||||
|   | |||||||
							
								
								
									
										23
									
								
								apps/template/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								apps/template/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -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} | ||||||
|  | ) | ||||||
							
								
								
									
										3
									
								
								apps/template/build.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										3
									
								
								apps/template/build.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | #!/bin/env sh | ||||||
|  |  | ||||||
|  | cmake -B .build "$@" && cmake --build .build | ||||||
							
								
								
									
										26
									
								
								apps/template/game.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								apps/template/game.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | |||||||
|  | #include "townengine/game_api.h" | ||||||
|  | #include "game.h" | ||||||
|  | #include "state.h" | ||||||
|  |  | ||||||
|  | #include <malloc.h> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 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); | ||||||
|  | } | ||||||
							
								
								
									
										13
									
								
								apps/template/game.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								apps/template/game.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | |||||||
|  | #ifndef GAME_H | ||||||
|  | #define GAME_H | ||||||
|  |  | ||||||
|  | #include "townengine/game_api.h" | ||||||
|  |  | ||||||
|  | #include <stdint.h> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void game_tick(void); | ||||||
|  | void game_end(void); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #endif | ||||||
							
								
								
									
										12
									
								
								apps/template/state.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								apps/template/state.h
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||||
							
								
								
									
										31
									
								
								apps/testgame/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								apps/testgame/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -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} | ||||||
|  | ) | ||||||
							
								
								
									
										3
									
								
								apps/testgame/build.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										3
									
								
								apps/testgame/build.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | #!/bin/env sh | ||||||
|  |  | ||||||
|  | cmake -B .build "$@" && cmake --build .build | ||||||
| @@ -11,12 +11,14 @@ static void ingame_tick(struct state *state) { | |||||||
|     world_drawdef(scn->world); |     world_drawdef(scn->world); | ||||||
|     player_calc(scn->player); |     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")) |     if (input_is_action_pressed(&ctx.input, "player_left")) | ||||||
|         cam.pos.x -= 0.01f; |         cam.pos.x -= 0.01f; | ||||||
|     if (input_is_action_pressed(&ctx.input, "player_right")) |     if (input_is_action_pressed(&ctx.input, "player_right")) | ||||||
|         cam.pos.x += 0.01f; |         cam.pos.x += 0.01f; | ||||||
|  |     if (input_is_action_pressed(&ctx.input, "player_jump")) | ||||||
|  |         cam.pos.z -= 0.01f; | ||||||
| 
 | 
 | ||||||
|     push_camera(&cam); |     push_camera(&cam); | ||||||
| 
 | 
 | ||||||
| @@ -3,7 +3,6 @@ | |||||||
| #include "input.h" | #include "input.h" | ||||||
| #include "util.h" | #include "util.h" | ||||||
| #include "textures.h" | #include "textures.h" | ||||||
| #include "game/game.h" |  | ||||||
| #include "private/audio.h" | #include "private/audio.h" | ||||||
|  |  | ||||||
| #include <SDL2/SDL.h> | #include <SDL2/SDL.h> | ||||||
| @@ -20,6 +19,11 @@ | |||||||
| #include <limits.h> | #include <limits.h> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | /* application provided */ | ||||||
|  | extern void game_tick(void); | ||||||
|  | extern void game_end(void); | ||||||
|  |  | ||||||
|  |  | ||||||
| static void poll_events(void) { | static void poll_events(void) { | ||||||
|     SDL_Event e; |     SDL_Event e; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user