From 0cff8325ded7d6235e84496418e6a594613b230b Mon Sep 17 00:00:00 2001 From: 0scar Date: Sun, 18 Feb 2024 11:40:21 +0100 Subject: More reorganization --- src/core/include/engine/engine.h | 6 --- src/core/src/loop.c | 62 +------------------------ src/ctrl/src/input.c | 60 +++++++++++++++++++++++- src/rendering/include/engine/rendering/window.h | 2 + src/rendering/src/window.c | 13 ++++++ 5 files changed, 75 insertions(+), 68 deletions(-) diff --git a/src/core/include/engine/engine.h b/src/core/include/engine/engine.h index b4749f7..e15be11 100644 --- a/src/core/include/engine/engine.h +++ b/src/core/include/engine/engine.h @@ -45,12 +45,6 @@ void render_set_zoom(f32 new_zoom); void render_adjust_zoom(f32 diff); void render_add_unit(RenderUnit* u); -/* Input handling */ -void engine_input_ctx_push(i_ctx* ctx); -void engine_input_ctx_pop(void); -void engine_input_ctx_reset(void); -void get_mousepos(double *x, double *y); - /* move this */ void delay(uint32_t ms); diff --git a/src/core/src/loop.c b/src/core/src/loop.c index fd6c4e7..2657e9f 100644 --- a/src/core/src/loop.c +++ b/src/core/src/loop.c @@ -372,7 +372,7 @@ i32 engine_run(Platform* p, StateType initial_state, void* state_arg) { void* retval = State_free(state, mem); memory_clear(mem); - engine_input_ctx_reset(); + i_ctx_reset(); // Reset camera to default camera p->cam = &default_camera; @@ -425,63 +425,3 @@ void engine_stop(Platform* p) { /* Set the maximum framerate */ void engine_fps_max(u64 cap) { /* does nothing */ } - -/* Pushes an input context onto the input handling stack */ -void engine_input_ctx_push(i_ctx* ctx) { - if (GLOBAL_PLATFORM->bindings == NULL) { - GLOBAL_PLATFORM->bindings = calloc(8, sizeof(i_ctx*)); - GLOBAL_PLATFORM->bindings_sz = 8; - } - - if (GLOBAL_PLATFORM->bindings_len + 1 >= GLOBAL_PLATFORM->bindings_sz) { - void* m = - realloc(GLOBAL_PLATFORM->bindings, GLOBAL_PLATFORM->bindings_sz + 8); - if (m == NULL) { - ERROR("Failed to allocate 8 bytes (%d): %s", errno, strerror(errno)); - exit(EXIT_FAILURE); - } - GLOBAL_PLATFORM->bindings_sz += 8; - } - - LOG("Bindings in ctx[%d]:", GLOBAL_PLATFORM->bindings_len); - for (isize i = 0; i < ctx->len; i++) { - switch (ctx->bindings[i].action.type) { - case InputType_error: - LOG("(error)"); - break; - - case InputType_action: - LOG("(action) %s", ctx->bindings[i].action.action.callback_str); - break; - - case InputType_state: - LOG("(+state) %s", ctx->bindings[i].action.state.activate_str); - LOG("(-state) %s", ctx->bindings[i].action.state.deactivate_str); - break; - - case InputType_range: - LOG("(range) --unhandled--"); - break; - } - } - - GLOBAL_PLATFORM->bindings[GLOBAL_PLATFORM->bindings_len++] = ctx; -} - -/* Pops an input context from the input stack */ -void engine_input_ctx_pop(void) { - if (GLOBAL_PLATFORM->bindings == NULL || GLOBAL_PLATFORM->bindings_sz == 0) - return; - i_ctx_t_free(GLOBAL_PLATFORM->bindings[--GLOBAL_PLATFORM->bindings_len]); -} - -/* Removes all input contexts from the input stack */ -void engine_input_ctx_reset(void) { - while (GLOBAL_PLATFORM->bindings_len > 0) { - i_ctx_t_free(GLOBAL_PLATFORM->bindings[--GLOBAL_PLATFORM->bindings_len]); - } -} - -void get_mousepos(double *x, double *y) { - glfwGetCursorPos(GLOBAL_PLATFORM->window->window, x, y); -} diff --git a/src/ctrl/src/input.c b/src/ctrl/src/input.c index 5fe643e..cbfd576 100644 --- a/src/ctrl/src/input.c +++ b/src/ctrl/src/input.c @@ -1,8 +1,10 @@ +#include +#include + #include #include #include #include -#include extern input_callback_t* callbacks[128]; extern usize callbacks_len; @@ -330,3 +332,59 @@ void i_bind_alt(binding_t* b, scancode_t s) { b->scancode_alt = s; } + +/* Pushes an input context onto the input handling stack */ +void i_ctx_push(i_ctx* ctx) { + if (GLOBAL_PLATFORM->bindings == NULL) { + GLOBAL_PLATFORM->bindings = calloc(8, sizeof(i_ctx*)); + GLOBAL_PLATFORM->bindings_sz = 8; + } + + if (GLOBAL_PLATFORM->bindings_len + 1 >= GLOBAL_PLATFORM->bindings_sz) { + void* m = + realloc(GLOBAL_PLATFORM->bindings, GLOBAL_PLATFORM->bindings_sz + 8); + if (m == NULL) { + ERROR("Failed to allocate 8 bytes (%d): %s", errno, strerror(errno)); + exit(EXIT_FAILURE); + } + GLOBAL_PLATFORM->bindings_sz += 8; + } + + LOG("Bindings in ctx[%d]:", GLOBAL_PLATFORM->bindings_len); + for (isize i = 0; i < ctx->len; i++) { + switch (ctx->bindings[i].action.type) { + case InputType_error: + LOG("(error)"); + break; + + case InputType_action: + LOG("(action) %s", ctx->bindings[i].action.action.callback_str); + break; + + case InputType_state: + LOG("(+state) %s", ctx->bindings[i].action.state.activate_str); + LOG("(-state) %s", ctx->bindings[i].action.state.deactivate_str); + break; + + case InputType_range: + LOG("(range) --unhandled--"); + break; + } + } + + GLOBAL_PLATFORM->bindings[GLOBAL_PLATFORM->bindings_len++] = ctx; +} + +/* Pops an input context from the input stack */ +void i_ctx_pop(void) { + if (GLOBAL_PLATFORM->bindings == NULL || GLOBAL_PLATFORM->bindings_sz == 0) + return; + i_ctx_t_free(GLOBAL_PLATFORM->bindings[--GLOBAL_PLATFORM->bindings_len]); +} + +/* Removes all input contexts from the input stack */ +void i_ctx_reset(void) { + while (GLOBAL_PLATFORM->bindings_len > 0) { + i_ctx_t_free(GLOBAL_PLATFORM->bindings[--GLOBAL_PLATFORM->bindings_len]); + } +} diff --git a/src/rendering/include/engine/rendering/window.h b/src/rendering/include/engine/rendering/window.h index 1111250..9d44855 100644 --- a/src/rendering/include/engine/rendering/window.h +++ b/src/rendering/include/engine/rendering/window.h @@ -43,6 +43,8 @@ void destroy_window(Window* w); // Renderer intializer(s) void init_render_opengl(Window* w); +void get_mousepos(double *x, double *y); + #undef API #ifdef __cplusplus } diff --git a/src/rendering/src/window.c b/src/rendering/src/window.c index f684272..1315424 100644 --- a/src/rendering/src/window.c +++ b/src/rendering/src/window.c @@ -198,3 +198,16 @@ void destroy_window(Window* w) { ERROR("Destroying unknown renderer type."); } } + +void get_mousepos(double *x, double *y) { + Window* w = GLOBAL_PLATFORM->window; + + switch(w->framework) { + case WINDOW_FRAMEWORK_GLFW: + glfwGetCursorPos(GLOBAL_PLATFORM->window->window, x, y); + break; + default: + ERROR("get_mouse_pos not implemented for chosen framework."); + } + +} -- cgit v1.3