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