FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_object.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_OBJECT_HPP_INCLUDED
18#define _FGE_C_OBJECT_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "C_childObjectsAccessor.hpp"
22#include "FastEngine/C_event.hpp"
23#include "FastEngine/C_propertyList.hpp"
24#include "FastEngine/C_quad.hpp"
25#include "FastEngine/C_rect.hpp"
26#include "FastEngine/C_tagList.hpp"
27#include "FastEngine/graphic/C_drawable.hpp"
28#include "FastEngine/graphic/C_transformable.hpp"
29#include "FastEngine/network/C_networkType.hpp"
30#include "FastEngine/object/C_objectAnchor.hpp"
31#include "FastEngine/vulkan/C_context.hpp"
32#include "json.hpp"
33
34#include <chrono>
35#include <string>
36
37#define FGE_OBJ_BADCLASSNAME "NULL"
38#define FGE_OBJ_NOSCENE nullptr
39#define FGE_OBJ_DEFAULT_COPYMETHOD(objClass) \
40 fge::Object* copy() override \
41 { \
42 return new objClass(*this); \
43 }
44
45#ifdef FGE_DEF_SERVER
46 #define FGE_OBJ_UPDATE_DECLARE \
47 void update(fge::Event& event, fge::DeltaTime const& deltaTime, fge::Scene& scene) override;
48#else
49 #define FGE_OBJ_UPDATE_DECLARE \
50 void update(fge::RenderTarget& target, fge::Event& event, fge::DeltaTime const& deltaTime, fge::Scene& scene) \
51 override;
52#endif //FGE_DEF_SERVER
53
54#ifdef FGE_DEF_SERVER
55 #define FGE_OBJ_UPDATE_BODY(class_) \
56 void class_::update([[maybe_unused]] fge::Event& event, [[maybe_unused]] fge::DeltaTime const& deltaTime, \
57 [[maybe_unused]] fge::Scene& scene)
58
59 #define FGE_OBJ_UPDATE_CALL(object_) object_.update(event, deltaTime, scene)
60 #define FGE_OBJ_UPDATE_PTRCALL(object_) object_->update(event, deltaTime, scene)
61#else
62 #define FGE_OBJ_UPDATE_BODY(class_) \
63 void class_::update([[maybe_unused]] fge::RenderTarget& target, [[maybe_unused]] fge::Event& event, \
64 [[maybe_unused]] fge::DeltaTime const& deltaTime, [[maybe_unused]] fge::Scene& scene)
65
66 #define FGE_OBJ_UPDATE_CALL(object_) object_.update(target, event, deltaTime, scene)
67 #define FGE_OBJ_UPDATE_PTRCALL(object_) object_->update(target, event, deltaTime, scene)
68#endif //FGE_DEF_SERVER
69
70#ifdef FGE_DEF_SERVER
71 #define FGE_OBJ_DRAW_DECLARE
72#else
73 #define FGE_OBJ_DRAW_DECLARE void draw(fge::RenderTarget& target, const fge::RenderStates& states) const override;
74#endif //FGE_DEF_SERVER
75
76#define FGE_OBJ_DRAW_BODY(class_) void class_::draw(fge::RenderTarget& target, const fge::RenderStates& states) const
77
78namespace fge
79{
80
81using DeltaTime = std::chrono::microseconds;
82
84class GuiElement;
85
86class Scene;
87
88class ObjectData;
89using ObjectDataWeak = std::weak_ptr<fge::ObjectData>;
90using ObjectDataShared = std::shared_ptr<fge::ObjectData>;
91
97#ifdef FGE_DEF_SERVER
98class FGE_API Object : public fge::Transformable, public fge::Anchor, public fge::OwnView
99#else
100class FGE_API Object : public fge::Drawable, public fge::Transformable, public fge::Anchor, public fge::OwnView
101#endif //FGE_DEF_SERVER
102{
103public:
104 Object();
105 Object(Object const& r);
106 Object(Object&& r) noexcept;
107 ~Object() override = default;
108
117 virtual fge::Object* copy();
118
124 virtual void first(fge::Scene& scene);
134 virtual void transfered(fge::Scene& oldScene, fge::Scene& newScene);
141 virtual void callbackRegister(fge::Event& event, fge::GuiElementHandler* guiElementHandlerPtr);
150#ifdef FGE_DEF_SERVER
151 virtual void update(fge::Event& event, fge::DeltaTime const& deltaTime, fge::Scene& scene);
152 void update(fge::Event& event, fge::DeltaTime const& deltaTime);
153#else
154 virtual void
155 update(fge::RenderTarget& target, fge::Event& event, fge::DeltaTime const& deltaTime, fge::Scene& scene);
156 void update(fge::RenderTarget& target, fge::Event& event, fge::DeltaTime const& deltaTime);
157#endif //FGE_DEF_SERVER
164#ifndef FGE_DEF_SERVER
165 virtual void draw(fge::RenderTarget& target, fge::RenderStates const& states) const override;
166#endif //FGE_DEF_SERVER
170 virtual void networkRegister();
176 virtual void netSignaled(int8_t signal);
182 virtual void removed(fge::Scene& scene);
183
189 virtual void save(nlohmann::json& jsonObject);
196 virtual void load(nlohmann::json& jsonObject, std::filesystem::path const& filePath);
202 virtual void pack(fge::net::Packet& pck);
208 virtual void unpack(fge::net::Packet const& pck);
209 //TODO: Apply network rules on every extraction method on every objects.
210
216 virtual char const* getClassName() const;
222 virtual char const* getReadableClassName() const;
223
229 [[nodiscard]] virtual fge::RectFloat getGlobalBounds() const;
230 [[nodiscard]] virtual fge::Quad getGlobalQuad() const;
236 [[nodiscard]] virtual fge::RectFloat getLocalBounds() const;
237 [[nodiscard]] virtual fge::Quad getLocalQuad() const;
238
247 bool saveInFile(std::filesystem::path const& path, int fieldWidth = 2, bool saveClassName = true);
255 bool loadFromFile(std::filesystem::path const& path, bool loadClassName = true);
262 [[nodiscard]] static std::unique_ptr<fge::Object> LoadFromFile(std::filesystem::path const& path);
263
270
276 glm::mat4 getParentsTransform() const;
282 fge::Vector2f getParentsScale() const;
287
288 //Data
289
292
293 //Network
294
296
297 //Scene control
298
299 fge::ObjectDataWeak _myObjectData;
300
301 enum class DrawModes : uint8_t
302 {
303 DRAW_IF_ON_TARGET,
304 DRAW_ALWAYS_HIDDEN,
305 DRAW_ALWAYS_DRAWN,
306
307 DRAW_DEFAULT = DRAW_IF_ON_TARGET
308 };
309 DrawModes _drawMode{DrawModes::DRAW_DEFAULT};
310
311 enum class CallbackContextModes : uint8_t
312 {
313 CONTEXT_MANUAL,
314 CONTEXT_AUTO,
315
316 CONTEXT_DEFAULT = CONTEXT_AUTO
317 };
318 CallbackContextModes _callbackContextMode{
319 CallbackContextModes::CONTEXT_DEFAULT};
320
321 enum class NetSyncModes : uint8_t
322 {
323 NO_SYNC,
324 FULL_SYNC,
325 DELTA_SYNC,
326
327#ifdef FGE_DEF_SERVER
328 NETSYNC_DEFAULT = FULL_SYNC
329#else
330 NETSYNC_DEFAULT = NO_SYNC
331#endif //FGE_DEF_SERVER
332 };
333 NetSyncModes _netSyncMode{NetSyncModes::NETSYNC_DEFAULT};
335
336 //Child objects
337
338 enum ChildrenControlFlags : uint8_t
339 {
340 CHILDREN_AUTO_CLEAR_ON_REMOVE = 1 << 0,
341 CHILDREN_AUTO_UPDATE = 1 << 1,
342 CHILDREN_AUTO_DRAW = 1 << 2,
343
344 CHILDREN_DEFAULT = CHILDREN_AUTO_CLEAR_ON_REMOVE
345 };
346 using ChildrenControlFlags_t = std::underlying_type_t<ChildrenControlFlags>;
347
348 ChildrenControlFlags_t _childrenControlFlags{CHILDREN_DEFAULT};
350};
351
352} // namespace fge
353
354#endif // _FGE_C_OBJECT_HPP_INCLUDED
Definition C_objectAnchor.hpp:40
Definition C_childObjectsAccessor.hpp:43
Definition C_drawable.hpp:36
This class is a wrapper for SDL events.
Definition C_event.hpp:59
A class to handle highest priority selection of GUI elements.
Definition C_guiElement.hpp:235
A base class for all GUI elements.
Definition C_guiElement.hpp:120
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
fge::net::NetworkTypeHandler _netList
The network types container of the object.
Definition C_object.hpp:295
fge::Vector2f getParentsScale() const
Retrieve recursively all parents scale by combining them.
CallbackContextModes _callbackContextMode
Tell a scene how the callbackRegister must be called.
Definition C_object.hpp:318
virtual void draw(fge::RenderTarget &target, fge::RenderStates const &states) const override
Method called every frame to draw the object.
virtual fge::Object * copy()
Duplicate the object.
fge::TagList _tags
The tags of the object.
Definition C_object.hpp:290
DrawModes _drawMode
Tell a scene when this object should be drawn.
Definition C_object.hpp:309
virtual void first(fge::Scene &scene)
Method called when the object is added to a scene for initialization purposes.
virtual char const * getClassName() const
Get the unique class name of the object.
virtual void load(nlohmann::json &jsonObject, std::filesystem::path const &filePath)
Load the object from a json object.
virtual char const * getReadableClassName() const
Get a readable version of the class name.
virtual void save(nlohmann::json &jsonObject)
Save the object to a json object.
virtual fge::RectFloat getGlobalBounds() const
Get the global bounds of the object.
fge::ObjectDataWeak _myObjectData
The object data of the object (valid only if the object is in a scene)
Definition C_object.hpp:299
fge::ChildObjectsAccessor _children
An access to child objects of this object.
Definition C_object.hpp:349
virtual void networkRegister()
Register all network types needed by the object.
NetSyncModes _netSyncMode
Tell a scene how the object must be synchronised.
Definition C_object.hpp:333
bool saveInFile(std::filesystem::path const &path, int fieldWidth=2, bool saveClassName=true)
Save the object in a file.
virtual void netSignaled(int8_t signal)
Method called when the object is signaled by the network.
virtual void pack(fge::net::Packet &pck)
Pack the object into a packet.
virtual fge::RectFloat getLocalBounds() const
Get the local bounds of the object (without any transformations)
static std::unique_ptr< fge::Object > LoadFromFile(std::filesystem::path const &path)
Static form of the loadFromFile method.
virtual fge::GuiElement * getGuiElement()
Get the GuiElement attached to this object if there is one.
void centerOriginFromLocalBounds()
Center the origin of the object from its local bounds.
fge::PropertyList _properties
The properties of the object.
Definition C_object.hpp:291
virtual void update(fge::RenderTarget &target, fge::Event &event, fge::DeltaTime const &deltaTime, fge::Scene &scene)
Main method called every frame.
bool loadFromFile(std::filesystem::path const &path, bool loadClassName=true)
Load the object from a file.
virtual void unpack(fge::net::Packet const &pck)
Unpack the object from a packet.
net::Identity _netOwner
The owner of the object.
Definition C_object.hpp:334
virtual void removed(fge::Scene &scene)
Method called when the object is removed from a scene.
ChildrenControlFlags_t _childrenControlFlags
The control flags of the child objects.
Definition C_object.hpp:348
virtual void callbackRegister(fge::Event &event, fge::GuiElementHandler *guiElementHandlerPtr)
Ask the object to register all callbacks it needs to receive events.
glm::mat4 getParentsTransform() const
Retrieve recursively all parents transform by combining them.
virtual void transfered(fge::Scene &oldScene, fge::Scene &newScene)
Method called when the object is transferred from a scene to another.
Definition C_view.hpp:156
A class that map a string to a Property.
Definition C_propertyList.hpp:35
Definition C_quad.hpp:29
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
Definition C_tagList.hpp:28
Definition C_transformable.hpp:34
A regroupment of network types.
Definition C_networkType.hpp:690
Definition C_packet.hpp:52
A class to represent a client or server identity with an IP address and a port.
Definition C_identity.hpp:31