diff options
| author | onelin <oscar@nelin.dk> | 2025-04-13 09:28:24 +0000 |
|---|---|---|
| committer | onelin <oscar@nelin.dk> | 2025-05-02 18:07:56 +0000 |
| commit | d0d3236687e265e6507a275fae506b17f2c85f6a (patch) | |
| tree | a28710f2608b54e70d190371a06c240d09dcbe89 | |
| parent | aa35d972ef76d23f90d602b684b87510ddbb6fc0 (diff) | |
Move bindings to Window
| -rw-r--r-- | src/core/include/engine/core/platform.h | 7 | ||||
| -rw-r--r-- | src/core/src/loop.c | 10 | ||||
| -rw-r--r-- | src/ctrl/src/input.c | 28 | ||||
| -rw-r--r-- | src/rendering/include/engine/rendering/platform.h | 2 | ||||
| -rw-r--r-- | src/rendering/include/engine/rendering/platform_glfw.h | 6 | ||||
| -rw-r--r-- | src/rendering/include/engine/rendering/window.h | 9 | ||||
| -rw-r--r-- | src/rendering/src/platform_glfw.c | 38 | ||||
| -rw-r--r-- | src/rendering/src/window.c | 33 |
8 files changed, 88 insertions, 45 deletions
diff --git a/src/core/include/engine/core/platform.h b/src/core/include/engine/core/platform.h index 0a1bb45..de39f71 100644 --- a/src/core/include/engine/core/platform.h +++ b/src/core/include/engine/core/platform.h @@ -9,7 +9,6 @@ extern "C" { #include <engine/core/types.h> #include <engine/core/memory.h> -#include <engine/ctrl/input.h> #include <engine/rendering/window.h> #include <engine/resources.h> @@ -37,12 +36,6 @@ typedef struct Instance { memory* mem; - /* The ctrl is probably the only sensible thing in this struct. */ - usize bindings_sz; - usize bindings_len; - i_ctx** bindings; - - binding_t bindings_global[NUM_GLOBAL_BINDINGS]; } Instance; #ifdef __cplusplus diff --git a/src/core/src/loop.c b/src/core/src/loop.c index 56567f6..4e9df36 100644 --- a/src/core/src/loop.c +++ b/src/core/src/loop.c @@ -116,7 +116,9 @@ Instance* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight Resources* resources = calloc(1, sizeof(Resources)); // TODO: Initialize them :) - w = Platform_GLFW.window_init(p, windowtitle, (ivec2){windowsize[0], windowsize[1]}, flags); + w = Window_new(windowtitle, + WINDOW_FRAMEWORK_GLFW, WINDOW_RENDERER_OPENGL, + (ivec2){windowsize[0], windowsize[1]}, flags); p->window = w; p->quit = false; @@ -132,10 +134,6 @@ Instance* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight p->mem = memory_new(initial_memory); - p->bindings = NULL; - p->bindings_sz = 0; - p->bindings_len = 0; - p->cam = &default_camera; glm_ortho_default(45.f, p->cam->per); @@ -149,7 +147,7 @@ Instance* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight #ifdef DAW_BUILD_HOTRELOAD #define State(name) \ - if (!State_reload(STATE_##name, p->bindings, p->bindings_len)) { \ + if (!State_reload(STATE_##name, p->window->bindings, p->window->bindings_len)) { \ ERROR("Failed to reload shared object file for state %s", #name); \ }; diff --git a/src/ctrl/src/input.c b/src/ctrl/src/input.c index 9da4114..52d5ce4 100644 --- a/src/ctrl/src/input.c +++ b/src/ctrl/src/input.c @@ -42,8 +42,8 @@ extern Instance* GLOBAL_PLATFORM; void key_callback(void* window, int key, int scancode, int action, int mods) { - const i_ctx* bindings = *GLOBAL_PLATFORM->bindings; - const usize bindings_len = GLOBAL_PLATFORM->bindings_len; + const i_ctx* bindings = *GLOBAL_PLATFORM->window->bindings; + const usize bindings_len = GLOBAL_PLATFORM->window->bindings_len; const u64 now = get_time(); @@ -369,22 +369,22 @@ void i_bind_alt(binding_t* b, keycode_t 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->window->bindings == NULL) { + GLOBAL_PLATFORM->window->bindings = calloc(8, sizeof(i_ctx*)); + GLOBAL_PLATFORM->window->bindings_sz = 8; } - if (GLOBAL_PLATFORM->bindings_len + 1 >= GLOBAL_PLATFORM->bindings_sz) { + if (GLOBAL_PLATFORM->window->bindings_len + 1 >= GLOBAL_PLATFORM->window->bindings_sz) { void* m = - realloc(GLOBAL_PLATFORM->bindings, GLOBAL_PLATFORM->bindings_sz + 8); + realloc(GLOBAL_PLATFORM->window->bindings, GLOBAL_PLATFORM->window->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; + GLOBAL_PLATFORM->window->bindings_sz += 8; } - LOG("Bindings in ctx[%d]:", GLOBAL_PLATFORM->bindings_len); + LOG("Bindings in ctx[%d]:", GLOBAL_PLATFORM->window->bindings_len); for (usize i = 0; i < ctx->len; i++) { switch (ctx->bindings[i].action.type) { case InputType_error: @@ -406,19 +406,19 @@ void i_ctx_push(i_ctx* ctx) { } } - GLOBAL_PLATFORM->bindings[GLOBAL_PLATFORM->bindings_len++] = ctx; + GLOBAL_PLATFORM->window->bindings[GLOBAL_PLATFORM->window->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) + if (GLOBAL_PLATFORM->window->bindings == NULL || GLOBAL_PLATFORM->window->bindings_sz == 0) return; - i_ctx_t_free(GLOBAL_PLATFORM->bindings[--GLOBAL_PLATFORM->bindings_len]); + i_ctx_t_free(GLOBAL_PLATFORM->window->bindings[--GLOBAL_PLATFORM->window->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]); + while (GLOBAL_PLATFORM->window->bindings_len > 0) { + i_ctx_t_free(GLOBAL_PLATFORM->window->bindings[--GLOBAL_PLATFORM->window->bindings_len]); } } diff --git a/src/rendering/include/engine/rendering/platform.h b/src/rendering/include/engine/rendering/platform.h index 9497fdb..ea51c47 100644 --- a/src/rendering/include/engine/rendering/platform.h +++ b/src/rendering/include/engine/rendering/platform.h @@ -32,7 +32,7 @@ struct Platform { * Returns: * A pointer to a struct Window, NULL on error. */ - Window* (*window_init)(const Instance *restrict i, const char *restrict title, ivec2 windowsize, const u32 flags); + Window* (*window_init)(const char *restrict title, ivec2 windowsize, const u32 flags); /* Destroy, close, and free up resources related to the window and the * platform library specific resources. diff --git a/src/rendering/include/engine/rendering/platform_glfw.h b/src/rendering/include/engine/rendering/platform_glfw.h index 056d130..949968d 100644 --- a/src/rendering/include/engine/rendering/platform_glfw.h +++ b/src/rendering/include/engine/rendering/platform_glfw.h @@ -1,5 +1,5 @@ -#ifndef PLATFORM_GLFW_H -#define PLATFORM_GLFW_H +#ifndef ENGINE_RENDERING_PLATFORM_GLFW_H +#define ENGINE_RENDERING_PLATFORM_GLFW_H #ifdef __cplusplus extern "C" { @@ -11,7 +11,7 @@ extern "C" { #include <engine/rendering/platform.h> #include <engine/rendering/window.h> -Window* window_init_glfw(const Instance *restrict i, const char *restrict windowtitle, ivec2 windowsize, const u32 flags); +Window* window_init_glfw(const char *restrict windowtitle, ivec2 windowsize, const u32 flags); void window_destroy_glfw(Window *restrict w); void window_resize_glfw(Window *restrict window, int width, int height); bool window_should_close_glfw(Window *restrict window); diff --git a/src/rendering/include/engine/rendering/window.h b/src/rendering/include/engine/rendering/window.h index 10d4ddd..a964b38 100644 --- a/src/rendering/include/engine/rendering/window.h +++ b/src/rendering/include/engine/rendering/window.h @@ -8,9 +8,9 @@ extern "C" { #include <cglm/ivec2.h> #include <engine/core/types.h> +#include <engine/ctrl/input.h> #ifndef ENGINE_RENDERING_WINDOW_H_EXCLUDE_EXTERNS -extern void (*window_poll_events)(void); extern u64 (*get_time)(void); #endif @@ -36,8 +36,15 @@ typedef struct { // Subject to change to a union of backend-dependent structs void* window; void* context; + + /* The ctrl is probably the only sensible thing in this struct. */ + usize bindings_sz; + usize bindings_len; + i_ctx** bindings; } Window; +Window* Window_new(const char *restrict title, Window_framework framework, Window_renderer renderer, ivec2 size, u32 flags); + void get_mousepos(double *x, double *y); #ifdef __cplusplus diff --git a/src/rendering/src/platform_glfw.c b/src/rendering/src/platform_glfw.c index 38c55a0..47638e2 100644 --- a/src/rendering/src/platform_glfw.c +++ b/src/rendering/src/platform_glfw.c @@ -11,15 +11,16 @@ #include <engine/core/logging.h> -#include <engine/rendering/platform_glfw.h> +#include <engine/rendering/platform.h> static void window_resize_callback(GLFWwindow *restrict window, int width, int height); static void framebuffer_resize_callback(GLFWwindow *restrict window, int width, int height); static void glfw_err_callback(int code, const char* description); static void render_init_opengl(Window *restrict w, const u32 flags); +static void window_size(GLFWwindow *restrict w, ivec2 *restrict dst); -Window* window_init_glfw(const Instance *restrict i, const char *restrict windowtitle, ivec2 windowsize, const u32 flags) { +Window* window_init_glfw(const char *restrict windowtitle, ivec2 windowsize, const u32 flags) { Window* ret = NULL; GLFWwindow* window = NULL; @@ -103,7 +104,7 @@ Window* window_init_glfw(const Instance *restrict i, const char *restrict window } // TODO: set this to `ret` once all the garbage is moved to `struct Window` - glfwSetWindowUserPointer(window, (void*)i); + glfwSetWindowUserPointer(window, (void*)ret); render_init_opengl(ret, flags); @@ -145,25 +146,26 @@ void window_poll_glfw(void) { // Helper function implementations static void window_resize_callback(GLFWwindow* window, int width, int height) { (void)width; (void)height; - Instance* i = glfwGetWindowUserPointer(window); + Window* w = glfwGetWindowUserPointer(window); glfwSwapBuffers(window); - if (i != NULL) { - const GladGLContext* gl = i->window->context; + if (w != NULL) { + const GladGLContext* gl = w->context; gl->Finish(); } } static void framebuffer_resize_callback(GLFWwindow* window, int width, int height) { (void)width; (void)height; - Instance* i = glfwGetWindowUserPointer(window); - if (i != NULL) { - const GladGLContext* gl = i->window->context; - Camera* c = i->cam; + Window* w = glfwGetWindowUserPointer(window); + if (w != NULL) { + const GladGLContext* gl = w->context; + //TODO: Move the camera to window->renderer + //Camera* c = w->cam; gl->Viewport(0,0, width, height); - i->window->windowsize[0] = width; - i->window->windowsize[1] = height; + w->windowsize[0] = width; + w->windowsize[1] = height; - r_reset_camera(c); + //r_reset_camera(c); } } @@ -218,3 +220,13 @@ static void render_init_opengl(Window *restrict w, const u32 flags) { w->context = ctx; w->renderer = WINDOW_RENDERER_OPENGL; } + +static void window_size(GLFWwindow *restrict w, ivec2 *restrict dst) { + ivec2 wsize; + vec2 wscaling; + glfwGetWindowContentScale(w, &wscaling[0], &wscaling[1]); + glfwGetWindowSize(w, &wsize[0], &wsize[1]); + + *dst[0] = (i32)((f32)wsize[0] * wscaling[0]); + *dst[1] = (i32)((f32)wsize[1] * wscaling[1]); +} diff --git a/src/rendering/src/window.c b/src/rendering/src/window.c index e78ede0..d761543 100644 --- a/src/rendering/src/window.c +++ b/src/rendering/src/window.c @@ -8,6 +8,9 @@ #define ENGINE_RENDERING_WINDOW_H_EXCLUDE_EXTERNS #include <engine/rendering/window.h> +#undef ENGINE_RENDERING_WINDOW_H_EXCLUDE_EXTERNS + +#include <engine/rendering/platform_glfw.h> #include <glad/gl.h> @@ -36,6 +39,36 @@ u64 (*get_time)(void) = platform_get_time_usec; #define DAW_WINDOW_FULLSCREEN (1 << 1) #define DAW_WINDOW_RESIZEABLE (1 << 2) +// Wrapper to get a specific window and set up related structures. +Window* Window_new(const char *restrict title, Window_framework framework, Window_renderer renderer, ivec2 size, u32 flags) { + Window* w = NULL; + + switch (framework) { + case WINDOW_FRAMEWORK_GLFW: + switch (renderer) { + case WINDOW_RENDERER_OPENGL: + /* For now, pass instance as NULL, fix it once all the necessary bs is + * out of struct Instance */ + w = Platform_GLFW.window_init(title, size, flags); + + w->bindings = NULL; + w->bindings_sz = 0; + w->bindings_len = 0; + + /// TODO + //w->cam = &default_camera; + return w; + break; + default: + ERROR("Unsupported renderer."); + } + break; + default: + ERROR("Unsupported framework."); + } + return NULL; +} + void get_mousepos(double *x, double *y) { Window* w = GLOBAL_PLATFORM->window; |
