summaryrefslogtreecommitdiff
path: root/src/rendering
diff options
context:
space:
mode:
authoronelin <oscar@nelin.dk>2025-04-13 09:28:24 +0000
committeronelin <oscar@nelin.dk>2025-05-02 18:07:56 +0000
commitd0d3236687e265e6507a275fae506b17f2c85f6a (patch)
treea28710f2608b54e70d190371a06c240d09dcbe89 /src/rendering
parentaa35d972ef76d23f90d602b684b87510ddbb6fc0 (diff)
Move bindings to Window
Diffstat (limited to 'src/rendering')
-rw-r--r--src/rendering/include/engine/rendering/platform.h2
-rw-r--r--src/rendering/include/engine/rendering/platform_glfw.h6
-rw-r--r--src/rendering/include/engine/rendering/window.h9
-rw-r--r--src/rendering/src/platform_glfw.c38
-rw-r--r--src/rendering/src/window.c33
5 files changed, 70 insertions, 18 deletions
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;