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