From 122f500a76dba4258daa1210421381bb70b1252d Mon Sep 17 00:00:00 2001 From: Atdunbg <979541498@qq.com> Date: Tue, 18 Nov 2025 13:52:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0spdlog=20=E5=92=8C=20glfw?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 6 + CMakeLists.txt | 4 +- Editor/Editor/Editor.cpp | 20 +++- Editor/Editor/Editor.h | 10 -- Prism/CMakeLists.txt | 29 ++++- Prism/src/Prism.h | 9 ++ Prism/src/Prism/Application.cpp | 12 -- Prism/src/Prism/Application.h | 20 ---- Prism/src/Prism/Core/Application.cpp | 50 +++++++++ Prism/src/Prism/Core/Application.h | 45 ++++++++ Prism/src/Prism/Core/Core.cpp | 63 +++++++++++ Prism/src/Prism/Core/Core.h | 17 +++ Prism/src/Prism/Core/EntryPoint.h | 19 ++++ .../src/Prism/Core/Events/ApplicationEvent.h | 76 +++++++++++++ Prism/src/Prism/Core/Events/Event.h | 87 ++++++++++++++ Prism/src/Prism/Core/Events/KeyEvent.h | 62 ++++++++++ Prism/src/Prism/Core/Events/MouseEvent.h | 106 ++++++++++++++++++ Prism/src/Prism/Core/Log.cpp | 29 +++++ Prism/src/Prism/Core/Log.h | 43 +++++++ Prism/src/Prism/Core/Window.cpp | 10 ++ Prism/src/Prism/Core/Window.h | 50 +++++++++ .../Prism/Platform/Windows/WindowsWindow.cpp | 101 +++++++++++++++++ .../Prism/Platform/Windows/WindowsWindow.h | 49 ++++++++ Prism/vendor/glfw | 1 + Prism/vendor/spdlog | 1 + Sandbox/Sandbox/Sandbox.cpp | 19 +++- Sandbox/Sandbox/Sandbox.h | 12 -- 27 files changed, 884 insertions(+), 66 deletions(-) create mode 100644 .gitmodules delete mode 100644 Editor/Editor/Editor.h delete mode 100644 Prism/src/Prism/Application.cpp delete mode 100644 Prism/src/Prism/Application.h create mode 100644 Prism/src/Prism/Core/Application.cpp create mode 100644 Prism/src/Prism/Core/Application.h create mode 100644 Prism/src/Prism/Core/Core.cpp create mode 100644 Prism/src/Prism/Core/EntryPoint.h create mode 100644 Prism/src/Prism/Core/Events/ApplicationEvent.h create mode 100644 Prism/src/Prism/Core/Events/Event.h create mode 100644 Prism/src/Prism/Core/Events/KeyEvent.h create mode 100644 Prism/src/Prism/Core/Events/MouseEvent.h create mode 100644 Prism/src/Prism/Core/Log.cpp create mode 100644 Prism/src/Prism/Core/Log.h create mode 100644 Prism/src/Prism/Core/Window.cpp create mode 100644 Prism/src/Prism/Core/Window.h create mode 100644 Prism/src/Prism/Platform/Windows/WindowsWindow.cpp create mode 100644 Prism/src/Prism/Platform/Windows/WindowsWindow.h create mode 160000 Prism/vendor/glfw create mode 160000 Prism/vendor/spdlog delete mode 100644 Sandbox/Sandbox/Sandbox.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..cf85a31 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "Prism/vendor/spdlog"] + path = Prism/vendor/spdlog + url = https://github.com/gabime/spdlog.git +[submodule "Prism/vendor/glfw"] + path = Prism/vendor/glfw + url = https://github.com/glfw/glfw.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 14c50ae..dc22c75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ -cmake_minimum_required(VERSION 3.31) +cmake_minimum_required(VERSION 3.10) project(Prism) -set(CMAKE_CXX_STANDARD 20) +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) diff --git a/Editor/Editor/Editor.cpp b/Editor/Editor/Editor.cpp index 5c04441..000f81a 100644 --- a/Editor/Editor/Editor.cpp +++ b/Editor/Editor/Editor.cpp @@ -2,11 +2,21 @@ // Created by sfd on 25-11-15. // -#include "Editor.h" -#include -int main() +#include "Prism/Core/Application.h" +#include "Prism/Core/EntryPoint.h" + + +class Editor : public Prism::Application { - std::cout << "Hello World!\n"; -} +public: + Editor() + { + } +}; + +Prism::Application* Prism::CreateApplication() +{ + return new Editor(); +} diff --git a/Editor/Editor/Editor.h b/Editor/Editor/Editor.h deleted file mode 100644 index b46f979..0000000 --- a/Editor/Editor/Editor.h +++ /dev/null @@ -1,10 +0,0 @@ -// -// Created by sfd on 25-11-15. -// - -#ifndef EDITOR_H -#define EDITOR_H - - - -#endif //EDITOR_H diff --git a/Prism/CMakeLists.txt b/Prism/CMakeLists.txt index 28e5445..d3a4cde 100644 --- a/Prism/CMakeLists.txt +++ b/Prism/CMakeLists.txt @@ -3,16 +3,30 @@ project(Prism) file(GLOB_RECURSE SRC_SOURCE src/**.cpp) +# configure +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +add_subdirectory(vendor/spdlog EXCLUDE_FROM_ALL) +add_subdirectory(vendor/glfw EXCLUDE_FROM_ALL) + # static library set(STATIC_LIBRARY ${PROJECT_NAME}-static) add_library(${STATIC_LIBRARY} STATIC ${SRC_SOURCE}) -target_compile_definitions(${STATIC_LIBRARY} PRIVATE PRISM_STATIC) +target_compile_definitions(${STATIC_LIBRARY} PRIVATE + PRISM_STATIC + $<$:PM_ENABLE_ASSERTS> +) target_include_directories(${STATIC_LIBRARY} PUBLIC - $ + $ + +) +target_link_libraries(${STATIC_LIBRARY} PRIVATE + spdlog + glfw ) set_target_properties(${STATIC_LIBRARY} PROPERTIES OUTPUT_NAME ${PROJECT_NAME} @@ -25,10 +39,17 @@ set(SHARED_LIBRARY ${PROJECT_NAME}-shared) add_library(${SHARED_LIBRARY} SHARED ${SRC_SOURCE}) -target_compile_definitions(${SHARED_LIBRARY} PRIVATE PRISM_SHARED BUILD_PRISM_DLL) +target_compile_definitions(${SHARED_LIBRARY} PRIVATE + PRISM_SHARED BUILD_PRISM_DLL + $<$:PM_ENABLE_ASSERTS> +) target_include_directories(${SHARED_LIBRARY} PUBLIC - $ + $ +) +target_link_libraries(${SHARED_LIBRARY} PRIVATE + spdlog + glfw ) set_target_properties(${SHARED_LIBRARY} PROPERTIES diff --git a/Prism/src/Prism.h b/Prism/src/Prism.h index b1b23f2..8f6cfe7 100644 --- a/Prism/src/Prism.h +++ b/Prism/src/Prism.h @@ -5,4 +5,13 @@ #ifndef PRISM_H #define PRISM_H + +#include "Prism/Core/Application.h" +#include "Prism/Core/EntryPoint.h" +#include "Prism/Core/Events/ApplicationEvent.h" +#include "Prism/Core/Events/Event.h" +#include "Prism/Core/Events/KeyEvent.h" +#include "Prism/Core/Events/MouseEvent.h" + + #endif //PRISM_H diff --git a/Prism/src/Prism/Application.cpp b/Prism/src/Prism/Application.cpp deleted file mode 100644 index bd79159..0000000 --- a/Prism/src/Prism/Application.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// -// Created by sfd on 25-11-15. -// - -#include "Application.h" - -namespace Prism -{ - Application::Application() - { - } -} diff --git a/Prism/src/Prism/Application.h b/Prism/src/Prism/Application.h deleted file mode 100644 index 4ab85f3..0000000 --- a/Prism/src/Prism/Application.h +++ /dev/null @@ -1,20 +0,0 @@ -// -// Created by sfd on 25-11-15. -// - -#ifndef APPLICATION_H -#define APPLICATION_H - -#include "Core/Core.h" - -namespace Prism -{ - class PRISM_API Application - { - public: - Application(); - }; -} - - -#endif //APPLICATION_H diff --git a/Prism/src/Prism/Core/Application.cpp b/Prism/src/Prism/Core/Application.cpp new file mode 100644 index 0000000..12dfcb5 --- /dev/null +++ b/Prism/src/Prism/Core/Application.cpp @@ -0,0 +1,50 @@ +// +// Created by sfd on 25-11-15. +// + +#include "Application.h" + +#include "Log.h" + +namespace Prism +{ + #define BIND_EVENT_FN(fn) std::bind(&Application::##fn, this, std::placeholders::_1) + + Application::Application() + { + m_Window = std::unique_ptr(Window::Create()); + m_Window->SetEventCallback(BIND_EVENT_FN(OnEvent)); + } + + Application::~Application() + { + } + + void Application::Run() + { + while (m_Running) + { + m_Window->OnUpdate(); + } + } + + void Application::OnEvent(Event& e) + { + EventDispatcher dispatcher(e); + dispatcher.Dispatch(BIND_EVENT_FN(OnWindowResize)); + dispatcher.Dispatch(BIND_EVENT_FN(OnWindowClose)); + + PM_CORE_INFO("{}", e.ToString()); + } + + bool Application::OnWindowResize(const WindowResizeEvent& e) + { + return false; + } + + bool Application::OnWindowClose(WindowCloseEvent& e) + { + m_Running = false; + return true; + } +} diff --git a/Prism/src/Prism/Core/Application.h b/Prism/src/Prism/Core/Application.h new file mode 100644 index 0000000..f78602c --- /dev/null +++ b/Prism/src/Prism/Core/Application.h @@ -0,0 +1,45 @@ +// +// Created by sfd on 25-11-15. +// + +#ifndef APPLICATION_H +#define APPLICATION_H + +#include + +#include "Core.h" +#include "Window.h" +#include "Events/ApplicationEvent.h" + +namespace Prism +{ + class PRISM_API Application + { + public: + Application(); + virtual ~Application(); + + void Run(); + + virtual void OnInit() {} + virtual void OnUpdate() {} + virtual void OnShutdown() {} + + virtual void OnEvent(Event& e); + private: + bool OnWindowResize(const WindowResizeEvent& e); + bool OnWindowClose(WindowCloseEvent& e); + + private: + std::unique_ptr m_Window; + + bool m_Running = true; + }; + + + // this function will implemented by client + Application* CreateApplication(); +} + + +#endif //APPLICATION_H diff --git a/Prism/src/Prism/Core/Core.cpp b/Prism/src/Prism/Core/Core.cpp new file mode 100644 index 0000000..d49397f --- /dev/null +++ b/Prism/src/Prism/Core/Core.cpp @@ -0,0 +1,63 @@ +// +// Created by sfd on 25-11-15. +// + +#include "Core.h" + + +#include "Log.h" + +namespace Prism { + + void InitializeCore() + { + Log::Init(); + + PM_CORE_TRACE("Initializing..."); + } + + void ShutdownCore() + { + PM_CORE_TRACE("Shutting down..."); + } + +} +#if defined(_WIN32) + +#include + + +BOOL APIENTRY DllMain(HMODULE hModule, + DWORD ul_reason_for_call, + LPVOID lpReserved +) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + Prism::InitializeCore(); + break; + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + break; + case DLL_PROCESS_DETACH: + Prism::ShutdownCore(); + break; + } + return TRUE; +} + + +#elif defined(__linux__) || defined(__unix__) + +__attribute__((constructor)) +static void library_load() { + Prism::InitializeCore(); +} + +__attribute__((destructor)) +static void library_unload() { + Prism::ShutdownCore(); +} + +#endif \ No newline at end of file diff --git a/Prism/src/Prism/Core/Core.h b/Prism/src/Prism/Core/Core.h index 139bade..cee601a 100644 --- a/Prism/src/Prism/Core/Core.h +++ b/Prism/src/Prism/Core/Core.h @@ -15,5 +15,22 @@ #define PRISM_API #endif +#define BIT(x) (1 << x) + + +#ifdef PM_ENABLE_ASSERTS + #if defined(_WIN32) + #define PM_DEBUGBREAK() __debugbreak() + #elif defined(__linux__) && (defined(__i386__) || defined(__x86_64__)) + #define PM_DEBUGBREAK() asm("int3") + #endif + + #define PM_ASSERT(x, ...) { if(!(x)) { PM_ERROR("Assertion Failed: {0}", __VA_ARGS__); PM_DEBUGBREAK(); } } + #define PM_CORE_ASSERT(x, ...) { if(!(x)) { PM_CORE_ERROR("Assertion Failed: {0}", __VA_ARGS__); PM_DEBUGBREAK(); } } +#else + #define PM_ASSERT(x, ...) + #define PM_CORE_ASSERT(x, ...) +#endif + #endif //CORE_H diff --git a/Prism/src/Prism/Core/EntryPoint.h b/Prism/src/Prism/Core/EntryPoint.h new file mode 100644 index 0000000..dab2303 --- /dev/null +++ b/Prism/src/Prism/Core/EntryPoint.h @@ -0,0 +1,19 @@ +// +// Created by sfd on 25-11-15. +// + +#ifndef ENTRYPOINT_H +#define ENTRYPOINT_H + +#include "Application.h" + +extern Prism::Application* Prism::CreateApplication(); + +int main(int argc, char** argv) +{ + Prism::Application* app = Prism::CreateApplication(); + app->Run(); + delete app; +} + +#endif //ENTRYPOINT_H diff --git a/Prism/src/Prism/Core/Events/ApplicationEvent.h b/Prism/src/Prism/Core/Events/ApplicationEvent.h new file mode 100644 index 0000000..72076ae --- /dev/null +++ b/Prism/src/Prism/Core/Events/ApplicationEvent.h @@ -0,0 +1,76 @@ +// +// Created by sfd on 25-11-16. +// + +#ifndef APPLICATIONEVENT_H +#define APPLICATIONEVENT_H + +#include + +#include "Event.h" + +namespace Prism { + + class PRISM_API WindowResizeEvent : public Event + { + public: + WindowResizeEvent(const uint32_t width, const uint32_t height) + : m_Width(width), m_Height(height) + { + } + + inline unsigned int GetWidth() const { return m_Width; } + inline unsigned int GetHeight() const { return m_Height; } + + std::string ToString() const override + { + std::stringstream ss; + ss << "WindowResizeEvent: " << m_Width << ", " << m_Height; + return ss.str(); + } + + EVENT_CLASS_TYPE(WindowResize) + EVENT_CLASS_CATEGORY(EventCategoryApplication) + + private: + uint32_t m_Width, m_Height; + }; + + class PRISM_API WindowCloseEvent : public Event + { + public: + WindowCloseEvent() {} + + EVENT_CLASS_TYPE(WindowClose) + EVENT_CLASS_CATEGORY(EventCategoryApplication) + }; + + class PRISM_API AppTickEvent : public Event + { + public: + AppTickEvent() {} + + EVENT_CLASS_TYPE(AppTick) + EVENT_CLASS_CATEGORY(EventCategoryApplication) + }; + + class PRISM_API AppUpdateEvent : public Event + { + public: + AppUpdateEvent() {} + + EVENT_CLASS_TYPE(AppUpdate) + EVENT_CLASS_CATEGORY(EventCategoryApplication) + }; + + class PRISM_API AppRenderEvent : public Event + { + public: + AppRenderEvent() {} + + EVENT_CLASS_TYPE(AppRender) + EVENT_CLASS_CATEGORY(EventCategoryApplication) + }; +} + +#endif //APPLICATIONEVENT_H diff --git a/Prism/src/Prism/Core/Events/Event.h b/Prism/src/Prism/Core/Events/Event.h new file mode 100644 index 0000000..99001a4 --- /dev/null +++ b/Prism/src/Prism/Core/Events/Event.h @@ -0,0 +1,87 @@ +// +// Created by sfd on 25-11-16. +// + +#ifndef EVENT_H +#define EVENT_H + +#include + +#include "../Core.h" + +namespace Prism +{ + enum class EventType + { + None = 0, + WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved, + AppTick, AppUpdate, AppRender, + KeyPressed, KeyReleased, + MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled + }; + + enum EventCategory + { + None = 0, + EventCategoryApplication = BIT(0), + EventCategoryInput = BIT(1), + EventCategoryKeyboard = BIT(2), + EventCategoryMouse = BIT(3), + EventCategoryMouseButton = BIT(4) + }; + +#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; } + +#define EVENT_CLASS_CATEGORY(category) virtual int GetCategoryFlags() const override { return category; } + + class PRISM_API Event + { + friend class EventDispatcher; + public: + virtual EventType GetEventType() const = 0; + virtual const char* GetName() const = 0; + virtual int GetCategoryFlags() const = 0; + virtual std::string ToString() const { return GetName(); } + + inline bool IsInCategory(EventCategory category) + { + return GetCategoryFlags() & category; + } + protected: + bool m_Handled = false; + }; + + class EventDispatcher + { + template + using EventFn = std::function; + public: + EventDispatcher(Event& event) + : m_Event(event) + { + } + + template + bool Dispatch(EventFn func) + { + if (m_Event.GetEventType() == T::GetStaticType()) + { + m_Event.m_Handled = func(*(T*)&m_Event); + return true; + } + return false; + } + private: + Event& m_Event; + }; + + inline std::ostream& operator<<(std::ostream& os, const Event& e) + { + return os << e.ToString(); + } + +} + +#endif //EVENT_H diff --git a/Prism/src/Prism/Core/Events/KeyEvent.h b/Prism/src/Prism/Core/Events/KeyEvent.h new file mode 100644 index 0000000..fb180f0 --- /dev/null +++ b/Prism/src/Prism/Core/Events/KeyEvent.h @@ -0,0 +1,62 @@ +// +// Created by sfd on 25-11-16. +// + +#ifndef KEYEVENT_H +#define KEYEVENT_H + +#include "Event.h" + +namespace Prism { + + class PRISM_API KeyEvent : public Event + { + public: + inline int GetKeyCode() const { return m_KeyCode; } + + EVENT_CLASS_CATEGORY(EventCategoryKeyboard | EventCategoryInput) + protected: + KeyEvent(int keycode) + : m_KeyCode(keycode) {} + + int m_KeyCode; + }; + + class PRISM_API KeyPressedEvent : public KeyEvent + { + public: + KeyPressedEvent(int keycode, int repeatCount) + : KeyEvent(keycode), m_RepeatCount(repeatCount) {} + + inline int GetRepeatCount() const { return m_RepeatCount; } + + std::string ToString() const override + { + std::stringstream ss; + ss << "KeyPressedEvent: " << m_KeyCode << " (" << m_RepeatCount << " repeats)"; + return ss.str(); + } + + EVENT_CLASS_TYPE(KeyPressed) + private: + int m_RepeatCount; + }; + + class PRISM_API KeyReleasedEvent : public KeyEvent + { + public: + KeyReleasedEvent(int keycode) + : KeyEvent(keycode) {} + + std::string ToString() const override + { + std::stringstream ss; + ss << "KeyReleasedEvent: " << m_KeyCode; + return ss.str(); + } + + EVENT_CLASS_TYPE(KeyReleased) + }; +} + +#endif //KEYEVENT_H diff --git a/Prism/src/Prism/Core/Events/MouseEvent.h b/Prism/src/Prism/Core/Events/MouseEvent.h new file mode 100644 index 0000000..c563be9 --- /dev/null +++ b/Prism/src/Prism/Core/Events/MouseEvent.h @@ -0,0 +1,106 @@ +// +// Created by sfd on 25-11-16. +// + +#ifndef MOUSEEVENT_H +#define MOUSEEVENT_H + +#include "Event.h" + +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) {} + + inline float GetX() const { return m_MouseX; } + inline float GetY() const { return m_MouseY; } + + std::string ToString() const override + { + std::stringstream ss; + ss << "MouseMovedEvent: " << m_MouseX << ", " << m_MouseY; + return ss.str(); + } + + EVENT_CLASS_TYPE(MouseMoved) + EVENT_CLASS_CATEGORY(EventCategoryMouse | EventCategoryInput) + private: + float m_MouseX, m_MouseY; + }; + class PRISM_API MouseScrolledEvent : public Event + { + public: + MouseScrolledEvent(float offsetX, float offsetY) + : m_XOffset(offsetX), m_YOffset(offsetY) {} + + inline float GetOffsetX() const { return m_XOffset; } + inline float GetOffsetY() const { return m_YOffset; } + + std::string ToString() const override + { + std::stringstream ss; + ss << "MouseScrolledEvent: " << m_XOffset << ", " << m_YOffset; + return ss.str(); + } + + EVENT_CLASS_TYPE(MouseScrolled) + EVENT_CLASS_CATEGORY(EventCategoryMouse | EventCategoryInput) + private: + float m_XOffset, m_YOffset; + }; + + class PRISM_API MouseButtonEvent : public Event + { + public: + inline int GetMouseButton() const { return m_Button; } + + EVENT_CLASS_CATEGORY(EventCategoryMouse | EventCategoryInput) + protected: + MouseButtonEvent(int button) + : m_Button(button) {} + + int m_Button; + }; + + class PRISM_API MouseButtonPressedEvent : public MouseButtonEvent + { + public: + MouseButtonPressedEvent(int button, int repeatCount) + : MouseButtonEvent(button), m_RepeatCount(repeatCount) {} + + inline int GetRepeatCount() const { return m_RepeatCount; } + + std::string ToString() const override + { + std::stringstream ss; + ss << "MouseButtonPressedEvent: " << m_Button; + return ss.str(); + } + + EVENT_CLASS_TYPE(MouseButtonPressed) + private: + int m_RepeatCount; + }; + + class PRISM_API MouseButtonReleasedEvent : public MouseButtonEvent + { + public: + MouseButtonReleasedEvent(const int button) + : MouseButtonEvent(button) {} + + std::string ToString() const override + { + std::stringstream ss; + ss << "MouseButtonReleasedEvent: " << m_Button; + return ss.str(); + } + + EVENT_CLASS_TYPE(MouseButtonReleased) + }; + +} + +#endif //MOUSEEVENT_H diff --git a/Prism/src/Prism/Core/Log.cpp b/Prism/src/Prism/Core/Log.cpp new file mode 100644 index 0000000..1dad074 --- /dev/null +++ b/Prism/src/Prism/Core/Log.cpp @@ -0,0 +1,29 @@ +// +// Created by sfd on 25-11-15. +// + +#include "Log.h" + +#include "spdlog/sinks/stdout_color_sinks-inl.h" + +#ifdef _DEBUG +#define LOG_LEVEL spdlog::level::trace +#else + #define LOG_LEVEL spdlog::level::trace +#endif + +namespace Prism +{ + std::shared_ptr Log::s_CoreLogger; + std::shared_ptr Log::s_ClientLogger; + + void Log::Init() + { + spdlog::set_pattern("%^[%T] [%n]%v%$"); + s_CoreLogger = spdlog::stdout_color_mt("PRISM"); + s_CoreLogger->set_level(LOG_LEVEL); + + s_ClientLogger = spdlog::stdout_color_mt("APP"); + s_ClientLogger->set_level(LOG_LEVEL); + } +} diff --git a/Prism/src/Prism/Core/Log.h b/Prism/src/Prism/Core/Log.h new file mode 100644 index 0000000..46e92f3 --- /dev/null +++ b/Prism/src/Prism/Core/Log.h @@ -0,0 +1,43 @@ +// +// Created by sfd on 25-11-15. +// + +#ifndef LOG_H +#define LOG_H + +#include "spdlog/spdlog.h" +#include "spdlog/fmt/bundled/ostream.h" + +#include "Core.h" + +namespace Prism +{ + class Log + { + public: + static void Init(); + + inline static std::shared_ptr& GetCoreLogger() { return s_CoreLogger; } + inline static std::shared_ptr& GetClientLogger() { return s_ClientLogger; } + private: + static std::shared_ptr s_CoreLogger; + static std::shared_ptr s_ClientLogger; + }; +} + + +#define PM_CORE_TRACE(...) ::Prism::Log::GetCoreLogger()->trace("[TRACE]: " __VA_ARGS__) +#define PM_CORE_DEBUG(...) ::Prism::Log::GetCoreLogger()->debug("[DEBUG]: " __VA_ARGS__) +#define PM_CORE_INFO(...) ::Prism::Log::GetCoreLogger()->info("[INFO]: " __VA_ARGS__) +#define PM_CORE_WARN(...) ::Prism::Log::GetCoreLogger()->warn("[WARN]: " __VA_ARGS__) +#define PM_CORE_ERROR(...) ::Prism::Log::GetCoreLogger()->error("[ERROR]: " __VA_ARGS__) +#define PM_CORE_FATAL(...) ::Prism::Log::GetCoreLogger()->critical("[FATAL]: " __VA_ARGS__) + +#define PM_CLIENT_TRACE(...) ::Prism::Log::GetClientLogger()->trace("[TRACE]: " __VA_ARGS__) +#define PM_CLIENT_DEBUG(...) ::Prism::Log::GetClientLogger()->debug("[DEBUG]: " __VA_ARGS__) +#define PM_CLIENT_INFO(...) ::Prism::Log::GetClientLogger()->info("[INFO]: " __VA_ARGS__) +#define PM_CLIENT_WARN(...) ::Prism::Log::GetClientLogger()->warn("[WARN]: " __VA_ARGS__) +#define PM_CLIENT_ERROR(...) ::Prism::Log::GetClientLogger()->error("[ERROR]: " __VA_ARGS__) +#define PM_CLIENT_FATAL(...) ::Prism::Log::GetClientLogger()->critical("[FATAL]: " __VA_ARGS__) + +#endif //LOG_H diff --git a/Prism/src/Prism/Core/Window.cpp b/Prism/src/Prism/Core/Window.cpp new file mode 100644 index 0000000..5cbe115 --- /dev/null +++ b/Prism/src/Prism/Core/Window.cpp @@ -0,0 +1,10 @@ +// +// Created by sfd on 25-11-16. +// + +#include "Window.h" + + +namespace Prism +{ +} diff --git a/Prism/src/Prism/Core/Window.h b/Prism/src/Prism/Core/Window.h new file mode 100644 index 0000000..374b92d --- /dev/null +++ b/Prism/src/Prism/Core/Window.h @@ -0,0 +1,50 @@ +// +// Created by sfd on 25-11-16. +// + +#ifndef WINDOW_H +#define WINDOW_H +#include +#include + +#include "Events/Event.h" +#include "Prism/Core/Core.h" + + +namespace Prism +{ + struct WindowProps + { + std::string Title; + unsigned int Width; + unsigned int Height; + + WindowProps(const std::string& title = "Prism Engine", + unsigned int width = 1280, + unsigned int height = 720) + : Title(title), Width(width), Height(height) + { + } + }; + + class PRISM_API Window + { + public: + using EventCallbackFn = std::function; + + virtual ~Window() {} + + virtual void OnUpdate() = 0; + virtual void SetEventCallback(const EventCallbackFn& callback) = 0; + virtual unsigned int GetWidth() const = 0; + virtual unsigned int GetHeight() const = 0; + + virtual void SetVSync(bool enable) = 0; + virtual bool const IsVSync() const = 0; + + static Window* Create(const WindowProps& props = WindowProps()); + }; +} + + +#endif //WINDOW_H diff --git a/Prism/src/Prism/Platform/Windows/WindowsWindow.cpp b/Prism/src/Prism/Platform/Windows/WindowsWindow.cpp new file mode 100644 index 0000000..358da79 --- /dev/null +++ b/Prism/src/Prism/Platform/Windows/WindowsWindow.cpp @@ -0,0 +1,101 @@ +// +// Created by sfd on 25-11-16. +// + +#include "WindowsWindow.h" + +#include "Prism/Core/Log.h" +#include "Prism/Core/Events/ApplicationEvent.h" + +namespace Prism +{ + + static bool s_GLFWInitialized = false; + + static void GLFWErrorCallback(int error, const char* description) + { + PM_CORE_ERROR("GLFW Error ({0}): {1}", error, description); + } + + + + Window* Window::Create(const WindowProps& props) + { + return new WindowsWindow(props); + } + + WindowsWindow::WindowsWindow(const WindowProps& props) + { + WindowsWindow::Init(props); + } + + WindowsWindow::~WindowsWindow() + { + } + + void WindowsWindow::OnUpdate() + { + glfwPollEvents(); + glfwSwapBuffers(m_Window); + } + + void WindowsWindow::SetVSync(bool enable) + { + if (enable) + glfwSwapInterval(1); + else + glfwSwapInterval(0); + + m_Data.VSync = enable; + } + + void WindowsWindow::Init(const WindowProps& props) + { + m_Data.Title = props.Title; + m_Data.Width = props.Width; + m_Data.Height = props.Height; + + PM_CORE_INFO("Creating window {0}, ({1}, {2})", props.Title, props.Width, props.Height); + + if (!s_GLFWInitialized) + { + int success = glfwInit(); + PM_CORE_ASSERT(success, "Could not initialize GLFW!"); + glfwSetErrorCallback(GLFWErrorCallback); + + s_GLFWInitialized = true; + } + + m_Window = glfwCreateWindow((int)props.Width, (int)props.Height, m_Data.Title.c_str(), nullptr, nullptr); + glfwMakeContextCurrent(m_Window); + glfwSetWindowUserPointer(m_Window, &m_Data); + SetVSync(true); + + // Set GLFW callbacks + glfwSetWindowSizeCallback(m_Window, [](GLFWwindow* window, int width, int height) + { + auto& data = *((WindowData*)glfwGetWindowUserPointer(window)); + data.Width = width; + data.Height = height; + + WindowResizeEvent event(width, height); + data.EventCallback(event); + }); + + + glfwSetWindowCloseCallback(m_Window, [](GLFWwindow* window) + { + auto& data = *((WindowData*)glfwGetWindowUserPointer(window)); + + WindowCloseEvent event; + data.EventCallback(event); + }); + + } + + void WindowsWindow::Shutdown() + { + } + + +} diff --git a/Prism/src/Prism/Platform/Windows/WindowsWindow.h b/Prism/src/Prism/Platform/Windows/WindowsWindow.h new file mode 100644 index 0000000..00877b6 --- /dev/null +++ b/Prism/src/Prism/Platform/Windows/WindowsWindow.h @@ -0,0 +1,49 @@ +// +// Created by sfd on 25-11-16. +// + +#ifndef WINDOWSWINDOW_H +#define WINDOWSWINDOW_H +#include "Prism/Core/Window.h" + +#include + +namespace Prism +{ + class WindowsWindow : public Window + { + public: + WindowsWindow(const WindowProps& props); + virtual ~WindowsWindow(); + + void OnUpdate() override; + + inline uint32_t GetWidth() const override { return m_Data.Width; } + inline uint32_t GetHeight() const override { return m_Data.Height; } + + inline void SetEventCallback(const EventCallbackFn& callback) override { m_Data.EventCallback = callback; } + bool const IsVSync() const override { return m_Data.VSync; } + void SetVSync(bool enable) override; + + private: + virtual void Init(const WindowProps& props); + virtual void Shutdown(); + + private: + GLFWwindow* m_Window; + + struct WindowData + { + std::string Title; + uint32_t Width, Height; + bool VSync; + + EventCallbackFn EventCallback; + }; + WindowData m_Data; + + }; +} + + +#endif //WINDOWSWINDOW_H diff --git a/Prism/vendor/glfw b/Prism/vendor/glfw new file mode 160000 index 0000000..162896e --- /dev/null +++ b/Prism/vendor/glfw @@ -0,0 +1 @@ +Subproject commit 162896e5b9a40dc382c5c438cd12c90a5ff86ddd diff --git a/Prism/vendor/spdlog b/Prism/vendor/spdlog new file mode 160000 index 0000000..cdbd64e --- /dev/null +++ b/Prism/vendor/spdlog @@ -0,0 +1 @@ +Subproject commit cdbd64e2305a712ec9a360e5a1e05aa1dcab85e7 diff --git a/Sandbox/Sandbox/Sandbox.cpp b/Sandbox/Sandbox/Sandbox.cpp index 1d71f5c..d55e2bb 100644 --- a/Sandbox/Sandbox/Sandbox.cpp +++ b/Sandbox/Sandbox/Sandbox.cpp @@ -2,4 +2,21 @@ // Created by sfd on 25-11-15. // -#include "Sandbox.h" + +#include "Prism/Core/Application.h" +#include "Prism/Core/EntryPoint.h" + +class Sandbox : public Prism::Application +{ +public: + Sandbox() + { + } + +}; + + +Prism::Application* Prism::CreateApplication() +{ + return new Sandbox(); +} diff --git a/Sandbox/Sandbox/Sandbox.h b/Sandbox/Sandbox/Sandbox.h deleted file mode 100644 index 53253db..0000000 --- a/Sandbox/Sandbox/Sandbox.h +++ /dev/null @@ -1,12 +0,0 @@ -// -// Created by sfd on 25-11-15. -// - -#ifndef SANDBOX_H -#define SANDBOX_H - - - - - -#endif //SANDBOX_H