1 #ifndef DALI_TOOLKIT_PARTICLE_SYSTEM_INTERNAL_PARTICLE_LIST_H
2 #define DALI_TOOLKIT_PARTICLE_SYSTEM_INTERNAL_PARTICLE_LIST_H
4 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <dali-toolkit/public-api/particle-system/particle-list.h>
22 #include <dali-toolkit/public-api/particle-system/particle.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/public-api/common/vector-wrapper.h>
27 #include <dali/public-api/common/list-wrapper.h>
28 #include <dali/devel-api/common/map-wrapper.h>
33 namespace Dali::Toolkit::ParticleSystem::Internal
36 struct StreamDataTypeWrapper
38 static ParticleStream::StreamDataType GetType()
44 struct ParticleDataStream
46 ~ParticleDataStream() = default;
48 ParticleDataStream(uint32_t capacity, const T& defaultValue, ParticleStream::StreamDataType dataType)
49 : ParticleDataStream(capacity, sizeof(T), &defaultValue, dataType)
54 * Creates new stream of requested capacity and (optionally) fills with default data
56 ParticleDataStream(uint32_t capacity, uint32_t dataSize, const void* defaultValue, ParticleStream::StreamDataType dataType)
58 this->capacity = capacity;
59 data.resize(capacity * dataSize);
62 for(auto i = 0u; i < capacity; ++i)
64 auto dstPtr = data.data() + (i * dataSize);
65 std::copy(reinterpret_cast<const uint8_t*>(defaultValue), reinterpret_cast<const uint8_t*>(defaultValue) + dataSize, dstPtr);
70 this->dataSize = dataSize;
73 void SetStreamName(const char* name)
78 void SetStreamLocal(bool local)
84 * Converts raw data into requested type (does not guarantee compatibility)
89 return reinterpret_cast<T*>(data.data());
92 ParticleStream::StreamDataType type;
93 std::vector<uint8_t> data;
94 std::string streamName;
98 bool localStream{true};
102 * Particle list stores particle-specific data and manages the particles memory
103 * It can return a sub-list.
105 * ParticleList manages the storage memory.
109 class ParticleList : public Dali::BaseObject
113 ParticleList(uint32_t capacity, ParticleSystem::ParticleList::ParticleStreamTypeFlags streamFlags);
118 * Returns raw pointer to the stream data
120 void* GetRawStream(uint32_t index);
123 * Returns number of available data streams
126 uint32_t GetStreamCount() const;
129 * Returns number of particles per list
132 uint32_t GetParticleCount() const;
135 * Returns number of currently active particles
138 uint32_t GetActiveParticleCount() const;
141 * Returns stream data-type
145 ParticleStream::StreamDataType GetStreamDataType(uint32_t streamIndex);
148 * Returns stream data type size
152 [[nodiscard]] uint32_t GetStreamDataTypeSize(uint32_t streamIndex) const;
154 [[nodiscard]] const std::string& GetStreamName(uint32_t streamIndex) const;
156 [[nodiscard]] bool IsStreamLocal(uint32_t streamIndex) const;
159 * Allocates new particle in the streams
163 ParticleSystem::Particle NewParticle(float lifetime);
165 void* GetDefaultStream(ParticleStreamTypeFlagBit streamBit);
167 uint32_t GetDefaultStreamIndex(ParticleStreamTypeFlagBit streamBit);
169 std::list<ParticleSystem::Particle>& GetParticles();
171 void ReleaseParticle(uint32_t particleIndex);
173 uint32_t GetStreamElementSize(bool includeLocalStream);
177 uint32_t AddStream(const T& defaultValue, const char* streamName, bool localStream)
179 return AddStream(sizeof(T), &defaultValue, StreamDataTypeWrapper<T>::GetType(), streamName, localStream);
184 * Adds new stream and returns index
186 uint32_t AddStream(uint32_t sizeOfDataType, const void* defaultValue, ParticleStream::StreamDataType dataType, const char* streamName, bool localStream);
189 std::vector<char> mBuffer[2];
191 uint32_t mAliveParticleCount{0u};
192 uint32_t mMaxParticleCount;
195 std::vector<std::unique_ptr<ParticleDataStream>> mDataStreams;
197 std::vector<uint32_t> mFreeChain;
198 int32_t mFreeIndex{0u};
200 std::map<uint32_t, uint32_t> mBuiltInStreamMap;
202 std::list<ParticleSystem::Particle> mParticles;
204 uint32_t mParticleStreamElementSizeWithLocal{0u};
205 uint32_t mParticleStreamElementSize{0u};
208 } // namespace Dali::Toolkit::ParticleSystem::Internal
209 namespace Dali::Toolkit::ParticleSystem
211 inline Internal::ParticleList& GetImplementation(ParticleSystem::ParticleList& source)
213 DALI_ASSERT_ALWAYS(source && "ParticleList handle is empty");
215 BaseObject& handle = source.GetBaseObject();
217 return static_cast<Internal::ParticleList&>(handle);
220 inline const Internal::ParticleList& GetImplementation(const ParticleSystem::ParticleList& source)
222 DALI_ASSERT_ALWAYS(source && "ParticleList handle is empty");
224 const BaseObject& handle = source.GetBaseObject();
226 return static_cast<const Internal::ParticleList&>(handle);
229 } // namespace Dali::Toolkit::ParticleSystem
231 #endif // DALI_TOOLKIT_PARTICLE_SYSTEM_INTERNAL_PARTICLE_LIST_H