From f70881e3646425faa1113e3ac83b5379d7ffe07b Mon Sep 17 00:00:00 2001 From: Atdunbg <979541498@qq.com> Date: Thu, 20 Nov 2025 00:24:23 +0800 Subject: [PATCH] pch,layers --- CMakeLists.txt | 2 +- Editor/Editor/Editor.cpp | 6 ++ Prism/CMakeLists.txt | 28 ++++++-- Prism/src/Prism/Core/Application.cpp | 34 +++++++++- Prism/src/Prism/Core/Application.h | 8 ++- Prism/src/Prism/Core/Core.cpp | 7 +- Prism/src/Prism/Core/Events/Event.h | 15 ++++- Prism/src/Prism/Core/Events/MouseEvent.h | 11 ++- Prism/src/Prism/Core/ImGui/ImGuiLayer.cpp | 51 ++++++++++++++ Prism/src/Prism/Core/ImGui/ImGuiLayer.h | 28 ++++++++ Prism/src/Prism/Core/Layer.cpp | 19 ++++++ Prism/src/Prism/Core/Layer.h | 30 +++++++++ Prism/src/Prism/Core/LayerStack.cpp | 48 +++++++++++++ Prism/src/Prism/Core/LayerStack.h | 33 +++++++++ Prism/src/Prism/Core/Log.cpp | 1 + Prism/src/Prism/Core/Log.h | 2 - .../Prism/Platform/Windows/WindowsWindow.cpp | 67 +++++++++++++++++++ Prism/src/pmpch.h | 23 +++++++ Sandbox/Sandbox/Sandbox.cpp | 6 ++ 19 files changed, 392 insertions(+), 27 deletions(-) create mode 100644 Prism/src/Prism/Core/ImGui/ImGuiLayer.cpp create mode 100644 Prism/src/Prism/Core/ImGui/ImGuiLayer.h create mode 100644 Prism/src/Prism/Core/Layer.cpp create mode 100644 Prism/src/Prism/Core/Layer.h create mode 100644 Prism/src/Prism/Core/LayerStack.cpp create mode 100644 Prism/src/Prism/Core/LayerStack.h create mode 100644 Prism/src/pmpch.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dc22c75..0f6c9cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(Prism) set(CMAKE_CXX_STANDARD 17) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin-int) -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin-int) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) # set MSVC output directory diff --git a/Editor/Editor/Editor.cpp b/Editor/Editor/Editor.cpp index 000f81a..6fea737 100644 --- a/Editor/Editor/Editor.cpp +++ b/Editor/Editor/Editor.cpp @@ -5,6 +5,7 @@ #include "Prism/Core/Application.h" #include "Prism/Core/EntryPoint.h" +#include "Prism/Core/ImGui/ImGuiLayer.h" class Editor : public Prism::Application @@ -13,6 +14,11 @@ public: Editor() { } + + virtual void OnInit() override + { + PushLayer(new Prism::ImGuiLayer("ImGui Layer")); + } }; diff --git a/Prism/CMakeLists.txt b/Prism/CMakeLists.txt index d3a4cde..5ffdbbb 100644 --- a/Prism/CMakeLists.txt +++ b/Prism/CMakeLists.txt @@ -10,6 +10,20 @@ add_subdirectory(vendor/spdlog EXCLUDE_FROM_ALL) add_subdirectory(vendor/glfw EXCLUDE_FROM_ALL) +# link libraries +set(LINK_LIBRARIES + spdlog + glfw +) + +# link library opengl +if(WIN32) + list(APPEND LINK_LIBRARIES opengl32) +elseif(UNIX AND NOT APPLE) + list(APPEND LINK_LIBRARIES GL) +endif() + + # static library set(STATIC_LIBRARY ${PROJECT_NAME}-static) @@ -25,15 +39,17 @@ target_include_directories(${STATIC_LIBRARY} PUBLIC ) target_link_libraries(${STATIC_LIBRARY} PRIVATE - spdlog - glfw + ${LINK_LIBRARIES} ) +target_precompile_headers(${STATIC_LIBRARY} PRIVATE + src/pmpch.h +) + set_target_properties(${STATIC_LIBRARY} PROPERTIES OUTPUT_NAME ${PROJECT_NAME} ARCHIVE_OUTPUT_NAME ${PROJECT_NAME} ) - # shared library set(SHARED_LIBRARY ${PROJECT_NAME}-shared) @@ -48,8 +64,10 @@ target_include_directories(${SHARED_LIBRARY} PUBLIC $ ) target_link_libraries(${SHARED_LIBRARY} PRIVATE - spdlog - glfw + ${LINK_LIBRARIES} +) +target_precompile_headers(${SHARED_LIBRARY} PRIVATE + src/pmpch.h ) set_target_properties(${SHARED_LIBRARY} PROPERTIES diff --git a/Prism/src/Prism/Core/Application.cpp b/Prism/src/Prism/Core/Application.cpp index 12dfcb5..ef0a8e5 100644 --- a/Prism/src/Prism/Core/Application.cpp +++ b/Prism/src/Prism/Core/Application.cpp @@ -2,13 +2,19 @@ // Created by sfd on 25-11-15. // +#include "pmpch.h" #include "Application.h" -#include "Log.h" +#include "GLFW/glfw3.h" namespace Prism { + #ifdef _MSC_VER #define BIND_EVENT_FN(fn) std::bind(&Application::##fn, this, std::placeholders::_1) + #else + #define BIND_EVENT_FN(fn) std::bind(&Application::fn, this, std::placeholders::_1) + #endif + Application::Application() { @@ -22,10 +28,20 @@ namespace Prism void Application::Run() { + OnInit(); + while (m_Running) { + glClearColor(0.2, 0.2, 0.2, 1); + glClear(GL_COLOR_BUFFER_BIT); + + for (Layer* layer : m_LayerStack) + layer->OnUpdate(); + m_Window->OnUpdate(); } + + OnShutdown(); } void Application::OnEvent(Event& e) @@ -34,7 +50,21 @@ namespace Prism dispatcher.Dispatch(BIND_EVENT_FN(OnWindowResize)); dispatcher.Dispatch(BIND_EVENT_FN(OnWindowClose)); - PM_CORE_INFO("{}", e.ToString()); + for (auto it = m_LayerStack.end(); it != m_LayerStack.begin();) + { + (*--it)->OnEvent(e); + if (e.Handled) break; + } + } + + void Application::PushLayer(Layer* layer) + { + m_LayerStack.PushLayer(layer); + } + + void Application::PushOverlay(Layer* layer) + { + m_LayerStack.PushOverlay(layer); } bool Application::OnWindowResize(const WindowResizeEvent& e) diff --git a/Prism/src/Prism/Core/Application.h b/Prism/src/Prism/Core/Application.h index f78602c..cc3348d 100644 --- a/Prism/src/Prism/Core/Application.h +++ b/Prism/src/Prism/Core/Application.h @@ -5,9 +5,7 @@ #ifndef APPLICATION_H #define APPLICATION_H -#include - -#include "Core.h" +#include "LayerStack.h" #include "Window.h" #include "Events/ApplicationEvent.h" @@ -26,6 +24,9 @@ namespace Prism virtual void OnShutdown() {} virtual void OnEvent(Event& e); + + void PushLayer(Layer* layer); + void PushOverlay(Layer* layer); private: bool OnWindowResize(const WindowResizeEvent& e); bool OnWindowClose(WindowCloseEvent& e); @@ -34,6 +35,7 @@ namespace Prism std::unique_ptr m_Window; bool m_Running = true; + LayerStack m_LayerStack; }; diff --git a/Prism/src/Prism/Core/Core.cpp b/Prism/src/Prism/Core/Core.cpp index d49397f..96ee4f1 100644 --- a/Prism/src/Prism/Core/Core.cpp +++ b/Prism/src/Prism/Core/Core.cpp @@ -2,11 +2,12 @@ // Created by sfd on 25-11-15. // +#include "pmpch.h" + #include "Core.h" - - #include "Log.h" + namespace Prism { void InitializeCore() @@ -24,8 +25,6 @@ namespace Prism { } #if defined(_WIN32) -#include - BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, diff --git a/Prism/src/Prism/Core/Events/Event.h b/Prism/src/Prism/Core/Events/Event.h index 99001a4..2e8ce71 100644 --- a/Prism/src/Prism/Core/Events/Event.h +++ b/Prism/src/Prism/Core/Events/Event.h @@ -5,7 +5,7 @@ #ifndef EVENT_H #define EVENT_H -#include +#include "pmpch.h" #include "../Core.h" @@ -30,9 +30,17 @@ namespace Prism EventCategoryMouseButton = BIT(4) }; +#ifdef _MSC_VER +// MSVC #define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::##type; }\ virtual EventType GetEventType() const override { return GetStaticType(); }\ virtual const char* GetName() const override { return #type; } +#else +// GCC/Clang +#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::type; }\ +virtual EventType GetEventType() const override { return GetStaticType(); }\ +virtual const char* GetName() const override { return #type; } +#endif #define EVENT_CLASS_CATEGORY(category) virtual int GetCategoryFlags() const override { return category; } @@ -40,6 +48,8 @@ virtual const char* GetName() const override { return #type; } { friend class EventDispatcher; public: + bool Handled = false; + virtual EventType GetEventType() const = 0; virtual const char* GetName() const = 0; virtual int GetCategoryFlags() const = 0; @@ -50,7 +60,6 @@ virtual const char* GetName() const override { return #type; } return GetCategoryFlags() & category; } protected: - bool m_Handled = false; }; class EventDispatcher @@ -68,7 +77,7 @@ virtual const char* GetName() const override { return #type; } { if (m_Event.GetEventType() == T::GetStaticType()) { - m_Event.m_Handled = func(*(T*)&m_Event); + m_Event.Handled = func(*(T*)&m_Event); return true; } return false; diff --git a/Prism/src/Prism/Core/Events/MouseEvent.h b/Prism/src/Prism/Core/Events/MouseEvent.h index c563be9..1e5e699 100644 --- a/Prism/src/Prism/Core/Events/MouseEvent.h +++ b/Prism/src/Prism/Core/Events/MouseEvent.h @@ -12,8 +12,8 @@ namespace Prism { class PRISM_API MouseMovedEvent : public Event { public: - MouseMovedEvent(float x, float y, float dx, float dy) - : m_MouseX(x), m_MouseY(y), m_MouseDX(x), m_MouseDY(dy) {} + MouseMovedEvent(float x, float y) + : m_MouseX(x), m_MouseY(y) {} inline float GetX() const { return m_MouseX; } inline float GetY() const { return m_MouseY; } @@ -68,10 +68,8 @@ namespace Prism { class PRISM_API MouseButtonPressedEvent : public MouseButtonEvent { public: - MouseButtonPressedEvent(int button, int repeatCount) - : MouseButtonEvent(button), m_RepeatCount(repeatCount) {} - - inline int GetRepeatCount() const { return m_RepeatCount; } + MouseButtonPressedEvent(int button) + : MouseButtonEvent(button) {} std::string ToString() const override { @@ -82,7 +80,6 @@ namespace Prism { EVENT_CLASS_TYPE(MouseButtonPressed) private: - int m_RepeatCount; }; class PRISM_API MouseButtonReleasedEvent : public MouseButtonEvent diff --git a/Prism/src/Prism/Core/ImGui/ImGuiLayer.cpp b/Prism/src/Prism/Core/ImGui/ImGuiLayer.cpp new file mode 100644 index 0000000..417abec --- /dev/null +++ b/Prism/src/Prism/Core/ImGui/ImGuiLayer.cpp @@ -0,0 +1,51 @@ +// +// Created by sfd on 25-11-19. +// + +#include "ImGuiLayer.h" + +#include "Prism/Core/Log.h" + +namespace Prism +{ + ImGuiLayer::ImGuiLayer() + : Layer("ImGui Layer") + { + } + + ImGuiLayer::ImGuiLayer(const std::string& name) + : Layer(name) + { + } + + ImGuiLayer::~ImGuiLayer() + { + } + + void ImGuiLayer::OnAttach() + { + Layer::OnAttach(); + } + + void ImGuiLayer::OnDetach() + { + Layer::OnDetach(); + } + + void ImGuiLayer::OnUpdate() + { + Layer::OnUpdate(); + } + + void ImGuiLayer::OnEvent(Event& e) + { + if (GetName() == "Second Layer" && e.IsInCategory(EventCategoryKeyboard)) + { + e.Handled = true; + } + if (GetName() == "Second Layer") + PM_CLIENT_INFO("{0}: {1}", GetName(), e.ToString()); + else if (GetName() == "ImGui Layer") + PM_CLIENT_WARN("{0}: {1}", GetName(), e.ToString()); + } +} diff --git a/Prism/src/Prism/Core/ImGui/ImGuiLayer.h b/Prism/src/Prism/Core/ImGui/ImGuiLayer.h new file mode 100644 index 0000000..88bb844 --- /dev/null +++ b/Prism/src/Prism/Core/ImGui/ImGuiLayer.h @@ -0,0 +1,28 @@ +// +// Created by sfd on 25-11-19. +// + +#ifndef IMGUILAYER_H +#define IMGUILAYER_H + +#include "Prism/Core/Layer.h" + +namespace Prism +{ + class PRISM_API ImGuiLayer : public Layer + { + public: + ImGuiLayer(); + explicit ImGuiLayer(const std::string& name); + + virtual ~ImGuiLayer() override; + + virtual void OnAttach() override; + virtual void OnDetach() override; + virtual void OnUpdate() override; + virtual void OnEvent(Event& e) override; + }; +} + + +#endif //IMGUILAYER_H diff --git a/Prism/src/Prism/Core/Layer.cpp b/Prism/src/Prism/Core/Layer.cpp new file mode 100644 index 0000000..1f24ef7 --- /dev/null +++ b/Prism/src/Prism/Core/Layer.cpp @@ -0,0 +1,19 @@ +// +// Created by sfd on 25-11-19. +// + +#include "Layer.h" + +#include + +namespace Prism +{ + Layer::Layer(std::string name) + : m_DebugName(std::move(name)) + { + } + + Layer::~Layer() + { + } +} diff --git a/Prism/src/Prism/Core/Layer.h b/Prism/src/Prism/Core/Layer.h new file mode 100644 index 0000000..78124f6 --- /dev/null +++ b/Prism/src/Prism/Core/Layer.h @@ -0,0 +1,30 @@ +// +// Created by sfd on 25-11-19. +// + +#ifndef LAYER_H +#define LAYER_H + + +#include "Events/Event.h" + +namespace Prism +{ + class PRISM_API Layer + { + public: + Layer(std::string name = "Layer"); + virtual ~Layer(); + + virtual void OnAttach() {} + virtual void OnDetach() {} + virtual void OnUpdate() {} + virtual void OnEvent(Event& e) {} + + inline const std::string& GetName() const { return m_DebugName; } + protected: + std::string m_DebugName; + }; +} + +#endif //LAYER_H diff --git a/Prism/src/Prism/Core/LayerStack.cpp b/Prism/src/Prism/Core/LayerStack.cpp new file mode 100644 index 0000000..c01a73f --- /dev/null +++ b/Prism/src/Prism/Core/LayerStack.cpp @@ -0,0 +1,48 @@ +// +// Created by sfd on 25-11-19. +// + +#include "pmpch.h" +#include "LayerStack.h" + + +namespace Prism +{ + LayerStack::LayerStack() + { + m_LayerIterator = m_Layers.begin(); + } + + LayerStack::~LayerStack() + { + for (const Layer* layer : m_Layers) + delete layer; + } + + void LayerStack::PushLayer(Layer* layer) + { + m_LayerIterator = m_Layers.emplace(m_LayerIterator, layer); + } + + void LayerStack::PushOverlay(Layer* layer) + { + m_Layers.emplace_back(layer); + } + + void LayerStack::PopLayer(const Layer* layer) + { + const auto it = std::find(m_Layers.begin(), m_Layers.end(), layer); + if (it != m_Layers.end()) + { + m_Layers.erase(it); + m_LayerIterator--; + } + } + + void LayerStack::PopOverlay(const Layer* layer) + { + const auto it = std::find(m_Layers.begin(), m_Layers.end(), layer); + if (it != m_Layers.end()) + m_Layers.erase(it); + } +} diff --git a/Prism/src/Prism/Core/LayerStack.h b/Prism/src/Prism/Core/LayerStack.h new file mode 100644 index 0000000..d758060 --- /dev/null +++ b/Prism/src/Prism/Core/LayerStack.h @@ -0,0 +1,33 @@ +// +// Created by sfd on 25-11-19. +// + +#ifndef LAYERSTACK_H +#define LAYERSTACK_H + +#include "Core.h" +#include "Layer.h" + +namespace Prism +{ + class PRISM_API LayerStack + { + public: + LayerStack(); + ~LayerStack(); + + void PushLayer(Layer* layer); + void PushOverlay(Layer* layer); + void PopLayer(const Layer* layer); + void PopOverlay(const Layer* layer); + + std::vector::iterator begin() { return m_Layers.begin(); } + std::vector::iterator end() { return m_Layers.end(); } + private: + std::vector m_Layers; + std::vector::iterator m_LayerIterator; + }; +} + + +#endif //LAYERSTACK_H diff --git a/Prism/src/Prism/Core/Log.cpp b/Prism/src/Prism/Core/Log.cpp index 1dad074..853faa5 100644 --- a/Prism/src/Prism/Core/Log.cpp +++ b/Prism/src/Prism/Core/Log.cpp @@ -2,6 +2,7 @@ // Created by sfd on 25-11-15. // +#include "pmpch.h" #include "Log.h" #include "spdlog/sinks/stdout_color_sinks-inl.h" diff --git a/Prism/src/Prism/Core/Log.h b/Prism/src/Prism/Core/Log.h index 46e92f3..701080a 100644 --- a/Prism/src/Prism/Core/Log.h +++ b/Prism/src/Prism/Core/Log.h @@ -6,9 +6,7 @@ #define LOG_H #include "spdlog/spdlog.h" -#include "spdlog/fmt/bundled/ostream.h" -#include "Core.h" namespace Prism { diff --git a/Prism/src/Prism/Platform/Windows/WindowsWindow.cpp b/Prism/src/Prism/Platform/Windows/WindowsWindow.cpp index 358da79..480180e 100644 --- a/Prism/src/Prism/Platform/Windows/WindowsWindow.cpp +++ b/Prism/src/Prism/Platform/Windows/WindowsWindow.cpp @@ -2,10 +2,13 @@ // Created by sfd on 25-11-16. // +#include "pmpch.h" #include "WindowsWindow.h" #include "Prism/Core/Log.h" #include "Prism/Core/Events/ApplicationEvent.h" +#include "Prism/Core/Events/KeyEvent.h" +#include "Prism/Core/Events/MouseEvent.h" namespace Prism { @@ -91,6 +94,70 @@ namespace Prism data.EventCallback(event); }); + + glfwSetKeyCallback(m_Window, [](GLFWwindow* window, int key, int scancode, int action, int mods) + { + auto& data = *((WindowData*)glfwGetWindowUserPointer(window)); + + switch (action) + { + case GLFW_PRESS: + { + KeyPressedEvent event(key, 0); + data.EventCallback(event); + break; + } + case GLFW_RELEASE: + { + KeyReleasedEvent event(key); + data.EventCallback(event); + break; + } + case GLFW_REPEAT: + { + KeyPressedEvent event(key, 1); + data.EventCallback(event); + break; + } + } + }); + + glfwSetMouseButtonCallback(m_Window, [](GLFWwindow* window, int button, int action, int mods) + { + auto& data = *((WindowData*)glfwGetWindowUserPointer(window)); + + switch (action) + { + case GLFW_PRESS: + { + MouseButtonPressedEvent event(button); + data.EventCallback(event); + break; + } + case GLFW_RELEASE: + { + MouseButtonReleasedEvent event(button); + data.EventCallback(event); + break; + } + } + }); + + glfwSetScrollCallback(m_Window, [](GLFWwindow* window, double xOffset, double yOffset) + { + auto& data = *((WindowData*)glfwGetWindowUserPointer(window)); + + MouseScrolledEvent event((float)xOffset, (float)yOffset); + data.EventCallback(event); + }); + + glfwSetCursorPosCallback(m_Window, [](GLFWwindow* window, double x, double y) + { + auto& data = *((WindowData*)glfwGetWindowUserPointer(window)); + + MouseMovedEvent event((float)x, (float)y); + data.EventCallback(event); + }); } void WindowsWindow::Shutdown() diff --git a/Prism/src/pmpch.h b/Prism/src/pmpch.h new file mode 100644 index 0000000..b9e7289 --- /dev/null +++ b/Prism/src/pmpch.h @@ -0,0 +1,23 @@ +// +// Created by sfd on 25-11-19. +// + +#ifndef PMPCH_H +#define PMPCH_H + +#if defined(_WIN32) +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Prism/Core/Core.h" + +#endif //PMPCH_H diff --git a/Sandbox/Sandbox/Sandbox.cpp b/Sandbox/Sandbox/Sandbox.cpp index d55e2bb..4c8c1f5 100644 --- a/Sandbox/Sandbox/Sandbox.cpp +++ b/Sandbox/Sandbox/Sandbox.cpp @@ -5,6 +5,7 @@ #include "Prism/Core/Application.h" #include "Prism/Core/EntryPoint.h" +#include "Prism/Core/ImGui/ImGuiLayer.h" class Sandbox : public Prism::Application { @@ -13,6 +14,11 @@ public: { } + virtual void OnInit() override + { + PushLayer(new Prism::ImGuiLayer()); + PushLayer(new Prism::ImGuiLayer("Second Layer")); + } };