A 3D game engine from scratch.
at main 79 lines 2.7 kB view raw
1// (c) 2020 Vlad-Stefan Harbuz <vlad@vladh.net> 2 3#pragma once 4 5#include <GLFW/glfw3.h> 6#include "types.hpp" 7 8class input { 9public: 10 static constexpr u32 MAX_TEXT_INPUT_LENGTH = 512; 11 static constexpr u32 MAX_TEXT_INPUT_COMMAND_LENGTH = 50; 12 static constexpr u32 MAX_TEXT_INPUT_ARGUMENTS_LENGTH = 13 MAX_TEXT_INPUT_LENGTH - MAX_TEXT_INPUT_COMMAND_LENGTH; 14 15 enum class CursorType { 16 none, 17 arrow, 18 ibeam, 19 crosshair, 20 hand, 21 hresize, 22 vresize, 23 }; 24 25 struct State { 26 GLFWwindow *window; 27 bool is_cursor_enabled; 28 v2 mouse_pos; 29 v2 mouse_offset; 30 v2 mouse_3d_offset; 31 f64 mouse_3d_sensitivity; 32 bool mouse_button_states[GLFW_MOUSE_BUTTON_LAST]; 33 u32 n_mouse_button_state_changes_this_frame[GLFW_MOUSE_BUTTON_LAST]; 34 bool key_states[GLFW_KEY_LAST]; 35 u32 n_key_state_changes_this_frame[GLFW_KEY_LAST]; 36 GLFWcursor *current_cursor; 37 GLFWcursor *arrow_cursor; 38 GLFWcursor *ibeam_cursor; 39 GLFWcursor *crosshair_cursor; 40 GLFWcursor *hand_cursor; 41 GLFWcursor *hresize_cursor; 42 GLFWcursor *vresize_cursor; 43 bool have_ever_gotten_mouse_pos; 44 bool is_text_input_enabled; 45 char text_input[MAX_TEXT_INPUT_LENGTH]; 46 }; 47 48 static char * get_text_input(); 49 static bool is_cursor_enabled(); 50 static void set_is_cursor_enabled(bool val); 51 static bool is_text_input_enabled(); 52 static v2 get_mouse_offset(); 53 static v2 get_mouse_3d_offset(); 54 static void update_mouse_button(int button, int action, int mods); 55 static bool is_mouse_button_down(int button); 56 static bool is_mouse_button_up(int button); 57 static bool is_mouse_button_now_down(int button); 58 static bool is_mouse_button_now_up(int button); 59 static void update_mouse(v2 new_mouse_pos); 60 static void clear_text_input(); 61 static void enable_text_input(); 62 static void disable_text_input(); 63 static void do_text_input_backspace(); 64 static void update_text_input(u32 codepoint); 65 static void update_keys(int key, int scancode, int action, int mods); 66 static bool is_key_down(int key); 67 static bool is_key_up(int key); 68 static bool is_key_now_down(int key); 69 static bool is_key_now_up(int key); 70 static bool is_mouse_in_bb(v2 topleft, v2 bottomright); 71 static void set_cursor(GLFWcursor *new_cursor); 72 static void set_cursor(CursorType type); 73 static void reset_n_mouse_button_state_changes_this_frame(); 74 static void reset_n_key_state_changes_this_frame(); 75 static void init(input::State *state, GLFWwindow *window); 76 77private: 78 static input::State *state; 79};