]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Cleanup GLFW callbacks and add fullscreen key
authorElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 12 Jul 2021 17:43:11 +0000 (19:43 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 12 Jul 2021 17:43:11 +0000 (19:43 +0200)
src/camera.c
src/camera.h
src/client.c
src/hud.c
src/hud.h
src/input.c
src/input.h

index d759e75ae43914d5f6f754431d244cd138662b9d..6964f0aa1dfdc4dd1cfa10844c7fd346877c2954 100644 (file)
@@ -60,7 +60,7 @@ void set_camera_angle(f32 yaw, f32 pitch)
        update_camera();
 }
 
-void set_window_size(int width, int height)
+void camera_on_resize(int width, int height)
 {
        mat4x4_perspective(camera.projection, 86.1f / 180.0f * M_PI, (float) width / (float) height, 0.01f, 1000.0f);
 }
index b03b1ab6503285b44426787680542912d1220469..affdbcf7e72f1550e94da2c6ce0c7592239980d5 100644 (file)
@@ -10,7 +10,7 @@
 void init_camera(GLFWwindow *window, ShaderProgram *prog);
 void set_camera_position(v3f pos);
 void set_camera_angle(f32 yaw, f32 pitch);
-void set_window_size(int width, int height);
+void camera_on_resize(int width, int height);
 void camera_enable();
 
 extern struct movement_dirs
index a4d56ae0ca5c599b085d7260761873ebc1263852..b4c17fbf37a44db3dabe8a7e0b6181f64656400e 100644 (file)
@@ -51,8 +51,20 @@ static void *reciever_thread(__attribute__((unused)) void *unused)
 static void framebuffer_size_callback(__attribute__((unused)) GLFWwindow* window, int width, int height)
 {
        glViewport(0, 0, width, height);
-       set_window_size(width, height);
-       hud_rescale(width, height);
+
+       camera_on_resize(width, height);
+       hud_on_resize(width, height);
+       input_on_resize(width, height);
+}
+
+static void cursor_pos_callback(__attribute__((unused)) GLFWwindow* window, double current_x, double current_y)
+{
+       input_on_cursor_pos(current_x, current_y);
+}
+
+static void window_pos_callback(__attribute__((unused)) GLFWwindow* window, int x, int y)
+{
+       input_on_window_pos(x, y);
 }
 
 static void client_loop()
@@ -103,18 +115,21 @@ static void client_loop()
        set_camera_position((v3f) {0.0f, 0.0f, 0.0f});
        set_camera_angle(0.0f, 0.0f);
 
-       set_window_size(width, height);
+       camera_on_resize(width, height);
 
        hud_init(prog);
-       hud_rescale(width, height);
+       hud_on_resize(width, height);
 
        hud_add(RESSOURCEPATH "textures/crosshair.png", (v3f) {0.0f, 0.0f, 0.0f}, (v2f) {1.0f, 1.0f}, HUD_SCALE_TEXTURE);
 
        init_input(&client, window);
+       input_on_resize(width, height);
 
        clientplayer_add_to_scene(&client.player);
 
        glfwSetFramebufferSizeCallback(window, &framebuffer_size_callback);
+       glfwSetCursorPosCallback(window, &cursor_pos_callback);
+       glfwSetWindowPosCallback(window, &window_pos_callback);
 
        struct timespec ts, ts_old;
        clock_gettime(CLOCK_REALTIME, &ts_old);
index 47ee4abe0967d5947696311dbded253a1f6843de..fe20893e128a8b866c5f931d3d8ab3bff53a2b50 100644 (file)
--- a/src/hud.c
+++ b/src/hud.c
@@ -75,7 +75,7 @@ static void element_transform(HUDElement *element)
 #pragma GCC diagnostic pop
 }
 
