diff --git a/Navigator/src/Navigator.h b/Navigator/src/Navigator.h index f1f9b70..377bc78 100644 --- a/Navigator/src/Navigator.h +++ b/Navigator/src/Navigator.h @@ -18,5 +18,6 @@ #include "Navigator/Logger.h" #include "Navigator/Application.h" +#include "Navigator/Physics/PhysicsEventBuilder.h" -#endif \ No newline at end of file +#endif diff --git a/Navigator/src/Navigator/Application.cpp b/Navigator/src/Navigator/Application.cpp index 76ab60d..7e7440d 100644 --- a/Navigator/src/Navigator/Application.cpp +++ b/Navigator/src/Navigator/Application.cpp @@ -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::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(); } } -} \ No newline at end of file +} diff --git a/Navigator/src/Navigator/ParameterMap.cpp b/Navigator/src/Navigator/ParameterMap.cpp new file mode 100644 index 0000000..6fc5a3d --- /dev/null +++ b/Navigator/src/Navigator/ParameterMap.cpp @@ -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; + } +} diff --git a/Navigator/src/Navigator/ParameterMap.h b/Navigator/src/Navigator/ParameterMap.h index e687994..49ce4a7 100644 --- a/Navigator/src/Navigator/ParameterMap.h +++ b/Navigator/src/Navigator/ParameterMap.h @@ -1,14 +1,20 @@ #ifndef PARAMETER_MAP_H #define PARAMETER_MAP_H +#include + namespace Navigator { struct ParameterData { - double value = 0; - bool validFlag = false; + std::atomic value=0.0; + std::atomic 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 m_pdata; }; + /* + Global parameter accesing, storage + */ class ParameterMap { public: + + using Iter = std::unordered_map>::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(); } + inline void SetParameter(const std::string& name, std::shared_ptr& 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 m_map; - static ParameterMap s_map; + std::unordered_map> m_map; + static ParameterMap* s_instance; }; ParameterMap* CreateParameterMap(); } -#endif \ No newline at end of file +#endif