mirror of
https://github.com/gwm17/Specter.git
synced 2024-11-22 18:28:52 -05:00
Fleshed out parameter system
This commit is contained in:
parent
2176992f81
commit
58b6612a3e
|
@ -18,5 +18,6 @@
|
|||
|
||||
#include "Navigator/Logger.h"
|
||||
#include "Navigator/Application.h"
|
||||
#include "Navigator/Physics/PhysicsEventBuilder.h"
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "Application.h"
|
||||
#include "ParameterMap.h"
|
||||
#include "Renderer/Renderer.h"
|
||||
#include "Renderer/RenderCommand.h"
|
||||
|
||||
|
@ -7,14 +8,21 @@ namespace Navigator {
|
|||
Application* Application::s_instance = nullptr;
|
||||
|
||||
Application::Application() :
|
||||
m_runFlag(true)
|
||||
m_runFlag(true), m_physThread(nullptr)
|
||||
{
|
||||
s_instance = this;
|
||||
|
||||
m_window = std::unique_ptr<Window>(Window::Create());
|
||||
m_window->SetEventCallback(BIND_EVENT_FUNCTION(Application::OnEvent));
|
||||
|
||||
/*order is important, must be pMap then evb*/
|
||||
CreateParameterMap();
|
||||
CreatePhysicsEventBuilder();
|
||||
|
||||
NavParameter par("joseph","mama");
|
||||
par.SetValue(8);
|
||||
NAV_INFO("Does the par exist? {0}", ParameterMap::GetInstance().IsParameterValid("joseph"));
|
||||
NAV_INFO("What is its value? {0}", ParameterMap::GetInstance().GetParameterValue("joseph"));
|
||||
|
||||
PushLayer(new Layer());
|
||||
|
||||
|
@ -120,4 +128,4 @@ namespace Navigator {
|
|||
m_window->OnUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
63
Navigator/src/Navigator/ParameterMap.cpp
Normal file
63
Navigator/src/Navigator/ParameterMap.cpp
Normal file
|
@ -0,0 +1,63 @@
|
|||
//
|
||||
// ParameterMap.cpp
|
||||
// Navigator
|
||||
//
|
||||
// Created by Gordon McCann on 1/7/22.
|
||||
//
|
||||
|
||||
#include "ParameterMap.h"
|
||||
|
||||
namespace Navigator {
|
||||
|
||||
NavParameter::NavParameter(const std::string& name, const std::string& alias) :
|
||||
m_name(name)
|
||||
{
|
||||
NAV_INFO("Making a new parameter named {0}...",name);
|
||||
m_pdata = nullptr;
|
||||
ParameterMap& map = ParameterMap::GetInstance();
|
||||
auto iter = map.find(name);
|
||||
if(iter == map.end())
|
||||
{
|
||||
NAV_INFO("Added it to the map.");
|
||||
map.AddParameter(name);
|
||||
}
|
||||
|
||||
NAV_INFO("Setting the memory...");
|
||||
map.SetParameter(name, m_pdata);
|
||||
}
|
||||
|
||||
NavParameter::~NavParameter() {}
|
||||
|
||||
ParameterMap* ParameterMap::s_instance = nullptr;
|
||||
|
||||
ParameterMap* CreateParameterMap() { return new ParameterMap(); }
|
||||
|
||||
ParameterMap::ParameterMap()
|
||||
{
|
||||
NAV_INFO("Created ParameterMap");
|
||||
if(s_instance != nullptr)
|
||||
NAV_ERROR("Cannot have multiple instances of ParameterMap!!!");
|
||||
|
||||
s_instance = this;
|
||||
}
|
||||
|
||||
ParameterMap::~ParameterMap() {}
|
||||
|
||||
double ParameterMap::GetParameterValue(const std::string& name)
|
||||
{
|
||||
auto iter = m_map.find(name);
|
||||
if(iter != m_map.end())
|
||||
return iter->second->value;
|
||||
else
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
bool ParameterMap::IsParameterValid(const std::string& name)
|
||||
{
|
||||
auto iter = m_map.find(name);
|
||||
if(iter != m_map.end())
|
||||
return iter->second->validFlag;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,14 +1,20 @@
|
|||
#ifndef PARAMETER_MAP_H
|
||||
#define PARAMETER_MAP_H
|
||||
|
||||
#include <atomic>
|
||||
|
||||
namespace Navigator {
|
||||
|
||||
struct ParameterData
|
||||
{
|
||||
double value = 0;
|
||||
bool validFlag = false;
|
||||
std::atomic<double> value=0.0;
|
||||
std::atomic<bool> validFlag=false;
|
||||
};
|
||||
|
||||
/*
|
||||
For use inside of the physics thread only!!!!!! Do not use elsewhere as complex operations on parameter values are !not!
|
||||
guaranteed to be thread-safe, only the accesing is!
|
||||
*/
|
||||
class NavParameter
|
||||
{
|
||||
|
||||
|
@ -16,34 +22,47 @@ namespace Navigator {
|
|||
NavParameter(const std::string& name, const std::string& alias);
|
||||
~NavParameter();
|
||||
|
||||
inline void SetValue(double value) { p.value = value; }
|
||||
inline bool IsValid() const { return m_pdata->validFlag; }
|
||||
inline void Invalidate() { m_pdata->validFlag = false; }
|
||||
inline void SetValue(double value) { m_pdata->validFlag = true; m_pdata->value = value; }
|
||||
inline double GetValue() const { return m_pdata->value; }
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
ParameterData p;
|
||||
std::string m_name;
|
||||
std::shared_ptr<ParameterData> m_pdata;
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
Global parameter accesing, storage
|
||||
*/
|
||||
class ParameterMap
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
using Iter = std::unordered_map<std::string, std::shared_ptr<ParameterData>>::iterator;
|
||||
|
||||
ParameterMap();
|
||||
~ParameterMap();
|
||||
inline void AddParameter(const std::string& name) { m_map[name] = nullptr; }
|
||||
inline void SetParameter(const std::string& name, ParameterData* param) { m_map[name] = param; }
|
||||
inline void AddParameter(const std::string& name) { m_map[name] = std::make_shared<ParameterData>(); }
|
||||
inline void SetParameter(const std::string& name, std::shared_ptr<ParameterData>& param) { param = m_map[name]; }
|
||||
double GetParameterValue(const std::string& name);
|
||||
bool IsParameterValid(const std::string& name);
|
||||
void ResetParameters();
|
||||
inline Iter end() { return m_map.end(); }
|
||||
inline Iter begin() { return m_map.begin(); }
|
||||
inline Iter find(const std::string& name) { return m_map.find(name); }
|
||||
|
||||
inline static ParameterMap& GetParameterMap() { return s_map; }
|
||||
inline static ParameterMap& GetInstance() { return *s_instance; }
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, ParameterData*> m_map;
|
||||
static ParameterMap s_map;
|
||||
std::unordered_map<std::string, std::shared_ptr<ParameterData>> m_map;
|
||||
static ParameterMap* s_instance;
|
||||
|
||||
};
|
||||
|
||||
ParameterMap* CreateParameterMap();
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue
Block a user