pch,layers

This commit is contained in:
2025-11-20 00:24:23 +08:00
parent 122f500a76
commit f70881e364
19 changed files with 392 additions and 27 deletions

View File

@ -4,7 +4,7 @@ project(Prism)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin-int) 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(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# set MSVC output directory # set MSVC output directory

View File

@ -5,6 +5,7 @@
#include "Prism/Core/Application.h" #include "Prism/Core/Application.h"
#include "Prism/Core/EntryPoint.h" #include "Prism/Core/EntryPoint.h"
#include "Prism/Core/ImGui/ImGuiLayer.h"
class Editor : public Prism::Application class Editor : public Prism::Application
@ -13,6 +14,11 @@ public:
Editor() Editor()
{ {
} }
virtual void OnInit() override
{
PushLayer(new Prism::ImGuiLayer("ImGui Layer"));
}
}; };

View File

@ -10,6 +10,20 @@ add_subdirectory(vendor/spdlog EXCLUDE_FROM_ALL)
add_subdirectory(vendor/glfw 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 # static library
set(STATIC_LIBRARY ${PROJECT_NAME}-static) set(STATIC_LIBRARY ${PROJECT_NAME}-static)
@ -25,15 +39,17 @@ target_include_directories(${STATIC_LIBRARY} PUBLIC
) )
target_link_libraries(${STATIC_LIBRARY} PRIVATE target_link_libraries(${STATIC_LIBRARY} PRIVATE
spdlog ${LINK_LIBRARIES}
glfw
) )
target_precompile_headers(${STATIC_LIBRARY} PRIVATE
src/pmpch.h
)
set_target_properties(${STATIC_LIBRARY} PROPERTIES set_target_properties(${STATIC_LIBRARY} PROPERTIES
OUTPUT_NAME ${PROJECT_NAME} OUTPUT_NAME ${PROJECT_NAME}
ARCHIVE_OUTPUT_NAME ${PROJECT_NAME} ARCHIVE_OUTPUT_NAME ${PROJECT_NAME}
) )
# shared library # shared library
set(SHARED_LIBRARY ${PROJECT_NAME}-shared) set(SHARED_LIBRARY ${PROJECT_NAME}-shared)
@ -48,8 +64,10 @@ target_include_directories(${SHARED_LIBRARY} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
) )
target_link_libraries(${SHARED_LIBRARY} PRIVATE target_link_libraries(${SHARED_LIBRARY} PRIVATE
spdlog ${LINK_LIBRARIES}
glfw )
target_precompile_headers(${SHARED_LIBRARY} PRIVATE
src/pmpch.h
) )
set_target_properties(${SHARED_LIBRARY} PROPERTIES set_target_properties(${SHARED_LIBRARY} PROPERTIES

View File

@ -2,13 +2,19 @@
// Created by sfd on 25-11-15. // Created by sfd on 25-11-15.
// //
#include "pmpch.h"
#include "Application.h" #include "Application.h"
#include "Log.h" #include "GLFW/glfw3.h"
namespace Prism namespace Prism
{ {
#ifdef _MSC_VER
#define BIND_EVENT_FN(fn) std::bind(&Application::##fn, this, std::placeholders::_1) #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() Application::Application()
{ {
@ -22,10 +28,20 @@ namespace Prism
void Application::Run() void Application::Run()
{ {
OnInit();
while (m_Running) 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(); m_Window->OnUpdate();
} }
OnShutdown();
} }
void Application::OnEvent(Event& e) void Application::OnEvent(Event& e)
@ -34,7 +50,21 @@ namespace Prism
dispatcher.Dispatch<WindowResizeEvent>(BIND_EVENT_FN(OnWindowResize)); dispatcher.Dispatch<WindowResizeEvent>(BIND_EVENT_FN(OnWindowResize));
dispatcher.Dispatch<WindowCloseEvent>(BIND_EVENT_FN(OnWindowClose)); dispatcher.Dispatch<WindowCloseEvent>(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) bool Application::OnWindowResize(const WindowResizeEvent& e)

View File

@ -5,9 +5,7 @@
#ifndef APPLICATION_H #ifndef APPLICATION_H
#define APPLICATION_H #define APPLICATION_H
#include <memory> #include "LayerStack.h"
#include "Core.h"
#include "Window.h" #include "Window.h"
#include "Events/ApplicationEvent.h" #include "Events/ApplicationEvent.h"
@ -26,6 +24,9 @@ namespace Prism
virtual void OnShutdown() {} virtual void OnShutdown() {}
virtual void OnEvent(Event& e); virtual void OnEvent(Event& e);
void PushLayer(Layer* layer);
void PushOverlay(Layer* layer);
private: private:
bool OnWindowResize(const WindowResizeEvent& e); bool OnWindowResize(const WindowResizeEvent& e);
bool OnWindowClose(WindowCloseEvent& e); bool OnWindowClose(WindowCloseEvent& e);
@ -34,6 +35,7 @@ namespace Prism
std::unique_ptr<Window> m_Window; std::unique_ptr<Window> m_Window;
bool m_Running = true; bool m_Running = true;
LayerStack m_LayerStack;
}; };

View File

@ -2,11 +2,12 @@
// Created by sfd on 25-11-15. // Created by sfd on 25-11-15.
// //
#include "pmpch.h"
#include "Core.h" #include "Core.h"
#include "Log.h" #include "Log.h"
namespace Prism { namespace Prism {
void InitializeCore() void InitializeCore()
@ -24,8 +25,6 @@ namespace Prism {
} }
#if defined(_WIN32) #if defined(_WIN32)
#include <Windows.h>
BOOL APIENTRY DllMain(HMODULE hModule, BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call, DWORD ul_reason_for_call,

View File

@ -5,7 +5,7 @@
#ifndef EVENT_H #ifndef EVENT_H
#define EVENT_H #define EVENT_H
#include <ostream> #include "pmpch.h"
#include "../Core.h" #include "../Core.h"
@ -30,9 +30,17 @@ namespace Prism
EventCategoryMouseButton = BIT(4) EventCategoryMouseButton = BIT(4)
}; };
#ifdef _MSC_VER
// MSVC
#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::##type; }\ #define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::##type; }\
virtual EventType GetEventType() const override { return GetStaticType(); }\ virtual EventType GetEventType() const override { return GetStaticType(); }\
virtual const char* GetName() const override { return #type; } 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; } #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; friend class EventDispatcher;
public: public:
bool Handled = false;
virtual EventType GetEventType() const = 0; virtual EventType GetEventType() const = 0;
virtual const char* GetName() const = 0; virtual const char* GetName() const = 0;
virtual int GetCategoryFlags() const = 0; virtual int GetCategoryFlags() const = 0;
@ -50,7 +60,6 @@ virtual const char* GetName() const override { return #type; }
return GetCategoryFlags() & category; return GetCategoryFlags() & category;
} }
protected: protected:
bool m_Handled = false;
}; };
class EventDispatcher class EventDispatcher
@ -68,7 +77,7 @@ virtual const char* GetName() const override { return #type; }
{ {
if (m_Event.GetEventType() == T::GetStaticType()) if (m_Event.GetEventType() == T::GetStaticType())
{ {
m_Event.m_Handled = func(*(T*)&m_Event); m_Event.Handled = func(*(T*)&m_Event);
return true; return true;
} }
return false; return false;

View File

@ -12,8 +12,8 @@ namespace Prism {
class PRISM_API MouseMovedEvent : public Event class PRISM_API MouseMovedEvent : public Event
{ {
public: public:
MouseMovedEvent(float x, float y, float dx, float dy) MouseMovedEvent(float x, float y)
: m_MouseX(x), m_MouseY(y), m_MouseDX(x), m_MouseDY(dy) {} : m_MouseX(x), m_MouseY(y) {}
inline float GetX() const { return m_MouseX; } inline float GetX() const { return m_MouseX; }
inline float GetY() const { return m_MouseY; } inline float GetY() const { return m_MouseY; }
@ -68,10 +68,8 @@ namespace Prism {
class PRISM_API MouseButtonPressedEvent : public MouseButtonEvent class PRISM_API MouseButtonPressedEvent : public MouseButtonEvent
{ {
public: public:
MouseButtonPressedEvent(int button, int repeatCount) MouseButtonPressedEvent(int button)
: MouseButtonEvent(button), m_RepeatCount(repeatCount) {} : MouseButtonEvent(button) {}
inline int GetRepeatCount() const { return m_RepeatCount; }
std::string ToString() const override std::string ToString() const override
{ {
@ -82,7 +80,6 @@ namespace Prism {
EVENT_CLASS_TYPE(MouseButtonPressed) EVENT_CLASS_TYPE(MouseButtonPressed)
private: private:
int m_RepeatCount;
}; };
class PRISM_API MouseButtonReleasedEvent : public MouseButtonEvent class PRISM_API MouseButtonReleasedEvent : public MouseButtonEvent

View File

@ -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());
}
}

View File

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

View File

@ -0,0 +1,19 @@
//
// Created by sfd on 25-11-19.
//
#include "Layer.h"
#include <utility>
namespace Prism
{
Layer::Layer(std::string name)
: m_DebugName(std::move(name))
{
}
Layer::~Layer()
{
}
}

View File

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

View File

@ -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);
}
}

View File

@ -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<Layer*>::iterator begin() { return m_Layers.begin(); }
std::vector<Layer*>::iterator end() { return m_Layers.end(); }
private:
std::vector<Layer*> m_Layers;
std::vector<Layer*>::iterator m_LayerIterator;
};
}
#endif //LAYERSTACK_H

View File

@ -2,6 +2,7 @@
// Created by sfd on 25-11-15. // Created by sfd on 25-11-15.
// //
#include "pmpch.h"
#include "Log.h" #include "Log.h"
#include "spdlog/sinks/stdout_color_sinks-inl.h" #include "spdlog/sinks/stdout_color_sinks-inl.h"

View File

@ -6,9 +6,7 @@
#define LOG_H #define LOG_H
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "spdlog/fmt/bundled/ostream.h"
#include "Core.h"
namespace Prism namespace Prism
{ {

View File

@ -2,10 +2,13 @@
// Created by sfd on 25-11-16. // Created by sfd on 25-11-16.
// //
#include "pmpch.h"
#include "WindowsWindow.h" #include "WindowsWindow.h"
#include "Prism/Core/Log.h" #include "Prism/Core/Log.h"
#include "Prism/Core/Events/ApplicationEvent.h" #include "Prism/Core/Events/ApplicationEvent.h"
#include "Prism/Core/Events/KeyEvent.h"
#include "Prism/Core/Events/MouseEvent.h"
namespace Prism namespace Prism
{ {
@ -91,6 +94,70 @@ namespace Prism
data.EventCallback(event); 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() void WindowsWindow::Shutdown()

23
Prism/src/pmpch.h Normal file
View File

@ -0,0 +1,23 @@
//
// Created by sfd on 25-11-19.
//
#ifndef PMPCH_H
#define PMPCH_H
#if defined(_WIN32)
#include <Windows.h>
#endif
#include <memory>
#include <vector>
#include <string>
#include <array>
#include <unordered_map>
#include <functional>
#include <algorithm>
#include <ostream>
#include "Prism/Core/Core.h"
#endif //PMPCH_H

View File

@ -5,6 +5,7 @@
#include "Prism/Core/Application.h" #include "Prism/Core/Application.h"
#include "Prism/Core/EntryPoint.h" #include "Prism/Core/EntryPoint.h"
#include "Prism/Core/ImGui/ImGuiLayer.h"
class Sandbox : public Prism::Application class Sandbox : public Prism::Application
{ {
@ -13,6 +14,11 @@ public:
{ {
} }
virtual void OnInit() override
{
PushLayer(new Prism::ImGuiLayer());
PushLayer(new Prism::ImGuiLayer("Second Layer"));
}
}; };