handle window exits more gracefully and appropriately.

This commit is contained in:
2025-09-15 14:15:40 +02:00
parent 1e10fec9c6
commit dc3abf992b
5 changed files with 41 additions and 8 deletions

View File

@@ -2,9 +2,8 @@
* Licensed under the MIT Licence. See LICENSE for details */
#include "render.h"
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include <glad/gl.h>
#include <stdint.h>
#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;
}

View File

@@ -2,9 +2,9 @@
* Licensed under the MIT Licence. See LICENSE for details */
#pragma once
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include <glad/gl.h>
int render_init(void);
void render_update(GLFWwindow *win);
void render_free(void);

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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;