add event polling

This commit is contained in:
lauchmelder 2025-01-05 22:03:37 +01:00
parent 1943b5be64
commit 6ff27718e4
8 changed files with 80 additions and 1 deletions

View file

@ -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

View file

@ -0,0 +1,30 @@
#include "LinuxInput.hpp"
#include "quark/Application.hpp"
#include <GLFW/glfw3.h>
namespace qk {
Input* Input::instance = new LinuxInput;
bool LinuxInput::IntIsKeyDown(int keyCode) {
auto window = std::any_cast<GLFWwindow*>(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<GLFWwindow*>(Application::GetInstance().GetWindow().GetNativeWindow());
int state = glfwGetMouseButton(window, button);
return state == GLFW_PRESS;
}
std::pair<float, float> LinuxInput::IntGetMousePosition() {
auto window = std::any_cast<GLFWwindow*>(Application::GetInstance().GetWindow().GetNativeWindow());
double x, y;
glfwGetCursorPos(window, &x, &y);
return std::make_pair(x, y);
}
}

View file

@ -0,0 +1,16 @@
#pragma once
#include "quark/Input.hpp"
#include <utility>
namespace qk {
class LinuxInput :
public Input
{
protected:
virtual bool IntIsKeyDown(int keyCode) override;
virtual bool IntIsMouseDown(int button) override;
virtual std::pair<float, float> IntGetMousePosition() override;
};
}

View file

@ -1,5 +1,4 @@
#include "LinuxWindow.hpp"
#include "imgui.h"
#include "quark/Core.hpp"
#include "quark/Logger.hpp"
#include "quark/events/ApplicationEvent.hpp"

View file

@ -1,6 +1,7 @@
#pragma once
#include <GLFW/glfw3.h>
#include <any>
#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();

View file

@ -1,5 +1,6 @@
#include "Application.hpp"
#include "quark/Core.hpp"
#include "quark/Input.hpp"
#include <spdlog/spdlog.h>
namespace qk {
@ -18,6 +19,9 @@ namespace qk {
layer->OnUpdate();
}
auto [x, y] = Input::GetMousePosition();
QK_CORE_TRACE("{0}, {1}", x, y);
window->OnUpdate();
}
}

21
quark/src/quark/Input.hpp Normal file
View file

@ -0,0 +1,21 @@
#pragma once
// IWYU pragma: private, include <quark.hpp>
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<float, float> GetMousePosition() { return instance->IntGetMousePosition(); }
protected:
virtual bool IntIsKeyDown(int keyCode) = 0;
virtual bool IntIsMouseDown(int button) = 0;
virtual std::pair<float, float> IntGetMousePosition() = 0;
private:
static Input* instance;
};
}

View file

@ -1,6 +1,7 @@
#pragma once
#include "quark/events/Event.hpp"
#include <any>
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<Window> Create(const WindowConfig& config = WindowConfig());
};
}