Skip to content

Commit

Permalink
[#2] Refine SleipnirEngine interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDmitry committed Dec 6, 2018
1 parent 47a6f8c commit e2f9ae6
Show file tree
Hide file tree
Showing 15 changed files with 403 additions and 98 deletions.
10 changes: 9 additions & 1 deletion SleipnirEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,23 @@ target_include_directories(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME}
PUBLIC
Mule::Utilities
Tulpar::Audio
)

# internal static libraries
if (UNIX)
set(EXPORT_SYMBOLS_PREFIX "-Wl,--whole-archive")
set(EXPORT_SYMBOLS_SUFFIX "-Wl,--no-whole-archive")
endif()

target_link_libraries(${PROJECT_NAME}
PUBLIC
${EXPORT_SYMBOLS_PREFIX}
Sleipnir::Engine::ECS
Sleipnir::Engine::Utility
${EXPORT_SYMBOLS_SUFFIX}
)


include(SleipnirInstallInfo)

set_target_properties(
Expand All @@ -108,6 +115,7 @@ install( DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
PATTERN "*.imp"
PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
PATTERN "utility/InternalLoggers.hpp" EXCLUDE
)

include(CMakePackageConfigHelpers)
Expand Down
3 changes: 2 additions & 1 deletion SleipnirEngine/ecs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ set(SLEIPNIR_ECS_SYSTEM_SOURCES

source/system/Lifetime.cpp
source/system/Render.cpp
source/system/Time.cpp
source/system/TimeBase.cpp
source/system/Timer.cpp
source/system/TimeWithPhysics.cpp
)

set(SLEIPNIR_ECS_COMMON_SOURCES
Expand Down
30 changes: 18 additions & 12 deletions SleipnirEngine/ecs/include/sleipnir/ecs/Systems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#define SLEIPNIR_ECS_SYSTEMS_HPP

#include <sleipnir/ecs/system/ISystem.hpp>
#include <sleipnir/ecs/system/TimeBase.hpp>

#include <sleipnir/ecs/entity/World.hpp>
#include <sleipnir/ecs/WorldTime.hpp>

#include <cstdint>
#include <memory>
#include <set>

namespace sleipnir
Expand All @@ -30,25 +32,30 @@ class Systems final
//! Bujlt-in priorities for default systems
enum class DefaultPriority : uint16_t
{
Time = 0x0000
, Input = 0x1000
Input = 0x1000
, Lifetime = 0x2000
, Timer = 0x3000
, Physics = 0x4000
, Render = 0x5000
, Audio = 0x6000
};

/** @brief Basic constructor
*
* Registers default systems
* Registers default time systems
*
* @param world entity world
* @param worldTime time holder
*/
Systems(entity::World& world, WorldTime& worldTime);
Systems(WorldTime& worldTime);

//! Basic destructor
~Systems();
//! Default destructor
~Systems() = default;

/** @brief Set time system
*
* @param timeSystem smart poitner to a time system
*/
void SetTimeSystem(std::shared_ptr<system::TimeBase> timeSystem);

/** @brief Add system to the pool
*
Expand Down Expand Up @@ -88,7 +95,9 @@ class Systems final

friend bool operator<(Entry const& lhs, Entry const& rhs)
{
return lhs.priority < rhs.priority;
return (lhs.priority < rhs.priority) ? true
: ((lhs.priority > rhs.priority) ? false
: (lhs.pSystem < rhs.pSystem));
}

friend bool operator==(Entry const& lhs, system::ISystem* pSystem)
Expand All @@ -102,10 +111,7 @@ class Systems final
}
};

struct BuiltInSystems;

BuiltInSystems* m_pBuiltInSystems;

std::shared_ptr<system::TimeBase> m_timeSystem;
std::set<Entry> m_systems;
};

Expand Down
73 changes: 73 additions & 0 deletions SleipnirEngine/ecs/include/sleipnir/ecs/system/TimeBase.hpp
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
* (http://opensource.org/licenses/MIT)
*/

#ifndef SLEIPNIR_ECS_SYSTEM_TIME_HPP
#define SLEIPNIR_ECS_SYSTEM_TIME_HPP
#ifndef SLEIPNIR_ECS_SYSTEM_TIME_WITH_PHYSICS_HPP
#define SLEIPNIR_ECS_SYSTEM_TIME_WITH_PHYSICS_HPP

#include <sleipnir/ecs/WorldTime.hpp>

#include <sleipnir/ecs/system/TimeBase.hpp>
#include <sleipnir/ecs/system/physics/Physics.hpp>

namespace sleipnir
Expand All @@ -23,21 +24,21 @@ namespace system
* In addition to time factor control, this system would throttle time
* whenever physics system is unable to catch up to world time
*/
class Time
class TimeWithPhysics : public TimeBase
{
public:
//! Shortcut to time unit
using TimeUnit = WorldTime::TimeUnit;
using TimeUnit = TimeBase::TimeUnit;

/** @brief Basic constructor
*
* @param worldTime time holder
* @param physicsSystem physics system
*/
Time(WorldTime& worldTime, physics::Physics& physicsSystem);
TimeWithPhysics(WorldTime& worldTime, physics::Physics& physicsSystem);

//! Default destsructor
~Time() = default;
~TimeWithPhysics() = default;

/** @brief Method invoked each loop cycle
*
Expand All @@ -52,33 +53,16 @@ class Time
*
* @return world time duration
*/
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; }

//! Time flow factor
float factor;
virtual TimeUnit Update(TimeUnit realDuration) override;

private:
//! Raw duration of current frame
TimeUnit m_realDuration;

//! World duration of current frame
TimeUnit m_worldDuration;

//! Time holder
WorldTime& m_worldTime;

//! Physics system
physics::Physics& m_physicsSystem;

};

} // namespace system
} // namespace ecs
} // namespace sleipnir

