From 92f63bbdbfc214849c203511bbcb1be0a4865588 Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 15 Feb 2024 11:33:11 +0100 Subject: Proper input system --- src/GFX/Mouse.cpp | 24 ------------------------ src/GFX/Mouse.hpp | 20 -------------------- src/GFX/Window.cpp | 26 ++++++++------------------ src/GFX/Window.hpp | 26 +++++++++++--------------- 4 files changed, 19 insertions(+), 77 deletions(-) delete mode 100644 src/GFX/Mouse.cpp delete mode 100644 src/GFX/Mouse.hpp (limited to 'src/GFX') diff --git a/src/GFX/Mouse.cpp b/src/GFX/Mouse.cpp deleted file mode 100644 index 49f6972..0000000 --- a/src/GFX/Mouse.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "Mouse.hpp" - -namespace MC::GFX { - -Vector<2> Mouse::update(GLFWwindow* window) { - Real x, y; - glfwGetCursorPos(window, &x, &y); - - if (m_first_event) { - m_last_x = x; - m_last_y = y; - - m_first_event = false; - } - - Vector<2> movement{static_cast(x) - m_last_x, static_cast(y) - m_last_y}; - - m_last_x = x; - m_last_y = y; - - return movement; -} - -} \ No newline at end of file diff --git a/src/GFX/Mouse.hpp b/src/GFX/Mouse.hpp deleted file mode 100644 index ad940d4..0000000 --- a/src/GFX/Mouse.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include -#include "../Math/Vector.hpp" - -namespace MC::GFX { - -class Mouse { -public: - Mouse() = default; - - Vector<2> update(GLFWwindow* window); -private: - Bool m_first_event = true; - - Real m_last_x = 0.0f; - Real m_last_y = 0.0f; -}; - -} \ No newline at end of file diff --git a/src/GFX/Window.cpp b/src/GFX/Window.cpp index 33e76d5..5c391c8 100644 --- a/src/GFX/Window.cpp +++ b/src/GFX/Window.cpp @@ -3,10 +3,12 @@ #include "../Common/Assert.hpp" #include "../ThreadRole.hpp" #include "Window.hpp" +#include "../Common/Casts.hpp" +#include namespace MC::GFX { -Window::Window(const Char* title, U32 width, U32 height) { +Window::Window(std::string const& title, U32 const width, U32 const height) { ASSERT_MAIN_THREAD(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); @@ -14,7 +16,7 @@ Window::Window(const Char* title, U32 width, U32 height) { glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_DOUBLEBUFFER, GL_TRUE); - m_window = glfwCreateWindow(width, height, title, nullptr, nullptr); + m_window = glfwCreateWindow(TO(I32, width), TO(I32, height), title.c_str(), nullptr, nullptr); if (m_window == nullptr) { throw std::runtime_error("Failed to create window."); } @@ -36,32 +38,20 @@ GLFWwindow* Window::get() const { return m_window; } -void Window::close() { +void Window::close() const { glfwSetWindowShouldClose(m_window, true); } -Vector<2> Window::mouse_delta() { - return m_mouse.update(m_window); -} - -Bool Window::key(I32 key, I32 type) const { - return glfwGetKey(m_window, key) == type; -} - -Bool Window::mouse(I32 key, I32 type) const { - return glfwGetMouseButton(m_window, key) == type; -} - -void Window::start_render() { +void Window::start_render() const { glfwSwapBuffers(m_window); } -void Window::poll_events() { +void Window::poll_events() const { ASSERT_MAIN_THREAD(); glfwPollEvents(); } -void Window::on_size_change(void (callback)(GLFWwindow*, I32, I32)) { +void Window::on_size_change(void (callback)(GLFWwindow*, I32, I32)) const { glfwSetFramebufferSizeCallback(m_window, callback); } diff --git a/src/GFX/Window.hpp b/src/GFX/Window.hpp index c26b0fd..86920b5 100644 --- a/src/GFX/Window.hpp +++ b/src/GFX/Window.hpp @@ -1,35 +1,31 @@ #pragma once -#include "../Common/Sizes.hpp" -#include "../Math/Vector.hpp" +#include #include -#include "Mouse.hpp" +#include "../Common/Pure.hpp" +#include "../Common/Sizes.hpp" namespace MC::GFX { class Window { public: - Window(const Char* title, U32 width, U32 height); + Window(std::string const& title, U32 width, U32 height); ~Window(); - GLFWwindow* get() const; + PURE GLFWwindow* get() const; - void on_size_change(void (* callback)(GLFWwindow*, I32, I32)); + void on_size_change(void (* callback)(GLFWwindow*, I32, I32)) const; void attach() const; void detach() const; - void close(); - void start_render(); - void poll_events(); - Vector<2> mouse_delta(); + void close() const; + void start_render() const; + void poll_events() const; - Bool key(I32 key, I32 type) const; - Bool mouse(I32 key, I32 type) const; - Bool should_close() const; + PURE Bool should_close() const; private: GLFWwindow* m_window; - Mouse m_mouse; }; -} \ No newline at end of file +} -- cgit 1.4.1