summaryrefslogtreecommitdiff
path: root/src/rendering/src
diff options
context:
space:
mode:
authoronelin <oscar@nelin.dk>2025-04-11 09:48:42 +0000
committeronelin <oscar@nelin.dk>2025-04-11 09:51:56 +0000
commit0dfd07a115ab626c97080dbe87e3c4904f091041 (patch)
treea53861eb1f6bf570ba1a5422179c817662352bc1 /src/rendering/src
parentf6b60693e298ca9c155be75ed2e4c687f3fa0fa5 (diff)
Remove old glfw window src
Diffstat (limited to 'src/rendering/src')
-rw-r--r--src/rendering/src/platform_glfw.c3
-rw-r--r--src/rendering/src/window.c227
2 files changed, 8 insertions, 222 deletions
diff --git a/src/rendering/src/platform_glfw.c b/src/rendering/src/platform_glfw.c
index 8237593..38c55a0 100644
--- a/src/rendering/src/platform_glfw.c
+++ b/src/rendering/src/platform_glfw.c
@@ -4,6 +4,7 @@
#undef GLAD_GL_IMPLEMENTATION
// TODO: import vulkan thingymajig once I get around to it.
+// TODO: move OpenGL initialization code at some point
#undef GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
@@ -101,6 +102,8 @@ Window* window_init_glfw(const Instance *restrict i, const char *restrict window
INFO("WINDOW SIZE: %d x %d -> %d x %d", wsize[0], wsize[1], ret->windowsize[0], ret->windowsize[1]);
}
+ // TODO: set this to `ret` once all the garbage is moved to `struct Window`
+ glfwSetWindowUserPointer(window, (void*)i);
render_init_opengl(ret, flags);
diff --git a/src/rendering/src/window.c b/src/rendering/src/window.c
index 4cf35f2..e784158 100644
--- a/src/rendering/src/window.c
+++ b/src/rendering/src/window.c
@@ -19,54 +19,6 @@
extern Instance* GLOBAL_PLATFORM;
-void (*window_poll_events)(void) = NULL;
-
-/* wrapper to get time in ms */
-u64 (*get_time)(void) = NULL;
-
-/* GLFW And vulkan spaghetti boiler */
-void glfw_err_callback(int code, const char* description) {
- ERROR("glfw [%d]: %s\n", code, description);
- // Terminate?
- exit(EXIT_FAILURE);
-}
-
-void window_resize_callback(GLFWwindow* window, int width, int height) {
- (void)width; (void)height;
- const GladGLContext* gl = GLOBAL_PLATFORM->window->context;
- glfwSwapBuffers(window);
- gl->Finish();
-}
-
-void framebuffer_resize_callback(GLFWwindow* window, int width, int height) {
- (void)window;
- const GladGLContext* gl = GLOBAL_PLATFORM->window->context;
- Camera* c = GLOBAL_PLATFORM->cam;
- gl->Viewport(0,0, width, height);
- GLOBAL_PLATFORM->window->windowsize[0] = width;
- GLOBAL_PLATFORM->window->windowsize[1] = height;
-
- r_reset_camera(c);
-}
-
-void window_resize(Window* window, int width, int height) {
- /* TODO: Handle different frameworks */
- window_resize_callback(window->window, width, height);
- framebuffer_resize_callback(window->window, width, height);
-}
-
-/* This is very glfw specific */
-GladGLContext* create_context(GLFWwindow *window) {
- glfwMakeContextCurrent(window);
-
- GladGLContext* context = (GladGLContext*)malloc(sizeof(GladGLContext));
- if (!context) return NULL;
-
- int version = gladLoadGLContext(context, glfwGetProcAddress);
- INFO("Loaded OpenGL %d.%d", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
-
- return context;
-}
/* Should honestly just write my own */
static inline u64 glfw_gettime_msec(void) {
@@ -87,183 +39,14 @@ static inline u64 platform_get_time_usec(void) {
}
return (u64)(t.tv_sec * 1000000 + t.tv_nsec / 1000);
}
-#define DAW_WINDOW_VSYNC (1 << 0)
-#define DAW_WINDOW_FULLSCREEN (1 << 1)
-#define DAW_WINDOW_RESIZEABLE (1 << 2)
-
-Window* init_window_glfw(
- const char* windowtitle, ivec2 windowsize,
- const u32 flags
- ) {
- Window* ret = NULL;
- GLFWwindow* window = NULL;
-
- glfwSetErrorCallback(&glfw_err_callback);
-
- INFO_("initializing glfw...");
- if (glfwInit() == GLFW_FALSE) {
- const char *desc;
- int code = glfwGetError(&desc);
- ERROR("failed to initialize glfw [%d]: %s\n", code, *desc);
- exit(EXIT_FAILURE);
- } else
- printf("ok\n");
-
-
- INFO_("initializing window...");
- //glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
- if (!(flags & DAW_WINDOW_RESIZEABLE)) {
- glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
- }
-
- glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
- glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
-
- glfwWindowHint(GLFW_SAMPLES, 0); // Disable anti aliasing
-
- // Use a modern opengl version
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
-
- // Lean and mean
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
-
-#ifdef __APPLE__
- // To make MacOS happy; should not be needed
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
-#endif
-
- /* "On Wayland specifically, you need to swap the buffers
- * once of a window for it to become visible." */
- {
- GLFWmonitor* mon = NULL;
- if (flags & DAW_WINDOW_FULLSCREEN) mon = glfwGetPrimaryMonitor();
-
- window = glfwCreateWindow(windowsize[0], windowsize[1], windowtitle, mon, NULL);
- }
-
- if (window == NULL) {
- ERROR("Failed to create GLFW window!\n");
- const char *desc;
- int code = glfwGetError(&desc);
- ERROR("failed to initialize glfw window [%d]: %s\n", code, desc);
- exit(EXIT_FAILURE);
- } else
- printf("ok\n");
-
- // Setup callbacks
- glfwSetFramebufferSizeCallback(window, framebuffer_resize_callback);
- glfwSetWindowSizeCallback(window, window_resize_callback);
- glfwSetKeyCallback(window, (GLFWkeyfun)key_callback);
-
- // Create the window datastructure
- ret = (Window*)calloc(1, sizeof(Window));
- ret->framework = WINDOW_FRAMEWORK_GLFW;
- ret->renderer = WINDOW_RENDERER_NONE;
- ret->window = window;
- /* Last parameter is used for the renderer */
- ret->context = NULL;
-
- {
- ivec2 wsize;
- vec2 wscaling;
- glfwGetWindowContentScale(window, &wscaling[0], &wscaling[1]);
- glfwGetWindowSize(window, &wsize[0], &wsize[1]);
-
- ret->windowsize[0] = (i32)((f32)wsize[0] * wscaling[0]);
- ret->windowsize[1] = (i32)((f32)wsize[1] * wscaling[1]);
-
- INFO("WINDOW CONTENT SCALING: %.2f x %.2f", wscaling[0], wscaling[1]);
- INFO("WINDOW SIZE: %d x %d -> %d x %d", wsize[0], wsize[1], ret->windowsize[0], ret->windowsize[1]);
- }
-
-
- window_poll_events = &glfwPollEvents;
- //get_time = &platform_get_time_usec;
- get_time = &platform_get_time_usec;
-
- return ret;
-}
-
-/* Initializes opengl using the window */
-void init_render_opengl(Window* w, const u32 flags) {
- if (w == NULL || w->window == NULL) {
- ERROR("Window is not initialized");
- return;
- }
-
- if (w->renderer != WINDOW_RENDERER_NONE || w->context != NULL) {
- ERROR("Window already initialized with a renderer!");
- return;
- }
-
- if (w->framework != WINDOW_FRAMEWORK_GLFW) {
- ERROR("Trying to initialize OpenGL with incompatible window");
- return;
- }
-
- // This is GLFW specific
- GladGLContext *ctx = create_context((GLFWwindow*)w->window);
-
- if (ctx == NULL) {
- ERROR("Failed to create glad context");
- exit(EXIT_FAILURE);
- }
-
- if (w->framework == WINDOW_FRAMEWORK_GLFW) {
- if (flags & DAW_WINDOW_VSYNC) {
- glfwSwapInterval(1);
- } else {
- glfwSwapInterval(0);
- }
-
-
- ctx->Viewport(0, 0, w->windowsize[0], w->windowsize[1]);
- }
-
-#ifdef _DEBUG
- ctx->ClearColor((float)0x10 / 255.f, (float)0x0a / 255.f, (float)0x33 / 255.f, 0.f);
-#else
- ctx->ClearColor(0x0, 0x0, 0x0, 0.f);
-#endif
-
- // Make sure faces closest to the camera are drawn on-top of faces that are
- // further away
- ctx->Enable(GL_DEPTH_TEST);
- ctx->DepthFunc(GL_LESS);
-
- w->context = ctx;
- w->renderer = WINDOW_RENDERER_OPENGL;
-}
-void destroy_window_glfw(GLFWwindow* w) {
- glfwDestroyWindow(w);
+/* wrapper to get time in ms */
+u64 (*get_time)(void) = platform_get_time_usec;
- // If we ever do multi-window support, we need to make sure this is the last
- // window before terminating
- glfwTerminate();
-}
-void destroy_window(Window* w) {
- switch(w->framework) {
- case WINDOW_FRAMEWORK_GLFW:
- destroy_window_glfw(w->window);
- w->window = NULL;
- break;
- default:
- ERROR("Destroying unknown window type.");
- }
-
- switch(w->renderer) {
- case WINDOW_RENDERER_OPENGL:
- // Missing unloader function in glad MX library
- free(w->context);
- w->context = NULL;
- break;
- default:
- ERROR("Destroying unknown renderer type.");
- }
-}
+#define DAW_WINDOW_VSYNC (1 << 0)
+#define DAW_WINDOW_FULLSCREEN (1 << 1)
+#define DAW_WINDOW_RESIZEABLE (1 << 2)
void get_mousepos(double *x, double *y) {
Window* w = GLOBAL_PLATFORM->window;