FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_childObjectsAccessor.hpp
1/*
2 * Copyright 2025 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_CHILDOBJECTSACCESSOR_HPP_INCLUDED
18#define _FGE_C_CHILDOBJECTSACCESSOR_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/graphic/C_renderStates.hpp"
22#include "FastEngine/graphic/C_renderTarget.hpp"
23#include <chrono>
24#include <limits>
25#include <memory>
26#include <vector>
27
28namespace fge
29{
30
31using DeltaTime = std::chrono::microseconds;
32
33class Event;
34class Scene;
35class Object;
36class ObjectData;
37using ObjectPtr = std::unique_ptr<fge::Object>;
38using ObjectDataWeak = std::weak_ptr<fge::ObjectData>;
39using ObjectDataShared = std::shared_ptr<fge::ObjectData>;
40using ObjectPlan = uint16_t;
41
42class FGE_API ChildObjectsAccessor
43{
44public:
45 explicit ChildObjectsAccessor(fge::Object* owner);
46 ChildObjectsAccessor([[maybe_unused]] ChildObjectsAccessor const& r) {}
47 ChildObjectsAccessor([[maybe_unused]] ChildObjectsAccessor&& r) noexcept {}
48 ~ChildObjectsAccessor();
49
50 ChildObjectsAccessor& operator=([[maybe_unused]] ChildObjectsAccessor const& r) { return *this; }
51 ChildObjectsAccessor& operator=([[maybe_unused]] ChildObjectsAccessor&& r) noexcept { return *this; }
52
53 void clear();
54 void clearDetachedObjects();
55
56 fge::ObjectDataShared addExistingObject(fge::Object* object,
57 std::size_t insertionIndex = std::numeric_limits<std::size_t>::max());
58 fge::ObjectDataShared addNewObject(fge::ObjectPtr&& newObject,
59 std::size_t insertionIndex = std::numeric_limits<std::size_t>::max());
60
61 fge::ObjectDataShared addExistingDetachedObject(fge::Object* object, fge::ObjectPlan newPlan) const;
62 fge::ObjectDataShared addNewDetachedObject(fge::ObjectPtr&& newObject, fge::ObjectPlan newPlan) const;
63
64 bool detachObject(std::size_t index, fge::ObjectPlan newPlan);
65
66 [[nodiscard]] std::size_t getSize() const;
67 [[nodiscard]] fge::Object const* get(std::size_t index) const;
68 [[nodiscard]] fge::Object* get(std::size_t index);
69 [[nodiscard]] fge::ObjectDataShared getSharedPtr(std::size_t index) const;
70
71 void remove(std::size_t index);
72 void remove(std::size_t first, std::size_t last);
73
74 void sceneUpdate(fge::Scene& scene);
75
76#ifdef FGE_DEF_SERVER
77 void update(fge::Event& event, fge::DeltaTime const& deltaTime, fge::Scene& scene);
78#else
79 void update(fge::RenderTarget& target, fge::Event& event, fge::DeltaTime const& deltaTime, fge::Scene& scene) const;
80 void draw(fge::RenderTarget& target, fge::RenderStates const& states) const;
81#endif //FGE_DEF_SERVER
82
83 void putInFront(std::size_t index);
84 void putInBack(std::size_t index);
85
86 [[nodiscard]] std::size_t getActualIteratedIndex() const;
87
88 [[nodiscard]] std::size_t getIndex(fge::Object* object) const;
89
90private:
91 struct DataContext
92 {
94 {
95 void operator()(fge::ObjectData* data) const;
96 };
97
98 fge::Object* _objPtr;
99 fge::ObjectDataShared _objData;
100 };
101
102 void cleanupDetachedObjects() const;
103
104 mutable std::vector<fge::ObjectDataWeak> g_detachedObjects;
105 std::vector<DataContext> g_data;
106 mutable std::size_t g_actualIteratedIndex{std::numeric_limits<std::size_t>::max()};
107 fge::Object* g_owner{nullptr};
108};
109
110template<class TObject>
111class DeclareChild
112{
113public:
114 template<class TOwner, class... TArgs>
115 constexpr DeclareChild(TOwner* owner, TArgs&&... args) :
116 g_object(std::forward<TArgs>(args)...)
117 {
118 owner->_children.addExistingObject(&this->g_object);
119 }
120
121 [[nodiscard]] constexpr TObject* operator->() { return &this->g_object; }
122 [[nodiscard]] constexpr TObject const* operator->() const { return &this->g_object; }
123
124 [[nodiscard]] constexpr TObject& get() { return this->g_object; }
125 [[nodiscard]] constexpr TObject const& get() const { return this->g_object; }
126
127 template<class TOwner>
128 inline bool detach(TOwner* owner, fge::ObjectPlan newPlan)
129 {
130 return owner->_children.detachObject(owner->_children.getIndex(&this->g_object), newPlan);
131 }
132
133private:
134 TObject g_object;
135};
136
137} // namespace fge
138
139#endif // _FGE_C_CHILDOBJECTSACCESSOR_HPP_INCLUDED
This class is a wrapper for SDL events.
Definition C_event.hpp:59
Data wrapper representing an Object in a Scene.
Definition C_scene.hpp:170
The Object class is the base class for all objects in the engine.
Definition C_object.hpp:102
The RenderStates class contains all the information needed to render something.
Definition C_renderStates.hpp:412
Definition C_renderTarget.hpp:56
A scene contain a collection of object and handle them.
Definition C_scene.hpp:465