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