From 691b6b712df5ab5345bd0b5b0848e968bb089d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20J=C3=B6nsson?= Date: Fri, 29 Dec 2023 01:36:03 +0100 Subject: [PATCH] Started work on console variables --- .../Volt/Console/ConsoleVariableRegistry.h | 151 ++++++++++++++++++ .../Volt/src/Volt/Rendering/SceneRenderer.cpp | 4 + 2 files changed, 155 insertions(+) create mode 100644 Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h diff --git a/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h b/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h new file mode 100644 index 000000000..6764eef0a --- /dev/null +++ b/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h @@ -0,0 +1,151 @@ +#pragma once + +#include "Volt/Core/Base.h" + +#include +#include + +namespace Volt +{ + class RegisteredConsoleVariableBase + { + public: + virtual const void* Get() const = 0; + virtual void Set(const void* value) = 0; + + virtual std::string_view GetName() const = 0; + virtual std::string_view GetDescription() const = 0; + }; + + template + class RegisteredConsoleVariable : public RegisteredConsoleVariableBase + { + public: + RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description); + + const void* Get() const override; + void Set(const void* value) override; + + std::string_view GetName() const override { return m_variableName; } + std::string_view GetDescription() const override { return m_description; } + + private: + T m_value; + std::string_view m_variableName; + std::string_view m_description; + }; + + template + class ConsoleVariable + { + public: + ConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description); + + const T& GetValue() const { return *reinterpret_cast(m_variableReference->Get()); } + void SetValue(const T& value) { m_variableReference->Set(&value); } + + T& operator=(const T& other) + { + if (this == &other) + { + return GetValue(); + } + + SetValue(other); + } + + private: + Weak> m_variableReference; + }; + + template + class ConsoleVariableRef + { + public: + ConsoleVariableRef(std::string_view variableName); + + const T& GetValue() const { return *reinterpret_cast(m_variableReference->Get()); } + void SetValue(const T& value) { m_variableReference->Set(&value); } + + T& operator=(const T& other) + { + if (this == &other) + { + return GetValue(); + } + + SetValue(other); + } + + private: + Weak> m_variableReference; + }; + + class ConsoleVariableRegistry + { + public: + template + static Weak> RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description); + + template + static Weak> FindVariable(std::string_view variableName); + + static std::unordered_map>& GetRegisteredVariables() { return s_registeredVariables; } + + private: + inline static std::unordered_map> s_registeredVariables; + }; + + template + inline RegisteredConsoleVariable::RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description) + : m_value(defaultValue) + { + } + + template + inline const void* RegisteredConsoleVariable::Get() const + { + return reinterpret_cast(&m_value); + } + + template + inline void RegisteredConsoleVariable::Set(const void* value) + { + m_value = *reinterpret_cast(value); + } + + template + inline ConsoleVariable::ConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description) + { + m_variableReference = ConsoleVariableRegistry::RegisterVariable(variableName, defaultValue, description); + } + + template + inline Weak> ConsoleVariableRegistry::RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description) + { + Ref> consoleVariable = CreateRef>(variableName, defaultValue, description); + + VT_CORE_ASSERT(!s_registeredVariables.contains(variableName), "Command variable with name already registered!"); + s_registeredVariables[variableName] = consoleVariable; + + return consoleVariable; + } + + template + inline Weak> ConsoleVariableRegistry::FindVariable(std::string_view variableName) + { + if (s_registeredVariables.contains(variableName)) + { + return s_registeredVariables.at(variableName); + } + + return Weak>(); + } + + template + inline ConsoleVariableRef::ConsoleVariableRef(std::string_view variableName) + { + m_variableReference = ConsoleVariableRegistry::FindVariable(variableName); + VT_CORE_ASSERT(m_variableReference, "Variable with name not found!"); + } +} diff --git a/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp b/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp index dc61ffef0..3398fe2d1 100644 --- a/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp +++ b/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp @@ -78,8 +78,12 @@ #include "Volt/Utility/Noise.h" #include "Volt/Platform/ThreadUtility.h" +#include "Volt/Console/ConsoleVariableRegistry.h" + namespace Volt { + static ConsoleVariable s_testCVar = ConsoleVariable("r.test", 0, "Test Variable"); + namespace Utility { inline static Ref CreateRenderPipeline(Ref shader, std::vector attachments, std::string_view name, CompareOperator depthCompare = CompareOperator::GreaterEqual)