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 1/4] 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) From a474e8bf9a2d542d9ad75228fd7ca90a8756fe94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20J=C3=B6nsson?= Date: Fri, 29 Dec 2023 23:48:24 +0100 Subject: [PATCH 2/4] Implemented base console variables --- Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp | 68 ++++++++++++++++++- .../Volt/Console/ConsoleVariableRegistry.h | 56 +++++++++++---- 2 files changed, 108 insertions(+), 16 deletions(-) diff --git a/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp b/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp index 1216446f4..7662c0e62 100644 --- a/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp +++ b/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp @@ -3,6 +3,9 @@ #include #include +#include + +#include namespace Utility { @@ -48,10 +51,73 @@ void LogPanel::UpdateMainContent() myCategories.emplace_back("Default"); } + ImGui::SameLine(); + ImGui::PushItemWidth(200.f); + + static std::string commandStr; + + if (ImGui::InputTextWithHintString("##commandLine", "Command...", &commandStr, ImGuiInputTextFlags_EnterReturnsTrue)) + { + auto strings = Utility::SplitStringsByCharacter(commandStr, ' '); + if (!strings.empty()) + { + if (Volt::ConsoleVariableRegistry::VariableExists(strings[0])) + { + auto variable = Volt::ConsoleVariableRegistry::GetVariable(strings[0]); + + std::string message = strings[0] + " = "; + + if (strings.size() > 1) + { + if (variable->IsFloat()) + { + const float value = std::stof(strings[1]); + variable->Set(&value); + } + else if (variable->IsInteger()) + { + const int32_t value = std::stoi(strings[1]); + variable->Set(&value); + } + else if (variable->IsString()) + { + variable->Set(&strings[1]); + } + + message += strings[1]; + } + else + { + if (variable->IsFloat()) + { + message += std::to_string(*static_cast(variable->Get())); + } + else if (variable->IsInteger()) + { + message += std::to_string(*static_cast(variable->Get())); + } + else if (variable->IsString()) + { + message += *static_cast(variable->Get()); + } + + } + + VT_CORE_TRACE(message); + } + else + { + VT_CORE_TRACE("Command {0} not found!", strings[0]); + } + } + + commandStr = ""; + } + + ImGui::SameLine(); static int32_t logLevel = 0; - ImGui::PushItemWidth(200.f); if (ImGui::Combo("##level", &logLevel, "Trace\0Info\0Warning\0Error\0Critical")) { switch (logLevel) diff --git a/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h b/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h index 6764eef0a..669763d0a 100644 --- a/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h +++ b/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h @@ -15,19 +15,30 @@ namespace Volt virtual std::string_view GetName() const = 0; virtual std::string_view GetDescription() const = 0; + + virtual bool IsInteger() const = 0; + virtual bool IsFloat() const = 0; + virtual bool IsString() const = 0; }; - template + template + concept ValidConsoleVariableType = std::is_same_v || std::is_same_v || std::is_same_v; + + template class RegisteredConsoleVariable : public RegisteredConsoleVariableBase { public: RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description); - const void* Get() const override; + [[nodiscard]] 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; } + [[nodiscard]] inline std::string_view GetName() const override { return m_variableName; } + [[nodiscard]] inline std::string_view GetDescription() const override { return m_description; } + + [[nodiscard]] inline constexpr bool IsInteger() const override { return std::is_integral_v; } + [[nodiscard]] inline constexpr bool IsFloat() const override { return std::is_floating_point_v; } + [[nodiscard]] inline constexpr bool IsString() const override { return std::is_same_v; } private: T m_value; @@ -35,7 +46,7 @@ namespace Volt std::string_view m_description; }; - template + template class ConsoleVariable { public: @@ -58,7 +69,7 @@ namespace Volt Weak> m_variableReference; }; - template + template class ConsoleVariableRef { public: @@ -84,43 +95,53 @@ namespace Volt class ConsoleVariableRegistry { public: - template + template static Weak> RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description); - template + template static Weak> FindVariable(std::string_view variableName); + inline static Weak GetVariable(std::string_view variableName); + inline static bool VariableExists(const std::string& variableName); + static std::unordered_map>& GetRegisteredVariables() { return s_registeredVariables; } private: + ConsoleVariableRegistry() = delete; + inline static std::unordered_map> s_registeredVariables; }; - template + inline bool ConsoleVariableRegistry::VariableExists(const std::string& variableName) + { + return s_registeredVariables.contains(std::string_view(variableName)); + } + + template inline RegisteredConsoleVariable::RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description) : m_value(defaultValue) { } - template + template inline const void* RegisteredConsoleVariable::Get() const { return reinterpret_cast(&m_value); } - template + template inline void RegisteredConsoleVariable::Set(const void* value) { m_value = *reinterpret_cast(value); } - template + template inline ConsoleVariable::ConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description) { m_variableReference = ConsoleVariableRegistry::RegisterVariable(variableName, defaultValue, description); } - template + template inline Weak> ConsoleVariableRegistry::RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description) { Ref> consoleVariable = CreateRef>(variableName, defaultValue, description); @@ -131,7 +152,7 @@ namespace Volt return consoleVariable; } - template + template inline Weak> ConsoleVariableRegistry::FindVariable(std::string_view variableName) { if (s_registeredVariables.contains(variableName)) @@ -142,10 +163,15 @@ namespace Volt return Weak>(); } - template + template inline ConsoleVariableRef::ConsoleVariableRef(std::string_view variableName) { m_variableReference = ConsoleVariableRegistry::FindVariable(variableName); VT_CORE_ASSERT(m_variableReference, "Variable with name not found!"); } + + inline Weak ConsoleVariableRegistry::GetVariable(std::string_view variableName) + { + return s_registeredVariables.at(variableName); + } } From 91130e95e5d75fd10d6bc53567165df198bcc495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20J=C3=B6nsson?= Date: Fri, 29 Dec 2023 23:48:52 +0100 Subject: [PATCH 3/4] Removed test variable --- Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp b/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp index 3398fe2d1..2096df48c 100644 --- a/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp +++ b/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp @@ -82,8 +82,6 @@ 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) From d6d7ea9a3d2a1b953a4a1c77ca6285cd28fb5aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20J=C3=B6nsson?= Date: Sat, 30 Dec 2023 00:11:27 +0100 Subject: [PATCH 4/4] Made the console variables case insensitive --- Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp | 2 +- .../Volt/Console/ConsoleVariableRegistry.h | 42 +++++++++++-------- .../Volt/src/Volt/Rendering/SceneRenderer.cpp | 2 - 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp b/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp index 7662c0e62..53a82d4eb 100644 --- a/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp +++ b/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp @@ -65,7 +65,7 @@ void LogPanel::UpdateMainContent() { auto variable = Volt::ConsoleVariableRegistry::GetVariable(strings[0]); - std::string message = strings[0] + " = "; + std::string message = std::string(variable->GetName()) + " = "; if (strings.size() > 1) { diff --git a/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h b/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h index 669763d0a..9eb5f72da 100644 --- a/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h +++ b/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h @@ -2,6 +2,8 @@ #include "Volt/Core/Base.h" +#include "Volt/Utility/StringUtility.h" + #include #include @@ -28,7 +30,7 @@ namespace Volt class RegisteredConsoleVariable : public RegisteredConsoleVariableBase { public: - RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description); + RegisteredConsoleVariable(const std::string& variableName, const T& defaultValue, std::string_view description); [[nodiscard]] const void* Get() const override; void Set(const void* value) override; @@ -42,7 +44,7 @@ namespace Volt private: T m_value; - std::string_view m_variableName; + std::string m_variableName; std::string_view m_description; }; @@ -99,27 +101,28 @@ namespace Volt static Weak> RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description); template - static Weak> FindVariable(std::string_view variableName); + static Weak> FindVariable(const std::string& variableName); - inline static Weak GetVariable(std::string_view variableName); + inline static Weak GetVariable(const std::string& variableName); inline static bool VariableExists(const std::string& variableName); - static std::unordered_map>& GetRegisteredVariables() { return s_registeredVariables; } + static std::unordered_map>& GetRegisteredVariables() { return s_registeredVariables; } private: ConsoleVariableRegistry() = delete; - inline static std::unordered_map> s_registeredVariables; + inline static std::unordered_map> s_registeredVariables; }; inline bool ConsoleVariableRegistry::VariableExists(const std::string& variableName) { - return s_registeredVariables.contains(std::string_view(variableName)); + std::string tempVarName = ::Utility::ToLower(std::string(variableName)); + return s_registeredVariables.contains(tempVarName); } template - inline RegisteredConsoleVariable::RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description) - : m_value(defaultValue) + inline RegisteredConsoleVariable::RegisteredConsoleVariable(const std::string& variableName, const T& defaultValue, std::string_view description) + : m_value(defaultValue), m_variableName(variableName), m_description(description) { } @@ -144,20 +147,24 @@ namespace Volt template inline Weak> ConsoleVariableRegistry::RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description) { - Ref> consoleVariable = CreateRef>(variableName, defaultValue, description); + std::string tempVarName = ::Utility::ToLower(std::string(variableName)); + + Ref> consoleVariable = CreateRef>(tempVarName, defaultValue, description); - VT_CORE_ASSERT(!s_registeredVariables.contains(variableName), "Command variable with name already registered!"); - s_registeredVariables[variableName] = consoleVariable; + VT_CORE_ASSERT(!s_registeredVariables.contains(tempVarName), "Command variable with name already registered!"); + s_registeredVariables[tempVarName] = consoleVariable; return consoleVariable; } template - inline Weak> ConsoleVariableRegistry::FindVariable(std::string_view variableName) + inline Weak> ConsoleVariableRegistry::FindVariable(const std::string& variableName) { - if (s_registeredVariables.contains(variableName)) + const std::string tempVarName = ::Utility::ToLower(variableName); + + if (s_registeredVariables.contains(tempVarName)) { - return s_registeredVariables.at(variableName); + return s_registeredVariables.at(tempVarName); } return Weak>(); @@ -170,8 +177,9 @@ namespace Volt VT_CORE_ASSERT(m_variableReference, "Variable with name not found!"); } - inline Weak ConsoleVariableRegistry::GetVariable(std::string_view variableName) + inline Weak ConsoleVariableRegistry::GetVariable(const std::string& variableName) { - return s_registeredVariables.at(variableName); + const std::string tempVarName = ::Utility::ToLower(variableName); + return s_registeredVariables.at(tempVarName); } } diff --git a/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp b/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp index 2096df48c..dc61ffef0 100644 --- a/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp +++ b/Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp @@ -78,8 +78,6 @@ #include "Volt/Utility/Noise.h" #include "Volt/Platform/ThreadUtility.h" -#include "Volt/Console/ConsoleVariableRegistry.h" - namespace Volt { namespace Utility