#endif // SLEIPNIR_ECS_SYSTEM_TIME_HPP
#endif // SLEIPNIR_ECS_SYSTEM_TIME_WITH_PHYSICS_HPP
24 changes: 5 additions & 19 deletions SleipnirEngine/ecs/source/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,22 @@

#include <sleipnir/ecs/Systems.hpp>

#include <sleipnir/ecs/system/Time.hpp>
#include <sleipnir/ecs/system/physics/Physics.hpp>

#include <assert.h>

namespace sleipnir
{
namespace ecs
{

struct Systems::BuiltInSystems
Systems::Systems(WorldTime& worldTime)
: m_timeSystem(std::make_shared<system::TimeBase>(worldTime))
{
BuiltInSystems(entity::World& world, WorldTime& worldTime)
: physics(world, worldTime)
, time(worldTime, physics)
{}

system::physics::Physics physics;
system::Time time;
};

Systems::Systems(entity::World& world, WorldTime& worldTime)
: m_pBuiltInSystems(new BuiltInSystems(world, worldTime))
{
Add(&(m_pBuiltInSystems->physics), static_cast<uint16_t>(DefaultPriority::Physics));
}

Systems::~Systems()
void Systems::SetTimeSystem(std::shared_ptr<system::TimeBase> timeSystem)
{
delete m_pBuiltInSystems;
m_timeSystem = timeSystem;
}

void Systems::Add(system::ISystem* pSystem, uint16_t priority)
Expand Down Expand Up @@ -66,7 +52,7 @@ void Systems::Delete(system::ISystem* pSystem)

void Systems::RunOnce(WorldTime::TimeUnit realDuration)
{
m_pBuiltInSystems->time.Update(realDuration);
m_timeSystem->Update(realDuration);

for (Entry const& entry : m_systems)
{
Expand Down
44 changes: 44 additions & 0 deletions SleipnirEngine/ecs/source/system/TimeBase.cpp
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
Loading

0 comments on commit e2f9ae6

Please sign in to comment.