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>
32 namespace Dali::Toolkit::ParticleSystem::Internal
35 struct StreamDataTypeWrapper
37 static ParticleStream::StreamDataType GetType()
43 struct ParticleDataStream
45 ~ParticleDataStream() = default;
47 ParticleDataStream(uint32_t capacity, const T& defaultValue, ParticleStream::StreamDataType dataType)
48 : ParticleDataStream(capacity, sizeof(T), &defaultValue, dataType)
53 * Creates new stream of requested capacity and (optionally) fills with default data
55 ParticleDataStream(uint32_t capacity, uint32_t dataSize, const void* defaultValue, ParticleStream::StreamDataType dataType)
57 this->capacity = capacity;
58 data.resize(capacity * dataSize);
61 for(auto i = 0u; i < capacity; ++i)
63 auto dstPtr = data.data() + (i * dataSize);
64 std::copy(reinterpret_cast<const uint8_t*>(defaultValue), reinterpret_cast<const uint8_t*>(defaultValue) + dataSize, dstPtr);
69 this->dataSize = dataSize;
72 void SetStreamName(const char* name)
77 void SetStreamLocal(bool local)
83 * Converts raw data into requested type (does not guarantee compatibility)
88 return reinterpret_cast<T*>(data.data());
91 ParticleStream::StreamDataType type;
92 std::vector<uint8_t> data;
93 std::string streamName;
97 bool localStream{true};
101 * Particle list stores particle-specific data and manages the particles memory
102 * It can return a sub-list.
104 * ParticleList manages the storage memory.
108 class ParticleList : public Dali::BaseObject
112 ParticleList(uint32_t capacity, ParticleSystem::ParticleList::ParticleStreamTypeFlags streamFlags);
117 * Returns raw pointer to the stream data
119 void* GetRawStream(uint32_t index);
122 * Returns number of available data streams
125 uint32_t GetStreamCount() const;
128 * Returns number of particles per list
131 uint32_t GetParticleCount() const;
134 * Returns number of currently active particles
137 uint32_t GetActiveParticleCount() const;
140 * Returns stream data-type
144 ParticleStream::StreamDataType GetStreamDataType(uint32_t streamIndex);
147 * Returns stream data type size
151 [[nodiscard]] uint32_t GetStreamDataTypeSize(uint32_t streamIndex) const;
153 [[nodiscard]] const std::string& GetStreamName(uint32_t streamIndex) const;
155 [[nodiscard]] bool IsStreamLocal(uint32_t streamIndex) const;
158 * Allocates new particle in the streams
162 ParticleSystem::Particle NewParticle(float lifetime);
164 void* GetDefaultStream(ParticleStreamTypeFlagBit streamBit);
166 uint32_t GetDefaultStreamIndex(ParticleStreamTypeFlagBit streamBit);
168 std::list<ParticleSystem::Particle>& GetParticles();
170 void ReleaseParticle(uint32_t particleIndex);
172 uint32_t GetStreamElementSize(bool includeLocalStream);
176 uint32_t AddStream(const T& defaultValue, const char* streamName, bool localStream)
178 return AddStream(sizeof(T), &defaultValue, StreamDataTypeWrapper<T>::GetType(), streamName, localStream);
183 * Adds new stream and returns index
185 uint32_t AddStream(uint32_t sizeOfDataType, const void* defaultValue, ParticleStream::StreamDataType dataType, const char* streamName, bool localStream);
188 std::vector<char> mBuffer[2];
190 uint32_t mAliveParticleCount{0u};
191 uint32_t mMaxParticleCount;
194 std::vector<std::unique_ptr<ParticleDataStream>> mDataStreams;
196 std::vector<uint32_t> mFreeChain;
197 int32_t mFreeIndex{0u};
199 std::map<uint32_t, uint32_t> mBuiltInStreamMap;
201 std::list<ParticleSystem::Particle> mParticles;
203 uint32_t mParticleStreamElementSizeWithLocal{0u};
204 uint32_t mParticleStreamElementSize{0u};
207 } // namespace Dali::Toolkit::ParticleSystem::Internal
208 namespace Dali::Toolkit::ParticleSystem
210 inline Internal::ParticleList& GetImplementation(ParticleSystem::ParticleList& source)
212 DALI_ASSERT_ALWAYS(source && "ParticleList handle is empty");
214 BaseObject& handle = source.GetBaseObject();
216 return static_cast<Internal::ParticleList&>(handle);
219 inline const Internal::ParticleList& GetImplementation(const ParticleSystem::ParticleList& source)
221 DALI_ASSERT_ALWAYS(source && "ParticleList handle is empty");
223 const BaseObject& handle = source.GetBaseObject();
225 return static_cast<const Internal::ParticleList&>(handle);
228 } // namespace Dali::Toolkit::ParticleSystem
230 #endif // DALI_TOOLKIT_PARTICLE_SYSTEM_INTERNAL_PARTICLE_LIST_H