diff --git a/headers/enduro2d/high/_all.hpp b/headers/enduro2d/high/_all.hpp index 8fdf9090..64424fa1 100644 --- a/headers/enduro2d/high/_all.hpp +++ b/headers/enduro2d/high/_all.hpp @@ -42,7 +42,7 @@ #include "systems/flipbook_system.hpp" #include "systems/label_system.hpp" #include "systems/render_system.hpp" -#include "systems/spine_systems.hpp" +#include "systems/spine_system.hpp" #include "address.hpp" #include "asset.hpp" diff --git a/headers/enduro2d/high/_high.hpp b/headers/enduro2d/high/_high.hpp index 2e239c2f..64529eda 100644 --- a/headers/enduro2d/high/_high.hpp +++ b/headers/enduro2d/high/_high.hpp @@ -54,8 +54,7 @@ namespace e2d class flipbook_system; class label_system; class render_system; - class spine_pre_system; - class spine_post_system; + class spine_system; template < typename Asset, typename Content > class content_asset; diff --git a/headers/enduro2d/high/systems/_systems.hpp b/headers/enduro2d/high/systems/_systems.hpp new file mode 100644 index 00000000..4be97855 --- /dev/null +++ b/headers/enduro2d/high/systems/_systems.hpp @@ -0,0 +1,37 @@ +/******************************************************************************* + * This file is part of the "Enduro2D" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#pragma once + +#include "../_high.hpp" + +namespace e2d::systems +{ + struct update_event { + f32 dt{0.f}; + f32 time{0.f}; + }; + + struct pre_update_event { + f32 dt{0.f}; + f32 time{0.f}; + }; + + struct post_update_event { + f32 dt{0.f}; + f32 time{0.f}; + }; + + struct render_event {}; + struct pre_render_event {}; + struct post_render_event {}; + + using update_system = ecs::system; + using post_update_system = ecs::system; + + using render_system = ecs::system; + using post_render_system = ecs::system; +} diff --git a/headers/enduro2d/high/systems/flipbook_system.hpp b/headers/enduro2d/high/systems/flipbook_system.hpp index 1f9d383b..740ac909 100644 --- a/headers/enduro2d/high/systems/flipbook_system.hpp +++ b/headers/enduro2d/high/systems/flipbook_system.hpp @@ -6,15 +6,19 @@ #pragma once -#include "../_high.hpp" +#include "_systems.hpp" namespace e2d { - class flipbook_system final : public ecs::system { + class flipbook_system final + : public ecs::system> { public: flipbook_system(); - ~flipbook_system() noexcept final; - void process(ecs::registry& owner) override; + ~flipbook_system() noexcept; + + void process( + ecs::registry& owner, + const ecs::after& trigger) override; private: class internal_state; std::unique_ptr state_; diff --git a/headers/enduro2d/high/systems/label_system.hpp b/headers/enduro2d/high/systems/label_system.hpp index 89928a5e..57d672c5 100644 --- a/headers/enduro2d/high/systems/label_system.hpp +++ b/headers/enduro2d/high/systems/label_system.hpp @@ -6,15 +6,19 @@ #pragma once -#include "../_high.hpp" +#include "_systems.hpp" namespace e2d { - class label_system final : public ecs::system { + class label_system final + : public ecs::system> { public: label_system(); ~label_system() noexcept final; - void process(ecs::registry& owner) override; + + void process( + ecs::registry& owner, + const ecs::after& trigger) override; private: class internal_state; std::unique_ptr state_; diff --git a/headers/enduro2d/high/systems/render_system.hpp b/headers/enduro2d/high/systems/render_system.hpp index 60f11963..c8cb2d74 100644 --- a/headers/enduro2d/high/systems/render_system.hpp +++ b/headers/enduro2d/high/systems/render_system.hpp @@ -6,15 +6,19 @@ #pragma once -#include "../_high.hpp" +#include "_systems.hpp" namespace e2d { - class render_system final : public ecs::system { + class render_system final + : public ecs::system> { public: render_system(); - ~render_system() noexcept final; - void process(ecs::registry& owner) override; + ~render_system() noexcept; + + void process( + ecs::registry& owner, + const ecs::after& trigger) override; private: class internal_state; std::unique_ptr state_; diff --git a/headers/enduro2d/high/systems/spine_system.hpp b/headers/enduro2d/high/systems/spine_system.hpp new file mode 100644 index 00000000..d3c4fd4a --- /dev/null +++ b/headers/enduro2d/high/systems/spine_system.hpp @@ -0,0 +1,26 @@ +/******************************************************************************* + * This file is part of the "Enduro2D" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#pragma once + +#include "_systems.hpp" + +namespace e2d +{ + class spine_system final + : public ecs::system> { + public: + spine_system(); + ~spine_system() noexcept; + + void process( + ecs::registry& owner, + const ecs::after& trigger) override; + private: + class internal_state; + std::unique_ptr state_; + }; +} diff --git a/headers/enduro2d/high/systems/spine_systems.hpp b/headers/enduro2d/high/systems/spine_systems.hpp deleted file mode 100644 index c13844a8..00000000 --- a/headers/enduro2d/high/systems/spine_systems.hpp +++ /dev/null @@ -1,32 +0,0 @@ -/******************************************************************************* - * This file is part of the "Enduro2D" - * For conditions of distribution and use, see copyright notice in LICENSE.md - * Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com) - ******************************************************************************/ - -#pragma once - -#include "../_high.hpp" - -namespace e2d -{ - class spine_pre_system final : public ecs::system { - public: - spine_pre_system(); - ~spine_pre_system() noexcept final; - void process(ecs::registry& owner) override; - private: - class internal_state; - std::unique_ptr state_; - }; - - class spine_post_system final : public ecs::system { - public: - spine_post_system(); - ~spine_post_system() noexcept final; - void process(ecs::registry& owner) override; - private: - class internal_state; - std::unique_ptr state_; - }; -} diff --git a/headers/enduro2d/high/world.hpp b/headers/enduro2d/high/world.hpp index f894f740..928f335e 100644 --- a/headers/enduro2d/high/world.hpp +++ b/headers/enduro2d/high/world.hpp @@ -14,20 +14,6 @@ namespace e2d { class world final : public module { - public: - enum priorities : ecs::priority_t { - priority_update_section_begin = 0, - priority_pre_update = 500, - priority_update = 1000, - priority_post_update = 1500, - priority_update_section_end = 2000, - - priority_render_section_begin = 2500, - priority_pre_render = 3000, - priority_render = 3500, - priority_post_render = 4000, - priority_render_section_end = 4500 - }; public: world() = default; ~world() noexcept final; diff --git a/samples/sources/sample_03/sample_03.cpp b/samples/sources/sample_03/sample_03.cpp index 77d68f29..66babb27 100644 --- a/samples/sources/sample_03/sample_03.cpp +++ b/samples/sources/sample_03/sample_03.cpp @@ -13,10 +13,13 @@ namespace v3f axis; }; - class game_system final : public ecs::system { + class game_system final : public systems::update_system { public: - void process(ecs::registry& owner) override { - E2D_UNUSED(owner); + void process( + ecs::registry& owner, + const systems::update_event& event) override + { + E2D_UNUSED(owner, event); const keyboard& k = the().keyboard(); if ( k.is_key_just_released(keyboard_key::f12) ) { @@ -33,9 +36,13 @@ namespace } }; - class camera_system final : public ecs::system { + class camera_system final : public systems::render_system { public: - void process(ecs::registry& owner) override { + void process( + ecs::registry& owner, + const systems::render_event& event) override + { + E2D_UNUSED(event); owner.for_joined_components( [](const ecs::const_entity&, camera& cam){ if ( !cam.target() ) { @@ -48,18 +55,20 @@ namespace } }; - class rotator_system final : public ecs::system { + class rotator_system final : public systems::update_system { public: - void process(ecs::registry& owner) override { - const f32 time = the().time(); + void process( + ecs::registry& owner, + const systems::update_event& event) override + { owner.for_joined_components( - [&time](const ecs::const_entity&, const rotator& rot, actor& act){ - const node_iptr node = act.node(); - if ( node ) { - const q4f q = math::make_quat_from_axis_angle(make_rad(time), rot.axis); - node->rotation(q); - } - }); + [&event](const ecs::const_entity&, const rotator& rot, actor& act){ + const node_iptr node = act.node(); + if ( node ) { + const q4f q = math::make_quat_from_axis_angle(make_rad(event.time), rot.axis); + node->rotation(q); + } + }); } }; @@ -154,9 +163,10 @@ namespace bool create_systems() { ecs::registry_filler(the().registry()) - .system(world::priority_update) - .system(world::priority_update) - .system(world::priority_pre_render); + .feature(ecs::feature() + .add_system() + .add_system() + .add_system()); return true; } }; diff --git a/samples/sources/sample_04/sample_04.cpp b/samples/sources/sample_04/sample_04.cpp index 435f48ef..c334f89e 100644 --- a/samples/sources/sample_04/sample_04.cpp +++ b/samples/sources/sample_04/sample_04.cpp @@ -9,10 +9,13 @@ using namespace e2d; namespace { - class game_system final : public ecs::system { + class game_system final : public systems::update_system { public: - void process(ecs::registry& owner) override { - E2D_UNUSED(owner); + void process( + ecs::registry& owner, + const systems::update_event& event) override + { + E2D_UNUSED(owner, event); const keyboard& k = the().keyboard(); if ( k.is_key_just_released(keyboard_key::f12) ) { @@ -29,9 +32,13 @@ namespace } }; - class camera_system final : public ecs::system { + class camera_system final : public systems::render_system { public: - void process(ecs::registry& owner) override { + void process( + ecs::registry& owner, + const systems::render_event& event) override + { + E2D_UNUSED(event); owner.for_joined_components( [](const ecs::const_entity&, camera& cam){ if ( !cam.target() ) { @@ -61,8 +68,9 @@ namespace bool create_systems() { ecs::registry_filler(the().registry()) - .system(world::priority_update) - .system(world::priority_pre_render); + .feature(ecs::feature() + .add_system() + .add_system()); return true; } }; diff --git a/samples/sources/sample_06/sample_06.cpp b/samples/sources/sample_06/sample_06.cpp index c8c64dfb..d0f1ac62 100644 --- a/samples/sources/sample_06/sample_06.cpp +++ b/samples/sources/sample_06/sample_06.cpp @@ -9,10 +9,13 @@ using namespace e2d; namespace { - class game_system final : public ecs::system { + class game_system final : public systems::update_system { public: - void process(ecs::registry& owner) override { - E2D_UNUSED(owner); + void process( + ecs::registry& owner, + const systems::update_event& event) override + { + E2D_UNUSED(owner, event); const keyboard& k = the().keyboard(); if ( k.is_key_just_released(keyboard_key::f12) ) { @@ -68,9 +71,13 @@ namespace } }; - class camera_system final : public ecs::system { + class camera_system final : public systems::render_system { public: - void process(ecs::registry& owner) override { + void process( + ecs::registry& owner, + const systems::render_event& event) override + { + E2D_UNUSED(event); owner.for_joined_components( [](const ecs::const_entity&, camera& cam){ if ( !cam.target() ) { @@ -100,8 +107,9 @@ namespace bool create_systems() { ecs::registry_filler(the().registry()) - .system(world::priority_update) - .system(world::priority_pre_render); + .feature(ecs::feature() + .add_system() + .add_system()); return true; } }; diff --git a/sources/enduro2d/core/render_impl/render_opengl_base.hpp b/sources/enduro2d/core/render_impl/render_opengl_base.hpp index 0f42de90..be110117 100644 --- a/sources/enduro2d/core/render_impl/render_opengl_base.hpp +++ b/sources/enduro2d/core/render_impl/render_opengl_base.hpp @@ -12,7 +12,6 @@ #if E2D_RENDER_MODE == E2D_RENDER_MODE_OPENGL || E2D_RENDER_MODE == E2D_RENDER_MODE_OPENGLES #if E2D_RENDER_MODE == E2D_RENDER_MODE_OPENGL -# define GLEW_STATIC # include #elif E2D_RENDER_MODE == E2D_RENDER_MODE_OPENGLES # include diff --git a/sources/enduro2d/high/starter.cpp b/sources/enduro2d/high/starter.cpp index 8cc67463..0a153a04 100644 --- a/sources/enduro2d/high/starter.cpp +++ b/sources/enduro2d/high/starter.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include namespace { @@ -45,11 +45,14 @@ namespace bool initialize() final { ecs::registry_filler(the().registry()) - .system(world::priority_pre_update) - .system(world::priority_pre_update) - .system(world::priority_pre_update) - .system(world::priority_post_update) - .system(world::priority_render); + .feature(ecs::feature() + .add_system()) + .feature(ecs::feature() + .add_system()) + .feature(ecs::feature() + .add_system()) + .feature(ecs::feature() + .add_system()); return !application_ || application_->initialize(); } @@ -60,17 +63,24 @@ namespace } bool frame_tick() final { - the().registry().process_systems_in_range( - world::priority_update_section_begin, - world::priority_update_section_end); + engine& e = the(); + const f32 dt = e.delta_time(); + const f32 time = e.time(); + + ecs::registry& registry = the().registry(); + registry.process_event(systems::pre_update_event{dt,time}); + registry.process_event(systems::update_event{dt,time}); + registry.process_event(systems::post_update_event{dt,time}); + return !the().should_close() || (application_ && !application_->on_should_close()); } void frame_render() final { - the().registry().process_systems_in_range( - world::priority_render_section_begin, - world::priority_render_section_end); + ecs::registry& registry = the().registry(); + registry.process_event(systems::pre_render_event{}); + registry.process_event(systems::render_event{}); + registry.process_event(systems::post_render_event{}); } private: starter::application_uptr application_; diff --git a/sources/enduro2d/high/systems/flipbook_system.cpp b/sources/enduro2d/high/systems/flipbook_system.cpp index 5877ec3c..b1aa8067 100644 --- a/sources/enduro2d/high/systems/flipbook_system.cpp +++ b/sources/enduro2d/high/systems/flipbook_system.cpp @@ -82,8 +82,8 @@ namespace e2d internal_state() = default; ~internal_state() noexcept = default; - void process(ecs::registry& owner) { - update_flipbook_timers(the().delta_time(), owner); + void process(f32 dt, ecs::registry& owner) { + update_flipbook_timers(dt, owner); update_flipbook_sprites(owner); } }; @@ -96,7 +96,10 @@ namespace e2d : state_(new internal_state()) {} flipbook_system::~flipbook_system() noexcept = default; - void flipbook_system::process(ecs::registry& owner) { - state_->process(owner); + void flipbook_system::process( + ecs::registry& owner, + const ecs::after& trigger) + { + state_->process(trigger.event.dt, owner); } } diff --git a/sources/enduro2d/high/systems/label_system.cpp b/sources/enduro2d/high/systems/label_system.cpp index 2be21be9..c3dceaa2 100644 --- a/sources/enduro2d/high/systems/label_system.cpp +++ b/sources/enduro2d/high/systems/label_system.cpp @@ -382,7 +382,11 @@ namespace e2d : state_(new internal_state()) {} label_system::~label_system() noexcept = default; - void label_system::process(ecs::registry& owner) { + void label_system::process( + ecs::registry& owner, + const ecs::after& trigger) + { + E2D_UNUSED(trigger); state_->process(owner); } } diff --git a/sources/enduro2d/high/systems/render_system.cpp b/sources/enduro2d/high/systems/render_system.cpp index 9be52781..b6c313ee 100644 --- a/sources/enduro2d/high/systems/render_system.cpp +++ b/sources/enduro2d/high/systems/render_system.cpp @@ -117,7 +117,11 @@ namespace e2d : state_(new internal_state()) {} render_system::~render_system() noexcept = default; - void render_system::process(ecs::registry& owner) { + void render_system::process( + ecs::registry& owner, + const ecs::after& trigger) + { + E2D_UNUSED(trigger); state_->process(owner); } } diff --git a/sources/enduro2d/high/systems/spine_pre_system.cpp b/sources/enduro2d/high/systems/spine_pre_system.cpp deleted file mode 100644 index 16c26d0e..00000000 --- a/sources/enduro2d/high/systems/spine_pre_system.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/******************************************************************************* - * This file is part of the "Enduro2D" - * For conditions of distribution and use, see copyright notice in LICENSE.md - * Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com) - ******************************************************************************/ - -#include - -#include -#include -#include - -#include - -namespace e2d -{ - // - // internal_state - // - - class spine_pre_system::internal_state final { - public: - internal_state() = default; - ~internal_state() noexcept = default; - - void process(ecs::registry& owner) { - update_animations(owner); - } - private: - void update_animations(ecs::registry& owner) { - const float dt = the().delta_time(); - owner.for_each_component([dt]( - const ecs::const_entity&, - spine_player& p) - { - spSkeleton* skeleton = p.skeleton().get(); - spAnimationState* anim_state = p.animation().get(); - - if ( !skeleton || !anim_state ) { - return; - } - - spSkeleton_update(skeleton, dt); - spAnimationState_update(anim_state, dt); - spAnimationState_apply(anim_state, skeleton); - spSkeleton_updateWorldTransform(skeleton); - }); - } - }; - - // - // spine_pre_system - // - - spine_pre_system::spine_pre_system() - : state_(new internal_state()) {} - spine_pre_system::~spine_pre_system() noexcept = default; - - void spine_pre_system::process(ecs::registry& owner) { - state_->process(owner); - } -} diff --git a/sources/enduro2d/high/systems/spine_post_system.cpp b/sources/enduro2d/high/systems/spine_system.cpp similarity index 75% rename from sources/enduro2d/high/systems/spine_post_system.cpp rename to sources/enduro2d/high/systems/spine_system.cpp index d6f754ab..0f14bdab 100644 --- a/sources/enduro2d/high/systems/spine_post_system.cpp +++ b/sources/enduro2d/high/systems/spine_system.cpp @@ -4,7 +4,7 @@ * Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com) ******************************************************************************/ -#include +#include #include #include @@ -166,6 +166,59 @@ namespace spSkeletonData& skeleton_data_; spAnimationState& animation_state_; }; + + void clear_events(ecs::registry& owner) { + owner.for_each_component([ + ](const ecs::const_entity&, spine_player_evt& pe) { + pe.clear_events(); + }); + } + + void update_animations(f32 dt, ecs::registry& owner) { + owner.for_each_component([dt]( + const ecs::const_entity&, + spine_player& p) + { + spSkeleton* skeleton = p.skeleton().get(); + spAnimationState* anim_state = p.animation().get(); + + if ( !skeleton || !anim_state ) { + return; + } + + spSkeleton_update(skeleton, dt); + spAnimationState_update(anim_state, dt); + spAnimationState_apply(anim_state, skeleton); + spSkeleton_updateWorldTransform(skeleton); + }); + } + + void process_commands(ecs::registry& owner) { + owner.for_joined_components([]( + ecs::entity e, + const spine_player_cmd& pc, + spine_player& p) + { + spSkeleton* skeleton = p.skeleton().get(); + spAnimationState* animation_state = p.animation().get(); + + if ( !skeleton || !skeleton->data || !animation_state ) { + return; + } + + command_visitor v(e, *skeleton->data, *animation_state); + for ( const auto& cmd : pc.commands() ) { + std::visit(v, cmd); + } + }); + } + + void clear_commands(ecs::registry& owner) { + owner.for_each_component([ + ](const ecs::const_entity&, spine_player_cmd& pc) { + pc.clear_commands(); + }); + } } namespace e2d @@ -174,64 +227,34 @@ namespace e2d // internal_state // - class spine_post_system::internal_state final { + class spine_system::internal_state final { public: internal_state() = default; ~internal_state() noexcept = default; - void process(ecs::registry& owner) { + void process(f32 dt, ecs::registry& owner) { process_commands(owner); clear_commands(owner); clear_events(owner); - } - private: - void process_commands(ecs::registry& owner) { - owner.for_joined_components([]( - ecs::entity e, - const spine_player_cmd& pc, - spine_player& p) - { - spSkeleton* skeleton = p.skeleton().get(); - spAnimationState* animation_state = p.animation().get(); - - if ( !skeleton || !skeleton->data || !animation_state ) { - return; - } - - command_visitor v(e, *skeleton->data, *animation_state); - for ( const auto& cmd : pc.commands() ) { - std::visit(v, cmd); - } - }); - } - - void clear_commands(ecs::registry& owner) { - owner.for_each_component([ - ](const ecs::const_entity&, spine_player_cmd& pc) { - pc.clear_commands(); - }); - } - - void clear_events(ecs::registry& owner) { - owner.for_each_component([ - ](const ecs::const_entity&, spine_player_evt& pe) { - pe.clear_events(); - }); + update_animations(dt, owner); } }; // - // spine_post_system + // spine_system // - spine_post_system::spine_post_system() + spine_system::spine_system() : state_(new internal_state()) {} - spine_post_system::~spine_post_system() noexcept { + spine_system::~spine_system() noexcept { spAnimationState_disposeStatics(); } - void spine_post_system::process(ecs::registry& owner) { - state_->process(owner); + void spine_system::process( + ecs::registry& owner, + const ecs::after& trigger) + { + state_->process(trigger.event.dt, owner); } }