improve OpenGL initialization

added more comments, logging and made some default values more explicit
This commit is contained in:
2025-06-03 10:48:04 +02:00
parent bec9ae4051
commit 991c071920

View File

@@ -2,6 +2,10 @@
// Licensed under the MIT Licence. See LICENSE for details // Licensed under the MIT Licence. See LICENSE for details
#include "window.h" #include "window.h"
#include <stdio.h>
#include "../error.h"
// include before GLFW // include before GLFW
#define GLAD_GL_IMPLEMENTATION #define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h> #include <glad/gl.h>
@@ -21,22 +25,37 @@
static GLFWwindow* win = NULL; static GLFWwindow* win = NULL;
int window_init(void) { int window_init(void) {
#ifndef NDEBUG
glfwWindowHint(GLFW_CONTEXT_DEBUG, GLFW_TRUE);
#endif
// initialize the window // initialize the window
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // sets the profile to "core", so old, deprecated functions are disabled.
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_RED_BITS, 8);
glfwWindowHint(GLFW_GREEN_BITS, 8);
glfwWindowHint(GLFW_BLUE_BITS, 8);
glfwWindowHint(GLFW_ALPHA_BITS, 0);
win = glfwCreateWindow(WIN_DEFAULT_WIDTH, WIN_DEFAULT_HEIGHT, WIN_NAME, NULL, NULL); win = glfwCreateWindow(WIN_DEFAULT_WIDTH, WIN_DEFAULT_HEIGHT, WIN_NAME, NULL, NULL);
if (!win) return 1; if (!win) return 1;
// setup OpenGL
glfwMakeContextCurrent(win);
if (!gladLoadGL(glfwGetProcAddress)) return 1;
// configure callbacks // configure callbacks
glfwSetKeyCallback(win, key_callback); glfwSetKeyCallback(win, key_callback);
glfwSwapInterval(1); // wait 1 screen update for a redraw a.k.a. "vsync". (not really applicable in this case but eh) glfwSwapInterval(1); // wait 1 screen update for a redraw a.k.a. "vsync". (not really applicable in this case but eh)
// setup OpenGL // print the OpenGL version information
glfwMakeContextCurrent(win); debug("version info:");
if (!gladLoadGL(glfwGetProcAddress)) return 1; debug("\tvendor: %s", glGetString(GL_VENDOR));
debug("\trenderer: %s", glGetString(GL_RENDERER));
debug("\tversion: %s", glGetString(GL_VERSION));
debug("\tshading lang: %s", glGetString(GL_SHADING_LANGUAGE_VERSION));
return 0; return 0;
} }