From 879ab8c80954ebe9f4762070ff5215608263bd12 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Mon, 3 Jun 2024 22:57:18 +0300 Subject: [PATCH 1/4] Added ryml to linker list, loading EngineConfiguration from a file(if exists) --- CMakeLists.txt | 2 +- openVulkanoCpp/Base/EngineConfiguration.cpp | 94 +++++++++++++++++++++ openVulkanoCpp/Base/EngineConfiguration.hpp | 46 +++------- 3 files changed, 106 insertions(+), 36 deletions(-) create mode 100644 openVulkanoCpp/Base/EngineConfiguration.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ad81b9..7121b05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,7 +94,7 @@ endif() list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR}/deps/INSTALL) -target_link_libraries(openVulkanoCpp PRIVATE magic_enum yaml-cpp fmt spdlog glm pugixml stb eigen utf8cpp imgui_internal TracyClient stud-uuid) +target_link_libraries(openVulkanoCpp PRIVATE magic_enum yaml-cpp fmt spdlog glm pugixml stb eigen utf8cpp imgui_internal TracyClient stud-uuid ryml) add_compile_definitions(LIBARCHIVE_STATIC) add_compile_definitions(NOMINMAX) diff --git a/openVulkanoCpp/Base/EngineConfiguration.cpp b/openVulkanoCpp/Base/EngineConfiguration.cpp new file mode 100644 index 0000000..b6446ee --- /dev/null +++ b/openVulkanoCpp/Base/EngineConfiguration.cpp @@ -0,0 +1,94 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include +#include +#include +#include + +#include "EngineConfiguration.hpp" +#include "IO/AppFolders.hpp" + +namespace OpenVulkano +{ + EngineConfiguration::EngineConfiguration() + { + const std::string filePath = AppFolders::GetAppConfigHomeDir().string() + "/EngineConfig.yml"; + std::ifstream file(filePath); + if (!file.is_open()) + { + return; + } + + const char *NUM_THREADS_STR = "num_threads"; + const char *FRAMEBUFFER_CLEAR_COLOR_STR = "framebuffer_clear_color"; + const char *PREFER_FRAMEBUFFER_FORMAT_SRGB_STR = "prefer_framebuffer_format_srgb"; + + file.seekg(0, std::ios::end); + size_t size = file.tellg(); + std::string buffer(size, ' '); + file.seekg(0); + file.read(buffer.data(), size); + + ryml::Tree tree = ryml::parse_in_arena(ryml::to_csubstr(buffer)); + ryml::NodeRef root = tree.rootref(); + + if (root.has_child(NUM_THREADS_STR) && root[NUM_THREADS_STR].val().is_integer()) + { + root[NUM_THREADS_STR] >> m_numThreads; + } + + if (root.has_child(FRAMEBUFFER_CLEAR_COLOR_STR) && root[FRAMEBUFFER_CLEAR_COLOR_STR].is_seq() && root[FRAMEBUFFER_CLEAR_COLOR_STR].num_children() == 4) + { + auto clearColorNode = root[FRAMEBUFFER_CLEAR_COLOR_STR]; + for (size_t i = 0; i < 4; ++i) + { + clearColorNode[i] >> m_frameBufferClearColor[i]; + } + } + + if (root.has_child(PREFER_FRAMEBUFFER_FORMAT_SRGB_STR)) + { + root[PREFER_FRAMEBUFFER_FORMAT_SRGB_STR] >> m_preferFramebufferFormatSRGB; + } + } + + EngineConfiguration* EngineConfiguration::GetEngineConfiguration() + { + static EngineConfiguration config; + return &config; + } + + void EngineConfiguration::SetNumThreads(uint32_t numThreads) + { + m_numThreads = numThreads; + } + + uint32_t EngineConfiguration::GetNumThreads() const + { + return std::max(static_cast(1), m_numThreads); + } + + void EngineConfiguration::SetFrameBufferClearColor(std::array frameBufferClearColor) + { + m_frameBufferClearColor = frameBufferClearColor; + } + + const std::array& EngineConfiguration::GetFrameBufferClearColor() const + { + return m_frameBufferClearColor; + } + + bool EngineConfiguration::GetPreferFramebufferFormatSRGB() const + { + return m_preferFramebufferFormatSRGB; + } + + void EngineConfiguration::SetPreferFramebufferFormatSRGB(bool sRGB) + { + m_preferFramebufferFormatSRGB = sRGB; + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Base/EngineConfiguration.hpp b/openVulkanoCpp/Base/EngineConfiguration.hpp index 867a522..d94a095 100644 --- a/openVulkanoCpp/Base/EngineConfiguration.hpp +++ b/openVulkanoCpp/Base/EngineConfiguration.hpp @@ -8,6 +8,7 @@ #include #include +#include #undef max namespace OpenVulkano @@ -15,7 +16,7 @@ namespace OpenVulkano class EngineConfiguration { private: - EngineConfiguration() = default; + EngineConfiguration(); ~EngineConfiguration() = default; uint32_t m_numThreads = 1; @@ -23,40 +24,15 @@ namespace OpenVulkano bool m_preferFramebufferFormatSRGB = true; public: - [[nodiscard]] static EngineConfiguration* GetEngineConfiguration() - { - static EngineConfiguration config; - return &config; - } + [[nodiscard]] static EngineConfiguration* GetEngineConfiguration(); - void SetNumThreads(uint32_t numThreads) - { - m_numThreads = numThreads; - } - - [[nodiscard]] uint32_t GetNumThreads() const - { - return std::max(static_cast(1), m_numThreads); - } - - void SetFrameBufferClearColor(std::array frameBufferClearColor) - { - m_frameBufferClearColor = frameBufferClearColor; - } - - [[nodiscard]] const std::array& GetFrameBufferClearColor() const - { - return m_frameBufferClearColor; - } - - [[nodiscard]] bool GetPreferFramebufferFormatSRGB() const - { - return m_preferFramebufferFormatSRGB; - } - - void SetPreferFramebufferFormatSRGB(bool sRGB) - { - m_preferFramebufferFormatSRGB = sRGB; - } + void SetNumThreads(uint32_t numThreads); + [[nodiscard]] uint32_t GetNumThreads() const; + + void SetFrameBufferClearColor(std::array frameBufferClearColor); + [[nodiscard]] const std::array& GetFrameBufferClearColor() const; + + [[nodiscard]] bool GetPreferFramebufferFormatSRGB() const; + void SetPreferFramebufferFormatSRGB(bool sRGB); }; } \ No newline at end of file From 4e5b9319e68e92aff69ba6dea0617e314aef5709 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Tue, 4 Jun 2024 11:17:41 +0300 Subject: [PATCH 2/4] Getters/setters moved to the header file --- openVulkanoCpp/Base/EngineConfiguration.cpp | 30 --------------------- openVulkanoCpp/Base/EngineConfiguration.hpp | 12 ++++----- 2 files changed, 6 insertions(+), 36 deletions(-) diff --git a/openVulkanoCpp/Base/EngineConfiguration.cpp b/openVulkanoCpp/Base/EngineConfiguration.cpp index b6446ee..a3b2b54 100644 --- a/openVulkanoCpp/Base/EngineConfiguration.cpp +++ b/openVulkanoCpp/Base/EngineConfiguration.cpp @@ -61,34 +61,4 @@ namespace OpenVulkano static EngineConfiguration config; return &config; } - - void EngineConfiguration::SetNumThreads(uint32_t numThreads) - { - m_numThreads = numThreads; - } - - uint32_t EngineConfiguration::GetNumThreads() const - { - return std::max(static_cast(1), m_numThreads); - } - - void EngineConfiguration::SetFrameBufferClearColor(std::array frameBufferClearColor) - { - m_frameBufferClearColor = frameBufferClearColor; - } - - const std::array& EngineConfiguration::GetFrameBufferClearColor() const - { - return m_frameBufferClearColor; - } - - bool EngineConfiguration::GetPreferFramebufferFormatSRGB() const - { - return m_preferFramebufferFormatSRGB; - } - - void EngineConfiguration::SetPreferFramebufferFormatSRGB(bool sRGB) - { - m_preferFramebufferFormatSRGB = sRGB; - } } \ No newline at end of file diff --git a/openVulkanoCpp/Base/EngineConfiguration.hpp b/openVulkanoCpp/Base/EngineConfiguration.hpp index d94a095..8a926b4 100644 --- a/openVulkanoCpp/Base/EngineConfiguration.hpp +++ b/openVulkanoCpp/Base/EngineConfiguration.hpp @@ -26,13 +26,13 @@ namespace OpenVulkano public: [[nodiscard]] static EngineConfiguration* GetEngineConfiguration(); - void SetNumThreads(uint32_t numThreads); - [[nodiscard]] uint32_t GetNumThreads() const; + void SetNumThreads(uint32_t numThreads) { m_numThreads = numThreads; } + [[nodiscard]] uint32_t GetNumThreads() const { return std::max(static_cast(1), m_numThreads); } - void SetFrameBufferClearColor(std::array frameBufferClearColor); - [[nodiscard]] const std::array& GetFrameBufferClearColor() const; + void SetFrameBufferClearColor(std::array frameBufferClearColor) { m_frameBufferClearColor = frameBufferClearColor; } + [[nodiscard]] const std::array& GetFrameBufferClearColor() const { return m_frameBufferClearColor; } - [[nodiscard]] bool GetPreferFramebufferFormatSRGB() const; - void SetPreferFramebufferFormatSRGB(bool sRGB); + [[nodiscard]] bool GetPreferFramebufferFormatSRGB() const {return m_preferFramebufferFormatSRGB; } + void SetPreferFramebufferFormatSRGB(bool sRGB) { m_preferFramebufferFormatSRGB = sRGB; } }; } \ No newline at end of file From e17e8c294742a851f478a5202819067c14367fa1 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Tue, 4 Jun 2024 11:18:01 +0300 Subject: [PATCH 3/4] Moved consts to the namespace --- openVulkanoCpp/Base/EngineConfiguration.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/openVulkanoCpp/Base/EngineConfiguration.cpp b/openVulkanoCpp/Base/EngineConfiguration.cpp index a3b2b54..169907f 100644 --- a/openVulkanoCpp/Base/EngineConfiguration.cpp +++ b/openVulkanoCpp/Base/EngineConfiguration.cpp @@ -14,6 +14,13 @@ namespace OpenVulkano { + namespace + { + const char *NUM_THREADS_STR = "num_threads"; + const char *FRAMEBUFFER_CLEAR_COLOR_STR = "framebuffer_clear_color"; + const char *PREFER_FRAMEBUFFER_FORMAT_SRGB_STR = "prefer_framebuffer_format_srgb"; + } + EngineConfiguration::EngineConfiguration() { const std::string filePath = AppFolders::GetAppConfigHomeDir().string() + "/EngineConfig.yml"; @@ -22,11 +29,7 @@ namespace OpenVulkano { return; } - - const char *NUM_THREADS_STR = "num_threads"; - const char *FRAMEBUFFER_CLEAR_COLOR_STR = "framebuffer_clear_color"; - const char *PREFER_FRAMEBUFFER_FORMAT_SRGB_STR = "prefer_framebuffer_format_srgb"; - + file.seekg(0, std::ios::end); size_t size = file.tellg(); std::string buffer(size, ' '); From 0692e52bb9102510c5b5206422001d8d050a8726 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Tue, 4 Jun 2024 11:27:15 +0300 Subject: [PATCH 4/4] Using Utils::ReadFile to get contents of the yml file --- openVulkanoCpp/Base/EngineConfiguration.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/openVulkanoCpp/Base/EngineConfiguration.cpp b/openVulkanoCpp/Base/EngineConfiguration.cpp index 169907f..6045606 100644 --- a/openVulkanoCpp/Base/EngineConfiguration.cpp +++ b/openVulkanoCpp/Base/EngineConfiguration.cpp @@ -11,6 +11,7 @@ #include "EngineConfiguration.hpp" #include "IO/AppFolders.hpp" +#include "Utils.hpp" namespace OpenVulkano { @@ -23,20 +24,12 @@ namespace OpenVulkano EngineConfiguration::EngineConfiguration() { - const std::string filePath = AppFolders::GetAppConfigHomeDir().string() + "/EngineConfig.yml"; - std::ifstream file(filePath); - if (!file.is_open()) - { + const std::string filePath = (AppFolders::GetAppConfigHomeDir() / "EngineConfig.yml").string(); + Array fileContents = Utils::ReadFile(filePath, true); + if(fileContents.Size() == 0) return; - } - - file.seekg(0, std::ios::end); - size_t size = file.tellg(); - std::string buffer(size, ' '); - file.seekg(0); - file.read(buffer.data(), size); - ryml::Tree tree = ryml::parse_in_arena(ryml::to_csubstr(buffer)); + ryml::Tree tree = ryml::parse_in_arena(ryml::to_csubstr(fileContents.Data())); ryml::NodeRef root = tree.rootref(); if (root.has_child(NUM_THREADS_STR) && root[NUM_THREADS_STR].val().is_integer())