]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/window.c
808c36f304a2249d8ae996d89ac4ceb11ac9d898
[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/scene.h"
10 #include "client/window.h"
11 #include "util.h"
12
13 struct Window window;
14
15 static void framebuffer_size_callback(unused GLFWwindow *handle, int width, int height)
16 {
17         glViewport(0, 0, width, height);
18
19         if (! window.fullscreen) {
20                 window.small_bounds.width = width;
21                 window.small_bounds.height = height;
22         }
23
24         scene_on_resize(width, height);
25         gui_on_resize(width, height);
26         game_on_resize(width, height);
27 }
28
29 static void cursor_pos_callback(unused GLFWwindow *handle, double current_x, double current_y)
30 {
31         input_on_cursor_pos(current_x, current_y);
32 }
33
34 static void window_pos_callback(unused GLFWwindow *handle, int x, int y)
35 {
36         if (! window.fullscreen) {
37                 window.small_bounds.x = x;
38                 window.small_bounds.y = y;
39         }
40 }
41
42 void window_enter_fullscreen()
43 {
44         window.fullscreen = true;
45         GLFWmonitor *monitor = glfwGetPrimaryMonitor();
46         const GLFWvidmode *vidmode = glfwGetVideoMode(monitor);
47         glfwSetWindowMonitor(window.handle, monitor, 0, 0, vidmode->width, vidmode->height, 0);
48
49         debug_menu_update_fullscreen();
50 }
51
52 void window_exit_fullscreen()
53 {
54         window.fullscreen = false;
55         glfwSetWindowMonitor(window.handle, NULL, window.small_bounds.x, window.small_bounds.y, window.small_bounds.width, window.small_bounds.height, 0);
56
57         debug_menu_update_fullscreen();
58 }
59
60 bool window_init(int width, int height)
61 {
62         if(! glfwInit()) {
63                 fprintf(stderr, "Failed to initialize GLFW\n");
64                 return false;
65         }
66
67         glfwWindowHint(GLFW_SAMPLES, client_config.antialiasing);
68         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
69         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
70         glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
71
72         window.handle = glfwCreateWindow(width, height, "Dragonblocks", NULL, NULL);
73
74         window.small_bounds.width = width;
75         window.small_bounds.height = height;
76
77         if (! window.handle) {
78                 fprintf(stderr, "Failed to create window\n");
79                 glfwTerminate();
80                 return false;
81         }
82
83         glfwMakeContextCurrent(window.handle);
84
85         if (glewInit() != GLEW_OK) {
86                 fprintf(stderr, "Failed to initialize GLEW\n");
87                 return false;
88         }
89
90         glfwSetFramebufferSizeCallback(window.handle, &framebuffer_size_callback);
91         glfwSetCursorPosCallback(window.handle, &cursor_pos_callback);
92         glfwSetWindowPosCallback(window.handle, &window_pos_callback);
93
94         return true;
95 }