FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_objSpriteBatches.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_OBJSPRITEBATCHES_HPP_INCLUDED
18#define _FGE_C_OBJSPRITEBATCHES_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "C_object.hpp"
22#include "FastEngine/accessor/C_texture.hpp"
23#include "FastEngine/extra/extra_function.hpp"
24
25#define FGE_OBJSPRITEBATCHES_CLASSNAME "FGE:OBJ:SPRITEBATCHES"
26
27#define FGE_OBJSPRITEBATCHES_SHADER_VERTEX "FGE:OBJ:SPRITEBATCHES:VERTEX"
28#define FGE_OBJSPRITEBATCHES_SHADER_FRAGMENT "FGE:OBJ:SPRITEBATCHES:FRAGMENT"
29
30#define FGE_OBJSPRITEBATCHES_VERTEX_COUNT 4
31
32#define FGE_OBJSPRITEBATCHES_MAXIMUM_TEXTURES FGE_MULTIUSE_POOL_MAX_COMBINED_IMAGE_SAMPLER
33
34namespace fge
35{
36
37class FGE_API ObjSpriteBatches : public fge::Object
38{
39public:
42 explicit ObjSpriteBatches(fge::Texture texture);
43
44 FGE_OBJ_DEFAULT_COPYMETHOD(fge::ObjSpriteBatches)
45
46 void addTexture(fge::Texture texture);
47 void setTexture(std::size_t index, fge::Texture texture);
48 void setTexture(fge::Texture texture);
49 fge::Texture const& getTexture(std::size_t index) const;
50 std::size_t getTextureCount() const;
51 void clearTexture();
52
53 void clear();
54 fge::Transformable& addSprite(fge::RectInt const& rectangle, uint32_t textureIndex = 0);
55 void resize(std::size_t size);
56 void setTextureRect(std::size_t index, fge::RectInt const& rectangle);
57 void setColor(std::size_t index, fge::Color const& color);
58 void setSpriteTexture(std::size_t spriteIndex, uint32_t textureIndex);
59 [[nodiscard]] std::size_t getSpriteCount() const;
60
61 [[nodiscard]] std::optional<fge::RectInt> getTextureRect(std::size_t index) const;
62 [[nodiscard]] std::optional<fge::Color> getColor(std::size_t index) const;
63
64 [[nodiscard]] fge::Transformable* getTransformable(std::size_t index);
65 [[nodiscard]] fge::Transformable const* getTransformable(std::size_t index) const;
66
67 FGE_OBJ_DRAW_DECLARE
68
69 void save(nlohmann::json& jsonObject, fge::Scene* scene) override;
70 void load(nlohmann::json& jsonObject, fge::Scene* scene) override;
71 void pack(fge::net::Packet& pck) override;
72 void unpack(fge::net::Packet const& pck) override;
73
74 char const* getClassName() const override;
75 char const* getReadableClassName() const override;
76
77 [[nodiscard]] fge::RectFloat getGlobalBounds() const override;
78 [[nodiscard]] std::optional<fge::RectFloat> getGlobalBounds(std::size_t index) const;
79 [[nodiscard]] fge::RectFloat getLocalBounds() const override;
80 [[nodiscard]] std::optional<fge::RectFloat> getLocalBounds(std::size_t index) const;
81
82private:
83 void updatePositions(std::size_t index);
84 void updateTexCoords(std::size_t index);
85 void updateBuffers() const;
86 void updateTextures(bool sizeHasChanged);
87
88 struct InstanceData
89 {
90 InstanceData() = default;
91 explicit InstanceData(fge::RectInt const& textureRect, glm::uint textureIndex) :
92 _textureRect(textureRect),
93 _textureIndex(textureIndex)
94 {}
95
96 fge::Transformable _transformable;
97 fge::RectInt _textureRect;
98 glm::uint _textureIndex{0};
99 };
100 struct InstanceDataBuffer
101 {
102 alignas(16) glm::mat4 _transform;
103 alignas(16) glm::uint _textureIndex{0};
104 };
105
106 std::vector<fge::Texture> g_textures;
107
108 std::vector<InstanceData> g_instancesData;
109 mutable fge::vulkan::UniformBuffer g_instancesTransform;
110 mutable fge::vulkan::UniformBuffer g_instancesIndirectCommands;
111 mutable fge::vulkan::DescriptorSet g_descriptorSets[2];
112 fge::vulkan::VertexBuffer g_instancesVertices;
113
114 mutable bool g_needBuffersUpdate;
115 bool const g_featureMultiDrawIndirect;
116};
117
118} // namespace fge
119
120#endif // _FGE_C_OBJSPRITEBATCHES_HPP_INCLUDED
Definition C_color.hpp:35
Definition C_objSpriteBatches.hpp:38
void load(nlohmann::json &jsonObject, fge::Scene *scene) override
Load the object from a json object.
fge::RectFloat getLocalBounds() const override
Get the local bounds of the object (without any transformations)
void save(nlohmann::json &jsonObject, fge::Scene *scene) override
Save the object to a json object.
char const * getClassName() const override
Get the unique class name of the object.
char const * getReadableClassName() const override
Get a readable version of the class name.
fge::RectFloat getGlobalBounds() const override
Get the global bounds of the object.
void unpack(fge::net::Packet const &pck) override
Unpack the object from a packet.
void pack(fge::net::Packet &pck) override
Pack the object into a packet.
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
This class is a wrapper for the texture manager to allow easy manipulation.
Definition C_texture.hpp:36
Definition C_transformable.hpp:34
Definition C_packet.hpp:70
This class abstract the vulkan descriptor set for easier use.
Definition C_descriptorSet.hpp:41
Definition C_uniformBuffer.hpp:32
Definition C_vertexBuffer.hpp:45