summaryrefslogtreecommitdiff
path: root/platformer/src/window.c
blob: 1f4d517e29c94c521e88ba258e56102bc1115561 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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;
}