From dc3abf992b546b22c180e86d57a6eb13cebb0e60 Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 15 Sep 2025 14:15:40 +0200 Subject: [PATCH] handle window exits more gracefully and appropriately. --- src/io/render.c | 12 ++++++++++-- src/io/render.h | 4 ++-- src/io/window.c | 10 +++++++++- src/io/window.h | 17 +++++++++++++++-- src/main.c | 6 +++++- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/io/render.c b/src/io/render.c index 42e04ab..2964487 100644 --- a/src/io/render.c +++ b/src/io/render.c @@ -2,9 +2,8 @@ * Licensed under the MIT Licence. See LICENSE for details */ #include "render.h" -#include - #include +#include #include #include "../error.h" @@ -80,3 +79,12 @@ void render_update(GLFWwindow *win) { glBindVertexArray(vao); glDrawArrays(GL_POINTS, 0, VERTC); } + +void render_free(void) { + glDeleteVertexArrays(1, &vao); + glDeleteBuffers(1, &vbo); + glDeleteProgram(pipe); + vbo = 0; + vao = 0; + pipe = 0; +} diff --git a/src/io/render.h b/src/io/render.h index 3245a2b..33a2814 100644 --- a/src/io/render.h +++ b/src/io/render.h @@ -2,9 +2,9 @@ * Licensed under the MIT Licence. See LICENSE for details */ #pragma once -#include - #include +#include int render_init(void); void render_update(GLFWwindow *win); +void render_free(void); diff --git a/src/io/window.c b/src/io/window.c index 2b2e093..0c34df2 100644 --- a/src/io/window.c +++ b/src/io/window.c @@ -69,5 +69,13 @@ void window_loop(void) { glfwSwapBuffers(win); } - glfwDestroyWindow(win); +void window_close(void) { + assert(win); + glfwSetWindowShouldClose(win, 1); +} + +void window_free(void) { + glfwDestroyWindow(win); + render_free(); + win = NULL; } diff --git a/src/io/window.h b/src/io/window.h index 12ca51b..90b3f85 100644 --- a/src/io/window.h +++ b/src/io/window.h @@ -2,5 +2,18 @@ * Licensed under the MIT Licence. See LICENSE for details */ #pragma once -int window_init(void); // initializes the global window, returns non-zero upon failure -void window_loop(void); // performs the window updates +/* Set up the window, enabling OpenGL, and + * configuring the settings that are needed. + * Returns `0` upon success, otherwise `1`. */ +int window_init(void); + +/* Calls the update loop for the window. + * This function does not exit until the window does. */ +void window_loop(void); + +/* Requests the window to close (gracefully). */ +void window_close(void); + +/* Cleans up all resources held by the window. + * If the window is still open, it will be terminated. */ +void window_free(void); diff --git a/src/main.c b/src/main.c index d657284..386cece 100644 --- a/src/main.c +++ b/src/main.c @@ -14,6 +14,10 @@ static void error_callback(int err, const char *const msg) { } static void quit(void) { + window_free(); + + /* terminates GLFW; destroying any + * remaining windows, or other resources held by GLFW. */ glfwTerminate(); } @@ -30,7 +34,7 @@ int main(int argc, char **argv) { fatal("failed to initialise!"); window_loop(); - quit(); + window_free(); // return success, since some architectures do not follow 0=success return EXIT_SUCCESS;