11#include " Application/Application.hpp"
2+ #include " Entities/System.hpp"
23#include " Files/Model.hpp"
34#include " Graphics/Gl/Viewport.hpp"
45#include " Graphics/Renderer.hpp"
@@ -10,17 +11,38 @@ using namespace Pixf::Core::Math;
1011using namespace Pixf ::Core::Graphics;
1112using namespace Pixf ::Core::Debug;
1213
14+ struct Backpack {
15+ Matrix4f transform{};
16+ Model backpack;
17+ float dist = 0 .0F ;
18+ float rot = 0 .0F ;
19+ };
20+
21+ struct BackpackSystem final : Entities::System {
22+ void Update (Entities::Registry ®istry, const double deltaTime) override {
23+ registry.ForEach <Backpack>([deltaTime](Backpack &backpack) {
24+ backpack.dist += 5 .0F * deltaTime;
25+ backpack.rot += 5 .0F * deltaTime;
26+
27+ backpack.transform = Matrix4f::Identity ();
28+ backpack.transform .ApplyRotation (backpack.rot , Vector3f::Up ());
29+ backpack.transform .ApplyTranslation (Vector3f (0 .0F , 0 .0F , backpack.dist ));
30+ });
31+ }
32+ };
33+
1334class RenderStage final : public Application::Stage {
1435public:
15- void OnAttach (Application::State &state) override { backpack = Files::LoadModel (" backpack.obj" , state.resources ); }
36+ void OnAttach (Application::State &state) override {
37+ const auto entity = state.entityRegistry .CreateEntity ();
38+ const Backpack backpack{.backpack = Files::LoadModel (" backpack.obj" , state.resources )};
1639
17- void Update (Application::State & state, const double deltaTime) override {
18- dist += 5 . 0F * deltaTime ;
19- rot += 5 . 0F * deltaTime;
40+ state. entityRegistry . AddComponent (entity, backpack);
41+ state. systemRegistry . Register <BackpackSystem>() ;
42+ }
2043
21- transform = Matrix4f::Identity ();
22- transform.ApplyRotation (rot, Vector3f::Up ());
23- transform.ApplyTranslation (Vector3f (0 .0F , 0 .0F , dist));
44+ void Update (Application::State &state, const double deltaTime) override {
45+ state.systemRegistry .Update (state.entityRegistry , deltaTime);
2446 }
2547
2648 void Render (Application::State &state, double deltaTime) override {
@@ -29,18 +51,14 @@ class RenderStage final : public Application::Stage {
2951 Renderer &renderer = state.renderer ;
3052 renderer.BeginPass ({.projectionMatrix = Matrix4f::Perspective (60 .0F , 4 .0F / 3 .0F , 0 .1F , 100 .0F )});
3153
32- for (auto &[mesh, material]: backpack.elements ) {
33- renderer.Submit ({.mesh = mesh, .material = material, .modelMatrix = transform});
34- }
54+ state.entityRegistry .ForEach <Backpack>([&renderer](Backpack &backpack) {
55+ for (auto &[mesh, material]: backpack.backpack .elements ) {
56+ renderer.Submit ({.mesh = mesh, .material = material, .modelMatrix = backpack.transform });
57+ }
58+ });
3559
3660 renderer.Render (viewport, state.resources );
3761 }
38-
39- private:
40- Matrix4f transform;
41- Model backpack;
42- float dist = 0 .0F ;
43- float rot = 0 .0F ;
4462};
4563
4664class App final : public Application::Application {
0 commit comments