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