FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_object.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_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, const std::chrono::microseconds& deltaTime, fge::Scene& scene) override;
48#else
49 #define FGE_OBJ_UPDATE_DECLARE \
50 void update(fge::RenderWindow& screen, fge::Event& event, const std::chrono::microseconds& deltaTime, \
51 fge::Scene& scene) 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, \
57 [[maybe_unused]] const std::chrono::microseconds& deltaTime, \
58 [[maybe_unused]] fge::Scene& scene)
59
60 #define FGE_OBJ_UPDATE_CALL(object_) object_.update(event, deltaTime, scene)
61 #define FGE_OBJ_UPDATE_PTRCALL(object_) object_->update(event, deltaTime, scene)
62#else
63 #define FGE_OBJ_UPDATE_BODY(class_) \
64 void class_::update([[maybe_unused]] fge::RenderWindow& screen, [[maybe_unused]] fge::Event& event, \
65 [[maybe_unused]] const std::chrono::microseconds& deltaTime, \
66 [[maybe_unused]] fge::Scene& scene)
67
68 #define FGE_OBJ_UPDATE_CALL(object_) object_.update(screen, event, deltaTime, scene)
69 #define FGE_OBJ_UPDATE_PTRCALL(object_) object_->update(screen, event, deltaTime, scene)
70#endif //FGE_DEF_SERVER
71
72#ifdef FGE_DEF_SERVER
73 #define FGE_OBJ_DRAW_DECLARE
74#else
75 #define FGE_OBJ_DRAW_DECLARE void draw(fge::RenderTarget& target, const fge::RenderStates& states) const override;
76#endif //FGE_DEF_SERVER
77
78#define FGE_OBJ_DRAW_BODY(class_) void class_::draw(fge::RenderTarget& target, const fge::RenderStates& states) const
79
80namespace fge
81{
82
83class GuiElementHandler;
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
99#else
100class FGE_API Object : public fge::Drawable, public fge::Transformable, public fge::Anchor
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, std::chrono::microseconds const& deltaTime, fge::Scene& scene);
152 void update(fge::Event& event, std::chrono::microseconds const& deltaTime);
153#else
154 virtual void
155 update(fge::RenderWindow& screen, fge::Event& event, std::chrono::microseconds const& deltaTime, fge::Scene& scene);
156 void update(fge::RenderWindow& screen, fge::Event& event, std::chrono::microseconds 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
190 virtual void save(nlohmann::json& jsonObject, fge::Scene* scene);
197 virtual void load(nlohmann::json& jsonObject, fge::Scene* scene);
203 virtual void pack(fge::net::Packet& pck);
209 virtual void unpack(fge::net::Packet const& pck);
210 //TODO: Apply network rules on every extraction method on every objects.
211
217 virtual char const* getClassName() const;
223 virtual char const* getReadableClassName() const;
224
230 [[nodiscard]] virtual fge::RectFloat getGlobalBounds() const;
231 [[nodiscard]] virtual fge::Quad getGlobalQuad() const;
237 [[nodiscard]] virtual fge::RectFloat getLocalBounds() const;
238 [[nodiscard]] virtual fge::Quad getLocalQuad() const;
239
246 bool saveInFile(std::string const& path);
253 bool loadFromFile(std::string const& path);
260 static fge::Object* LoadFromFile(std::string const& path);
261
268
274 glm::mat4 getParentsTransform() const;
280 fge::Vector2f getParentsScale() const;
281
282 //Data
283
286
287 //Network
288
290
291 //Scene control
292
293 fge::ObjectDataWeak _myObjectData;
294
295 enum class DrawModes : uint8_t
296 {
297 DRAW_IF_ON_TARGET,
298 DRAW_ALWAYS_HIDDEN,
299 DRAW_ALWAYS_DRAWN,
300
301 DRAW_DEFAULT = DRAW_IF_ON_TARGET
302 };
303 fge::Object::DrawModes _drawMode{
304 fge::Object::DrawModes::DRAW_DEFAULT};
305
306 enum class CallbackContextModes : uint8_t
307 {
308 CONTEXT_MANUAL,
309 CONTEXT_AUTO,
310
311 CONTEXT_DEFAULT = CONTEXT_AUTO
312 };
313 fge::Object::CallbackContextModes _callbackContextMode{
314 fge::Object::CallbackContextModes::
315 CONTEXT_DEFAULT};
316
317 //Child objects
318
319 enum ChildrenControlFlags : uint8_t
320 {
321 CHILDREN_AUTO_CLEAR_ON_REMOVE = 1 << 0,
322 CHILDREN_AUTO_UPDATE = 1 << 1,
323 CHILDREN_AUTO_DRAW = 1 << 2,
324
325 CHILDREN_DEFAULT = CHILDREN_AUTO_CLEAR_ON_REMOVE
326 };
327 using ChildrenControlFlags_t = std::underlying_type_t<ChildrenControlFlags>;
328
329 ChildrenControlFlags_t _childrenControlFlags{CHILDREN_DEFAULT};
331};
332
333} // namespace fge
334
335#endif // _FGE_C_OBJECT_HPP_INCLUDED
Definition C_objectAnchor.hpp:40
Definition C_childObjectsAccessor.hpp:40
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
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:289
fge::Vector2f getParentsScale() const
Retrieve recursively all parents scale by combining them.
bool saveInFile(std::string const &path)
Save the object in a file.
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:284
bool loadFromFile(std::string const &path)
Load the object from a file.
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 char const * getReadableClassName() const
Get a readable version of the class name.
virtual void update(fge::RenderWindow &screen, fge::Event &event, std::chrono::microseconds const &deltaTime, fge::Scene &scene)
Main method called every frame.
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:293
virtual void save(nlohmann::json &jsonObject, fge::Scene *scene)
Save the object to a json object.
fge::ChildObjectsAccessor _children
An access to child objects of this object.
Definition C_object.hpp:330
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.
static fge::Object * LoadFromFile(std::string const &path)
Static form of the loadFromFile method.
virtual fge::RectFloat getLocalBounds() const
Get the local bounds of the object (without any transformations)
virtual fge::GuiElement * getGuiElement()
Get the GuiElement attached to this object if there is one.
fge::PropertyList _properties
The properties of the object.
Definition C_object.hpp:285
virtual void unpack(fge::net::Packet const &pck)
Unpack the object from a packet.
virtual void removed(fge::Scene &scene)
Method called when the object is removed from a scene.
virtual void load(nlohmann::json &jsonObject, fge::Scene *scene)
Load the object from a json object.
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.
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
Definition C_renderWindow.hpp:51
A scene contain a collection of object and handle them.
Definition C_scene.hpp:450
Definition C_tagList.hpp:28
Definition C_transformable.hpp:34
A regroupment of network types.
Definition C_networkType.hpp:590
Definition C_packet.hpp:70