diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..65ef5a9 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "quark/vendor/imgui"] + path = quark/vendor/imgui + url = https://github.com/ocornut/imgui diff --git a/quark/CMakeLists.txt b/quark/CMakeLists.txt index 86d7c39..dabe61d 100644 --- a/quark/CMakeLists.txt +++ b/quark/CMakeLists.txt @@ -1,9 +1,7 @@ project(quark CXX) -FetchContent_MakeAvailable(spdlog) -FetchContent_MakeAvailable(glfw3) - -add_subdirectory(glad) +FetchContent_MakeAvailable(spdlog glfw3) +add_subdirectory(vendor) add_library(quark SHARED) @@ -15,6 +13,9 @@ target_sources(quark src/quark/LayerStack.cpp src/platform/linux/LinuxWindow.cpp + src/platform/glfw/IoTranslateGlfw.cpp + + src/quark/imgui/ImGuiLayer.cpp PUBLIC FILE_SET HEADERS BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/src FILES @@ -32,7 +33,7 @@ target_sources(quark src/quark/events/KeyEvent.hpp src/quark/events/MouseEvent.hpp - src/platform/linux/LinuxWindow.hpp + src/quark/imgui/ImGuiLayer.hpp ) target_include_directories(quark INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src) @@ -42,10 +43,15 @@ target_precompile_headers(quark src/qkpch.hpp ) +target_compile_definitions(quark + PUBLIC GLFW_INCLUDE_NONE +) + target_link_libraries(quark spdlog::spdlog glad glfw + imgui ) include(GNUInstallDirs) diff --git a/quark/src/platform/IoTranslate.hpp b/quark/src/platform/IoTranslate.hpp new file mode 100644 index 0000000..851d9e9 --- /dev/null +++ b/quark/src/platform/IoTranslate.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include + +namespace qk { + ImGuiMouseButton TranslateMouseButton(int button); + int TranslateKey(int key); +} diff --git a/quark/src/platform/glfw/IoTranslateGlfw.cpp b/quark/src/platform/glfw/IoTranslateGlfw.cpp new file mode 100644 index 0000000..5236d3d --- /dev/null +++ b/quark/src/platform/glfw/IoTranslateGlfw.cpp @@ -0,0 +1,140 @@ +#include +#include + +namespace qk { + ImGuiMouseButton TranslateMouseButton(int button) { + switch (button) { + case GLFW_MOUSE_BUTTON_LEFT: return ImGuiMouseButton_Left; + case GLFW_MOUSE_BUTTON_RIGHT: return ImGuiMouseButton_Right; + case GLFW_MOUSE_BUTTON_MIDDLE: return ImGuiMouseButton_Middle; + } + + return button; + } + + int TranslateKey(int key) { + switch (key) { + case GLFW_KEY_TAB: return ImGuiKey_Tab; + case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow; + case GLFW_KEY_RIGHT: return ImGuiKey_RightArrow; + case GLFW_KEY_UP: return ImGuiKey_UpArrow; + case GLFW_KEY_DOWN: return ImGuiKey_DownArrow; + case GLFW_KEY_PAGE_UP: return ImGuiKey_PageUp; + case GLFW_KEY_PAGE_DOWN: return ImGuiKey_PageDown; + case GLFW_KEY_HOME: return ImGuiKey_Home; + case GLFW_KEY_END: return ImGuiKey_End; + case GLFW_KEY_INSERT: return ImGuiKey_Insert; + case GLFW_KEY_DELETE: return ImGuiKey_Delete; + case GLFW_KEY_BACKSPACE: return ImGuiKey_Backspace; + case GLFW_KEY_SPACE: return ImGuiKey_Space; + case GLFW_KEY_ENTER: return ImGuiKey_Enter; + case GLFW_KEY_ESCAPE: return ImGuiKey_Escape; + case GLFW_KEY_LEFT_CONTROL: return (ImGuiKey)(ImGuiKey_LeftCtrl | ImGuiMod_Ctrl); + case GLFW_KEY_LEFT_SHIFT: return (ImGuiKey)(ImGuiKey_LeftShift | ImGuiMod_Shift); + case GLFW_KEY_LEFT_ALT: return (ImGuiKey)(ImGuiKey_LeftAlt | ImGuiMod_Alt); + case GLFW_KEY_LEFT_SUPER: return (ImGuiKey)(ImGuiKey_LeftSuper | ImGuiMod_Super); + case GLFW_KEY_RIGHT_CONTROL: return (ImGuiKey)(ImGuiKey_RightCtrl | ImGuiMod_Ctrl); + case GLFW_KEY_RIGHT_SHIFT: return (ImGuiKey)(ImGuiKey_RightShift | ImGuiMod_Shift); + case GLFW_KEY_RIGHT_ALT: return (ImGuiKey)(ImGuiKey_RightAlt | ImGuiMod_Alt); + case GLFW_KEY_RIGHT_SUPER: return (ImGuiKey)(ImGuiKey_RightSuper | ImGuiMod_Super); + case GLFW_KEY_MENU: return ImGuiKey_Menu; + case GLFW_KEY_0: return ImGuiKey_0; + case GLFW_KEY_1: return ImGuiKey_1; + case GLFW_KEY_2: return ImGuiKey_2; + case GLFW_KEY_3: return ImGuiKey_3; + case GLFW_KEY_4: return ImGuiKey_4; + case GLFW_KEY_5: return ImGuiKey_5; + case GLFW_KEY_6: return ImGuiKey_6; + case GLFW_KEY_7: return ImGuiKey_7; + case GLFW_KEY_8: return ImGuiKey_8; + case GLFW_KEY_9: return ImGuiKey_9; + case GLFW_KEY_A: return ImGuiKey_A; + case GLFW_KEY_B: return ImGuiKey_B; + case GLFW_KEY_C: return ImGuiKey_C; + case GLFW_KEY_D: return ImGuiKey_D; + case GLFW_KEY_E: return ImGuiKey_E; + case GLFW_KEY_F: return ImGuiKey_F; + case GLFW_KEY_G: return ImGuiKey_G; + case GLFW_KEY_H: return ImGuiKey_H; + case GLFW_KEY_I: return ImGuiKey_I; + case GLFW_KEY_J: return ImGuiKey_J; + case GLFW_KEY_K: return ImGuiKey_K; + case GLFW_KEY_L: return ImGuiKey_L; + case GLFW_KEY_M: return ImGuiKey_M; + case GLFW_KEY_N: return ImGuiKey_N; + case GLFW_KEY_O: return ImGuiKey_O; + case GLFW_KEY_P: return ImGuiKey_P; + case GLFW_KEY_Q: return ImGuiKey_Q; + case GLFW_KEY_R: return ImGuiKey_R; + case GLFW_KEY_S: return ImGuiKey_S; + case GLFW_KEY_T: return ImGuiKey_T; + case GLFW_KEY_U: return ImGuiKey_U; + case GLFW_KEY_V: return ImGuiKey_V; + case GLFW_KEY_W: return ImGuiKey_W; + case GLFW_KEY_X: return ImGuiKey_X; + case GLFW_KEY_Y: return ImGuiKey_Y; + case GLFW_KEY_Z: return ImGuiKey_Z; + case GLFW_KEY_F1: return ImGuiKey_F1; + case GLFW_KEY_F2: return ImGuiKey_F2; + case GLFW_KEY_F3: return ImGuiKey_F3; + case GLFW_KEY_F4: return ImGuiKey_F4; + case GLFW_KEY_F5: return ImGuiKey_F5; + case GLFW_KEY_F6: return ImGuiKey_F6; + case GLFW_KEY_F7: return ImGuiKey_F7; + case GLFW_KEY_F8: return ImGuiKey_F8; + case GLFW_KEY_F9: return ImGuiKey_F9; + case GLFW_KEY_F10: return ImGuiKey_F10; + case GLFW_KEY_F11: return ImGuiKey_F11; + case GLFW_KEY_F12: return ImGuiKey_F12; + case GLFW_KEY_F13: return ImGuiKey_F13; + case GLFW_KEY_F14: return ImGuiKey_F14; + case GLFW_KEY_F15: return ImGuiKey_F15; + case GLFW_KEY_F16: return ImGuiKey_F16; + case GLFW_KEY_F17: return ImGuiKey_F17; + case GLFW_KEY_F18: return ImGuiKey_F18; + case GLFW_KEY_F19: return ImGuiKey_F19; + case GLFW_KEY_F20: return ImGuiKey_F20; + case GLFW_KEY_F21: return ImGuiKey_F21; + case GLFW_KEY_F22: return ImGuiKey_F22; + case GLFW_KEY_F23: return ImGuiKey_F23; + case GLFW_KEY_F24: return ImGuiKey_F24; + case GLFW_KEY_APOSTROPHE: return ImGuiKey_Apostrophe; + case GLFW_KEY_COMMA: return ImGuiKey_Comma; + case GLFW_KEY_MINUS: return ImGuiKey_Minus; + case GLFW_KEY_PERIOD: return ImGuiKey_Period; + case GLFW_KEY_SLASH: return ImGuiKey_Slash; + case GLFW_KEY_SEMICOLON: return ImGuiKey_Semicolon; + case GLFW_KEY_EQUAL: return ImGuiKey_Equal; + case GLFW_KEY_LEFT_BRACKET: return ImGuiKey_LeftBracket; + case GLFW_KEY_BACKSLASH: return ImGuiKey_Backslash; + case GLFW_KEY_RIGHT_BRACKET: return ImGuiKey_RightBracket; + case GLFW_KEY_GRAVE_ACCENT: return ImGuiKey_GraveAccent; + case GLFW_KEY_CAPS_LOCK: return ImGuiKey_CapsLock; + case GLFW_KEY_SCROLL_LOCK: return ImGuiKey_ScrollLock; + case GLFW_KEY_NUM_LOCK: return ImGuiKey_NumLock; + case GLFW_KEY_PRINT_SCREEN: return ImGuiKey_PrintScreen; + case GLFW_KEY_PAUSE: return ImGuiKey_Pause; + case GLFW_KEY_KP_0: return ImGuiKey_Keypad0; + case GLFW_KEY_KP_1: return ImGuiKey_Keypad1; + case GLFW_KEY_KP_2: return ImGuiKey_Keypad2; + case GLFW_KEY_KP_3: return ImGuiKey_Keypad3; + case GLFW_KEY_KP_4: return ImGuiKey_Keypad4; + case GLFW_KEY_KP_5: return ImGuiKey_Keypad5; + case GLFW_KEY_KP_6: return ImGuiKey_Keypad6; + case GLFW_KEY_KP_7: return ImGuiKey_Keypad7; + case GLFW_KEY_KP_8: return ImGuiKey_Keypad8; + case GLFW_KEY_KP_9: return ImGuiKey_Keypad9; + case GLFW_KEY_KP_DECIMAL: return ImGuiKey_KeypadDecimal; + case GLFW_KEY_KP_DIVIDE: return ImGuiKey_KeypadDivide; + case GLFW_KEY_KP_MULTIPLY: return ImGuiKey_KeypadMultiply; + case GLFW_KEY_KP_SUBTRACT: return ImGuiKey_KeypadSubtract; + case GLFW_KEY_KP_ADD: return ImGuiKey_KeypadAdd; + case GLFW_KEY_KP_ENTER: return ImGuiKey_KeypadEnter; + case GLFW_KEY_KP_EQUAL: return ImGuiKey_KeypadEqual; + } + + return ImGuiKey_None; + } + +} + diff --git a/quark/src/platform/linux/LinuxWindow.cpp b/quark/src/platform/linux/LinuxWindow.cpp index b842e6f..045ff03 100644 --- a/quark/src/platform/linux/LinuxWindow.cpp +++ b/quark/src/platform/linux/LinuxWindow.cpp @@ -1,4 +1,5 @@ #include "LinuxWindow.hpp" +#include "imgui.h" #include "quark/Core.hpp" #include "quark/Logger.hpp" #include "quark/events/ApplicationEvent.hpp" @@ -6,6 +7,7 @@ #include "quark/events/MouseEvent.hpp" #include +#include namespace qk { static bool GLFWInitialized = false; @@ -46,6 +48,12 @@ namespace qk { window = glfwCreateWindow((int)config.width, (int)config.height, config.title.c_str(), nullptr, nullptr); glfwMakeContextCurrent(window); + + int version = gladLoadGL(glfwGetProcAddress); + QK_CORE_ASSERT(version, "Failed to load OpenGL"); + + QK_CORE_INFO("Loaded OpenGL {0}.{1}", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); + glfwSetWindowUserPointer(window, &data); InstallGLFWCallbacks(); @@ -97,6 +105,13 @@ namespace qk { } }); + glfwSetCharCallback(window, [] (GLFWwindow* window, unsigned int key) { + WindowData* data = reinterpret_cast(glfwGetWindowUserPointer(window)); + + KeyTypedEvent event(key); + data->eventCallback(event); + }); + glfwSetMouseButtonCallback(window, [] (GLFWwindow* window, int button, int action, int modes) { WindowData* data = reinterpret_cast(glfwGetWindowUserPointer(window)); diff --git a/quark/src/platform/linux/LinuxWindow.hpp b/quark/src/platform/linux/LinuxWindow.hpp index 2058692..1f914ce 100644 --- a/quark/src/platform/linux/LinuxWindow.hpp +++ b/quark/src/platform/linux/LinuxWindow.hpp @@ -1,7 +1,6 @@ #pragma once #include - #include "quark/Window.hpp" namespace qk { @@ -22,8 +21,8 @@ namespace qk { bool HasVSync() const override; private: - virtual void Init(const WindowConfig& config); - virtual void Destroy(); + void Init(const WindowConfig& config); + void Destroy(); void InstallGLFWCallbacks(); diff --git a/quark/src/quark/Application.cpp b/quark/src/quark/Application.cpp index 3c39752..e2ca179 100644 --- a/quark/src/quark/Application.cpp +++ b/quark/src/quark/Application.cpp @@ -1,10 +1,13 @@ #include "Application.hpp" -#include "quark.hpp" +#include "quark/Core.hpp" #include namespace qk { Application::Application() { + QK_CORE_ASSERT(instance == nullptr, "Application already exists"); + instance = this; + window = Window::Create(); window->SetEventCallback(std::bind(&Application::OnEvent, this, std::placeholders::_1)); } @@ -35,10 +38,12 @@ namespace qk { void Application::PushLayer(Layer* layer) { layers.PushLayer(layer); + layer->OnAttach(); } void Application::PushOverlay(Layer* layer) { layers.PushOverlay(layer); + layer->OnAttach(); } bool Application::OnWindowClose(WindowCloseEvent& e) { diff --git a/quark/src/quark/Application.hpp b/quark/src/quark/Application.hpp index 22e9d2c..bdc7b31 100644 --- a/quark/src/quark/Application.hpp +++ b/quark/src/quark/Application.hpp @@ -20,6 +20,8 @@ namespace qk { void PushLayer(Layer* layer); void PushOverlay(Layer* layer); + inline Window& GetWindow() { return *window; }; + private: bool OnWindowClose(WindowCloseEvent& e); @@ -28,8 +30,16 @@ namespace qk { bool isRunning = true; LayerStack layers; + + public: + static Application& GetInstance() { + return *instance; + } + + private: + inline static Application* instance = nullptr; }; - std::unique_ptr CreateApplication(); + Application* CreateApplication(); } diff --git a/quark/src/quark/Core.hpp b/quark/src/quark/Core.hpp index a98da97..54f0ea3 100644 --- a/quark/src/quark/Core.hpp +++ b/quark/src/quark/Core.hpp @@ -1,8 +1,8 @@ #pragma once - #ifndef NDEBUG # include +# include "quark/Logger.hpp" # define _LINE_TO_STR(x) #x # define LINE_TO_STR(x) _LINE_TO_STR(x) # define QK_ASSERT(x, ...) { if (!(x)) { QK_CRITICAL(__FILE__ ":" LINE_TO_STR(__LINE__) ": Assertion failed: {0}", __VA_ARGS__); raise(SIGTRAP); } } diff --git a/quark/src/quark/Entrypoint.cpp b/quark/src/quark/Entrypoint.cpp index 4424b3f..72a14a6 100644 --- a/quark/src/quark/Entrypoint.cpp +++ b/quark/src/quark/Entrypoint.cpp @@ -1,13 +1,13 @@ #include "Application.hpp" #include "Logger.hpp" -extern std::unique_ptr qk::CreateApplication(); +extern qk::Application* qk::CreateApplication(); int main(int argc, char** argv) { qk::Logger::Init(); QK_CORE_INFO("Logger initialised"); - auto application = qk::CreateApplication(); + qk::Application* application = qk::CreateApplication(); application->Run(); return 0; diff --git a/quark/src/quark/events/Event.hpp b/quark/src/quark/events/Event.hpp index ac7251a..d4d24a2 100644 --- a/quark/src/quark/events/Event.hpp +++ b/quark/src/quark/events/Event.hpp @@ -5,6 +5,8 @@ #include #include +#define BIND_EVENT_CB(cb) std::bind(&cb, this, std::placeholders::_1) + namespace qk { enum class EventType { None = 0, @@ -16,6 +18,7 @@ namespace qk { WindowMoved, KeyPressed, + KeyTyped, KeyReleased, MouseButtonPressed, diff --git a/quark/src/quark/events/KeyEvent.hpp b/quark/src/quark/events/KeyEvent.hpp index 46b8a37..589017d 100644 --- a/quark/src/quark/events/KeyEvent.hpp +++ b/quark/src/quark/events/KeyEvent.hpp @@ -42,6 +42,25 @@ namespace qk { int repeatCount; }; + class KeyTypedEvent + : public KeyEvent + { + public: + KeyTypedEvent(int keyCode) : + KeyEvent(keyCode) + { } + + virtual operator std::string() const override { + std::stringstream ss; + ss << "KeyTypedEvent: " << keyCode; + return ss.str(); + } + + EVENT_CLASS_TYPE(KeyTyped) + private: + }; + + class KeyReleasedEvent : public KeyEvent { diff --git a/quark/src/quark/events/MouseEvent.hpp b/quark/src/quark/events/MouseEvent.hpp index c3f8ed3..122dfc9 100644 --- a/quark/src/quark/events/MouseEvent.hpp +++ b/quark/src/quark/events/MouseEvent.hpp @@ -36,6 +36,9 @@ namespace qk { offsetX(offsetX), offsetY(offsetY) { } + inline float GetX() const { return offsetX; } + inline float GetY() const { return offsetY; } + virtual operator std::string() const override { std::stringstream ss; ss << "MouseScrolledEvent: " << offsetX << ", " << offsetY; diff --git a/quark/src/quark/imgui/ImGuiLayer.cpp b/quark/src/quark/imgui/ImGuiLayer.cpp new file mode 100644 index 0000000..6d0279d --- /dev/null +++ b/quark/src/quark/imgui/ImGuiLayer.cpp @@ -0,0 +1,181 @@ +#include "ImGuiLayer.hpp" + +// TODO: Remove these, these are temporary +#include +#include + +#include +#include +#include + +#include "platform/IoTranslate.hpp" +#include "quark/Application.hpp" +#include "quark/events/Event.hpp" + +namespace qk { + ImGuiLayer::ImGuiLayer() : + Layer("ImGui Quark") + { + + } + + ImGuiLayer::~ImGuiLayer() { + + } + + void ImGuiLayer::OnAttach() { + ImGui::CreateContext(); + ImGui::StyleColorsDark(); + + ImGuiIO& io = ImGui::GetIO(); + Application& app = Application::GetInstance(); + + io.DisplaySize = ImVec2(app.GetWindow().GetWidth(), app.GetWindow().GetHeight()); + + io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; + io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; + // io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; + // io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; + + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + // io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; + + ImGui_ImplOpenGL3_Init("#version 410"); + } + + void ImGuiLayer::OnDetach() { + + } + + void ImGuiLayer::OnUpdate() { + ImGui_ImplOpenGL3_NewFrame(); + ImGui::NewFrame(); + + float time = (float)glfwGetTime(); + ImGui::GetIO().DeltaTime = (lastTime > 0.0f) ? (time - lastTime) : (1.0f / 60.0f); + lastTime = time; + + static bool show = true; + ImGui::ShowDemoWindow(&show); + + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + } + + void ImGuiLayer::OnEvent(Event& event) { + EventDispatcher dispatcher(event); + + dispatcher.Dispatch(BIND_EVENT_CB(ImGuiLayer::OnMouseButtonPressed)); + dispatcher.Dispatch(BIND_EVENT_CB(ImGuiLayer::OnMouseButtonReleased)); + dispatcher.Dispatch(BIND_EVENT_CB(ImGuiLayer::OnMouseMoved)); + dispatcher.Dispatch(BIND_EVENT_CB(ImGuiLayer::OnMouseScrolled)); + + dispatcher.Dispatch(BIND_EVENT_CB(ImGuiLayer::OnKeyPressedEvent)); + dispatcher.Dispatch(BIND_EVENT_CB(ImGuiLayer::OnKeyReleasedEvent)); + dispatcher.Dispatch(BIND_EVENT_CB(ImGuiLayer::OnKeyTypedEvent)); + + dispatcher.Dispatch(BIND_EVENT_CB(ImGuiLayer::OnWindowResized)); + } + + bool ImGuiLayer::OnMouseButtonPressed(MouseButtonPressedEvent& e) { + ImGuiIO& io = ImGui::GetIO(); + io.AddMouseButtonEvent(TranslateMouseButton(e.GetMouseButton()), true); + + return false; + } + + bool ImGuiLayer::OnMouseButtonReleased(MouseButtonReleasedEvent& e) { + ImGuiIO& io = ImGui::GetIO(); + io.AddMouseButtonEvent(TranslateMouseButton(e.GetMouseButton()), false); + + return false; + } + + bool ImGuiLayer::OnMouseMoved(MouseMovedEvent& e) { + ImGuiIO& io = ImGui::GetIO(); + io.AddMousePosEvent(e.GetX(), e.GetY()); + + return false; + } + + bool ImGuiLayer::OnMouseScrolled(MouseScrolledEvent& e) { + ImGuiIO& io = ImGui::GetIO(); + io.AddMouseWheelEvent(e.GetX(), e.GetY()); + + return false; + } + + bool ImGuiLayer::OnKeyPressedEvent(KeyPressedEvent& e) { + ImGuiIO& io = ImGui::GetIO(); + int key = TranslateKey(e.GetKeyCode()); + int mod = ImGuiMod_Mask_ & key; + + if ((mod & ImGuiMod_Ctrl) != 0) { + io.AddKeyEvent(ImGuiMod_Ctrl, true); + } + + if ((mod & ImGuiMod_Shift) != 0) { + io.AddKeyEvent(ImGuiMod_Shift, true); + } + + if ((mod & ImGuiMod_Alt) != 0) { + io.AddKeyEvent(ImGuiMod_Alt, true); + } + + if ((mod & ImGuiMod_Super) != 0) { + io.AddKeyEvent(ImGuiMod_Super, true); + } + + io.AddKeyEvent((ImGuiKey)(key & ~ImGuiMod_Mask_), true); + + return false; + } + + bool ImGuiLayer::OnKeyReleasedEvent(KeyReleasedEvent& e) { + ImGuiIO& io = ImGui::GetIO(); + int key = TranslateKey(e.GetKeyCode()); + int mod = ImGuiMod_Mask_ & key; + + if ((mod & ImGuiMod_Ctrl) != 0) { + io.AddKeyEvent(ImGuiMod_Ctrl, false); + } + + if ((mod & ImGuiMod_Shift) != 0) { + io.AddKeyEvent(ImGuiMod_Shift, false); + } + + if ((mod & ImGuiMod_Alt) != 0) { + io.AddKeyEvent(ImGuiMod_Alt, false); + } + + if ((mod & ImGuiMod_Super) != 0) { + io.AddKeyEvent(ImGuiMod_Super, false); + } + + io.AddKeyEvent((ImGuiKey)(key & ~ImGuiMod_Mask_), false); + + return false; + } + + bool ImGuiLayer::OnKeyTypedEvent(KeyTypedEvent& e) { + ImGuiIO& io = ImGui::GetIO(); + int c = e.GetKeyCode(); + + if (c > 0 && c < 0x10000) { + io.AddInputCharacter(c); + } + + return false; + } + + bool ImGuiLayer::OnWindowResized(WindowResizeEvent& e) { + ImGuiIO& io = ImGui::GetIO(); + io.DisplaySize = ImVec2(e.GetWidth(), e.GetHeight()); + io.DisplayFramebufferScale = ImVec2(1.f, 1.f); + + glViewport(0, 0, e.GetWidth(), e.GetHeight()); + + return false; + } + +} diff --git a/quark/src/quark/imgui/ImGuiLayer.hpp b/quark/src/quark/imgui/ImGuiLayer.hpp new file mode 100644 index 0000000..6e67a60 --- /dev/null +++ b/quark/src/quark/imgui/ImGuiLayer.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include "quark/Layer.hpp" +#include "quark/events/Event.hpp" +#include "quark/events/MouseEvent.hpp" +#include "quark/events/KeyEvent.hpp" +#include "quark/events/ApplicationEvent.hpp" + +namespace qk { + class ImGuiLayer : + public Layer + { + public: + ImGuiLayer(); + ~ImGuiLayer(); + + void OnAttach() override; + void OnDetach() override; + + void OnUpdate() override; + void OnEvent(Event& event) override; + + private: + bool OnMouseButtonPressed(MouseButtonPressedEvent& e); + bool OnMouseButtonReleased(MouseButtonReleasedEvent& e); + bool OnMouseMoved(MouseMovedEvent& e); + bool OnMouseScrolled(MouseScrolledEvent& e); + + bool OnKeyPressedEvent(KeyPressedEvent& e); + bool OnKeyReleasedEvent(KeyReleasedEvent& e); + bool OnKeyTypedEvent(KeyTypedEvent& e); + + bool OnWindowResized(WindowResizeEvent& e); + + private: + float lastTime = 0.0f; + }; +} diff --git a/quark/vendor/CMakeLists.txt b/quark/vendor/CMakeLists.txt new file mode 100644 index 0000000..ff00edf --- /dev/null +++ b/quark/vendor/CMakeLists.txt @@ -0,0 +1,41 @@ +######################## +# GLAD +######################## + +add_library(glad + glad/src/gl.c +) + +target_include_directories(glad + PUBLIC glad/include +) + +set_property( + TARGET glad + PROPERTY POSITION_INDEPENDENT_CODE ON +) + +######################## +# ImGui +######################## + +add_library(imgui + imgui/imgui.cpp + imgui/imgui_draw.cpp + imgui/imgui_tables.cpp + imgui/imgui_widgets.cpp + imgui/imgui_demo.cpp + + # TODO: this is shit + imgui/backends/imgui_impl_opengl3.cpp +) + +target_include_directories(imgui + PUBLIC imgui +) + +set_property( + TARGET imgui + PROPERTY POSITION_INDEPENDENT_CODE ON +) + diff --git a/quark/vendor/glad/CMakeLists.txt b/quark/vendor/glad/CMakeLists.txt deleted file mode 100644 index 985f996..0000000 --- a/quark/vendor/glad/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -add_library(glad - src/gl.c -) - -target_include_directories(glad - PUBLIC include -) diff --git a/quark/vendor/imgui b/quark/vendor/imgui new file mode 160000 index 0000000..eb642a9 --- /dev/null +++ b/quark/vendor/imgui @@ -0,0 +1 @@ +Subproject commit eb642a9535055eacca6c6f7f7310fb5b41a96424 diff --git a/sandbox/src/Application.cpp b/sandbox/src/Application.cpp index bae3f2f..419b6ee 100644 --- a/sandbox/src/Application.cpp +++ b/sandbox/src/Application.cpp @@ -1,5 +1,5 @@ -#include #include +#include "quark/imgui/ImGuiLayer.hpp" class ExampleLayer : public qk::Layer @@ -10,7 +10,7 @@ public: { } void OnUpdate() override { - QK_TRACE("ExampleLayer::Update"); + // QK_TRACE("ExampleLayer::Update"); } void OnEvent(qk::Event& event) override { @@ -24,9 +24,10 @@ class Sandbox : public: Sandbox() { PushLayer(new ExampleLayer); + PushOverlay(new qk::ImGuiLayer); } }; -std::unique_ptr qk::CreateApplication() { - return std::make_unique(); +qk::Application* qk::CreateApplication() { + return new Sandbox; }