diff options
Diffstat (limited to '')
| -rw-r--r-- | platformer/src/window.c | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/platformer/src/window.c b/platformer/src/window.c new file mode 100644 index 0000000..1f4d517 --- /dev/null +++ b/platformer/src/window.c @@ -0,0 +1,175 @@ +/* + * ============================================================ + * FILE: window.c + * + * PURPOSE: Creates and owns the single game window and its OpenGL context. + * This is the ONLY file that calls GLFW window/context functions + * and glewInit. (controls.c calls GLFW input functions.) + * + * FITS IN: main.c calls window_init() first and window_shutdown() last. + * Every frame main.c calls window_poll_events() and window_swap(). + * Implements the interface in window.h. + * + * IMPLEMENT: + * - Private state: `static GLFWwindow *s_window;` + * - Include order matters: <GL/glew.h> BEFORE <GLFW/glfw3.h>. + * - window_init(): + * 1. glfwSetErrorCallback -> print GLFW errors to stderr. + * 2. glfwInit(). Note that this also initializes GLFW's joystick/gamepad + * support, which controls.c depends on. + * 3. Hints: GL 3.3, GLFW_OPENGL_CORE_PROFILE, GLFW_OPENGL_FORWARD_COMPAT. + * 4. Create a WINDOW_WIDTH x WINDOW_HEIGHT window titled WINDOW_TITLE + * (config.h), make its context current. + * 5. glewExperimental = GL_TRUE; glewInit(). With a core profile, glewInit + * can leave a harmless GL_INVALID_ENUM behind, so call glGetError() + * once to clear it and add a comment explaining why. + * 6. glfwSwapInterval(1) for vsync. + * 7. Framebuffer-size callback -> glViewport(0, 0, w, h). + * 8. Enable GL_DEPTH_TEST here (it's global GL state set once). + * On any failure: print a clear message, clean up, return false. + * - window_framebuffer_size / window_aspect: use glfwGetFramebufferSize + * (NOT the window size, because they differ on HiDPI displays). Guard against + * height == 0 (minimized window) so the aspect never divides by zero. + * - Everything else is a thin wrapper over the matching glfw* call. + * + * CONVENTIONS: `s_` prefix for file-private statics. Messages go to stderr. + * + * NOT IN SCOPE: Input handling (that's controls.c), drawing (render.c), + * fullscreen, window icons, resizable-minimum-size logic. + * + * DONE WHEN: A window opens at the configured size and title with a + * 3.3 core context, closes cleanly, and resizing it keeps the + * 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; +} |
