diff --git a/include/blt/gfx/input.h b/include/blt/gfx/input.h new file mode 100644 index 0000000..93f3a02 --- /dev/null +++ b/include/blt/gfx/input.h @@ -0,0 +1,100 @@ +#pragma once +/* + * Created by Brett on 15/12/23. + * Licensed under GNU General Public License V3.0 + * See LICENSE file for license detail + */ + +#ifndef BLT_WITH_GRAPHICS_TEMPLATE_INPUT_H +#define BLT_WITH_GRAPHICS_TEMPLATE_INPUT_H + +#include +#include +#include + +namespace blt::gfx +{ + enum class KEY_STATE : std::uint8_t + { + RELEASE = 0, PRESS = 1, REPEAT = 2 + }; + + enum class MOUSE_STATE : std::uint8_t + { + RELEASE = 0, PRESS = 1 + }; + + class input_manager + { + private: + KEY_STATE* key_state; + MOUSE_STATE* mouse_state; + std::size_t key_size = 256; + std::size_t mouse_size = 16; + + void expand_keys(std::size_t min) + { + auto new_size = std::max(key_size + 256, min); + auto* new_key = new KEY_STATE[new_size]; + std::memset(new_key, 0, new_size * sizeof(KEY_STATE)); + std::memcpy(new_key, key_state, key_size * sizeof(KEY_STATE)); + delete[] key_state; + key_state = new_key; + key_size = new_size; + } + + void expand_mouse(std::size_t min) + { + auto new_size = std::max(mouse_size + 16, min); + auto* new_mouse = new MOUSE_STATE[new_size]; + std::memset(new_mouse, 0, new_size * sizeof(MOUSE_STATE)); + std::memcpy(new_mouse, mouse_state, mouse_size * sizeof(MOUSE_STATE)); + delete[] mouse_state; + mouse_state = new_mouse; + mouse_size = new_size; + } + + public: + input_manager() + { + key_state = new KEY_STATE[key_size]; + mouse_state = new MOUSE_STATE[mouse_size]; + std::memset(key_state, 0, key_size * sizeof(KEY_STATE)); + std::memset(mouse_state, 0, mouse_size * sizeof(MOUSE_STATE)); + } + + KEY_STATE& key(std::size_t key) + { + if (key >= key_size) + expand_keys(key); + return key_state[key]; + } + + bool isKeyPressed(std::size_t key){ + if (key >= key_size) + expand_keys(key); + return key_state[key] != KEY_STATE::RELEASE; + } + + MOUSE_STATE& mouse(std::size_t mouse) + { + if (mouse >= mouse_size) + expand_mouse(mouse); + return mouse_state[mouse]; + } + + bool isMousePressed(std::size_t mouse){ + if (mouse >= mouse_size) + expand_mouse(mouse); + return mouse_state[mouse] != MOUSE_STATE::RELEASE; + } + + ~input_manager() + { + delete[] key_state; + delete[] mouse_state; + } + }; +} + +#endif //BLT_WITH_GRAPHICS_TEMPLATE_INPUT_H diff --git a/libraries/BLT b/libraries/BLT index d589056..bda044d 160000 --- a/libraries/BLT +++ b/libraries/BLT @@ -1 +1 @@ -Subproject commit d589056a8df584bebc8cdf0bc077613a56367221 +Subproject commit bda044de1eda8a556c5174461227ae12c8c891d3 diff --git a/src/blt/gfx/window.cpp b/src/blt/gfx/window.cpp index f67ce87..4b2aa81 100644 --- a/src/blt/gfx/window.cpp +++ b/src/blt/gfx/window.cpp @@ -6,6 +6,7 @@ #include #include #include +#include void error_callback(int error, const char* description) { @@ -18,9 +19,10 @@ namespace blt::gfx // because we aren't meant to have multiple GLFW windows (especially with GLAD) we will keep the window as global state.1 struct { - GLFWwindow* window; - std::int32_t width; - std::int32_t height; + GLFWwindow* window = nullptr; + input_manager inputManager; + std::int32_t width = 0; + std::int32_t height = 0; } window_state; void create_callbacks()