From 60d1452f0160dd9f3ea52b8e4a74371a4ec042bb Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Thu, 1 Dec 2022 15:54:28 +0000 Subject: [PATCH] Add resource manager --- CMakeLists.txt | 17 ++-- include/_resource_manager.hpp | 85 ++++++++++++++++ include/resource_manager.hpp | 97 ++++++------------- include/scene_manager.hpp | 3 + include/{resources => }/texture.hpp | 2 + include/texture_manager.hpp | 23 +++++ release.sh | 3 - ...urce_manager.cpp => _resource_manager.cpp} | 0 src/application.cpp | 1 - src/gfx_device_vulkan.cpp | 3 +- src/scene_manager.cpp | 6 +- src/{resources => }/texture.cpp | 4 + src/texture_manager.cpp | 15 +++ test/src/game.cpp | 2 +- 14 files changed, 178 insertions(+), 83 deletions(-) create mode 100644 include/_resource_manager.hpp rename include/{resources => }/texture.hpp (96%) create mode 100644 include/texture_manager.hpp delete mode 100755 release.sh rename src/{resource_manager.cpp => _resource_manager.cpp} (100%) rename src/{resources => }/texture.cpp (99%) create mode 100644 src/texture_manager.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a2ade2f..bc28cc5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 3.24) # options option(ENGINE_BUILD_TEST "Compile the test program" ON) option(ENGINE_BUILD_VULKAN "Use Vulkan 1.3 for graphics" ON) -option(ENGINE_BUILD_OPENGL "Use OpenGL 4.5 for graphics" OFF) project(engine LANGUAGES CXX VERSION "0.1.0" @@ -12,8 +11,12 @@ project(engine LANGUAGES CXX set(SRC_FILES "src/application.cpp" "src/window.cpp" + "src/input_manager.cpp" "src/scene_manager.cpp" + "src/texture_manager.cpp" + "src/texture.cpp" + "src/gfx_device_vulkan.cpp" "src/scene.cpp" @@ -31,8 +34,14 @@ set(INCLUDE_FILES "include/window.hpp" "include/inputs/keyboard.hpp" "include/inputs/mouse.hpp" + "include/input_manager.hpp" "include/scene_manager.hpp" + "include/resource_manager.hpp" + "include/texture_manager.hpp" + "include/texture.hpp" + + "include/gfx.hpp" "include/gfx_device.hpp" @@ -76,8 +85,6 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # figure out what graphics api to use if (ENGINE_BUILD_VULKAN) target_compile_definitions(${PROJECT_NAME} PRIVATE "ENGINE_BUILD_VULKAN") -elseif(ENGINE_BUILD_OPENGL) - target_compile_definitions(${PROJECT_NAME} PRIVATE "ENGINE_BUILD_OPENGL") else() target_compile_definitions(${PROJECT_NAME} PRIVATE "ENGINE_BUILD_NULL") endif() @@ -119,12 +126,9 @@ if(ENGINE_BUILD_VULKAN) set(VOLK_HEADERS_ONLY ON) add_subdirectory(dependencies/volk) target_link_libraries(${PROJECT_NAME} PRIVATE volk_headers) - # Vulkan Memory Allocator target_include_directories(${PROJECT_NAME} PRIVATE dependencies/VulkanMemoryAllocator/include) - # shaderc - if (MSVC) include(FindVulkan) find_package(Vulkan COMPONENTS shaderc_combined) @@ -132,7 +136,6 @@ if(ENGINE_BUILD_VULKAN) else() target_link_libraries(${PROJECT_NAME} PRIVATE shaderc_shared) endif() - endif() # SDL2: diff --git a/include/_resource_manager.hpp b/include/_resource_manager.hpp new file mode 100644 index 0000000..923e7b1 --- /dev/null +++ b/include/_resource_manager.hpp @@ -0,0 +1,85 @@ +#pragma once + +#include "engine_api.h" + +#include "resources/resource.hpp" + +#include +#include + +// Doesn't own resources, only holds weak_ptrs + +namespace engine { + + class ENGINE_API ResourceManager { + + public: + ResourceManager(); + ResourceManager(const ResourceManager&) = delete; + ResourceManager& operator=(const ResourceManager&) = delete; + ~ResourceManager() = default; + + template + std::shared_ptr create(const std::string& name); + + // creates the resource if it is not in the map or the weak_ptr has expired + template + std::shared_ptr get(const std::string& name); + + std::unique_ptr getResourcesListString(); + + std::vector> getAllResourcesOfType(const std::string& type); + + std::filesystem::path getFilePath(const std::string& name); + + private: + std::filesystem::path m_resourcesPath; + std::unordered_map> m_resources; + + }; + + template + std::shared_ptr ResourceManager::create(const std::string& name) + { + if (std::is_base_of::value == false) { + throw std::runtime_error("ResourceManager::create() error: specified type is not a subclass of 'Resource'"); + } + auto resource = std::make_shared(getFilePath(name)); + m_resources.emplace(name, std::dynamic_pointer_cast(resource)); + return resource; + } + + template + std::shared_ptr ResourceManager::get(const std::string& name) + { + + if (std::is_base_of::value == false) { + throw std::runtime_error("ResourceManager::get() error: specified type is not a subclass of 'Resource'"); + } + + if (m_resources.contains(name)) { + + std::weak_ptr res = m_resources.at(name); + + if (res.expired() == false) { + // resource definitely exists + auto castedSharedPtr = std::dynamic_pointer_cast(res.lock()); + if (castedSharedPtr == nullptr) { + throw std::runtime_error("error: attempt to get Resource which already exists as another type"); + } + else { + return castedSharedPtr; + } + } + else { + // Resource in map but no longer exists. Delete it. + m_resources.erase(name); + } + + } + + return create(name); + + } + +} \ No newline at end of file diff --git a/include/resource_manager.hpp b/include/resource_manager.hpp index 923e7b1..f33cf81 100644 --- a/include/resource_manager.hpp +++ b/include/resource_manager.hpp @@ -1,85 +1,48 @@ #pragma once -#include "engine_api.h" - -#include "resources/resource.hpp" - -#include #include - -// Doesn't own resources, only holds weak_ptrs +#include +#include +#include namespace engine { - class ENGINE_API ResourceManager { + template + class ResourceManager { public: - ResourceManager(); + ResourceManager() {} + virtual ~ResourceManager() {} ResourceManager(const ResourceManager&) = delete; ResourceManager& operator=(const ResourceManager&) = delete; - ~ResourceManager() = default; - template - std::shared_ptr create(const std::string& name); + std::shared_ptr add(const std::string& name, std::unique_ptr&& resource) + { + if (m_resources.contains(name) == false) { + std::shared_ptr ptr = std::move(resource); + m_resources.emplace(name, ptr); + } + else { + throw std::runtime_error("Cannot add a resource which already exists"); + } + return m_resources.at(name).lock(); + } - // creates the resource if it is not in the map or the weak_ptr has expired - template - std::shared_ptr get(const std::string& name); - - std::unique_ptr getResourcesListString(); - - std::vector> getAllResourcesOfType(const std::string& type); - - std::filesystem::path getFilePath(const std::string& name); + std::shared_ptr get(const std::string& name) + { + if (m_resources.contains(name)) { + std::weak_ptr resource = m_resources.at(name); + if (resource.expired() == false) { + return resource.lock(); + } + } + return {}; + } private: - std::filesystem::path m_resourcesPath; - std::unordered_map> m_resources; + // weak ptrs are used to check a resource's use count. If the use count of a resource hits 0, the resource can safely be deleted. + std::unordered_map> m_resources{}; }; - template - std::shared_ptr ResourceManager::create(const std::string& name) - { - if (std::is_base_of::value == false) { - throw std::runtime_error("ResourceManager::create() error: specified type is not a subclass of 'Resource'"); - } - auto resource = std::make_shared(getFilePath(name)); - m_resources.emplace(name, std::dynamic_pointer_cast(resource)); - return resource; - } - - template - std::shared_ptr ResourceManager::get(const std::string& name) - { - - if (std::is_base_of::value == false) { - throw std::runtime_error("ResourceManager::get() error: specified type is not a subclass of 'Resource'"); - } - - if (m_resources.contains(name)) { - - std::weak_ptr res = m_resources.at(name); - - if (res.expired() == false) { - // resource definitely exists - auto castedSharedPtr = std::dynamic_pointer_cast(res.lock()); - if (castedSharedPtr == nullptr) { - throw std::runtime_error("error: attempt to get Resource which already exists as another type"); - } - else { - return castedSharedPtr; - } - } - else { - // Resource in map but no longer exists. Delete it. - m_resources.erase(name); - } - - } - - return create(name); - - } - } \ No newline at end of file diff --git a/include/scene_manager.hpp b/include/scene_manager.hpp index 043594b..6c42345 100644 --- a/include/scene_manager.hpp +++ b/include/scene_manager.hpp @@ -6,6 +6,7 @@ namespace engine { class Scene; // "scene.hpp" + class TextureManager; // "texture_manager.hpp" class SceneManager { @@ -23,6 +24,8 @@ namespace engine { std::vector> m_scenes; int m_activeSceneIndex = -1; + const std::unique_ptr m_textureManager; + }; } \ No newline at end of file diff --git a/include/resources/texture.hpp b/include/texture.hpp similarity index 96% rename from include/resources/texture.hpp rename to include/texture.hpp index ad4536f..9d739db 100644 --- a/include/resources/texture.hpp +++ b/include/texture.hpp @@ -1,3 +1,4 @@ +#if 0 #pragma once #include "engine_api.h" @@ -21,3 +22,4 @@ private: }; } +#endif \ No newline at end of file diff --git a/include/texture_manager.hpp b/include/texture_manager.hpp new file mode 100644 index 0000000..6be7769 --- /dev/null +++ b/include/texture_manager.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "resource_manager.hpp" + +namespace engine { + + class Texture { + + }; + + class TextureManager : public ResourceManager { + + public: + TextureManager(); + ~TextureManager() override; + TextureManager(const TextureManager&) = delete; + TextureManager& operator=(const TextureManager&) = delete; + + private: + + }; + +} \ No newline at end of file diff --git a/release.sh b/release.sh deleted file mode 100755 index ceec117..0000000 --- a/release.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -cd "Release/test" -./enginetest diff --git a/src/resource_manager.cpp b/src/_resource_manager.cpp similarity index 100% rename from src/resource_manager.cpp rename to src/_resource_manager.cpp diff --git a/src/application.cpp b/src/application.cpp index c911a18..5e22305 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -56,7 +56,6 @@ namespace engine { } m_gfx->waitIdle(); - } } diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 719805e..11d159c 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -1,6 +1,5 @@ // The implementation of the graphics layer using Vulkan 1.3. -//#undef ENGINE_BUILD_VULKAN #ifdef ENGINE_BUILD_VULKAN #include "gfx_device.hpp" @@ -1569,7 +1568,7 @@ namespace engine { renderPassInfo.renderArea.extent = pimpl->swapchain.extent; std::array clearValues{}; - clearValues[0].color = { {0.0f, 0.0f, 0.0f, 1.0f} }; + clearValues[0].color = { {0.1f, 0.1f, 0.1f, 1.0f} }; clearValues[1].depthStencil = { 1.0f, 0 }; renderPassInfo.clearValueCount = (uint32_t)clearValues.size(); renderPassInfo.pClearValues = clearValues.data(); diff --git a/src/scene_manager.cpp b/src/scene_manager.cpp index 9555d33..b9f311b 100644 --- a/src/scene_manager.cpp +++ b/src/scene_manager.cpp @@ -1,14 +1,16 @@ #include "scene_manager.hpp" #include "scene.hpp" - +#include "texture_manager.hpp" #include "log.hpp" namespace engine { SceneManager::SceneManager() + : m_textureManager(std::make_unique()) { - + auto tex = std::make_unique(); + m_textureManager->add("myTexture", std::move(tex)); } SceneManager::~SceneManager() {} diff --git a/src/resources/texture.cpp b/src/texture.cpp similarity index 99% rename from src/resources/texture.cpp rename to src/texture.cpp index 72b2445..5781c05 100644 --- a/src/resources/texture.cpp +++ b/src/texture.cpp @@ -1,3 +1,5 @@ +#if 0 + #include "resources/texture.hpp" #define STB_IMAGE_IMPLEMENTATION @@ -116,3 +118,5 @@ gfx::Texture* Texture::getHandle() } } + +#endif \ No newline at end of file diff --git a/src/texture_manager.cpp b/src/texture_manager.cpp new file mode 100644 index 0000000..fe7e42b --- /dev/null +++ b/src/texture_manager.cpp @@ -0,0 +1,15 @@ +#include "texture_manager.hpp" + +namespace engine { + + TextureManager::TextureManager() + { + + } + + TextureManager::~TextureManager() + { + + } + +} \ No newline at end of file diff --git a/test/src/game.cpp b/test/src/game.cpp index f7213e1..35cefa7 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -8,7 +8,7 @@ void playGame() engine::Application app(PROJECT_NAME, PROJECT_VERSION); // configure window - app.window()->setRelativeMouseMode(true); + app.window()->setRelativeMouseMode(false); app.gameLoop(); }