-void hud_rescale(int width, int height)
+void hud_on_resize(int width, int height)
 {
        hud.width = width;
        hud.height = height;
index 4e476d3ab547c34ba52d54c7b392520c046ac5ce..90c21b8ee1754ed7e491e98e0541ead9b5abf8cd 100644 (file)
--- a/src/hud.h
+++ b/src/hud.h
@@ -26,7 +26,7 @@ typedef struct
 
 void hud_init(ShaderProgram *prog);
 void hud_deinit();
-void hud_rescale(int width, int height);
+void hud_on_resize(int width, int height);
 void hud_render();
 HUDElement *hud_add(char *texture, v3f pos, v2f scale, HUDScaleType scale_type);
 
index 65cbcd0b502e22c27fdf68103952f5a76db4b038..6d9784cf30998b794031fd45970c79e14290303e 100644 (file)
@@ -17,10 +17,13 @@ static struct
        Client *client;
        HUDElement *pause_menu_hud;
        bool paused;
+       bool fullscreen;
        KeyListener pause_listener;
+       KeyListener fullscreen_listener;
+       int small_x, small_y, small_width, small_height;
 } input;
 
-static void cursor_pos_callback(__attribute__((unused)) GLFWwindow* window, double current_x, double current_y)
+void input_on_cursor_pos(double current_x, double current_y)
 {
        if (input.paused)
                return;
@@ -40,6 +43,22 @@ static void cursor_pos_callback(__attribute__((unused)) GLFWwindow* window, doub
        set_camera_angle(input.client->player.yaw, input.client->player.pitch);
 }
 
+void input_on_resize(int width, int height)
+{
+       if (! input.fullscreen) {
+               input.small_width = width;
+               input.small_height = height;
+       }
+}
+
+void input_on_window_pos(int x, int y)
+{
+       if (! input.fullscreen) {
+               input.small_x = x;
+               input.small_y = y;
+       }
+}
+
 static bool move(int forward, int backward, vec3 dir)
 {
        f32 sign;
@@ -83,6 +102,7 @@ static KeyListener create_key_listener(int key)
 void process_input()
 {
        do_key_listener(&input.pause_listener);
+       do_key_listener(&input.fullscreen_listener);
 
        if (input.pause_listener.fired) {
                input.paused = ! input.paused;
@@ -95,6 +115,18 @@ void process_input()
                }
        }
 
+       if (input.fullscreen_listener.fired) {
+               input.fullscreen = ! input.fullscreen;
+
+               if (input.fullscreen) {
+                       GLFWmonitor *monitor = glfwGetPrimaryMonitor();
+                       const GLFWvidmode *vidmode = glfwGetVideoMode(monitor);
+                       glfwSetWindowMonitor(input.window, monitor, 0, 0, vidmode->width, vidmode->height, 0);
+               } else {
+                       glfwSetWindowMonitor(input.window, NULL, input.small_x, input.small_y, input.small_width, input.small_height, 0);
+               }
+       }
+
        input.client->player.velocity.x = 0.0f;
        input.client->player.velocity.z = 0.0f;
 
@@ -115,8 +147,8 @@ void init_input(Client *client, GLFWwindow *window)
        input.paused = false;
 
        input.pause_listener = create_key_listener(GLFW_KEY_ESCAPE);
+       input.fullscreen_listener = create_key_listener(GLFW_KEY_F11);
 
-       glfwSetCursorPosCallback(input.window, &cursor_pos_callback);
        input.pause_menu_hud = hud_add(RESSOURCEPATH "textures/pause_layer.png", (v3f) {-1.0f, -1.0f, 0.5f}, (v2f) {1.0f, 1.0f}, HUD_SCALE_SCREEN);
 
        enter_game();
index 6b93a7031969fdf5d8b5dac8fe99f976c32ec1cb..4ad202aac85544e36e3c34e0f1ecc21846c22946 100644 (file)
@@ -5,5 +5,8 @@
 
 void process_input();
 void init_input(Client *client, GLFWwindow *window);
+void input_on_cursor_pos(double current_x, double current_y);
+void input_on_resize(int width, int height);
+void input_on_window_pos(int x, int y);
 
 #endif