diff --git a/SDK/include/NDK/BaseSystem.hpp b/SDK/include/NDK/BaseSystem.hpp index 99dcbf768..1e1b63208 100644 --- a/SDK/include/NDK/BaseSystem.hpp +++ b/SDK/include/NDK/BaseSystem.hpp @@ -35,17 +35,19 @@ namespace Ndk bool Filters(const Entity* entity) const; inline const EntityList& GetEntities() const; + inline float GetFixedUpdateRate() const; inline SystemIndex GetIndex() const; + inline float GetMaximumUpdateRate() const; inline int GetUpdateOrder() const; - inline float GetUpdateRate() const; inline World& GetWorld() const; inline bool IsEnabled() const; inline bool HasEntity(const Entity* entity) const; + inline void SetFixedUpdateRate(float updatePerSecond); + inline void SetMaximumUpdateRate(float updatePerSecond); void SetUpdateOrder(int updateOrder); - inline void SetUpdateRate(float updatePerSecond); inline void Update(float elapsedTime); @@ -93,8 +95,9 @@ namespace Ndk SystemIndex m_systemIndex; World* m_world; bool m_updateEnabled; + float m_fixedUpdateRate; + float m_maxUpdateRate; float m_updateCounter; - float m_updateRate; int m_updateOrder; static SystemIndex s_nextIndex; diff --git a/SDK/include/NDK/BaseSystem.inl b/SDK/include/NDK/BaseSystem.inl index 5969dd8e3..20b576b93 100644 --- a/SDK/include/NDK/BaseSystem.inl +++ b/SDK/include/NDK/BaseSystem.inl @@ -20,7 +20,8 @@ namespace Ndk m_updateEnabled(true), m_updateOrder(0) { - SetUpdateRate(30); + SetFixedUpdateRate(0); + SetMaximumUpdateRate(30); } /*! @@ -35,7 +36,8 @@ namespace Ndk m_systemIndex(system.m_systemIndex), m_updateEnabled(system.m_updateEnabled), m_updateCounter(0.f), - m_updateRate(system.m_updateRate), + m_fixedUpdateRate(system.m_fixedUpdateRate), + m_maxUpdateRate(system.m_maxUpdateRate), m_updateOrder(system.m_updateOrder) { } @@ -61,6 +63,24 @@ namespace Ndk return m_entities; } + /*! + * \brief Gets the maximum rate of update of the system + * \return Update rate + */ + inline float BaseSystem::GetFixedUpdateRate() const + { + return (m_fixedUpdateRate > 0.f) ? 1.f / m_fixedUpdateRate : 0.f; + } + + /*! + * \brief Gets the maximum rate of update of the system + * \return Update rate + */ + inline float BaseSystem::GetMaximumUpdateRate() const + { + return (m_maxUpdateRate > 0.f) ? 1.f / m_maxUpdateRate : 0.f; + } + /*! * \brief Gets the index of the system * \return Index of the system @@ -82,16 +102,6 @@ namespace Ndk return m_updateOrder; } - /*! - * \brief Gets the rate of update of the system - * \return Update rate - */ - - inline float BaseSystem::GetUpdateRate() const - { - return (m_updateRate > 0.f) ? 1.f / m_updateRate : 0.f; - } - /*! * \brief Gets the world on which the system operate * \return World in which the system is @@ -125,15 +135,25 @@ namespace Ndk } /*! - * \brief Sets the rate of update for the system + * \brief Sets the fixed update rate for the system + * + * \param updatePerSecond Update rate, 0 means update rate is not fixed + */ + inline void BaseSystem::SetFixedUpdateRate(float updatePerSecond) + { + m_updateCounter = 0.f; + m_fixedUpdateRate = (updatePerSecond > 0.f) ? 1.f / updatePerSecond : 0.f; // 0.f means no limit + } + + /*! + * \brief Sets the maximum update rate for the system * * \param updatePerSecond Update rate, 0 means as much as possible */ - - inline void BaseSystem::SetUpdateRate(float updatePerSecond) + inline void BaseSystem::SetMaximumUpdateRate(float updatePerSecond) { m_updateCounter = 0.f; - m_updateRate = (updatePerSecond > 0.f) ? 1.f / updatePerSecond : 0.f; // 0.f means no limit + m_maxUpdateRate = (updatePerSecond > 0.f) ? 1.f / updatePerSecond : 0.f; // 0.f means no limit } /*! @@ -147,18 +167,40 @@ namespace Ndk if (!IsEnabled()) return; - if (m_updateRate > 0.f) - { - m_updateCounter += elapsedTime; + m_updateCounter += elapsedTime; - while (m_updateCounter >= m_updateRate) + if (m_maxUpdateRate > 0.f) + { + if (m_updateCounter > elapsedTime) { - OnUpdate(m_updateRate); - m_updateCounter -= m_updateRate; + if (m_fixedUpdateRate > 0.f) + { + while (m_updateCounter >= m_fixedUpdateRate) + { + OnUpdate(m_fixedUpdateRate); + m_updateCounter -= m_fixedUpdateRate; + } + } + else + { + OnUpdate(m_maxUpdateRate); + m_updateCounter -= elapsedTime; + } } } else - OnUpdate(elapsedTime); + { + if (m_fixedUpdateRate > 0.f) + { + while (m_updateCounter >= m_fixedUpdateRate) + { + OnUpdate(m_fixedUpdateRate); + m_updateCounter -= m_fixedUpdateRate; + } + } + else + OnUpdate(elapsedTime); + } } /*! diff --git a/SDK/src/NDK/Systems/RenderSystem.cpp b/SDK/src/NDK/Systems/RenderSystem.cpp index 022621596..c137b18ef 100644 --- a/SDK/src/NDK/Systems/RenderSystem.cpp +++ b/SDK/src/NDK/Systems/RenderSystem.cpp @@ -36,7 +36,7 @@ namespace Ndk ChangeRenderTechnique(); SetDefaultBackground(Nz::ColorBackground::New()); SetUpdateOrder(100); //< Render last, after every movement is done - SetUpdateRate(0.f); //< We don't want any rate limit + SetMaximumUpdateRate(0.f); //< We don't want any rate limit } /*!