From 21e4432c60f23e68576e6cd35a8ede6ff88f420e Mon Sep 17 00:00:00 2001 From: Light Date: Tue, 15 Jun 2021 22:17:28 +0430 Subject: [PATCH] Maintenance - Some tidying - Minor fixes --- Engine/premake5.lua | 54 +++++++++++-------- Engine/src/Engine/Base.h | 7 +-- Engine/src/Engine/Core/Application.cpp | 5 +- Engine/src/Engine/Core/Window.h | 43 ++++++++++----- Engine/src/Engine/Debug/Exceptions.cpp | 3 +- Engine/src/Engine/Debug/Exceptions.h | 2 + Engine/src/Engine/Debug/Logger.cpp | 24 +++++++-- Engine/src/Engine/Debug/Logger.h | 3 ++ Engine/src/Engine/Graphics/Buffers.cpp | 4 +- Engine/src/Engine/Graphics/Renderer.cpp | 5 +- Engine/src/Engine/Layer/LayerStack.cpp | 3 +- Engine/src/LightEngine.h | 24 +++++---- .../GraphicsAPI/OpenGL/glVertexLayout.cpp | 5 +- Engine/src/Platform/OS/Windows/wWindow.cpp | 40 ++++++++++---- Engine/src/Platform/OS/Windows/wWindow.h | 16 +++--- Sandbox/src/SandboxApp.cpp | 3 +- 16 files changed, 158 insertions(+), 83 deletions(-) diff --git a/Engine/premake5.lua b/Engine/premake5.lua index 8d87102..9d1ed2c 100644 --- a/Engine/premake5.lua +++ b/Engine/premake5.lua @@ -7,53 +7,57 @@ project "Engine" objdir ("%{wks.location}/bin-int/" .. outputdir) -- Compiler -- + -- kind kind "StaticLib" + + -- language language "C++" cppdialect "C++17" + -- pch pchsource "src/Engine/ltpch.cpp" pchheader "ltpch.h" -- Project Files --- files { - "%{prj.location}/src/**.h", - "%{prj.location}/src/**.cpp", - "%{prj.location}/**.lua", + "%{prj.location}/src/**.h" , + "%{prj.location}/src/**.cpp" , + "%{prj.location}/**.lua" , + "%{prj.location}/dxgidebug.dll" , } -- Dependencies -- includedirs { - -- Engine - "%{prj.location}/src/", - "%{prj.location}/src/Engine/", - "%{prj.location}/src/Platform/GraphicsAPI", - "%{prj.location}/src/Platform/OS", + -- engine + "%{prj.location}/src/" , + "%{prj.location}/src/Engine/" , + "%{prj.location}/src/Platform/GraphicsAPI" , + "%{prj.location}/src/Platform/OS" , -- 3rd party (dependenciesdir .. "spdlog/include/"), - (dependenciesdir .. "glfw/include/"), - (dependenciesdir .. "glad/include"), - (dependenciesdir .. "imgui/"), - (dependenciesdir .. "imgui/backends"), - (dependenciesdir .. "glm/"), + (dependenciesdir .. "glfw/include/" ), + (dependenciesdir .. "glad/include" ), + (dependenciesdir .. "imgui/backends" ), + (dependenciesdir .. "imgui/" ), + (dependenciesdir .. "glm/" ), } links { - "GLFW", - "GLAD", - "ImGui", + "GLFW" , + "GLAD" , + "ImGui" , } --- Filters --- -- windows - filter "system:windows" defines "LIGHT_PLATFORM_WINDOWS" systemversion "latest" - staticruntime "On" + staticruntime "on" links { @@ -62,10 +66,6 @@ project "Engine" "D3DCompiler.lib" , } - filter "system:not windows" - excludes "%{prj.location}/src/Platform/GraphicsAPI/DirectX**" - excludes "%{prj.location}/src/Platform/OS/Windows**" - -- debug filter "configurations:Debug" defines "LIGHT_DEBUG" @@ -79,4 +79,12 @@ project "Engine" -- distribution filter "configurations:Distribution" defines "LIGHT_DIST" - optimize "on" \ No newline at end of file + optimize "on" + + --- Excludes --- + -- !windows + filter "system:not windows" + excludes "%{prj.location}/src/Platform/GraphicsAPI/DirectX**" + excludes "%{prj.location}/src/Platform/OS/Windows**" + -- !linux #todo: + -- !mac #todo: \ No newline at end of file diff --git a/Engine/src/Engine/Base.h b/Engine/src/Engine/Base.h index d4bbbe0..be92302 100644 --- a/Engine/src/Engine/Base.h +++ b/Engine/src/Engine/Base.h @@ -8,9 +8,9 @@ #include -#define LT_WIN(x) -#define LT_LIN(x) -#define LT_MAC(x) +#define LT_WIN(x) // Windows +#define LT_LIN(x) // Linux +#define LT_MAC(x) // Mac #if defined(LIGHT_PLATFORM_WINDOWS) #define LT_BUILD_PLATFORM "Windows" @@ -27,5 +27,6 @@ #define BIT(x) 1 << x +// #todo: log to file in distribution builds #define LT_ENGINE_ASSERT(x, ...) { if(!(x)) { LT_ENGINE_CRITICAL(__VA_ARGS__); __debugbreak(); } } #define LT_CLIENT_ASSERT(x, ...) { if(!(x)) { LT_CLIENT_CRITICAL(__VA_ARGS__); __debugbreak(); } } \ No newline at end of file diff --git a/Engine/src/Engine/Core/Application.cpp b/Engine/src/Engine/Core/Application.cpp index 36bdf96..8ce3a68 100644 --- a/Engine/src/Engine/Core/Application.cpp +++ b/Engine/src/Engine/Core/Application.cpp @@ -30,15 +30,16 @@ namespace Light { LT_ENGINE_ASSERT(!m_LayerStack.IsEmpty(), "Application::GameLoop: Layerstack is empty"); // Log window data + Logger::LogDebugData(); LogDebugData(); m_Window->GetGfxContext()->LogDebugData(); m_Window->GetGfxContext()->GetUserInterface()->LogDebugData(); // Show window - m_Window->SetVisible(true); + m_Window->SetVisibility(true); // GAMELOOP // - while (m_Window->IsOpen()) + while (!m_Window->IsClosed()) { // Events m_Window->PollEvents(); diff --git a/Engine/src/Engine/Core/Window.h b/Engine/src/Engine/Core/Window.h index 04ba305..8340151 100644 --- a/Engine/src/Engine/Core/Window.h +++ b/Engine/src/Engine/Core/Window.h @@ -2,6 +2,8 @@ #include "Base.h" +#include + namespace Light { class Event; @@ -10,15 +12,16 @@ namespace Light { struct WindowProperties { std::string title; - unsigned int width, height; - bool vsync; + glm::uvec2 size; + bool vsync, visible; }; class Window { protected: std::unique_ptr m_GraphicsContext; - bool b_Open; + WindowProperties m_Properties = {}; + bool b_Closed = false; public: static Window* Create(std::function callback); @@ -28,21 +31,33 @@ namespace Light { virtual ~Window() = default; - virtual void SetProperties(const WindowProperties& properties) = 0; - - virtual void SetVisible(bool visible) = 0; - - inline GraphicsContext* GetGfxContext() { return m_GraphicsContext.get(); } - - inline bool IsOpen() const { return b_Open; } - virtual void PollEvents() = 0; virtual void OnEvent(const Event& event) = 0; - virtual unsigned int GetHeight() = 0; - virtual unsigned int GetWidth() = 0; + // Setters // + virtual void SetProperties(const WindowProperties& properties) = 0; - virtual inline void* GetNativeHandle() = 0; + virtual void SetTitle(const std::string& title) = 0; + + virtual void SetSize(const glm::uvec2& size) = 0; // pass 0 for width or height for single dimension resizing + + inline void Close() { b_Closed = true; } + virtual void SetVSync(bool vsync, bool toggle = false) = 0; + virtual void SetVisibility(bool visible, bool toggle = false) = 0; + + + // Getters // + inline GraphicsContext* GetGfxContext() const { return m_GraphicsContext.get(); } + + inline const WindowProperties& GetProperties() const { return m_Properties; } + + inline const std::string& GetTitle() const { return m_Properties.title; } + + inline const glm::uvec2& GetSize() const { return m_Properties.size; } + + inline bool IsClosed() const { return b_Closed; } + inline bool IsVSync() const { return m_Properties.vsync; } + inline bool IsVisible() const { return m_Properties.visible; } protected: Window() = default; diff --git a/Engine/src/Engine/Debug/Exceptions.cpp b/Engine/src/Engine/Debug/Exceptions.cpp index ce6d7a0..6dc8c74 100644 --- a/Engine/src/Engine/Debug/Exceptions.cpp +++ b/Engine/src/Engine/Debug/Exceptions.cpp @@ -28,6 +28,7 @@ namespace Light { LT_ENGINE_CRITICAL("________________________________________"); } + LT_WIN( dxException::dxException(long hr, const char* file, int line) { char* message; @@ -44,6 +45,6 @@ namespace Light { LT_ENGINE_CRITICAL("________________________________________"); LocalFree(message); - } + }) } \ No newline at end of file diff --git a/Engine/src/Engine/Debug/Exceptions.h b/Engine/src/Engine/Debug/Exceptions.h index 392bca6..d1c4544 100644 --- a/Engine/src/Engine/Debug/Exceptions.h +++ b/Engine/src/Engine/Debug/Exceptions.h @@ -10,10 +10,12 @@ namespace Light { glException(unsigned int source, unsigned int type, unsigned int id, const char* msg); }; +#ifdef LIGHT_PLATFORM_WINDOWS // DirectX struct dxException : std::exception { dxException(long hr, const char* file, int line); }; +#endif } \ No newline at end of file diff --git a/Engine/src/Engine/Debug/Logger.cpp b/Engine/src/Engine/Debug/Logger.cpp index ee2db31..cff9d13 100644 --- a/Engine/src/Engine/Debug/Logger.cpp +++ b/Engine/src/Engine/Debug/Logger.cpp @@ -12,16 +12,30 @@ namespace Light { void Light::Logger::Initialize() { - // Set spdlog pattern + // set spdlog pattern spdlog::set_pattern("%^[%M:%S:%e] <%n>: %v%$"); - // Create loggers and set levels to minimum + // create loggers s_EngineLogger = spdlog::stdout_color_mt("Engine"); - s_EngineLogger->set_level(spdlog::level::trace); - s_ClientLogger = spdlog::stdout_color_mt("Client"); - s_ClientLogger->set_level(spdlog::level::trace); + // set level +#if defined(LIGHT_DEBUG) + s_EngineLogger->set_level(spdlog::level::trace); + s_ClientLogger->set_level(spdlog::level::trace); +#elif defined (LIGHT_RELEASE) + s_EngineLogger->set_level(spdlog::level::info); + s_ClientLogger->set_level(spdlog::level::info); +#else + s_EngineLogger->set_level(spdlog::level::off); + s_ClientLogger->set_level(spdlog::level::off); +#endif + + + } + + void Logger::LogDebugData() + { LT_ENGINE_INFO("________________________________________"); LT_ENGINE_INFO("Logger::"); LT_ENGINE_INFO(" ClientLevel: {}", Stringifier::spdlogLevel(s_ClientLogger->level())); diff --git a/Engine/src/Engine/Debug/Logger.h b/Engine/src/Engine/Debug/Logger.h index 4dc2955..eb1ed24 100644 --- a/Engine/src/Engine/Debug/Logger.h +++ b/Engine/src/Engine/Debug/Logger.h @@ -34,6 +34,7 @@ namespace Light { + // #todo: add a FileLogger class Logger { private: @@ -46,6 +47,8 @@ namespace Light { static inline std::shared_ptr GetEngineLogger() { return s_EngineLogger; } static inline std::shared_ptr GetClientLogger() { return s_ClientLogger; } + + static void LogDebugData(); }; } \ No newline at end of file diff --git a/Engine/src/Engine/Graphics/Buffers.cpp b/Engine/src/Engine/Graphics/Buffers.cpp index f97cf4d..10d98a8 100644 --- a/Engine/src/Engine/Graphics/Buffers.cpp +++ b/Engine/src/Engine/Graphics/Buffers.cpp @@ -18,8 +18,8 @@ namespace Light { case GraphicsAPI::OpenGL: return new glVertexBuffer(vertices, count); - case GraphicsAPI::DirectX: - return new dxVertexBuffer(vertices, stride, count, sharedContext); + case GraphicsAPI::DirectX: LT_WIN( + return new dxVertexBuffer(vertices, stride, count, sharedContext);) default: LT_ENGINE_ASSERT(false, "VertexBuffer::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); diff --git a/Engine/src/Engine/Graphics/Renderer.cpp b/Engine/src/Engine/Graphics/Renderer.cpp index cb6051e..a90d320 100644 --- a/Engine/src/Engine/Graphics/Renderer.cpp +++ b/Engine/src/Engine/Graphics/Renderer.cpp @@ -49,16 +49,19 @@ namespace Light { const float xMax = position.x + size.x; const float yMax = position.y + size.y; - + // TOP_LEFT m_QuadRenderer.mapCurrent[0].position = { xMin, yMin, position.z }; m_QuadRenderer.mapCurrent[0].tint = glm::vec4(0.1f, 0.1f, 1.0f, 1.0f); + // TOP_RIGHT m_QuadRenderer.mapCurrent[1].position = { xMax, yMin, position.z }; m_QuadRenderer.mapCurrent[1].tint = glm::vec4(0.3f, 0.3f, 0.3f, 1.0f); + // BOTTOM_RIGHT m_QuadRenderer.mapCurrent[2].position = { xMax, yMax, position.z }; m_QuadRenderer.mapCurrent[2].tint = glm::vec4(0.1f, 1.0f, 0.1f, 1.0f); + // BOTTOM_LEFT m_QuadRenderer.mapCurrent[3].position = { xMin, yMax, position.z }; m_QuadRenderer.mapCurrent[3].tint = glm::vec4(1.0f, 0.1f, 0.1f, 1.0f); diff --git a/Engine/src/Engine/Layer/LayerStack.cpp b/Engine/src/Engine/Layer/LayerStack.cpp index 18a1bc1..19202d0 100644 --- a/Engine/src/Engine/Layer/LayerStack.cpp +++ b/Engine/src/Engine/Layer/LayerStack.cpp @@ -14,7 +14,8 @@ namespace Light { LayerStack::LayerStack() { - s_Context = this; // TODO: ASSERT + LT_ENGINE_ASSERT(!s_Context, "LayerStack::LayerStack: context re-initialization") + s_Context = this; } LayerStack::~LayerStack() diff --git a/Engine/src/LightEngine.h b/Engine/src/LightEngine.h index 3755a02..7b72216 100644 --- a/Engine/src/LightEngine.h +++ b/Engine/src/LightEngine.h @@ -1,33 +1,39 @@ #pragma once -// Core +// Core ------------------------- #include "Core/Application.h" #include "Core/Window.h" +// ----------------------------- -// Debug +// Debug #include "Debug/Logger.h" +// ----------------------------- -// Events +// Events ---------------------- #include "Events/Event.h" #include "Events/KeyboardEvents.h" #include "Events/MouseEvents.h" #include "Events/WindowEvents.h" +// ----------------------------- -// Graphics +// Graphics -------------------- #include "Graphics/GraphicsContext.h" -#include "Graphics/RenderCommand.h" #include "Graphics/Renderer.h" +// ----------------------------- -// Layer +// Layer ----------------------- #include "Layer/Layer.h" #include "Layer/LayerStack.h" +// ----------------------------- -// UserInterface +// UserInterface --------------- #include "UserInterface/UserInterface.h" +// ----------------------------- -// Base +// Base ----------------------- #include "Base.h" #ifdef LIGHT_ENTRY_POINT #include "EntryPoint.h" -#endif \ No newline at end of file +#endif +// ----------------------------- \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.cpp b/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.cpp index 40415cf..4702868 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.cpp +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.cpp @@ -9,8 +9,9 @@ namespace Light { glVertexLayout::glVertexLayout(VertexBuffer* buffer, const std::vector>& elements) { - // sanity check + // check LT_ENGINE_ASSERT(dynamic_cast(buffer), "glVertexLayout::glVertexLayout: failed to cast VertexBuffer to glVertexBuffer"); + LT_ENGINE_ASSERT(!elements.empty(), "glVertexLayout::glVertexLayout: elements is empty"); // elements desc std::vector elementsDesc; @@ -22,7 +23,7 @@ namespace Light { stride += elementsDesc.back().typeSize * elementsDesc.back().count; } - // bind + // prepare glCreateVertexArrays(1, &m_ArrayID); buffer->Bind(); Bind(); diff --git a/Engine/src/Platform/OS/Windows/wWindow.cpp b/Engine/src/Platform/OS/Windows/wWindow.cpp index f64800c..6eb58e8 100644 --- a/Engine/src/Platform/OS/Windows/wWindow.cpp +++ b/Engine/src/Platform/OS/Windows/wWindow.cpp @@ -20,7 +20,7 @@ namespace Light { wWindow::wWindow(std::function callback) : m_EventCallback(callback) { - LT_ENGINE_ASSERT(glfwInit(), "wWindow::wWindow: glfwInit: failed to initialize glfw"); + LT_ENGINE_ASSERT(glfwInit(), "wWindow::wWindow: failed to initialize glfw"); glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); @@ -31,7 +31,7 @@ namespace Light { BindGlfwEvents(); m_GraphicsContext = std::unique_ptr(GraphicsContext::Create(GraphicsAPI::DirectX, m_Handle)); - LT_ENGINE_ASSERT(m_GraphicsContext, "wWindow::wWindow: graphics context creation failed"); + LT_ENGINE_ASSERT(m_GraphicsContext, "wWindow::wWindow: failed to create graphics context"); } wWindow::~wWindow() @@ -43,14 +43,16 @@ namespace Light { { m_Properties = properties; - glfwSetWindowSize(m_Handle, properties.width, properties.height); + glfwSetWindowSize(m_Handle, properties.size.x, properties.size.y); glfwSetWindowTitle(m_Handle, properties.title.c_str()); glfwSwapInterval((int)properties.vsync); } - void wWindow::SetVisible(bool visible) + void wWindow::SetVisibility(bool visible, bool toggle) { - if (visible) + m_Properties.visible = toggle ? !m_Properties.visible : visible; + + if (m_Properties.visible) glfwShowWindow(m_Handle); else glfwHideWindow(m_Handle); @@ -66,24 +68,40 @@ namespace Light { switch (event.GetEventType()) { case EventType::WindowClosed: - b_Open = false; + b_Closed = true; + break; + case EventType::WindowResized: m_GraphicsContext->OnWindowResize((const WindowResizedEvent&)event); + break; } } - unsigned int wWindow::GetWidth() + void wWindow::SetTitle(const std::string& title) { - return m_Properties.width; + m_Properties.title = title; + + glfwSetWindowTitle(m_Handle, m_Properties.title.c_str()); } - unsigned int wWindow::GetHeight() + void wWindow::SetVSync(bool vsync, bool toggle /*= false*/) { - return m_Properties.height; + m_Properties.vsync = toggle ? !m_Properties.vsync : vsync; + + glfwSwapInterval(m_Properties.vsync); + } + + void wWindow::SetSize(const glm::uvec2& size) + { + m_Properties.size.x = size.x == 0u ? m_Properties.size.x : size.x; + m_Properties.size.y = size.y == 0u ? m_Properties.size.y : size.y; + + glfwSetWindowSize(m_Handle, m_Properties.size.x, m_Properties.size.y); } void wWindow::BindGlfwEvents() { + // Mouse Events // glfwSetCursorPosCallback(m_Handle, [](GLFWwindow* window, double xpos, double ypos) { std::function callback = *(std::function*)glfwGetWindowUserPointer(window); @@ -106,6 +124,7 @@ namespace Light { callback(WheelScrolledEvent(yoffset)); }); + // Keyboard Events // glfwSetKeyCallback(m_Handle, [](GLFWwindow* window, int key, int scancode, int action, int mods) { std::function callback = *(std::function*)glfwGetWindowUserPointer(window); @@ -116,6 +135,7 @@ namespace Light { callback(KeyReleasedEvent(key)); }); + // Window Events // glfwSetWindowCloseCallback(m_Handle, [](GLFWwindow* window) { std::function callback = *(std::function*)glfwGetWindowUserPointer(window); diff --git a/Engine/src/Platform/OS/Windows/wWindow.h b/Engine/src/Platform/OS/Windows/wWindow.h index cd361e5..27605c1 100644 --- a/Engine/src/Platform/OS/Windows/wWindow.h +++ b/Engine/src/Platform/OS/Windows/wWindow.h @@ -13,7 +13,6 @@ namespace Light { { private: GLFWwindow* m_Handle = nullptr; - WindowProperties m_Properties = {}; std::function m_EventCallback; @@ -22,17 +21,18 @@ namespace Light { ~wWindow(); - virtual void SetProperties(const WindowProperties& properties) override; + void PollEvents() override; + void OnEvent(const Event& event) override; - virtual void SetVisible(bool visible) override; + // Setters // + void SetProperties(const WindowProperties& properties) override; - virtual void PollEvents() override; - virtual void OnEvent(const Event& event) override; + void SetTitle(const std::string& title) override; - virtual unsigned int GetWidth() override; - virtual unsigned int GetHeight() override; + void SetSize(const glm::uvec2& size) override; - virtual inline void* GetNativeHandle() override { return m_Handle; } + void SetVSync(bool vsync, bool toggle = false) override; + void SetVisibility(bool visible, bool toggle = false) override; private: void BindGlfwEvents(); diff --git a/Sandbox/src/SandboxApp.cpp b/Sandbox/src/SandboxApp.cpp index 01f8873..af04a1e 100644 --- a/Sandbox/src/SandboxApp.cpp +++ b/Sandbox/src/SandboxApp.cpp @@ -13,8 +13,7 @@ public: // Set window properties Light::WindowProperties properties; properties.title = "Sandbox"; - properties.width = 800u; - properties.height = 600u; + properties.size = glm::uvec2(800u, 600u); properties.vsync = true; m_Window->SetProperties(properties);