]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/window.c
Improve diagnostics on GLFW failure
[dragonblocks_alpha.git] / src / client / window.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <GL/glew.h>
4 #include <GL/gl.h>
5 #include "client/client_config.h"
6 #include "client/debug_menu.h"
7 #include "client/game.h"
8 #include "client/opengl.h"
9 #include "client/gui.h"
10 #include "client/input.h"
11 #include "client/window.h"
12
13 struct Window window;
14
15 static int small_x, small_y, small_width, small_height;
16
17 static void update_projection()
18 {
19         mat4x4_perspective(window.projection,
20                 window.fov / 180.0f * M_PI,
21                 (float) window.width / (float) window.height,
22                 0.01f, client_config.view_distance + 28.0f);
23 }
24
25 static void framebuffer_size_callback(__attribute__((unused)) GLFWwindow *handle, int width, int height)
26 {
27         glViewport(0, 0, width, height); GL_DEBUG
28         window.width = width;
29         window.height = height;
30
31         if (!window.fullscreen) {
32                 small_width = width;
33                 small_height = height;
34         }
35
36         update_projection();
37         gui_update_projection();
38 }
39
40 static void cursor_pos_callback(__attribute__((unused)) GLFWwindow *handle, double x, double y)
41 {
42         input_cursor(x, y);
43 }
44
45 static void window_pos_callback(__attribute__((unused)) GLFWwindow *handle, int x, int y)
46 {
47         if (!window.fullscreen) {
48                 small_x = x;
49                 small_y = y;
50         }
51 }
52
53 static void mouse_button_callback(__attribute__((unused)) GLFWwindow *handle, int button, int action, __attribute__((unused)) int mods)
54 {
55         if ((button == GLFW_MOUSE_BUTTON_RIGHT || button == GLFW_MOUSE_BUTTON_LEFT) && action == GLFW_PRESS)
56                 input_click(button == GLFW_MOUSE_BUTTON_LEFT);
57 }
58
59 static void error_callback(__attribute__((unused)) int error, const char *description)
60 {
61         fprintf(stderr, "[warning] GLFW error: %s\n", description);
62 }
63
64 void window_enter_fullscreen()
65 {
66         window.fullscreen = true;
67         GLFWmonitor *monitor = glfwGetPrimaryMonitor();
68         const GLFWvidmode *vidmode = glfwGetVideoMode(monitor);
69         glfwSetWindowMonitor(window.handle, monitor, 0, 0, vidmode->width, vidmode->height, 0);
70
71         debug_menu_changed(ENTRY_FULLSCREEN);
72 }
73
74 void window_exit_fullscreen()
75 {
76         window.fullscreen = false;
77         glfwSetWindowMonitor(window.handle, NULL, small_x, small_y, small_width, small_height, 0);
78
79         debug_menu_changed(ENTRY_FULLSCREEN);
80 }
81
82 void window_init()
83 {
84         if(!glfwInit()) {
85                 fprintf(stderr, "[error] failed to initialize GLFW\n");
86                 abort();
87         }
88
89         glfwSetErrorCallback(&error_callback);
90
91         glfwWindowHint(GLFW_SAMPLES, client_config.antialiasing);
92         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
93         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
94         glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
95
96         window.width = 1250;
97         window.height = 750;
98         window.handle = glfwCreateWindow(window.width, window.height, "Dragonblocks", NULL, NULL);
99         window.fullscreen = false;
100         window.fov = 86.1f;
101         update_projection();
102
103         small_width = window.width;
104         small_height = window.height;
105
106         if (!window.handle) {
107                 fprintf(stderr, "[error] failed to create window (does your machine support OpenGL 3.3?)\n");
108                 glfwTerminate();
109                 abort();
110         }
111
112         glfwMakeContextCurrent(window.handle);
113
114         if (!client_config.vsync)
115                 glfwSwapInterval(0);
116
117         if (glewInit() != GLEW_OK) {
118                 fprintf(stderr, "[error] failed to initialize GLEW\n");
119                 abort();
120         }
121
122         glfwSetFramebufferSizeCallback(window.handle, &framebuffer_size_callback);
123         glfwSetCursorPosCallback(window.handle, &cursor_pos_callback);
124         glfwSetWindowPosCallback(window.handle, &window_pos_callback);
125         glfwSetMouseButtonCallback(window.handle, &mouse_button_callback);
126 }