From e1b0ea16142beb29d2fbfdf4f02524eb117aee6d Mon Sep 17 00:00:00 2001 From: onelin Date: Thu, 18 Dec 2025 21:52:13 +0100 Subject: Add instance to window_init And make a mess in the meantime:( --- src/daw.c | 13 +++++++------ src/include/daw/platform_glfw.h | 4 +++- src/include/daw/window.h | 4 +++- src/platform_glfw.c | 13 ++++++++----- src/window.c | 2 +- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/daw.c b/src/daw.c index 25091c5..67b9178 100644 --- a/src/daw.c +++ b/src/daw.c @@ -113,6 +113,10 @@ Instance* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight /* initialize resources */ Resources* resources = calloc(1, sizeof(Resources)); + // Until we've gotten rid of this global, it is important to set it before + // `window_new`. + GLOBAL_PLATFORM = p; + w = Window_new(&Platform_GLFW, windowtitle, WINDOW_FRAMEWORK_GLFW, WINDOW_RENDERER_OPENGL, (ivec2){windowsize[0], windowsize[1]}, flags); @@ -133,8 +137,6 @@ Instance* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight INFO("Available cores: %d", nproc()); - GLOBAL_PLATFORM = p; - #ifdef DAW_BUILD_HOTRELOAD #define State(name) \ @@ -213,8 +215,7 @@ void framebuffer_size_callback_default(ivec3* dst,ivec2 src) { } void camera_reset_callback_default(Camera* dst, void* state, ivec2 src) { - *dst = (Camera)DEFAULT_CAMERA; - glm_ortho_default(45.f, dst->per); + r_perspective_ortho(dst, 45.f, src); } @@ -268,7 +269,7 @@ i32 engine_run(Instance* p, StateType initial_state, void* state_arg) { w->render_targets->framebuffer_size_callback[0] = &framebuffer_size_callback_default; w->render_targets->camera_reset_callback[0] = &camera_reset_callback_default; - camera_reset_callback_default(&default_camera, wsz); + camera_reset_callback_default(&default_camera, mem->data, wsz); w->render_targets->cam[0] = &default_camera; } @@ -369,7 +370,7 @@ i32 engine_run(Instance* p, StateType initial_state, void* state_arg) { // TODO set if null ..? w->render_targets->framebuffer_size_callback[0] = &framebuffer_size_callback_default; w->render_targets->camera_reset_callback[0] = &camera_reset_callback_default; - camera_reset_callback_default(&default_camera, (void*)(mem->data), wsz); + camera_reset_callback_default(&default_camera, mem->data, wsz); w->render_targets->cam[0] = &default_camera; } diff --git a/src/include/daw/platform_glfw.h b/src/include/daw/platform_glfw.h index 8311c5a..a877261 100644 --- a/src/include/daw/platform_glfw.h +++ b/src/include/daw/platform_glfw.h @@ -10,7 +10,9 @@ extern "C" { #include #include -Window* window_init_glfw(const char *restrict windowtitle, ivec2 windowsize, const u32 flags); +struct Instance; + +Window* window_init_glfw(struct Instance *restrict instance, 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); void window_size_glfw(Window *restrict w, ivec2 *restrict dst); diff --git a/src/include/daw/window.h b/src/include/daw/window.h index 3a4d1b0..3fe432d 100644 --- a/src/include/daw/window.h +++ b/src/include/daw/window.h @@ -16,6 +16,8 @@ extern "C" { #define DAW_WINDOW_FULLSCREEN (1 << 1) #define DAW_WINDOW_RESIZEABLE (1 << 2) +struct Instance; + typedef enum { WINDOW_FRAMEWORK_NONE = 0, WINDOW_FRAMEWORK_GLFW, @@ -67,7 +69,7 @@ struct Platform { * Returns: * A pointer to a struct Window, NULL on error. */ - Window* (*window_init)(const char *restrict title, ivec2 windowsize, const u32 flags); + Window* (*window_init)(struct Instance *restrict instance, 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/platform_glfw.c b/src/platform_glfw.c index 33614ff..ea743b5 100644 --- a/src/platform_glfw.c +++ b/src/platform_glfw.c @@ -11,6 +11,7 @@ #include +#include #include static void window_resize_callback(GLFWwindow *restrict window, int width, int height); @@ -20,7 +21,7 @@ static void render_init_opengl(Window *restrict w, const u32 flags); void window_size_glfw(GLFWwindow *restrict w, ivec2 *restrict dst); -Window* window_init_glfw(const char *restrict windowtitle, ivec2 windowsize, const u32 flags) { +Window* window_init_glfw(Instance *restrict instance, const char *restrict windowtitle, ivec2 windowsize, const u32 flags) { Window* ret = NULL; GLFWwindow* window = NULL; @@ -104,7 +105,7 @@ Window* window_init_glfw(const char *restrict windowtitle, ivec2 windowsize, con } // TODO: set this to `ret` once all the garbage is moved to `struct Window` - glfwSetWindowUserPointer(window, (void*)ret); + glfwSetWindowUserPointer(window, (void*)instance); render_init_opengl(ret, flags); @@ -147,7 +148,8 @@ void window_poll_glfw(void) { // Helper function implementations static void window_resize_callback(GLFWwindow* window, int width, int height) { (void)width; (void)height; - Window* w = glfwGetWindowUserPointer(window); + Instance* i = glfwGetWindowUserPointer(window); + Window* w = i->window; glfwSwapBuffers(window); if (w != NULL) { const GladGLContext* gl = w->context; @@ -157,7 +159,8 @@ static void window_resize_callback(GLFWwindow* window, int width, int height) { } static void framebuffer_resize_callback(GLFWwindow* window, int width, int height) { - Window* w = glfwGetWindowUserPointer(window); + Instance* i = glfwGetWindowUserPointer(window); + Window* w = i->window; if (w != NULL) { const GladGLContext* gl = w->context; //TODO: Move the camera to window->renderer @@ -166,7 +169,7 @@ static void framebuffer_resize_callback(GLFWwindow* window, int width, int heigh w->windowsize[0] = width; w->windowsize[1] = height; - window_reset_cameras(w, w->render_targets); + window_reset_cameras(w, i->mem->data, w->render_targets); //r_reset_camera(c); } DEBUG("FRAMEBUFFER RESIZE <%d,%d>\n", width, height); diff --git a/src/window.c b/src/window.c index e9ec62c..79e9a42 100644 --- a/src/window.c +++ b/src/window.c @@ -48,7 +48,7 @@ Window* Window_new(const struct Platform* p, const char *restrict title, Window_ case WINDOW_RENDERER_OPENGL: /* For now, pass instance as NULL, fix it once all the necessary bs is * out of struct Instance */ - w = p->window_init(title, size, flags); + w = p->window_init(GLOBAL_PLATFORM, title, size, flags); // Manually reset bindings et. al. w->bindings = NULL; -- cgit v1.3