application separation
This commit is contained in:
parent
922e521867
commit
a99cb340d8
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,3 +1,11 @@
|
||||
# ignore executables
|
||||
*
|
||||
!*.*
|
||||
!*/
|
||||
!Makefile
|
||||
|
||||
**/*.exe
|
||||
|
||||
.vs/
|
||||
.vscode/
|
||||
.idea/
|
||||
|
@ -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
|
||||
|
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);
|
||||
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);
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include "input.h"
|
||||
#include "util.h"
|
||||
#include "textures.h"
|
||||
#include "game/game.h"
|
||||
#include "private/audio.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
@ -20,6 +19,11 @@
|
||||
#include <limits.h>
|
||||
|
||||
|
||||
/* application provided */
|
||||
extern void game_tick(void);
|
||||
extern void game_end(void);
|
||||
|
||||
|
||||
static void poll_events(void) {
|
||||
SDL_Event e;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user