]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/window.c
Client: unify error handling
[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/gl_debug.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 void window_enter_fullscreen()
60 {
61         window.fullscreen = true;
62         GLFWmonitor *monitor = glfwGetPrimaryMonitor();
63         const GLFWvidmode *vidmode = glfwGetVideoMode(monitor);
64         glfwSetWindowMonitor(window.handle, monitor, 0, 0, vidmode->width, vidmode->height, 0);
65
66         debug_menu_changed(ENTRY_FULLSCREEN);
67 }
68
69 void window_exit_fullscreen()
70 {
71         window.fullscreen = false;
72         glfwSetWindowMonitor(window.handle, NULL, small_x, small_y, small_width, small_height, 0);
73
74         debug_menu_changed(ENTRY_FULLSCREEN);
75 }
76
77 void window_init()
78 {
79         if(!glfwInit()) {
80                 fprintf(stderr, "[error] failed to initialize GLFW\n");
81                 abort();
82         }
83
84         glfwWindowHint(GLFW_SAMPLES, client_config.antialiasing);
85         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
86         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
87         glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
88
89         window.width = 1250;
90         window.height = 750;
91         window.handle = glfwCreateWindow(window.width, window.height, "Dragonblocks", NULL, NULL);
92         window.fullscreen = false;
93         window.fov = 86.1f;
94         update_projection();
95
96         small_width = window.width;
97         small_height = window.height;
98
99         if (!window.handle) {
100                 fprintf(stderr, "[error] failed to create window\n");
101                 glfwTerminate();
102                 abort();
103         }
104
105         glfwMakeContextCurrent(window.handle);
106
107         if (!client_config.vsync)
108                 glfwSwapInterval(0);
109
110         if (glewInit() != GLEW_OK) {
111                 fprintf(stderr, "[error] failed to initialize GLEW\n");
112                 abort();
113         }
114
115         glfwSetFramebufferSizeCallback(window.handle, &framebuffer_size_callback);
116         glfwSetCursorPosCallback(window.handle, &cursor_pos_callback);
117         glfwSetWindowPosCallback(window.handle, &window_pos_callback);
118         glfwSetMouseButtonCallback(window.handle, &mouse_button_callback);
119 }