diff --git a/quark/CMakeLists.txt b/quark/CMakeLists.txt index dabe61d..c3e8b99 100644 --- a/quark/CMakeLists.txt +++ b/quark/CMakeLists.txt @@ -13,6 +13,7 @@ target_sources(quark src/quark/LayerStack.cpp src/platform/linux/LinuxWindow.cpp + src/platform/linux/LinuxInput.cpp src/platform/glfw/IoTranslateGlfw.cpp src/quark/imgui/ImGuiLayer.cpp diff --git a/quark/src/platform/linux/LinuxInput.cpp b/quark/src/platform/linux/LinuxInput.cpp new file mode 100644 index 0000000..9e44441 --- /dev/null +++ b/quark/src/platform/linux/LinuxInput.cpp @@ -0,0 +1,30 @@ +#include "LinuxInput.hpp" + +#include "quark/Application.hpp" +#include + +namespace qk { + Input* Input::instance = new LinuxInput; + + bool LinuxInput::IntIsKeyDown(int keyCode) { + auto window = std::any_cast(Application::GetInstance().GetWindow().GetNativeWindow()); + + int state = glfwGetKey(window, keyCode); + return (state == GLFW_PRESS || state == GLFW_RELEASE); + } + + bool LinuxInput::IntIsMouseDown(int button) { + auto window = std::any_cast(Application::GetInstance().GetWindow().GetNativeWindow()); + + int state = glfwGetMouseButton(window, button); + return state == GLFW_PRESS; + } + + std::pair LinuxInput::IntGetMousePosition() { + auto window = std::any_cast(Application::GetInstance().GetWindow().GetNativeWindow()); + double x, y; + + glfwGetCursorPos(window, &x, &y); + return std::make_pair(x, y); + } +} diff --git a/quark/src/platform/linux/LinuxInput.hpp b/quark/src/platform/linux/LinuxInput.hpp new file mode 100644 index 0000000..bb2ad8f --- /dev/null +++ b/quark/src/platform/linux/LinuxInput.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "quark/Input.hpp" +#include + +namespace qk { + class LinuxInput : + public Input + { + protected: + virtual bool IntIsKeyDown(int keyCode) override; + + virtual bool IntIsMouseDown(int button) override; + virtual std::pair IntGetMousePosition() override; + }; +} diff --git a/quark/src/platform/linux/LinuxWindow.cpp b/quark/src/platform/linux/LinuxWindow.cpp index 045ff03..f77575f 100644 --- a/quark/src/platform/linux/LinuxWindow.cpp +++ b/quark/src/platform/linux/LinuxWindow.cpp @@ -1,5 +1,4 @@ #include "LinuxWindow.hpp" -#include "imgui.h" #include "quark/Core.hpp" #include "quark/Logger.hpp" #include "quark/events/ApplicationEvent.hpp" diff --git a/quark/src/platform/linux/LinuxWindow.hpp b/quark/src/platform/linux/LinuxWindow.hpp index 1f914ce..ece5262 100644 --- a/quark/src/platform/linux/LinuxWindow.hpp +++ b/quark/src/platform/linux/LinuxWindow.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include "quark/Window.hpp" namespace qk { @@ -20,6 +21,10 @@ namespace qk { constexpr void SetVSync(bool enable) override; bool HasVSync() const override; + virtual std::any GetNativeWindow() const override { + return window; + } + private: void Init(const WindowConfig& config); void Destroy(); diff --git a/quark/src/quark/Application.cpp b/quark/src/quark/Application.cpp index e2ca179..d39f5b1 100644 --- a/quark/src/quark/Application.cpp +++ b/quark/src/quark/Application.cpp @@ -1,5 +1,6 @@ #include "Application.hpp" #include "quark/Core.hpp" +#include "quark/Input.hpp" #include namespace qk { @@ -18,6 +19,9 @@ namespace qk { layer->OnUpdate(); } + auto [x, y] = Input::GetMousePosition(); + QK_CORE_TRACE("{0}, {1}", x, y); + window->OnUpdate(); } } diff --git a/quark/src/quark/Input.hpp b/quark/src/quark/Input.hpp new file mode 100644 index 0000000..f18b895 --- /dev/null +++ b/quark/src/quark/Input.hpp @@ -0,0 +1,21 @@ +#pragma once +// IWYU pragma: private, include + +namespace qk { + class Input { + public: + static bool IsKeyDown(int keyCode) { return instance->IntIsKeyDown(keyCode); } + + static bool IsMouseDown(int button) { return instance->IntIsMouseDown(button); } + static std::pair GetMousePosition() { return instance->IntGetMousePosition(); } + + protected: + virtual bool IntIsKeyDown(int keyCode) = 0; + + virtual bool IntIsMouseDown(int button) = 0; + virtual std::pair IntGetMousePosition() = 0; + + private: + static Input* instance; + }; +} diff --git a/quark/src/quark/Window.hpp b/quark/src/quark/Window.hpp index 429a2dd..97d85de 100644 --- a/quark/src/quark/Window.hpp +++ b/quark/src/quark/Window.hpp @@ -1,6 +1,7 @@ #pragma once #include "quark/events/Event.hpp" +#include namespace qk { struct WindowConfig { @@ -27,6 +28,8 @@ namespace qk { virtual void SetVSync(bool enabled) = 0; virtual bool HasVSync() const = 0; + virtual std::any GetNativeWindow() const = 0; + static std::unique_ptr Create(const WindowConfig& config = WindowConfig()); }; }