-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2] Refine SleipnirEngine interface
- Loading branch information
Showing
15 changed files
with
403 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
SleipnirEngine/ecs/include/sleipnir/ecs/system/TimeBase.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (C) 2018 by Godlike | ||
* This code is licensed under the MIT license (MIT) | ||
* (http://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
#ifndef SLEIPNIR_ECS_SYSTEM_TIME_BASE_HPP | ||
#define SLEIPNIR_ECS_SYSTEM_TIME_BASE_HPP | ||
|
||
#include <sleipnir/ecs/WorldTime.hpp> | ||
|
||
namespace sleipnir | ||
{ | ||
namespace ecs | ||
{ | ||
namespace system | ||
{ | ||
|
||
//! Time system base class | ||
class TimeBase | ||
{ | ||
public: | ||
//! Shortcut to time unit | ||
using TimeUnit = WorldTime::TimeUnit; | ||
|
||
//! Default constructor | ||
TimeBase(WorldTime& worldTime); | ||
|
||
//! Default destructor | ||
virtual ~TimeBase() = default; | ||
|
||
/** @brief Method invoked each loop cycle | ||
* | ||
* Multiplies @p realDuration with @ref factor to calculate world time | ||
* duration of current frame. | ||
* | ||
* Updates @ref m_worldTime with calculated world time duration. | ||
* | ||
* @param realDuration raw duration of current frame | ||
* | ||
* @return world time duration | ||
*/ | ||
virtual TimeUnit Update(TimeUnit realDuration); | ||
|
||
//! Returns raw duration of current frame | ||
TimeUnit GetRealDuration() const { return m_realDuration; } | ||
|
||
//! Returns world duration of current frame | ||
TimeUnit GetWorldDuration() const { return m_worldDuration; } | ||
|
||
//! Set time flow factor | ||
void SetFactor(float factor) { m_factor = factor; } | ||
|
||
protected: | ||
//! Time flow factor | ||
float m_factor; | ||
|
||
//! Raw duration of current frame | ||
TimeUnit m_realDuration; | ||
|
||
//! World duration of current frame | ||
TimeUnit m_worldDuration; | ||
|
||
//! Time holder | ||
WorldTime& m_worldTime; | ||
|
||
}; | ||
|
||
} // namespace system | ||
} // namespace ecs | ||
} // namespace sleipnir | ||
|
||
#endif // SLEIPNIR_ECS_SYSTEM_TIME_BASE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (C) 2018 by Godlike | ||
* This code is licensed under the MIT license (MIT) | ||
* (http://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
#include <sleipnir/ecs/system/TimeBase.hpp> | ||
|
||
#include <cassert> | ||
|
||
namespace sleipnir | ||
{ | ||
namespace ecs | ||
{ | ||
namespace system | ||
{ | ||
|
||
TimeBase::TimeBase(WorldTime& worldTime) | ||
: m_factor(1.0f) | ||
, m_realDuration(0) | ||
, m_worldDuration(0) | ||
, m_worldTime(worldTime) | ||
{ | ||
|
||
} | ||
|
||
TimeBase::TimeUnit TimeBase::Update(TimeUnit realDuration) | ||
{ | ||
assert(m_factor > 0); | ||
|
||
m_realDuration = realDuration; | ||
|
||
TimeUnit const worldNow = m_worldTime.GetTime(); | ||
|
||
m_worldDuration = TimeUnit(static_cast<uint64_t>(static_cast<float>(realDuration.count()) * m_factor + 0.5f)); | ||
|
||
m_worldTime.SetTime(worldNow + m_worldDuration); | ||
|
||
return m_worldDuration; | ||
} | ||
|
||
} // namespace system | ||
} // namespace ecs | ||
} // namespace sleipnir |
Oops, something went wrong.