1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "animation/rs_render_particle_system.h"
17
18 #include <cstddef>
19 namespace OHOS {
20 namespace Rosen {
RSRenderParticleSystem(const std::vector<std::shared_ptr<ParticleRenderParams>> & particlesRenderParams)21 RSRenderParticleSystem::RSRenderParticleSystem(
22 const std::vector<std::shared_ptr<ParticleRenderParams>>& particlesRenderParams)
23 : particlesRenderParams_(particlesRenderParams)
24 {
25 CreateEmitter();
26 }
27
CreateEmitter()28 void RSRenderParticleSystem::CreateEmitter()
29 {
30 size_t index = 0;
31 for (size_t iter = 0; iter < particlesRenderParams_.size(); iter++) {
32 auto& particleRenderParams = particlesRenderParams_[iter];
33 if (particleRenderParams != nullptr) {
34 if (particleRenderParams->GetParticleType() == ParticleType::IMAGES) {
35 particleRenderParams->SetImageIndex(index++);
36 auto& image = particleRenderParams->GetParticleImage();
37 imageVector_.push_back(image);
38 }
39 emitters_.push_back(std::make_shared<RSRenderParticleEmitter>(particleRenderParams));
40 }
41 }
42 }
43
ClearEmitter()44 void RSRenderParticleSystem::ClearEmitter()
45 {
46 emitters_.clear();
47 }
48
Emit(int64_t deltaTime,std::vector<std::shared_ptr<RSRenderParticle>> & activeParticles,std::vector<std::shared_ptr<RSImage>> & imageVector)49 void RSRenderParticleSystem::Emit(int64_t deltaTime, std::vector<std::shared_ptr<RSRenderParticle>>& activeParticles,
50 std::vector<std::shared_ptr<RSImage>>& imageVector)
51 {
52 for (size_t iter = 0; iter < emitters_.size(); iter++) {
53 if (emitters_[iter] != nullptr) {
54 emitters_[iter]->EmitParticle(deltaTime);
55 auto& particles = emitters_[iter]->GetParticles();
56 activeParticles.insert(activeParticles.end(), particles.begin(), particles.end());
57 }
58 }
59 imageVector = imageVector_;
60 }
61
UpdateParticle(int64_t deltaTime,std::vector<std::shared_ptr<RSRenderParticle>> & activeParticles)62 void RSRenderParticleSystem::UpdateParticle(
63 int64_t deltaTime, std::vector<std::shared_ptr<RSRenderParticle>>& activeParticles)
64 {
65 if (activeParticles.empty()) {
66 return;
67 }
68 for (auto it = activeParticles.begin(); it != activeParticles.end();) {
69 // std::shared_ptr<RSRenderParticle> particle = *it;
70 if ((*it) == nullptr || !(*it)->IsAlive()) {
71 it = activeParticles.erase(it);
72 } else {
73 Update((*it), particleNoiseFields_, deltaTime);
74 ++it;
75 }
76 }
77 }
78
IsFinish(const std::vector<std::shared_ptr<RSRenderParticle>> & activeParticles)79 bool RSRenderParticleSystem::IsFinish(const std::vector<std::shared_ptr<RSRenderParticle>>& activeParticles)
80 {
81 bool finish = true;
82 if (!activeParticles.empty()) {
83 return false;
84 }
85 for (size_t iter = 0; iter < emitters_.size(); iter++) {
86 if (emitters_[iter] != nullptr) {
87 finish = finish && emitters_[iter]->IsEmitterFinish();
88 }
89 }
90 return finish;
91 }
92
UpdateEmitter(const std::vector<std::shared_ptr<ParticleRenderParams>> & particlesRenderParams)93 void RSRenderParticleSystem::UpdateEmitter(
94 const std::vector<std::shared_ptr<ParticleRenderParams>>& particlesRenderParams)
95 {
96 particlesRenderParams_ = particlesRenderParams;
97 }
98
UpdateNoiseField(const std::shared_ptr<ParticleNoiseFields> & particleNoiseFields)99 void RSRenderParticleSystem::UpdateNoiseField(const std::shared_ptr<ParticleNoiseFields>& particleNoiseFields)
100 {
101 particleNoiseFields_ = particleNoiseFields;
102 }
103 } // namespace Rosen
104 } // namespace OHOS
105