diff --git a/.gitmodules b/.gitmodules index afec923..1300e60 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "Dependencies/GLFW"] path = Dependencies/GLFW url = https://github.com/Light3039/glfw +[submodule "Dependencies/glm"] + path = Dependencies/glm + url = https://github.com/g-truc/glm diff --git a/Dependencies/glm b/Dependencies/glm new file mode 160000 index 0000000..6606249 --- /dev/null +++ b/Dependencies/glm @@ -0,0 +1 @@ +Subproject commit 66062497b104ca7c297321bd0e970869b1e6ece5 diff --git a/Engine/premake5.lua b/Engine/premake5.lua index 8cd5c70..575ef98 100644 --- a/Engine/premake5.lua +++ b/Engine/premake5.lua @@ -23,10 +23,13 @@ project "Engine" -- Dependencies -- includedirs { + -- Engine "%{prj.location}/src/Engine/", + -- 3rd party (dependenciesdir .. "spdlog/include/"), (dependenciesdir .. "glfw/include/"), + (dependenciesdir .. "glm/"), } links diff --git a/Engine/src/Engine/Core/Application.cpp b/Engine/src/Engine/Core/Application.cpp index c655537..b10405a 100644 --- a/Engine/src/Engine/Core/Application.cpp +++ b/Engine/src/Engine/Core/Application.cpp @@ -34,7 +34,7 @@ namespace Light { } } - void Application::OnEvent(Event& event) + void Application::OnEvent(const Event& event) { if (event.IsInCategory(WindowEventCategory)) m_Window->OnEvent(event); diff --git a/Engine/src/Engine/Core/Application.h b/Engine/src/Engine/Core/Application.h index 7ccd047..7a9833e 100644 --- a/Engine/src/Engine/Core/Application.h +++ b/Engine/src/Engine/Core/Application.h @@ -21,7 +21,7 @@ namespace Light { virtual ~Application(); void GameLoop(); - void OnEvent(Event& event); + void OnEvent(const Event& event); // To be defined in client project friend Application* CreateApplication(); diff --git a/Engine/src/Engine/Core/Window.h b/Engine/src/Engine/Core/Window.h index 1102c03..84bad4d 100644 --- a/Engine/src/Engine/Core/Window.h +++ b/Engine/src/Engine/Core/Window.h @@ -25,7 +25,7 @@ namespace Light { inline bool IsOpen() const { return b_Open; } virtual void OnUpdate() = 0; - virtual void OnEvent(Event& event) = 0; + virtual void OnEvent(const Event& event) = 0; virtual unsigned int GetHeight() = 0; virtual unsigned int GetWidth() = 0; diff --git a/Engine/src/Engine/Events/Event.h b/Engine/src/Engine/Events/Event.h index 34b99e7..3d041a0 100644 --- a/Engine/src/Engine/Events/Event.h +++ b/Engine/src/Engine/Events/Event.h @@ -13,11 +13,11 @@ namespace Light { None = 0, // input - MouseMoved, ButtonPressed, ButtonReleased, // mouse + MouseMoved, WheelScrolled, ButtonPressed, ButtonReleased, // mouse KeyPressed, KeyReleased, // keyboard // window - WindowMoved, WindowResized, WindowClosed, + WindowMoved, WindowResized, WindowClosed, WindowLostFocus, WindowGainFocus, }; enum EventCategory diff --git a/Engine/src/Engine/Events/KeyboardEvents.h b/Engine/src/Engine/Events/KeyboardEvents.h index b1537da..b9ddb6e 100644 --- a/Engine/src/Engine/Events/KeyboardEvents.h +++ b/Engine/src/Engine/Events/KeyboardEvents.h @@ -48,5 +48,4 @@ namespace Light { EVENT_CATEGORY(InputEventCategory | KeyboardEventCategory) }; - } \ No newline at end of file diff --git a/Engine/src/Engine/Events/MouseEvents.h b/Engine/src/Engine/Events/MouseEvents.h index 3d918d0..fda8e4c 100644 --- a/Engine/src/Engine/Events/MouseEvents.h +++ b/Engine/src/Engine/Events/MouseEvents.h @@ -4,6 +4,8 @@ #include "Event.h" +#include + #include namespace Light { @@ -11,24 +13,46 @@ namespace Light { class MouseMovedEvent : public Event { private: - const float m_PositionX, m_PositionY; + const glm::vec2 m_Position; public: - MouseMovedEvent(float x, float y) : m_PositionX(x), m_PositionY(y) {} + MouseMovedEvent(float x, float y) : m_Position(x, y) {} - inline float GetX() const { return m_PositionX; } - inline float GetY() const { return m_PositionY; } + inline const glm::vec2& GetPosition() const { return m_Position; } + + inline float GetX() const { return m_Position.x; } + inline float GetY() const { return m_Position.y; } virtual std::string GetInfoLog() const override { std::stringstream ss; - ss << "MouseMoved: " << m_PositionX << ", " << m_PositionY; + ss << "MouseMoved: " << m_Position.x << ", " << m_Position.y; return ss.str(); } EVENT_TYPE(MouseMoved) EVENT_CATEGORY(InputEventCategory | MouseEventCategory) }; + class WheelScrolledEvent : public Event + { + private: + const float m_Offset; + + public: + WheelScrolledEvent(float offset) : m_Offset(offset) {} + + inline float GetOffset() const { return m_Offset; } + + virtual std::string GetInfoLog() const override + { + std::stringstream ss; + ss << "WheelScrolled: " << m_Offset; + return ss.str(); + } + EVENT_TYPE(WheelScrolled) + EVENT_CATEGORY(InputEventCategory | MouseEventCategory) + }; + class ButtonPressedEvent : public Event { private: diff --git a/Engine/src/Engine/Events/WindowEvents.h b/Engine/src/Engine/Events/WindowEvents.h index 2618d54..9ce43fb 100644 --- a/Engine/src/Engine/Events/WindowEvents.h +++ b/Engine/src/Engine/Events/WindowEvents.h @@ -4,6 +4,8 @@ #include "Event.h" +#include + #include namespace Light { @@ -19,4 +21,66 @@ namespace Light { EVENT_CATEGORY(WindowEventCategory) }; + class WindowMovedEvent : public Event + { + private: + const glm::ivec2 m_Position; + + public: + WindowMovedEvent(int x, int y): m_Position(x, y) {} + + const glm::ivec2& GetPosition() { return m_Position; } + + virtual std::string GetInfoLog() const override + { + std::stringstream ss; + ss << "WindwoMoved: " << m_Position.x << ", " << m_Position.y; + return ss.str(); +; } + EVENT_TYPE(WindowMoved) + EVENT_CATEGORY(WindowEventCategory) + }; + + class WindowResizedEvent : public Event + { + private: + const glm::ivec2 m_Size; + + public: + WindowResizedEvent(int width, int height): m_Size(width, height) {} + + const glm::ivec2& GetSize() { return m_Size; } + + virtual std::string GetInfoLog() const override + { + std::stringstream ss; + ss << "WindowResized: " << m_Size.x << ", " << m_Size.y; + return ss.str(); + } + EVENT_TYPE(WindowResized) + EVENT_CATEGORY(WindowEventCategory) + }; + + class WindowLostFocusEvent : public Event + { + public: + virtual std::string GetInfoLog() const override + { + return "WindowLostFocus"; + } + EVENT_TYPE(WindowLostFocus) + EVENT_CATEGORY(WindowEventCategory) + }; + + class WindowGainFocusEvent : public Event + { + public: + virtual std::string GetInfoLog() const override + { + return "WindowGainFocus"; + } + EVENT_TYPE(WindowGainFocus) + EVENT_CATEGORY(WindowEventCategory) + }; + } \ No newline at end of file diff --git a/Engine/src/Engine/Layer/Layer.h b/Engine/src/Engine/Layer/Layer.h index a1d7641..e1df981 100644 --- a/Engine/src/Engine/Layer/Layer.h +++ b/Engine/src/Engine/Layer/Layer.h @@ -24,6 +24,7 @@ namespace Light { virtual bool OnMouseMoved(const MouseMovedEvent& event) { return false; } virtual bool OnButtonPressed(const ButtonPressedEvent& event) { return false; } virtual bool OnButtonReleased(const ButtonReleasedEvent& event) { return false; } + virtual bool OnWheelScrolled(const WheelScrolledEvent& event) { return false; } // Keyboard events virtual bool OnKeyPressed(const KeyPressedEvent& event) { return false; } @@ -31,7 +32,10 @@ namespace Light { // Window Events virtual bool OnWindowClosed(const WindowClosedEvent& event) { return false; } - + virtual bool OnWindowResized(const WindowResizedEvent& event) { return false; } + virtual bool OnWindowMoved(const WindowMovedEvent& event) { return false; } + virtual bool OnWindowLostFocus(const WindowLostFocusEvent& event) { return false; } + virtual bool OnWindowGainFocus(const WindowGainFocusEvent& event) { return false; } }; class TestLayer : public Layer @@ -43,6 +47,7 @@ namespace Light { virtual bool OnMouseMoved(const MouseMovedEvent& event) override { LT_ENGINE_TRACE("{}", event.GetInfoLog()); return false; } virtual bool OnButtonPressed(const ButtonPressedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } virtual bool OnButtonReleased(const ButtonReleasedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnWheelScrolled(const WheelScrolledEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } // Keyboard events virtual bool OnKeyPressed(const KeyPressedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } @@ -50,6 +55,10 @@ namespace Light { // Window events virtual bool OnWindowClosed(const WindowClosedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnWindowResized(const WindowResizedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnWindowMoved(const WindowMovedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnWindowLostFocus(const WindowLostFocusEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnWindowGainFocus(const WindowGainFocusEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } }; } \ No newline at end of file diff --git a/Engine/src/Engine/Layer/LayerStack.cpp b/Engine/src/Engine/Layer/LayerStack.cpp index a61327c..146f2bb 100644 --- a/Engine/src/Engine/Layer/LayerStack.cpp +++ b/Engine/src/Engine/Layer/LayerStack.cpp @@ -1,5 +1,6 @@ #include "LayerStack.h" +#include namespace Light { LayerStack::~LayerStack() @@ -26,14 +27,14 @@ namespace Light { LT_ENGINE_TRACE("LayerStack::PushLayer: Detatched[{}]", layer->GetName()); } - void LayerStack::OnEvent(Event& event) + void LayerStack::OnEvent(const Event& event) { switch (event.GetEventType()) { + // Mouse case EventType::MouseMoved: for (auto it = m_Begin; it != m_End; it++) - if ((*it)->OnMouseMoved((MouseMovedEvent&)event)) return; - return; + if ((*it)->OnButtonPressed((ButtonPressedEvent&)event)) return; case EventType::ButtonPressed: for (auto it = m_Begin; it != m_End; it++) @@ -43,7 +44,12 @@ namespace Light { for (auto it = m_Begin; it != m_End; it++) if ((*it)->OnButtonReleased((ButtonReleasedEvent&)event)) return; return; + case EventType::WheelScrolled: + for (auto it = m_Begin; it != m_End; it++) + if ((*it)->OnWheelScrolled((WheelScrolledEvent&)event)) return; + return; + // Keyboard case EventType::KeyPressed: for (auto it = m_Begin; it != m_End; it++) if ((*it)->OnKeyPressed((KeyPressedEvent&)event)) return; @@ -53,11 +59,29 @@ namespace Light { if ((*it)->OnKeyReleased((KeyReleasedEvent&)event)) return; return; + // Window case EventType::WindowClosed: for (auto it = m_Begin; it != m_End; it++) if ((*it)->OnWindowClosed((WindowClosedEvent&)event)) return; return; + case EventType::WindowResized: + for (auto it = m_Begin; it != m_End; it++) + if ((*it)->OnWindowResized((WindowResizedEvent&)event)) return; + return; + case EventType::WindowMoved: + for (auto it = m_Begin; it != m_End; it++) + if ((*it)->OnWindowMoved((WindowMovedEvent&)event)) return; + return; + case EventType::WindowLostFocus: + for (auto it = m_Begin; it != m_End; it++) + if ((*it)->OnWindowLostFocus((WindowLostFocusEvent&)event)) return; + return; + case EventType::WindowGainFocus: + for (auto it = m_Begin; it != m_End; it++) + if ((*it)->OnWindowGainFocus((WindowGainFocusEvent&)event)) return; + return; } } + } \ No newline at end of file diff --git a/Engine/src/Engine/Layer/LayerStack.h b/Engine/src/Engine/Layer/LayerStack.h index 5478887..1b9afce 100644 --- a/Engine/src/Engine/Layer/LayerStack.h +++ b/Engine/src/Engine/Layer/LayerStack.h @@ -17,13 +17,15 @@ namespace Light { std::vector::iterator m_Begin; std::vector::iterator m_End; + + public: ~LayerStack(); void PushLayer(Layer* layer); void PopLayer(Layer* layer); - void OnEvent(Event& event); + void OnEvent(const Event& event); std::vector::iterator begin() { return m_Layers.begin(); } std::vector::iterator end() { return m_Layers.end(); } diff --git a/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp b/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp index b965561..0fa2f45 100644 --- a/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp +++ b/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp @@ -35,7 +35,7 @@ namespace Light { glfwSwapBuffers(m_Handle); } - void wWindow::OnEvent(Event& event) + void wWindow::OnEvent(const Event& event) { switch (event.GetEventType()) { @@ -72,6 +72,12 @@ namespace Light { callback(ButtonReleasedEvent(button)); }); + glfwSetScrollCallback(m_Handle, [](GLFWwindow* window, double xoffset, double yoffset) + { + std::function callback = *(std::function*)glfwGetWindowUserPointer(window); + callback(WheelScrolledEvent(yoffset)); + }); + glfwSetKeyCallback(m_Handle, [](GLFWwindow* window, int key, int scancode, int action, int mods) { std::function callback = *(std::function*)glfwGetWindowUserPointer(window); @@ -87,6 +93,28 @@ namespace Light { std::function callback = *(std::function*)glfwGetWindowUserPointer(window); callback(WindowClosedEvent()); }); + + glfwSetWindowSizeCallback(m_Handle, [](GLFWwindow* window, int width, int height) + { + std::function callback = *(std::function*)glfwGetWindowUserPointer(window); + callback(WindowResizedEvent(width, height)); + }); + + glfwSetWindowPosCallback(m_Handle, [](GLFWwindow* window, int xpos, int ypos) + { + std::function callback = *(std::function*)glfwGetWindowUserPointer(window); + callback(WindowMovedEvent(xpos, ypos)); + }); + + glfwSetWindowFocusCallback(m_Handle, [](GLFWwindow* window, int focus) + { + std::function callback = *(std::function*)glfwGetWindowUserPointer(window); + + if(focus == GLFW_TRUE) + callback(WindowGainFocusEvent()); + else + callback(WindowLostFocusEvent()); + }); } } \ No newline at end of file diff --git a/Engine/src/Engine/Platforms/OS/Windows/wWindow.h b/Engine/src/Engine/Platforms/OS/Windows/wWindow.h index f28a349..d89b6e0 100644 --- a/Engine/src/Engine/Platforms/OS/Windows/wWindow.h +++ b/Engine/src/Engine/Platforms/OS/Windows/wWindow.h @@ -25,7 +25,7 @@ namespace Light { ~wWindow(); virtual void OnUpdate() override; - virtual void OnEvent(Event& event) override; + virtual void OnEvent(const Event& event) override; virtual unsigned int GetWidth() override; virtual unsigned int GetHeight() override; diff --git a/Sandbox/premake5.lua b/Sandbox/premake5.lua index 33585bd..68eae82 100644 --- a/Sandbox/premake5.lua +++ b/Sandbox/premake5.lua @@ -29,6 +29,7 @@ project "Sandbox" -- 3rd party (dependenciesdir .. "spdlog/include/"), + (dependenciesdir .. "glm/"), } links