summaryrefslogtreecommitdiff
path: root/platformer/src/window.c
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2026-09-23 23:40:11 -0400
committerSchark <jordan@schark.online>2026-09-23 23:40:11 -0400
commit23ef8bc20310bdfad52b07a3f1e4e61c98896841 (patch)
tree007cc1d6d73ebb38d2e06422d4050f769dd65535 /platformer/src/window.c
parentdab65412376d1b73381446f969c1461c24169811 (diff)
downloadgamedev-23ef8bc20310bdfad52b07a3f1e4e61c98896841.tar.gz
gamedev-23ef8bc20310bdfad52b07a3f1e4e61c98896841.zip
Working demogit add README.md src/
Diffstat (limited to 'platformer/src/window.c')
-rw-r--r--platformer/src/window.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/platformer/src/window.c b/platformer/src/window.c
index 455b249..1f4d517 100644
--- a/platformer/src/window.c
+++ b/platformer/src/window.c
@@ -42,3 +42,134 @@
* viewport correct.
* ============================================================
*/
+
+/* GLEW must come before GLFW: it defines the GL headers GLFW would include. */
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+
+#include <stdio.h>
+
+#include "config.h"
+#include "window.h"
+
+static GLFWwindow *s_window;
+
+static void on_glfw_error(int code, const char *description)
+{
+ fprintf(stderr, "GLFW error %d: %s\n", code, description);
+}
+
+static void on_framebuffer_resize(GLFWwindow *window, int w, int h)
+{
+ (void)window;
+ glViewport(0, 0, w, h);
+}
+
+bool window_init(void)
+{
+ glfwSetErrorCallback(on_glfw_error);
+
+ /* Also initializes GLFW's joystick/gamepad support, used by controls.c. */
+ if (!glfwInit()) {
+ fprintf(stderr, "window: glfwInit failed\n");
+ return false;
+ }
+
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
+ glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+ glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
+
+ s_window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, NULL, NULL);
+ if (!s_window) {
+ fprintf(stderr, "window: could not create a %dx%d window with an OpenGL 3.3 core context\n",
+ WINDOW_WIDTH, WINDOW_HEIGHT);
+ glfwTerminate();
+ return false;
+ }
+ glfwMakeContextCurrent(s_window);
+
+ /* glewExperimental makes GLEW load core-profile functions it would
+ * otherwise skip because it can't find them in the extension string. */
+ glewExperimental = GL_TRUE;
+ GLenum glew_status = glewInit();
+ if (glew_status != GLEW_OK) {
+ fprintf(stderr, "window: glewInit failed: %s\n", glewGetErrorString(glew_status));
+ glfwDestroyWindow(s_window);
+ s_window = NULL;
+ glfwTerminate();
+ return false;
+ }
+ /* In a core profile, glewInit calls glGetString(GL_EXTENSIONS), which no
+ * longer exists, so it leaves a harmless GL_INVALID_ENUM behind. Clear it
+ * so it doesn't look like a bug in our own code later. */
+ glGetError();
+
+ glfwSwapInterval(1); /* vsync: no tearing, and no burning a CPU core */
+
+ glfwSetFramebufferSizeCallback(s_window, on_framebuffer_resize);
+ int w, h;
+ glfwGetFramebufferSize(s_window, &w, &h);
+ glViewport(0, 0, w, h);
+
+ glEnable(GL_DEPTH_TEST);
+ return true;
+}
+
+void window_shutdown(void)
+{
+ if (s_window) {
+ glfwDestroyWindow(s_window);
+ s_window = NULL;
+ }
+ glfwTerminate();
+}
+
+bool window_should_close(void)
+{
+ return glfwWindowShouldClose(s_window);
+}
+
+void window_request_close(void)
+{
+ glfwSetWindowShouldClose(s_window, GLFW_TRUE);
+}
+
+void window_poll_events(void)
+{
+ glfwPollEvents();
+}
+
+void window_swap(void)
+{
+ glfwSwapBuffers(s_window);
+}
+
+double window_time(void)
+{
+ return glfwGetTime();
+}
+
+/* Framebuffer size, NOT window size: they differ on HiDPI displays. */
+void window_framebuffer_size(int *w, int *h)
+{
+ glfwGetFramebufferSize(s_window, w, h);
+}
+
+float window_aspect(void)
+{
+ int w, h;
+ window_framebuffer_size(&w, &h);
+ if (h <= 0) return 1.0f; /* minimized window: avoid dividing by zero */
+ return (float)w / (float)h;
+}
+
+void window_set_title(const char *title)
+{
+ glfwSetWindowTitle(s_window, title);
+}
+
+struct GLFWwindow *window_handle(void)
+{
+ return s_window;
+}