From 0ef7ac193dd82866908ea94afdec5241ab97a791 Mon Sep 17 00:00:00 2001 From: 0scar Date: Thu, 8 Feb 2024 10:10:14 +0100 Subject: Change state signatures --- src/core/include/engine/core/state.h | 4 ++-- src/core/include/engine/engine.h | 2 +- src/core/src/loop.c | 24 ++++++++++-------------- src/core/src/state.c | 18 ++++++++++-------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/core/include/engine/core/state.h b/src/core/include/engine/core/state.h index a4c56fa..f407acc 100644 --- a/src/core/include/engine/core/state.h +++ b/src/core/include/engine/core/state.h @@ -15,8 +15,8 @@ extern const char* StateTypeStr[]; StateType (*State_updateFunc(StateType type))(void*); -void State_init(StateType type, memory* mem); -void State_free(StateType type, memory* mem); +void State_init(StateType type, memory* mem, void* arg); +void* State_free(StateType type, memory* mem); StateType State_update(StateType type, memory* mem); /* Reloads shared object file associated with state */ diff --git a/src/core/include/engine/engine.h b/src/core/include/engine/engine.h index 7ba2cc8..11ee096 100644 --- a/src/core/include/engine/engine.h +++ b/src/core/include/engine/engine.h @@ -30,7 +30,7 @@ Platform* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight const usize initial_memory, const Asset_FontSpec* fonts[], const Asset_TextureSpec* textures[]); -i32 engine_run(Platform* p, StateType initial_state); +i32 engine_run(Platform* p, StateType initial_state, void* state_arg); void engine_stop(Platform* p); diff --git a/src/core/src/loop.c b/src/core/src/loop.c index 094fd98..635eea4 100644 --- a/src/core/src/loop.c +++ b/src/core/src/loop.c @@ -40,7 +40,7 @@ i32 nproc(void) { void delay( uint32_t ms ) { -#ifdef _WIN32 +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) Sleep( ms ); #else usleep( ms * 1000 ); @@ -367,7 +367,7 @@ Platform* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight return p; } -i32 engine_run(Platform* p, StateType initial_state) { +i32 engine_run(Platform* p, StateType initial_state, void* state_arg) { if (p == NULL) { ERROR("Platform is uninitialized.\n"); INFO("initialize with `engine_init`"); @@ -380,7 +380,7 @@ i32 engine_run(Platform* p, StateType initial_state) { { f64 state_init_time = get_time(); - State_init(state, mem); + State_init(state, mem, state_arg); INFO("Initializing state \"%s\" took %.1fms", StateTypeStr[state], (get_time() - state_init_time) * 1000.0); } @@ -443,23 +443,19 @@ i32 engine_run(Platform* p, StateType initial_state) { drawcall_reset(); - State_free(state, mem); + void* retval = State_free(state, mem); memory_clear(mem); engine_input_ctx_reset(); state = next_state; update_func = State_updateFunc(state); -//#ifdef BENCHMARK -// { -// f64 t = get_time(); -// State_init(state, mem); -// LOG("Initializing %s took %dms", StateTypeStr[state], -// (int)((get_time() - t) * 1000.0)); -// } -//#else - State_init(state, mem); -//#endif + { + f64 state_init_time = get_time(); + State_init(state, mem, retval); + INFO("Initializing state \"%s\" took %.1fms", StateTypeStr[state], + (get_time() - state_init_time) * 1000.0); + } } else { //#ifdef BENCHMARK // profile_num_drawcalls += drawcall_len; diff --git a/src/core/src/state.c b/src/core/src/state.c index b66c565..c393566 100644 --- a/src/core/src/state.c +++ b/src/core/src/state.c @@ -5,6 +5,8 @@ #include #include +//typedef void state_init_t(void*,void*); +//typedef void* (state_free_t(void*)); typedef StateType state_update_t(void*); const char* StateTypeStr[] = { @@ -18,8 +20,8 @@ const char* StateTypeStr[] = { // Setup API for states #define State(name) \ typedef struct name##_state name##_state; \ - typedef void(state_##name##_init_t)(name##_state*); \ - typedef void(state_##name##_free_t)(name##_state*); \ + typedef void(state_##name##_init_t)(name##_state*,void*); \ + typedef void*(state_##name##_free_t)(name##_state*); \ typedef StateType(state_##name##_update_t)(name##_state*); #include #undef State @@ -49,13 +51,11 @@ const char* StateTypeStr[] = { #include -void binding_t_free(binding_t* b); - -void State_init(StateType type, memory* mem) { +void State_init(StateType type, memory* mem, void* arg) { switch (type) { #define State(name) \ case (STATE_##name): { \ - name##_init(memory_allocate(mem, sizeof(name##_state))); \ + name##_init(memory_allocate(mem, sizeof(name##_state)), arg); \ break; \ } #include @@ -69,11 +69,12 @@ void State_init(StateType type, memory* mem) { } } -void State_free(StateType type, memory* mem) { +void* State_free(StateType type, memory* mem) { + void* state_retval = NULL; switch (type) { #define State(name) \ case (STATE_##name): { \ - name##_free(mem->data); \ + state_retval = name##_free(mem->data); \ break; \ } #include @@ -86,6 +87,7 @@ void State_free(StateType type, memory* mem) { exit(EXIT_FAILURE); } memory_clear(mem); + return state_retval; } StateType (*State_updateFunc(StateType type))(void*) { -- cgit v1.3