application separation

This commit is contained in:
2024-07-30 01:20:30 +03:00
parent 922e521867
commit a99cb340d8
23 changed files with 147 additions and 34 deletions

View 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
View File

@ -0,0 +1,3 @@
#!/bin/env sh
cmake -B .build "$@" && cmake --build .build

26
apps/template/game.c Normal file
View 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
View 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
View 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