From 0d8b874c0048ca088bb9e353a5f7c62e1f8cd0e8 Mon Sep 17 00:00:00 2001 From: hachem Date: Thu, 14 Aug 2025 00:42:05 +0200 Subject: feat: 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/state_manager.cpp | 2 +- src/core/state_manager.h | 10 +++++----- src/core/window.cpp | 2 +- src/core/window.h | 5 +++-- src/platform/opengl/opengl_renderer_api.cpp | 8 ++++---- src/platform/opengl/opengl_renderer_api.h | 6 +++++- src/platform/opengl/opengl_shader.cpp | 2 ++ src/platform/opengl/opengl_shader.h | 1 + src/platform/opengl/opengl_texture.cpp | 5 ++--- src/platform/opengl/opengl_texture.h | 2 ++ src/platform/opengl/opengl_vertex_array.cpp | 5 +++-- src/platform/opengl/opengl_vertex_array.h | 15 ++++++++------- src/platform/vulkan/vulkan_renderer_api.cpp | 2 +- src/platform/vulkan/vulkan_renderer_api.h | 3 ++- src/platform/vulkan/vulkan_vertex_array.cpp | 4 ++-- src/platform/vulkan/vulkan_vertex_array.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/vertex_array.cpp | 1 + src/rendering/vertex_array.h | 12 +++++++----- src/rendering/vertex_buffer.cpp | 1 + src/states/simulation_state.cpp | 13 ++++++------- src/states/simulation_state.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 b05e2b5..f2284cd 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 = create_scope(name, width, height); m_window->set_event_callback([this](Event& event) { on_event(event); @@ -109,9 +109,9 @@ namespace Donut Renderer::on_window_resize(1280, 720); RenderCommand::set_face_culling(false); - m_state_manager = std::make_unique(); - m_state_manager->register_state("Config", std::make_unique()); - m_state_manager->register_state("Simulation", std::make_unique()); + m_state_manager = create_scope(); + m_state_manager->register_state("Config", create_scope()); + m_state_manager->register_state("Simulation", create_scope()); m_state_manager->switch_to_state("Config"); } diff --git a/src/core/application.h b/src/core/application.h index 326e541..048503b 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 "state_manager.h" #include "log.h" +#include "state_manager.h" namespace Donut { @@ -29,8 +27,8 @@ namespace Donut void on_render(); void on_event(Event& event); private: - std::unique_ptr m_state_manager; - std::unique_ptr m_window; + Scope m_state_manager; + Scope m_window; bool m_running; bool m_minimized; diff --git a/src/core/camera.cpp b/src/core/camera.cpp index 06866e4..f580191 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 ec031a5..0411e20 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 = create_ref(); s_logger->set_log_level(LogLevel::Info); s_logger->enable_console_output(true); s_logger->enable_file_output(true); @@ -27,7 +28,7 @@ namespace Donut s_logger.reset(); } - std::shared_ptr Logger::get_logger() + Ref Logger::get_logger() { return s_logger; } diff --git a/src/core/log.h b/src/core/log.h index 77324f5..1477226 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 get_logger(); + static Ref get_logger(); template static void trace(const std::string_view& format, const Args&... args) @@ -135,7 +136,7 @@ namespace Donut std::ofstream m_log_file; 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..cbfbe82 --- /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 create_scope(Args&& ... args) + { + return std::make_unique(std::forward(args)...); + } + + template + constexpr Ref create_ref(Args&& ... args) + { + return std::make_shared(std::forward(args)...); + } +}; diff --git a/src/core/state_manager.cpp b/src/core/state_manager.cpp index 5da601c..728df15 100644 --- a/src/core/state_manager.cpp +++ b/src/core/state_manager.cpp @@ -19,7 +19,7 @@ namespace Donut m_current_state_name = ""; } - void StateManager::register_state(const std::string& state_name, std::unique_ptr state) + void StateManager::register_state(const std::string& state_name, Scope state) { if (m_states.find(state_name) != m_states.end()) DONUT_WARN("State '{}' already exists, overwriting", state_name); diff --git a/src/core/state_manager.h b/src/core/state_manager.h index 7d5e734..469c48f 100644 --- a/src/core/state_manager.h +++ b/src/core/state_manager.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 on_event(Event& event); - void register_state(const std::string& state_name, std::unique_ptr state); + void register_state(const std::string& state_name, Scope state); void switch_to_state(const std::string& state_name); std::string get_current_state_name() const { return m_current_state_name; } @@ -34,6 +34,6 @@ namespace Donut State* m_current_state = nullptr; std::string m_current_state_name = ""; - std::unordered_map> m_states; + std::unordered_map> m_states; }; } diff --git a/src/core/window.cpp b/src/core/window.cpp index a32dddb..f598357 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 ae79b6d..256f708 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/opengl_renderer_api.cpp b/src/platform/opengl/opengl_renderer_api.cpp index 1d41735..4542231 100644 --- a/src/platform/opengl/opengl_renderer_api.cpp +++ b/src/platform/opengl/opengl_renderer_api.cpp @@ -1,7 +1,7 @@ #include "opengl_renderer_api.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::draw_indexed(const std::shared_ptr& vertex_array, uint32_t index_count) + void OpenGLRendererAPI::draw_indexed(const Ref& vertex_array, uint32_t index_count) { uint32_t count = index_count ? index_count : vertex_array->get_index_buffer()->get_count(); glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr); diff --git a/src/platform/opengl/opengl_renderer_api.h b/src/platform/opengl/opengl_renderer_api.h index 15984d5..25accbb 100644 --- a/src/platform/opengl/opengl_renderer_api.h +++ b/src/platform/opengl/opengl_renderer_api.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 enable_depth_test() override; virtual void set_face_culling(bool enabled) override; - virtual void draw_indexed(const std::shared_ptr& vertex_array, + virtual void draw_indexed(const Ref& vertex_array, uint32_t index_count = 0) override; }; }; diff --git a/src/platform/opengl/opengl_shader.cpp b/src/platform/opengl/opengl_shader.cpp index f4b99bc..3b0108e 100644 --- a/src/platform/opengl/opengl_shader.cpp +++ b/src/platform/opengl/opengl_shader.cpp @@ -1,6 +1,8 @@ #include "opengl_shader.h" + #include #include + #include #include diff --git a/src/platform/opengl/opengl_shader.h b/src/platform/opengl/opengl_shader.h index 5870bda..6b1ceec 100644 --- a/src/platform/opengl/opengl_shader.h +++ b/src/platform/opengl/opengl_shader.h @@ -1,6 +1,7 @@ #pragma once #include "rendering/shader.h" + #include #include diff --git a/src/platform/opengl/opengl_texture.cpp b/src/platform/opengl/opengl_texture.cpp index 53ed06c..5d05fb4 100644 --- a/src/platform/opengl/opengl_texture.cpp +++ b/src/platform/opengl/opengl_texture.cpp @@ -1,5 +1,4 @@ #include "opengl_texture.h" -#include namespace Donut { @@ -39,7 +38,7 @@ namespace Donut uint32_t white_pixel = 0xFFFFFFFF; glTextureSubImage2D(m_renderer_id, 0, 0, 0, m_width, m_height, m_data_format, GL_UNSIGNED_BYTE, &white_pixel); - 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() @@ -52,7 +51,7 @@ namespace Donut uint32_t bpp = m_data_format == 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/opengl_texture.h b/src/platform/opengl/opengl_texture.h index 41b352a..705d770 100644 --- a/src/platform/opengl/opengl_texture.h +++ b/src/platform/opengl/opengl_texture.h @@ -1,6 +1,8 @@ #pragma once #include "rendering/texture.h" +#include "core/log.h" + #include namespace Donut diff --git a/src/platform/opengl/opengl_vertex_array.cpp b/src/platform/opengl/opengl_vertex_array.cpp index c57479b..e2fb766 100644 --- a/src/platform/opengl/opengl_vertex_array.cpp +++ b/src/platform/opengl/opengl_vertex_array.cpp @@ -1,4 +1,5 @@ #include + #include "opengl_vertex_array.h" #include "rendering/vertex_buffer.h" #include "rendering/index_buffer.h" @@ -25,7 +26,7 @@ namespace Donut glBindVertexArray(0); } - void OpenGLVertexArray::add_vertex_buffer(const std::shared_ptr& vertex_buffer) + void OpenGLVertexArray::add_vertex_buffer(const Ref& vertex_buffer) { glBindVertexArray(m_renderer_id); vertex_buffer->bind(); @@ -46,7 +47,7 @@ namespace Donut m_vertex_buffers.push_back(vertex_buffer); } - void OpenGLVertexArray::set_index_buffer(const std::shared_ptr& index_buffer) + void OpenGLVertexArray::set_index_buffer(const Ref& index_buffer) { glBindVertexArray(m_renderer_id); index_buffer->bind(); diff --git a/src/platform/opengl/opengl_vertex_array.h b/src/platform/opengl/opengl_vertex_array.h index d6986db..ff5cdd8 100644 --- a/src/platform/opengl/opengl_vertex_array.h +++ b/src/platform/opengl/opengl_vertex_array.h @@ -1,11 +1,12 @@ #pragma once +#include "core/memory.h" + #include "rendering/vertex_array.h" #include "rendering/vertex_buffer.h" #include "rendering/index_buffer.h" #include -#include namespace Donut { @@ -19,22 +20,22 @@ namespace Donut virtual void bind() const override; virtual void unbind() const override; - virtual void add_vertex_buffer(const std::shared_ptr& vertex_buffer) override; - virtual void set_index_buffer(const std::shared_ptr& index_buffer) override; + virtual void add_vertex_buffer(const Ref& vertex_buffer) override; + virtual void set_index_buffer(const Ref& index_buffer) override; - virtual const std::vector>& get_vertex_buffers() const override + virtual const std::vector>& get_vertex_buffers() const override { return m_vertex_buffers; } - virtual const std::shared_ptr& get_index_buffer() const override + virtual const Ref& get_index_buffer() const override { return m_index_buffer; } private: uint32_t m_renderer_id; uint32_t m_vertex_buffer_index = 0; - std::vector> m_vertex_buffers; - std::shared_ptr m_index_buffer; + std::vector> m_vertex_buffers; + Ref m_index_buffer; }; }; diff --git a/src/platform/vulkan/vulkan_renderer_api.cpp b/src/platform/vulkan/vulkan_renderer_api.cpp index b934317..f91a0ea 100644 --- a/src/platform/vulkan/vulkan_renderer_api.cpp +++ b/src/platform/vulkan/vulkan_renderer_api.cpp @@ -26,7 +26,7 @@ namespace Donut { } - void VulkanRendererAPI::draw_indexed(const std::shared_ptr& vertex_array, uint32_t index_count) + void VulkanRendererAPI::draw_indexed(const Ref& vertex_array, uint32_t index_count) { } }; diff --git a/src/platform/vulkan/vulkan_renderer_api.h b/src/platform/vulkan/vulkan_renderer_api.h index 0e1baf9..d1394b3 100644 --- a/src/platform/vulkan/vulkan_renderer_api.h +++ b/src/platform/vulkan/vulkan_renderer_api.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 enable_depth_test() override; virtual void set_face_culling(bool enabled) override; - virtual void draw_indexed(const std::shared_ptr& vertex_array, + virtual void draw_indexed(const Ref& vertex_array, uint32_t index_count = 0) override; }; }; diff --git a/src/platform/vulkan/vulkan_vertex_array.cpp b/src/platform/vulkan/vulkan_vertex_array.cpp index 31c2f74..8258696 100644 --- a/src/platform/vulkan/vulkan_vertex_array.cpp +++ b/src/platform/vulkan/vulkan_vertex_array.cpp @@ -18,12 +18,12 @@ namespace Donut { } - void VulkanVertexArray::add_vertex_buffer(const std::shared_ptr& vertex_buffer) + void VulkanVertexArray::add_vertex_buffer(const Ref& vertex_buffer) { m_vertex_buffers.push_back(vertex_buffer); } - void VulkanVertexArray::set_index_buffer(const std::shared_ptr& index_buffer) + void VulkanVertexArray::set_index_buffer(const Ref& index_buffer) { m_index_buffer = index_buffer; } diff --git a/src/platform/vulkan/vulkan_vertex_array.h b/src/platform/vulkan/vulkan_vertex_array.h index b36c421..f4356d9 100644 --- a/src/platform/vulkan/vulkan_vertex_array.h +++ b/src/platform/vulkan/vulkan_vertex_array.h @@ -1,6 +1,7 @@ #pragma once #include "rendering/vertex_array.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 add_vertex_buffer(const std::shared_ptr& vertex_buffer) override; - virtual void set_index_buffer(const std::shared_ptr& index_buffer) override; + virtual void add_vertex_buffer(const Ref& vertex_buffer) override; + virtual void set_index_buffer(const Ref& index_buffer) override; - virtual const std::vector>& get_vertex_buffers() const { return m_vertex_buffers; } - virtual const std::shared_ptr& get_index_buffer() const { return m_index_buffer; } + virtual const std::vector>& get_vertex_buffers() const { return m_vertex_buffers; } + virtual const Ref& get_index_buffer() const { return m_index_buffer; } private: uint32_t m_renderer_id; - std::vector> m_vertex_buffers; - std::shared_ptr m_index_buffer; + std::vector> m_vertex_buffers; + Ref m_index_buffer; }; }; diff --git a/src/rendering/renderer.cpp b/src/rendering/renderer.cpp index 46dd690..5e4682c 100644 --- a/src/rendering/renderer.cpp +++ b/src/rendering/renderer.cpp @@ -3,19 +3,18 @@ #include "platform/opengl/opengl_renderer_api.h" #include "platform/vulkan/vulkan_renderer_api.h" -#include #include namespace Donut { - std::unique_ptr RendererAPI::create() + Scope RendererAPI::create() { switch (s_api) { case API::OpenGL: - return std::make_unique(); + return create_scope(); case API::Vulkan: - return std::make_unique(); + return create_scope(); default: return nullptr; } @@ -38,8 +37,8 @@ namespace Donut RenderCommand::set_viewport(0, 0, width, height); } - void Renderer::submit(const std::shared_ptr& shader, - const std::shared_ptr& vertex_array, + void Renderer::submit(const Ref& shader, + const Ref& vertex_array, const glm::mat4& transform, const glm::mat4& view_projection) { @@ -51,7 +50,7 @@ namespace Donut RenderCommand::draw_indexed(vertex_array); } - std::unique_ptr RenderCommand::s_renderer_api = RendererAPI::create(); + Scope RenderCommand::s_renderer_api = RendererAPI::create(); void Renderer::set_clear_color(const glm::vec4& color) { diff --git a/src/rendering/renderer.h b/src/rendering/renderer.h index dd4c259..6212b21 100644 --- a/src/rendering/renderer.h +++ b/src/rendering/renderer.h @@ -1,10 +1,10 @@ #pragma once +#include "core/memory.h" #include "vertex_array.h" #include "shader.h" #include -#include namespace Donut { @@ -28,12 +28,12 @@ namespace Donut virtual void enable_depth_test() = 0; virtual void set_face_culling(bool enabled) = 0; - virtual void draw_indexed(const std::shared_ptr& vertex_array, + virtual void draw_indexed(const Ref& vertex_array, uint32_t index_count = 0) = 0; inline static API get_api() { return s_api; } inline static void set_api(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_renderer_api->set_face_culling(enabled); } - inline static void draw_indexed(const std::shared_ptr& vertex_array, uint32_t index_count = 0) + inline static void draw_indexed(const Ref& vertex_array, uint32_t index_count = 0) { s_renderer_api->draw_indexed(vertex_array, index_count); } private: - static std::unique_ptr s_renderer_api; + static Scope s_renderer_api; }; class Renderer @@ -88,8 +88,8 @@ namespace Donut static void on_window_resize(uint32_t width, uint32_t height); - static void submit(const std::shared_ptr& shader, - const std::shared_ptr& vertex_array, + static void submit(const Ref& shader, + const Ref& vertex_array, const glm::mat4& transform, const glm::mat4& view_projection); diff --git a/src/rendering/shader.cpp b/src/rendering/shader.cpp index 1e84125..1bbd0fc 100644 --- a/src/rendering/shader.cpp +++ b/src/rendering/shader.cpp @@ -1,8 +1,8 @@ #include "shader.h" #include "renderer.h" + #include "platform/opengl/opengl_shader.h" #include "platform/vulkan/vulkan_shader.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->get_name(); 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 f490f3c..814f0cf 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 36189a3..e14c09c 100644 --- a/src/rendering/texture.cpp +++ b/src/rendering/texture.cpp @@ -1,18 +1,19 @@ #include "texture.h" #include "renderer.h" -#include "../platform/opengl/opengl_texture.h" -#include "../platform/vulkan/vulkan_texture.h" + +#include "platform/opengl/opengl_texture.h" +#include "platform/vulkan/vulkan_texture.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::get_api()) { case RendererAPI::API::OpenGL: - return std::make_shared(width, height); + return create_ref(width, height); case RendererAPI::API::Vulkan: - return std::make_shared(width, height); + return create_ref(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::get_api()) { case RendererAPI::API::OpenGL: - return std::make_shared(path); + return create_ref(path); case RendererAPI::API::Vulkan: - return std::make_shared(path); + return create_ref(path); case RendererAPI::API::None: return nullptr; default: diff --git a/src/rendering/texture.h b/src/rendering/texture.h index e9f369f..cc5c59e 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); }; }; diff --git a/src/rendering/vertex_array.cpp b/src/rendering/vertex_array.cpp index 8057a3e..bb92ff3 100644 --- a/src/rendering/vertex_array.cpp +++ b/src/rendering/vertex_array.cpp @@ -1,5 +1,6 @@ #include "vertex_array.h" #include "renderer.h" + #include "platform/opengl/opengl_vertex_array.h" #include "platform/vulkan/vulkan_vertex_array.h" diff --git a/src/rendering/vertex_array.h b/src/rendering/vertex_array.h index 946f273..a39849f 100644 --- a/src/rendering/vertex_array.h +++ b/src/rendering/vertex_array.h @@ -1,8 +1,10 @@ #pragma once +#include "core/memory.h" + #include "vertex_buffer.h" #include "index_buffer.h" -#include + #include namespace Donut @@ -15,11 +17,11 @@ namespace Donut virtual void bind() const = 0; virtual void unbind() const = 0; - virtual void add_vertex_buffer(const std::shared_ptr& vertex_buffer) = 0; - virtual void set_index_buffer(const std::shared_ptr& index_buffer) = 0; + virtual void add_vertex_buffer(const Ref& vertex_buffer) = 0; + virtual void set_index_buffer(const Ref& index_buffer) = 0; - virtual const std::vector>& get_vertex_buffers() const = 0; - virtual const std::shared_ptr& get_index_buffer() const = 0; + virtual const std::vector>& get_vertex_buffers() const = 0; + virtual const Ref& get_index_buffer() const = 0; static VertexArray* create(); }; diff --git a/src/rendering/vertex_buffer.cpp b/src/rendering/vertex_buffer.cpp index 32ae10a..4929711 100644 --- a/src/rendering/vertex_buffer.cpp +++ b/src/rendering/vertex_buffer.cpp @@ -1,5 +1,6 @@ #include "vertex_buffer.h" #include "renderer.h" + #include "platform/opengl/opengl_vertex_buffer.h" #include "platform/vulkan/vulkan_vertex_buffer.h" diff --git a/src/states/simulation_state.cpp b/src/states/simulation_state.cpp index c925cd7..6a832cd 100644 --- a/src/states/simulation_state.cpp +++ b/src/states/simulation_state.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::on_enter() { - m_camera = std::make_unique(45.0f, 1280.0f / 720.0f, 0.1f, 100.0f); + m_camera = create_scope(45.0f, 1280.0f / 720.0f, 0.1f, 100.0f); m_camera->set_position({ 0.0f, 0.0f, 3.0f }); m_camera->set_mouse_sensitivity(0.1f); m_camera->set_movement_speed(5.0f); @@ -42,8 +41,8 @@ namespace Donut 4, 5, 1, 1, 0, 4 }; - m_vertex_array = std::shared_ptr(VertexArray::create()); - m_vertex_buffer = std::shared_ptr(VertexBuffer::create(vertices, sizeof(vertices))); + m_vertex_array = Ref(VertexArray::create()); + m_vertex_buffer = Ref(VertexBuffer::create(vertices, sizeof(vertices))); VertexBufferLayout layout; layout.push(3); @@ -53,10 +52,10 @@ namespace Donut m_vertex_array->add_vertex_buffer(m_vertex_buffer); - m_index_buffer = std::shared_ptr(IndexBuffer::create(indices, 36)); + m_index_buffer = Ref(IndexBuffer::create(indices, 36)); m_vertex_array->set_index_buffer(m_index_buffer); - 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* pixel_data = new uint32_t[256 * 256]; @@ -82,7 +81,7 @@ namespace Donut m_texture->set_data(pixel_data, 256 * 256 * 4); delete[] pixel_data; - m_compute_shader = std::shared_ptr(Shader::create("assets/texture_processor.glsl")); + m_compute_shader = Ref(Shader::create("assets/texture_processor.glsl")); if (!m_compute_shader) { diff --git a/src/states/simulation_state.h b/src/states/simulation_state.h index a0d9e73..051edf3 100644 --- a/src/states/simulation_state.h +++ b/src/states/simulation_state.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 handle_key_input(float delta_time); void update_texture(); private: - std::unique_ptr m_camera; + Scope m_camera; - std::shared_ptr m_vertex_array; - std::shared_ptr m_vertex_buffer; - std::shared_ptr m_index_buffer; - std::shared_ptr m_shader; - std::shared_ptr m_texture; + Ref m_vertex_array; + Ref m_vertex_buffer; + Ref m_index_buffer; + Ref m_shader; + Ref m_texture; - std::shared_ptr m_compute_shader; - std::shared_ptr m_processed_texture; + Ref m_compute_shader; + Ref m_processed_texture; float m_compute_brightness = 0.0f; float m_compute_contrast = 1.0f; -- cgit v1.3