Skip to content

Commit

Permalink
Started work on console variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Dec 29, 2023
1 parent 829cb27 commit 691b6b7
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
151 changes: 151 additions & 0 deletions Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#pragma once

#include "Volt/Core/Base.h"

#include <unordered_map>
#include <string_view>

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<typename T>
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<typename T>
class ConsoleVariable
{
public:
ConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description);

const T& GetValue() const { return *reinterpret_cast<T*>(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<RegisteredConsoleVariable<T>> m_variableReference;
};

template<typename T>
class ConsoleVariableRef
{
public:
ConsoleVariableRef(std::string_view variableName);

const T& GetValue() const { return *reinterpret_cast<T*>(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<RegisteredConsoleVariable<T>> m_variableReference;
};

class ConsoleVariableRegistry
{
public:
template<typename T>
static Weak<RegisteredConsoleVariable<T>> RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description);

template<typename T>
static Weak<RegisteredConsoleVariable<T>> FindVariable(std::string_view variableName);

static std::unordered_map<std::string_view, Ref<RegisteredConsoleVariableBase>>& GetRegisteredVariables() { return s_registeredVariables; }

private:
inline static std::unordered_map<std::string_view, Ref<RegisteredConsoleVariableBase>> s_registeredVariables;
};

template<typename T>
inline RegisteredConsoleVariable<T>::RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description)
: m_value(defaultValue)
{
}

template<typename T>
inline const void* RegisteredConsoleVariable<T>::Get() const
{
return reinterpret_cast<const void*>(&m_value);
}

template<typename T>
inline void RegisteredConsoleVariable<T>::Set(const void* value)
{
m_value = *reinterpret_cast<const T*>(value);
}

template<typename T>
inline ConsoleVariable<T>::ConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description)
{
m_variableReference = ConsoleVariableRegistry::RegisterVariable<T>(variableName, defaultValue, description);
}

template<typename T>
inline Weak<RegisteredConsoleVariable<T>> ConsoleVariableRegistry::RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description)
{
Ref<RegisteredConsoleVariable<T>> consoleVariable = CreateRef<RegisteredConsoleVariable<T>>(variableName, defaultValue, description);

VT_CORE_ASSERT(!s_registeredVariables.contains(variableName), "Command variable with name already registered!");
s_registeredVariables[variableName] = consoleVariable;

return consoleVariable;
}

template<typename T>
inline Weak<RegisteredConsoleVariable<T>> ConsoleVariableRegistry::FindVariable(std::string_view variableName)
{
if (s_registeredVariables.contains(variableName))
{
return s_registeredVariables.at(variableName);
}

return Weak<RegisteredConsoleVariable<T>>();
}

template<typename T>
inline ConsoleVariableRef<T>::ConsoleVariableRef(std::string_view variableName)
{
m_variableReference = ConsoleVariableRegistry::FindVariable(variableName);
VT_CORE_ASSERT(m_variableReference, "Variable with name not found!");
}
}
4 changes: 4 additions & 0 deletions Volt/Volt/src/Volt/Rendering/SceneRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@
#include "Volt/Utility/Noise.h"
#include "Volt/Platform/ThreadUtility.h"

#include "Volt/Console/ConsoleVariableRegistry.h"

namespace Volt
{
static ConsoleVariable<int32_t> s_testCVar = ConsoleVariable<int32_t>("r.test", 0, "Test Variable");

namespace Utility
{
inline static Ref<RenderPipeline> CreateRenderPipeline(Ref<Shader> shader, std::vector<FramebufferAttachment> attachments, std::string_view name, CompareOperator depthCompare = CompareOperator::GreaterEqual)
Expand Down

0 comments on commit 691b6b7

Please sign in to comment.