From e050dce79b67ae6386240567a994454711ab998d Mon Sep 17 00:00:00 2001 From: hachem Date: Thu, 14 Aug 2025 00:42:05 +0200 Subject: [add]: Custom memory managers and fix includes --- src/Core/Application.cpp | 8 ++++---- src/Core/Application.h | 10 ++++------ src/Core/Camera.cpp | 1 + src/Core/Log.cpp | 7 ++++--- src/Core/Log.h | 7 ++++--- src/Core/Memory.h | 24 ++++++++++++++++++++++++ src/Core/StateManager.cpp | 2 +- src/Core/StateManager.h | 10 +++++----- src/Core/Window.cpp | 2 +- src/Core/Window.h | 5 +++-- src/Platform/OpenGL/OpenGLRendererAPI.cpp | 8 ++++---- src/Platform/OpenGL/OpenGLRendererAPI.h | 6 +++++- src/Platform/OpenGL/OpenGLShader.cpp | 2 ++ src/Platform/OpenGL/OpenGLShader.h | 1 + src/Platform/OpenGL/OpenGLTexture.cpp | 5 ++--- src/Platform/OpenGL/OpenGLTexture.h | 2 ++ src/Platform/OpenGL/OpenGLVertexArray.cpp | 5 +++-- src/Platform/OpenGL/OpenGLVertexArray.h | 15 ++++++++------- src/Platform/Vulkan/VulkanRendererAPI.cpp | 2 +- src/Platform/Vulkan/VulkanRendererAPI.h | 3 ++- src/Platform/Vulkan/VulkanVertexArray.cpp | 4 ++-- src/Platform/Vulkan/VulkanVertexArray.h | 13 +++++++------ src/Rendering/Renderer.cpp | 13 ++++++------- src/Rendering/Renderer.h | 14 +++++++------- src/Rendering/Shader.cpp | 16 ++++++++-------- src/Rendering/Shader.h | 15 ++++++++------- src/Rendering/Texture.cpp | 17 +++++++++-------- src/Rendering/Texture.h | 7 ++++--- src/Rendering/VertexArray.cpp | 1 + src/Rendering/VertexArray.h | 12 +++++++----- src/Rendering/VertexBuffer.cpp | 1 + src/States/SimulationState.cpp | 13 ++++++------- src/States/SimulationState.h | 17 +++++++++-------- 33 files changed, 156 insertions(+), 112 deletions(-) create mode 100644 src/Core/Memory.h diff --git a/src/Core/Application.cpp b/src/Core/Application.cpp index 4a0726d..a886327 100644 --- a/src/Core/Application.cpp +++ b/src/Core/Application.cpp @@ -16,7 +16,7 @@ namespace Donut { s_Instance = this; - m_Window = std::make_unique(name, width, height); + m_Window = CreateScope(name, width, height); m_Window->SetEventCallback([this](Event& event) { OnEvent(event); @@ -109,9 +109,9 @@ namespace Donut Renderer::OnWindowResize(1280, 720); RenderCommand::SetFaceCulling(false); - m_StateManager = std::make_unique(); - m_StateManager->RegisterState("Config", std::make_unique()); - m_StateManager->RegisterState("Simulation", std::make_unique()); + m_StateManager = CreateScope(); + m_StateManager->RegisterState("Config", CreateScope()); + m_StateManager->RegisterState("Simulation", CreateScope()); m_StateManager->SwitchToState("Config"); } diff --git a/src/Core/Application.h b/src/Core/Application.h index 92ab781..3d0ba25 100644 --- a/src/Core/Application.h +++ b/src/Core/Application.h @@ -1,12 +1,10 @@ #pragma once -#include -#include - +#include "Memory.h" #include "Window.h" #include "Event.h" -#include "StateManager.h" #include "Log.h" +#include "StateManager.h" namespace Donut { @@ -29,8 +27,8 @@ namespace Donut void OnRender(); void OnEvent(Event& event); private: - std::unique_ptr m_StateManager; - std::unique_ptr m_Window; + Scope m_StateManager; + Scope m_Window; bool m_Running; bool m_Minimized; diff --git a/src/Core/Camera.cpp b/src/Core/Camera.cpp index ecfb6df..4dff2d7 100644 --- a/src/Core/Camera.cpp +++ b/src/Core/Camera.cpp @@ -1,4 +1,5 @@ #include "Camera.h" + #include namespace Donut diff --git a/src/Core/Log.cpp b/src/Core/Log.cpp index 90c7094..f2f3e44 100644 --- a/src/Core/Log.cpp +++ b/src/Core/Log.cpp @@ -1,4 +1,5 @@ #include "Log.h" + #include #if defined(DONUT_WINDOWS) @@ -7,11 +8,11 @@ namespace Donut { - std::shared_ptr Logger::s_Logger; + Ref Logger::s_Logger; void Logger::Init() { - s_Logger = std::make_shared(); + s_Logger = CreateRef(); s_Logger->SetLogLevel(LogLevel::INFO); s_Logger->EnableConsoleOutput(true); s_Logger->EnableFileOutput(true); @@ -27,7 +28,7 @@ namespace Donut s_Logger.reset(); } - std::shared_ptr Logger::GetLogger() + Ref Logger::GetLogger() { return s_Logger; } diff --git a/src/Core/Log.h b/src/Core/Log.h index ad1b959..c7b2e47 100644 --- a/src/Core/Log.h +++ b/src/Core/Log.h @@ -1,7 +1,8 @@ #pragma once +#include "Core/Memory.h" + #include -#include #include #include #include @@ -28,7 +29,7 @@ namespace Donut static void Init(); static void Shutdown(); - static std::shared_ptr GetLogger(); + static Ref GetLogger(); template static void Trace(const std::string_view& format, const Args&... args) @@ -135,7 +136,7 @@ namespace Donut std::ofstream m_LogFile; std::mutex m_Mutex; - static std::shared_ptr s_Logger; + static Ref s_Logger; }; } diff --git a/src/Core/Memory.h b/src/Core/Memory.h new file mode 100644 index 0000000..0abc6b1 --- /dev/null +++ b/src/Core/Memory.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +namespace Donut +{ + template + using Ref = std::shared_ptr; + + template + using Scope = std::unique_ptr; + + template + constexpr Scope CreateScope(Args&& ... args) + { + return std::make_unique(std::forward(args)...); + } + + template + constexpr Ref CreateRef(Args&& ... args) + { + return std::make_shared(std::forward(args)...); + } +}; \ No newline at end of file diff --git a/src/Core/StateManager.cpp b/src/Core/StateManager.cpp index 30d6b1d..266bafa 100644 --- a/src/Core/StateManager.cpp +++ b/src/Core/StateManager.cpp @@ -19,7 +19,7 @@ namespace Donut m_CurrentStateName = ""; } - void StateManager::RegisterState(const std::string& stateName, std::unique_ptr state) + void StateManager::RegisterState(const std::string& stateName, Scope state) { if (m_States.find(stateName) != m_States.end()) DONUT_WARN("State '{}' already exists, overwriting", stateName); diff --git a/src/Core/StateManager.h b/src/Core/StateManager.h index 06370f3..c0b2b13 100644 --- a/src/Core/StateManager.h +++ b/src/Core/StateManager.h @@ -1,11 +1,11 @@ #pragma once -#include +#include "Memory.h" +#include "State.h" + #include #include -#include "State.h" - namespace Donut { class StateManager @@ -18,7 +18,7 @@ namespace Donut void Render(); void OnEvent(Event& event); - void RegisterState(const std::string& stateName, std::unique_ptr state); + void RegisterState(const std::string& stateName, Scope state); void SwitchToState(const std::string& stateName); std::string GetCurrentStateName() const { return m_CurrentStateName; } @@ -34,6 +34,6 @@ namespace Donut State* m_CurrentState = nullptr; std::string m_CurrentStateName = ""; - std::unordered_map> m_States; + std::unordered_map> m_States; }; } diff --git a/src/Core/Window.cpp b/src/Core/Window.cpp index 832235a..73bd8db 100644 --- a/src/Core/Window.cpp +++ b/src/Core/Window.cpp @@ -1,6 +1,6 @@ #include "Window.h" + #include -#include namespace Donut { diff --git a/src/Core/Window.h b/src/Core/Window.h index 97bbbe2..c598bf2 100644 --- a/src/Core/Window.h +++ b/src/Core/Window.h @@ -1,10 +1,11 @@ #pragma once -#include -#include #include "Event.h" #include "Log.h" +#include +#include + namespace Donut { class Window diff --git a/src/Platform/OpenGL/OpenGLRendererAPI.cpp b/src/Platform/OpenGL/OpenGLRendererAPI.cpp index 7957fa0..3c49144 100644 --- a/src/Platform/OpenGL/OpenGLRendererAPI.cpp +++ b/src/Platform/OpenGL/OpenGLRendererAPI.cpp @@ -1,7 +1,7 @@ #include "OpenGLRendererAPI.h" + #include #include -#include namespace Donut { @@ -9,13 +9,13 @@ namespace Donut { if (!glfwGetCurrentContext()) { - std::cout << "No OpenGL context is current! Cannot initialize GLAD." << std::endl; + DONUT_ERROR("No OpenGL context is current! Cannot initialize GLAD."); return; } if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { - std::cout << "Failed to initialize GLAD!" << std::endl; + DONUT_ERROR("Failed to initialize GLAD!"); return; } @@ -61,7 +61,7 @@ namespace Donut glDisable(GL_CULL_FACE); } - void OpenGLRendererAPI::DrawIndexed(const std::shared_ptr& vertexArray, uint32_t indexCount) + void OpenGLRendererAPI::DrawIndexed(const Ref& vertexArray, uint32_t indexCount) { uint32_t count = indexCount ? indexCount : vertexArray->GetIndexBuffer()->GetCount(); glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr); diff --git a/src/Platform/OpenGL/OpenGLRendererAPI.h b/src/Platform/OpenGL/OpenGLRendererAPI.h index 8211504..97e508b 100644 --- a/src/Platform/OpenGL/OpenGLRendererAPI.h +++ b/src/Platform/OpenGL/OpenGLRendererAPI.h @@ -1,6 +1,10 @@ #pragma once +#include "Core/Memory.h" +#include "Core/Log.h" + #include "Rendering/Renderer.h" + #include namespace Donut @@ -17,7 +21,7 @@ namespace Donut virtual void EnableDepthTest() override; virtual void SetFaceCulling(bool enabled) override; - virtual void DrawIndexed(const std::shared_ptr& vertexArray, + virtual void DrawIndexed(const Ref& vertexArray, uint32_t indexCount = 0) override; }; }; diff --git a/src/Platform/OpenGL/OpenGLShader.cpp b/src/Platform/OpenGL/OpenGLShader.cpp index b24e158..b163cde 100644 --- a/src/Platform/OpenGL/OpenGLShader.cpp +++ b/src/Platform/OpenGL/OpenGLShader.cpp @@ -1,6 +1,8 @@ #include "OpenGLShader.h" + #include #include + #include #include diff --git a/src/Platform/OpenGL/OpenGLShader.h b/src/Platform/OpenGL/OpenGLShader.h index cb7fec1..e9a7dc2 100644 --- a/src/Platform/OpenGL/OpenGLShader.h +++ b/src/Platform/OpenGL/OpenGLShader.h @@ -1,6 +1,7 @@ #pragma once #include "Rendering/Shader.h" + #include #include diff --git a/src/Platform/OpenGL/OpenGLTexture.cpp b/src/Platform/OpenGL/OpenGLTexture.cpp index 72e40ee..e12dea4 100644 --- a/src/Platform/OpenGL/OpenGLTexture.cpp +++ b/src/Platform/OpenGL/OpenGLTexture.cpp @@ -1,5 +1,4 @@ #include "OpenGLTexture.h" -#include namespace Donut { @@ -40,7 +39,7 @@ namespace Donut uint32_t whitePixel = 0xFFFFFFFF; glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, m_DataFormat, GL_UNSIGNED_BYTE, &whitePixel); - std::cout << "Created default texture (stb_image not available for loading: " << path << ")" << std::endl; + DONUT_INFO("Created default texture (stb_image not available for loading: ", path, ")"); } OpenGLTexture2D::~OpenGLTexture2D() @@ -53,7 +52,7 @@ namespace Donut uint32_t bpp = m_DataFormat == GL_RGBA ? 4 : 3; if (size != m_Width * m_Height * bpp) { - std::cout << "Data must be entire texture!" << std::endl; + DONUT_ERROR("Data must be entire texture!"); return; } diff --git a/src/Platform/OpenGL/OpenGLTexture.h b/src/Platform/OpenGL/OpenGLTexture.h index 3c6c08a..1c3cb05 100644 --- a/src/Platform/OpenGL/OpenGLTexture.h +++ b/src/Platform/OpenGL/OpenGLTexture.h @@ -1,6 +1,8 @@ #pragma once #include "Rendering/Texture.h" +#include "Core/Log.h" + #include namespace Donut diff --git a/src/Platform/OpenGL/OpenGLVertexArray.cpp b/src/Platform/OpenGL/OpenGLVertexArray.cpp index c24441e..6019287 100644 --- a/src/Platform/OpenGL/OpenGLVertexArray.cpp +++ b/src/Platform/OpenGL/OpenGLVertexArray.cpp @@ -1,4 +1,5 @@ #include + #include "OpenGLVertexArray.h" #include "Rendering/VertexBuffer.h" #include "Rendering/IndexBuffer.h" @@ -25,7 +26,7 @@ namespace Donut glBindVertexArray(0); } - void OpenGLVertexArray::AddVertexBuffer(const std::shared_ptr& vertexBuffer) + void OpenGLVertexArray::AddVertexBuffer(const Ref& vertexBuffer) { glBindVertexArray(m_RendererID); vertexBuffer->Bind(); @@ -46,7 +47,7 @@ namespace Donut m_VertexBuffers.push_back(vertexBuffer); } - void OpenGLVertexArray::SetIndexBuffer(const std::shared_ptr& indexBuffer) + void OpenGLVertexArray::SetIndexBuffer(const Ref& indexBuffer) { glBindVertexArray(m_RendererID); indexBuffer->Bind(); diff --git a/src/Platform/OpenGL/OpenGLVertexArray.h b/src/Platform/OpenGL/OpenGLVertexArray.h index 545f02c..b6ae82f 100644 --- a/src/Platform/OpenGL/OpenGLVertexArray.h +++ b/src/Platform/OpenGL/OpenGLVertexArray.h @@ -1,11 +1,12 @@ #pragma once +#include "Core/Memory.h" + #include "Rendering/VertexArray.h" #include "Rendering/VertexBuffer.h" #include "Rendering/IndexBuffer.h" #include -#include namespace Donut { @@ -19,22 +20,22 @@ namespace Donut virtual void Bind() const override; virtual void Unbind() const override; - virtual void AddVertexBuffer(const std::shared_ptr& vertexBuffer) override; - virtual void SetIndexBuffer(const std::shared_ptr& indexBuffer) override; + virtual void AddVertexBuffer(const Ref& vertexBuffer) override; + virtual void SetIndexBuffer(const Ref& indexBuffer) override; - virtual const std::vector>& GetVertexBuffers() const override + virtual const std::vector>& GetVertexBuffers() const override { return m_VertexBuffers; } - virtual const std::shared_ptr& GetIndexBuffer() const override + virtual const Ref& GetIndexBuffer() const override { return m_IndexBuffer; } private: uint32_t m_RendererID; uint32_t m_VertexBufferIndex = 0; - std::vector> m_VertexBuffers; - std::shared_ptr m_IndexBuffer; + std::vector> m_VertexBuffers; + Ref m_IndexBuffer; }; }; diff --git a/src/Platform/Vulkan/VulkanRendererAPI.cpp b/src/Platform/Vulkan/VulkanRendererAPI.cpp index 377f100..86d9653 100644 --- a/src/Platform/Vulkan/VulkanRendererAPI.cpp +++ b/src/Platform/Vulkan/VulkanRendererAPI.cpp @@ -32,7 +32,7 @@ namespace Donut // TODO(Hachem): Implement Vulkan face culling setting } - void VulkanRendererAPI::DrawIndexed(const std::shared_ptr& vertexArray, uint32_t indexCount) + void VulkanRendererAPI::DrawIndexed(const Ref& vertexArray, uint32_t indexCount) { // TODO(Hachem): Implement Vulkan indexed drawing } diff --git a/src/Platform/Vulkan/VulkanRendererAPI.h b/src/Platform/Vulkan/VulkanRendererAPI.h index 73cfbdb..47342ac 100644 --- a/src/Platform/Vulkan/VulkanRendererAPI.h +++ b/src/Platform/Vulkan/VulkanRendererAPI.h @@ -1,5 +1,6 @@ #pragma once +#include "Core/Memory.h" #include "Rendering/Renderer.h" namespace Donut @@ -16,7 +17,7 @@ namespace Donut virtual void EnableDepthTest() override; virtual void SetFaceCulling(bool enabled) override; - virtual void DrawIndexed(const std::shared_ptr& vertexArray, + virtual void DrawIndexed(const Ref& vertexArray, uint32_t indexCount = 0) override; }; }; diff --git a/src/Platform/Vulkan/VulkanVertexArray.cpp b/src/Platform/Vulkan/VulkanVertexArray.cpp index 9fda0a3..de22f05 100644 --- a/src/Platform/Vulkan/VulkanVertexArray.cpp +++ b/src/Platform/Vulkan/VulkanVertexArray.cpp @@ -22,13 +22,13 @@ namespace Donut // TODO(Hachem): Implement Vulkan vertex array unbinding } - void VulkanVertexArray::AddVertexBuffer(const std::shared_ptr& vertexBuffer) + void VulkanVertexArray::AddVertexBuffer(const Ref& vertexBuffer) { // TODO(Hachem): Implement Vulkan vertex buffer addition m_VertexBuffers.push_back(vertexBuffer); } - void VulkanVertexArray::SetIndexBuffer(const std::shared_ptr& indexBuffer) + void VulkanVertexArray::SetIndexBuffer(const Ref& indexBuffer) { // TODO(Hachem): Implement Vulkan index buffer setting m_IndexBuffer = indexBuffer; diff --git a/src/Platform/Vulkan/VulkanVertexArray.h b/src/Platform/Vulkan/VulkanVertexArray.h index a6eef6a..e4484e5 100644 --- a/src/Platform/Vulkan/VulkanVertexArray.h +++ b/src/Platform/Vulkan/VulkanVertexArray.h @@ -1,6 +1,7 @@ #pragma once #include "Rendering/VertexArray.h" +#include "Core/Memory.h" namespace Donut { @@ -14,14 +15,14 @@ namespace Donut virtual void Bind() const override; virtual void Unbind() const override; - virtual void AddVertexBuffer(const std::shared_ptr& vertexBuffer) override; - virtual void SetIndexBuffer(const std::shared_ptr& indexBuffer) override; + virtual void AddVertexBuffer(const Ref& vertexBuffer) override; + virtual void SetIndexBuffer(const Ref& indexBuffer) override; - virtual const std::vector>& GetVertexBuffers() const { return m_VertexBuffers; } - virtual const std::shared_ptr& GetIndexBuffer() const { return m_IndexBuffer; } + virtual const std::vector>& GetVertexBuffers() const { return m_VertexBuffers; } + virtual const Ref& GetIndexBuffer() const { return m_IndexBuffer; } private: uint32_t m_RendererID; - std::vector> m_VertexBuffers; - std::shared_ptr m_IndexBuffer; + std::vector> m_VertexBuffers; + Ref m_IndexBuffer; }; }; diff --git a/src/Rendering/Renderer.cpp b/src/Rendering/Renderer.cpp index 577dcf4..c0684cb 100644 --- a/src/Rendering/Renderer.cpp +++ b/src/Rendering/Renderer.cpp @@ -3,19 +3,18 @@ #include "Platform/OpenGL/OpenGLRendererAPI.h" #include "Platform/Vulkan/VulkanRendererAPI.h" -#include #include namespace Donut { - std::unique_ptr RendererAPI::Create() + Scope RendererAPI::Create() { switch (s_API) { case API::OpenGL: - return std::make_unique(); + return CreateScope(); case API::Vulkan: - return std::make_unique(); + return CreateScope(); default: return nullptr; } @@ -38,8 +37,8 @@ namespace Donut RenderCommand::SetViewport(0, 0, width, height); } - void Renderer::Submit(const std::shared_ptr& shader, - const std::shared_ptr& vertexArray, + void Renderer::Submit(const Ref& shader, + const Ref& vertexArray, const glm::mat4& transform, const glm::mat4& viewProjection) { @@ -51,7 +50,7 @@ namespace Donut RenderCommand::DrawIndexed(vertexArray); } - std::unique_ptr RenderCommand::s_RendererAPI = RendererAPI::Create(); + Scope RenderCommand::s_RendererAPI = RendererAPI::Create(); void Renderer::SetClearColor(const glm::vec4& color) { diff --git a/src/Rendering/Renderer.h b/src/Rendering/Renderer.h index 61dae82..fa37fc8 100644 --- a/src/Rendering/Renderer.h +++ b/src/Rendering/Renderer.h @@ -1,10 +1,10 @@ #pragma once +#include "Core/Memory.h" #include "VertexArray.h" #include "Shader.h" #include -#include namespace Donut { @@ -28,12 +28,12 @@ namespace Donut virtual void EnableDepthTest() = 0; virtual void SetFaceCulling(bool enabled) = 0; - virtual void DrawIndexed(const std::shared_ptr& vertexArray, + virtual void DrawIndexed(const Ref& vertexArray, uint32_t indexCount = 0) = 0; inline static API GetAPI() { return s_API; } inline static void SetAPI(API api) { s_API = api; } - static std::unique_ptr Create(); + static Scope Create(); private: static API s_API; }; @@ -71,13 +71,13 @@ namespace Donut s_RendererAPI->SetFaceCulling(enabled); } - inline static void DrawIndexed(const std::shared_ptr& vertexArray, uint32_t indexCount = 0) + inline static void DrawIndexed(const Ref& vertexArray, uint32_t indexCount = 0) { s_RendererAPI->DrawIndexed(vertexArray, indexCount); } private: - static std::unique_ptr s_RendererAPI; + static Scope s_RendererAPI; }; class Renderer @@ -88,8 +88,8 @@ namespace Donut static void OnWindowResize(uint32_t width, uint32_t height); - static void Submit(const std::shared_ptr& shader, - const std::shared_ptr& vertexArray, + static void Submit(const Ref& shader, + const Ref& vertexArray, const glm::mat4& transform, const glm::mat4& viewProjection); diff --git a/src/Rendering/Shader.cpp b/src/Rendering/Shader.cpp index e38ce65..8266e00 100644 --- a/src/Rendering/Shader.cpp +++ b/src/Rendering/Shader.cpp @@ -1,8 +1,8 @@ #include "Shader.h" #include "Renderer.h" + #include "Platform/OpenGL/OpenGLShader.h" #include "Platform/Vulkan/VulkanShader.h" -#include namespace Donut { @@ -45,32 +45,32 @@ namespace Donut } } - void ShaderLibrary::Add(const std::shared_ptr& shader) + void ShaderLibrary::Add(const Ref& shader) { auto& name = shader->GetName(); Add(name, shader); } - void ShaderLibrary::Add(const std::string& name, const std::shared_ptr& shader) + void ShaderLibrary::Add(const std::string& name, const Ref& shader) { m_Shaders[name] = shader; } - std::shared_ptr ShaderLibrary::Load(const std::string& filepath) + Ref ShaderLibrary::Load(const std::string& filepath) { - auto shader = std::shared_ptr(Shader::Create(filepath)); + auto shader = Ref(Shader::Create(filepath)); Add(shader); return shader; } - std::shared_ptr ShaderLibrary::Load(const std::string& name, const std::string& filepath) + Ref ShaderLibrary::Load(const std::string& name, const std::string& filepath) { - auto shader = std::shared_ptr(Shader::Create(filepath)); + auto shader = Ref(Shader::Create(filepath)); Add(name, shader); return shader; } - std::shared_ptr ShaderLibrary::Get(const std::string& name) + Ref ShaderLibrary::Get(const std::string& name) { if (Exists(name)) return m_Shaders[name]; diff --git a/src/Rendering/Shader.h b/src/Rendering/Shader.h index ef0bdb1..e2d8d2a 100644 --- a/src/Rendering/Shader.h +++ b/src/Rendering/Shader.h @@ -1,8 +1,9 @@ #pragma once +#include "Core/Memory.h" + #include #include -#include #include #define SHADER_STORAGE_BARRIER_BIT 0x00002000 @@ -42,15 +43,15 @@ namespace Donut class ShaderLibrary { public: - void Add(const std::shared_ptr& shader); - void Add(const std::string& name, const std::shared_ptr& shader); - std::shared_ptr Load(const std::string& filepath); - std::shared_ptr Load(const std::string& name, const std::string& filepath); + void Add(const Ref& shader); + void Add(const std::string& name, const Ref& shader); + Ref Load(const std::string& filepath); + Ref Load(const std::string& name, const std::string& filepath); - std::shared_ptr Get(const std::string& name); + Ref Get(const std::string& name); bool Exists(const std::string& name) const; private: - std::unordered_map> m_Shaders; + std::unordered_map> m_Shaders; }; }; diff --git a/src/Rendering/Texture.cpp b/src/Rendering/Texture.cpp index 3a86dba..a96b816 100644 --- a/src/Rendering/Texture.cpp +++ b/src/Rendering/Texture.cpp @@ -1,18 +1,19 @@ #include "Texture.h" #include "Renderer.h" -#include "../Platform/OpenGL/OpenGLTexture.h" -#include "../Platform/Vulkan/VulkanTexture.h" + +#include "Platform/OpenGL/OpenGLTexture.h" +#include "Platform/Vulkan/VulkanTexture.h" namespace Donut { - std::shared_ptr Texture2D::Create(uint32_t width, uint32_t height) + Ref Texture2D::Create(uint32_t width, uint32_t height) { switch (Renderer::GetAPI()) { case RendererAPI::API::OpenGL: - return std::make_shared(width, height); + return CreateRef(width, height); case RendererAPI::API::Vulkan: - return std::make_shared(width, height); + return CreateRef(width, height); case RendererAPI::API::None: return nullptr; default: @@ -20,14 +21,14 @@ namespace Donut } } - std::shared_ptr Texture2D::Create(const std::string& path) + Ref Texture2D::Create(const std::string& path) { switch (Renderer::GetAPI()) { case RendererAPI::API::OpenGL: - return std::make_shared(path); + return CreateRef(path); case RendererAPI::API::Vulkan: - return std::make_shared(path); + return CreateRef(path); case RendererAPI::API::None: return nullptr; default: diff --git a/src/Rendering/Texture.h b/src/Rendering/Texture.h index 4d150c0..21941ee 100644 --- a/src/Rendering/Texture.h +++ b/src/Rendering/Texture.h @@ -1,6 +1,7 @@ #pragma once -#include +#include "Core/Memory.h" + #include #include @@ -27,7 +28,7 @@ namespace Donut : public Texture { public: - static std::shared_ptr Create(uint32_t width, uint32_t height); - static std::shared_ptr Create(const std::string& path); + static Ref Create(uint32_t width, uint32_t height); + static Ref Create(const std::string& path); }; }; \ No newline at end of file diff --git a/src/Rendering/VertexArray.cpp b/src/Rendering/VertexArray.cpp index 2b2549c..366675f 100644 --- a/src/Rendering/VertexArray.cpp +++ b/src/Rendering/VertexArray.cpp @@ -1,5 +1,6 @@ #include "VertexArray.h" #include "Renderer.h" + #include "Platform/OpenGL/OpenGLVertexArray.h" #include "Platform/Vulkan/VulkanVertexArray.h" diff --git a/src/Rendering/VertexArray.h b/src/Rendering/VertexArray.h index 8ae61a7..10fff89 100644 --- a/src/Rendering/VertexArray.h +++ b/src/Rendering/VertexArray.h @@ -1,8 +1,10 @@ #pragma once +#include "Core/Memory.h" + #include "VertexBuffer.h" #include "IndexBuffer.h" -#include + #include namespace Donut @@ -15,11 +17,11 @@ namespace Donut virtual void Bind() const = 0; virtual void Unbind() const = 0; - virtual void AddVertexBuffer(const std::shared_ptr& vertexBuffer) = 0; - virtual void SetIndexBuffer(const std::shared_ptr& indexBuffer) = 0; + virtual void AddVertexBuffer(const Ref& vertexBuffer) = 0; + virtual void SetIndexBuffer(const Ref& indexBuffer) = 0; - virtual const std::vector>& GetVertexBuffers() const = 0; - virtual const std::shared_ptr& GetIndexBuffer() const = 0; + virtual const std::vector>& GetVertexBuffers() const = 0; + virtual const Ref& GetIndexBuffer() const = 0; static VertexArray* Create(); }; diff --git a/src/Rendering/VertexBuffer.cpp b/src/Rendering/VertexBuffer.cpp index 57b4d87..a7e2504 100644 --- a/src/Rendering/VertexBuffer.cpp +++ b/src/Rendering/VertexBuffer.cpp @@ -1,5 +1,6 @@ #include "VertexBuffer.h" #include "Renderer.h" + #include "Platform/OpenGL/OpenGLVertexBuffer.h" #include "Platform/Vulkan/VulkanVertexBuffer.h" diff --git a/src/States/SimulationState.cpp b/src/States/SimulationState.cpp index 103fd0f..a37e527 100644 --- a/src/States/SimulationState.cpp +++ b/src/States/SimulationState.cpp @@ -6,7 +6,6 @@ #include "Rendering/Shader.h" #include "Rendering/Texture.h" -#include #include #include #include @@ -15,7 +14,7 @@ namespace Donut { void SimulationState::OnEnter() { - m_Camera = std::make_unique(45.0f, 1280.0f / 720.0f, 0.1f, 100.0f); + m_Camera = CreateScope(45.0f, 1280.0f / 720.0f, 0.1f, 100.0f); m_Camera->SetPosition({ 0.0f, 0.0f, 3.0f }); m_Camera->SetMouseSensitivity(0.1f); m_Camera->SetMovementSpeed(5.0f); @@ -42,8 +41,8 @@ namespace Donut 4, 5, 1, 1, 0, 4 }; - m_VertexArray = std::shared_ptr(VertexArray::Create()); - m_VertexBuffer = std::shared_ptr(VertexBuffer::Create(vertices, sizeof(vertices))); + m_VertexArray = Ref(VertexArray::Create()); + m_VertexBuffer = Ref(VertexBuffer::Create(vertices, sizeof(vertices))); VertexBufferLayout layout; layout.Push(3); @@ -53,10 +52,10 @@ namespace Donut m_VertexArray->AddVertexBuffer(m_VertexBuffer); - m_IndexBuffer = std::shared_ptr(IndexBuffer::Create(indices, 36)); + m_IndexBuffer = Ref(IndexBuffer::Create(indices, 36)); m_VertexArray->SetIndexBuffer(m_IndexBuffer); - m_Shader = std::shared_ptr(Shader::Create("Assets/Textured.glsl")); + m_Shader = Ref(Shader::Create("Assets/Textured.glsl")); m_Texture = Texture2D::Create(256, 256); uint32_t* pixelData = new uint32_t[256 * 256]; @@ -82,7 +81,7 @@ namespace Donut m_Texture->SetData(pixelData, 256 * 256 * 4); delete[] pixelData; - m_ComputeShader = std::shared_ptr(Shader::Create("Assets/TextureProcessor.glsl")); + m_ComputeShader = Ref(Shader::Create("Assets/TextureProcessor.glsl")); if (!m_ComputeShader) { diff --git a/src/States/SimulationState.h b/src/States/SimulationState.h index d2f0106..aa0b15b 100644 --- a/src/States/SimulationState.h +++ b/src/States/SimulationState.h @@ -1,5 +1,6 @@ #pragma once +#include "Core/Memory.h" #include "Core/State.h" #include "Core/Event.h" #include "Core/Camera.h" @@ -28,16 +29,16 @@ namespace Donut void HandleKeyInput(float deltaTime); void UpdateTexture(); private: - std::unique_ptr m_Camera; + Scope m_Camera; - std::shared_ptr m_VertexArray; - std::shared_ptr m_VertexBuffer; - std::shared_ptr m_IndexBuffer; - std::shared_ptr m_Shader; - std::shared_ptr m_Texture; + Ref m_VertexArray; + Ref m_VertexBuffer; + Ref m_IndexBuffer; + Ref m_Shader; + Ref m_Texture; - std::shared_ptr m_ComputeShader; - std::shared_ptr m_ProcessedTexture; + Ref m_ComputeShader; + Ref m_ProcessedTexture; float m_ComputeBrightness = 0.0f; float m_ComputeContrast = 1.0f; -- cgit v1.3