FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_task.hpp
1/*
2 * Copyright 2024 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_TASK_HPP_INCLUDED
18#define _FGE_C_TASK_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/C_event.hpp"
22#include "FastEngine/manager/task_manager.hpp"
23#include "FastEngine/network/C_networkType.hpp"
24#include <optional>
25#include <vector>
26
27#define FGE_TASK_DEFAULT_GETTER(type_) \
28 [[nodiscard]] fge::TaskTypeIndex getTypeIndex() const override \
29 { \
30 return fge::task::GetTaskIndex(typeid(type_)).value(); \
31 } \
32 [[nodiscard]] static fge::TaskTypeIndex GetTypeIndex() \
33 { \
34 return fge::task::GetTaskIndex(typeid(type_)).value(); \
35 }
36
37namespace fge
38{
39
40class Object;
41
42using TasksChecksum = uint16_t;
43using TaskTypeIndex = uint16_t;
44
45enum class TaskResult
46{
47 TASK_RESULT_ERROR,
48
49 TASK_RESULT_UNFINISHED,
50 TASK_RESULT_FINISHED,
51 TASK_RESULT_SUBTASK_REQUIRED
52};
53
54class TaskHandler;
55
65{
66public:
67 enum SyncType : uint8_t
68 {
69 SYNC_CHECKSUM,
70 SYNC_FULL
71 };
72
73 explicit NetworkTypeTasks(fge::TaskHandler* source);
74 ~NetworkTypeTasks() override = default;
75
76 void const* getSource() const override;
77
78 bool applyData(fge::net::Packet const& pck) override;
79 void packData(fge::net::Packet& pck, fge::net::Identity const& id) override;
80 void packData(fge::net::Packet& pck) override;
81
82 bool check() const override;
83 void forceCheck() override;
84 void forceUncheck() override;
85
86private:
87 fge::TaskHandler* g_tasksSource;
88 fge::TasksChecksum g_checksumCopy;
89};
90
104class FGE_API Task
105{
106public:
107 Task() = default;
108 virtual ~Task() = default;
109
131 virtual fge::TaskResult update(fge::TaskHandler& taskHandler,
132 fge::Event& event,
133 std::chrono::microseconds const& deltaTime,
134 fge::Scene* scenePtr) = 0;
135
144 [[nodiscard]] virtual fge::TaskTypeIndex getTypeIndex() const = 0;
150 [[nodiscard]] virtual std::string_view getStringStatus() const = 0;
151
157 [[nodiscard]] inline float getProgression() const { return this->_g_progress; }
158
164 virtual void pack(fge::net::Packet& pck) = 0;
170 virtual void unpackAndInit(fge::net::Packet const& pck) = 0;
171
179 [[nodiscard]] inline fge::Object* getParentObject() const { return this->g_parentObject; }
180 template<class T>
181 [[nodiscard]] inline T* getParentObject() const
182 {
183 return reinterpret_cast<T*>(this->g_parentObject);
184 }
185
186private:
187 friend class TaskHandler;
188 friend class NetworkTypeTasks;
189 inline void setParentObject(fge::Object* parentObject) { this->g_parentObject = parentObject; }
190
191 fge::Object* g_parentObject{nullptr};
192
193protected:
194 float _g_progress{0.0f};
195};
196
197using TaskList = std::vector<std::unique_ptr<fge::Task>>;
198
207class FGE_API TaskHandler
208{
209public:
210 TaskHandler() = default;
211 TaskHandler(TaskHandler const& r) = delete;
212 TaskHandler(TaskHandler&& r) noexcept;
213 ~TaskHandler() = default;
214
215 TaskHandler& operator=(TaskHandler const& r) = delete;
216 TaskHandler& operator=(TaskHandler&& r) noexcept = delete;
217
225 void setParentObject(fge::Object& parentObject);
226 [[nodiscard]] fge::Object* getParentObject() const;
227
228 [[nodiscard]] std::size_t getTaskSize() const;
229
239 template<class T = fge::Task>
240 T* setMainTask(std::unique_ptr<T>&& newTask);
241 template<class T>
242 T* setMainTask();
243 template<class T, class... TArgs>
244 T* setMainTaskAndInit(TArgs&&... args);
254 template<class T = fge::Task>
255 T* addSubTask(std::unique_ptr<T>&& newTask);
256 template<class T>
257 T* addSubTask();
258 template<class T, class... TArgs>
259 T* addSubTaskAndInit(TArgs&&... args);
260
261 [[nodiscard]] fge::Task* getMainTask() const;
262 [[nodiscard]] fge::Task* getActualTask() const;
263 [[nodiscard]] std::optional<fge::TaskTypeIndex> getActualTaskType() const;
264 void popTask();
265 void clearTasks();
266
267 [[nodiscard]] fge::TaskList const& getTasks() const;
268
269 [[nodiscard]] std::optional<fge::TaskTypeIndex> getLastTask() const;
270 void clearLastTask();
271
281
282 [[nodiscard]] fge::TasksChecksum getChecksum() const;
283
285
286private:
287 void computeChecksum();
288
289 fge::Object* g_parentObject{nullptr};
290 fge::TaskList g_tasks;
291 std::optional<fge::TaskTypeIndex> g_lastTask;
292 fge::TasksChecksum g_tasksChecksum{0};
293};
294
295} // namespace fge
296
297#include "FastEngine/C_task.inl"
298
299#endif // _FGE_C_TASK_HPP_INCLUDED
This class is used to handle callbacks in a safe way.
Definition C_callback.hpp:189
This class is a wrapper for SDL events.
Definition C_event.hpp:59
Network type for the TaskHandler.
Definition C_task.hpp:65
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
bool check() const override
Check if the value have been modified.
void forceCheck() override
Force the value to be modified (even if it is not)
void const * getSource() const override
Get the source pointer that have been used to create this network type.
void packData(fge::net::Packet &pck) override
Pack the data without any client identity.
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
void forceUncheck() override
Remove the forced modification of the value.
The Object class is the base class for all objects in the engine.
Definition C_object.hpp:102
A scene contain a collection of object and handle them.
Definition C_scene.hpp:450
Handle the tasks of an object.
Definition C_task.hpp:208
void setParentObject(fge::Object &parentObject)
Set the parent object of the TaskHandler.
fge::CallbackHandler< TaskHandler & > _onMainTaskChanged
Called when the main task is changed.
Definition C_task.hpp:284
void networkRegister(fge::net::NetworkTypeHandler &netList)
Register the network types of the tasks.
Base class for all tasks.
Definition C_task.hpp:105
virtual void pack(fge::net::Packet &pck)=0
Pack the task data into a packet.
virtual fge::TaskTypeIndex getTypeIndex() const =0
Get the type index of the task.
fge::Object * getParentObject() const
Get the parent object of the task.
Definition C_task.hpp:179
virtual void unpackAndInit(fge::net::Packet const &pck)=0
Unpack the task data from a packet and initialize the task.
virtual std::string_view getStringStatus() const =0
Get the custom status of the task as a string.
virtual fge::TaskResult update(fge::TaskHandler &taskHandler, fge::Event &event, std::chrono::microseconds const &deltaTime, fge::Scene *scenePtr)=0
Update the task.
float getProgression() const
Get the progression of the task as a percentage.
Definition C_task.hpp:157
Base class for a network type.
Definition C_networkType.hpp:70
A regroupment of network types.
Definition C_networkType.hpp:590
Definition C_packet.hpp:70
A class to represent a client or server identity with an IP address and a port.
Definition C_identity.hpp:31