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)21RSRenderParticleSystem::RSRenderParticleSystem( 22 const std::vector<std::shared_ptr<ParticleRenderParams>>& particlesRenderParams) 23 : particlesRenderParams_(particlesRenderParams) 24 { 25 CreateEmitter(); 26 } 27 CreateEmitter()28void RSRenderParticleSystem::CreateEmitter() 29 { 30 for (size_t iter = 0; iter < particlesRenderParams_.size(); iter++) { 31 auto particleRenderParams = particlesRenderParams_[iter]; 32 emitters_.push_back(std::make_shared<RSRenderParticleEmitter>(particleRenderParams)); 33 } 34 } 35 ClearEmitter()36void RSRenderParticleSystem::ClearEmitter() 37 { 38 emitters_.clear(); 39 } 40 Emit(int64_t deltaTime)41void RSRenderParticleSystem::Emit(int64_t deltaTime) 42 { 43 for (size_t iter = 0; iter < emitters_.size(); iter++) { 44 if (emitters_[iter] != nullptr) { 45 emitters_[iter]->EmitParticle(deltaTime); 46 auto particles = emitters_[iter]->GetParticles(); 47 activeParticles_.insert(activeParticles_.end(), particles.begin(), particles.end()); 48 } 49 } 50 } 51 UpdateParticle(int64_t deltaTime)52void RSRenderParticleSystem::UpdateParticle(int64_t deltaTime) 53 { 54 if (activeParticles_.empty()) { 55 return; 56 } 57 for (auto it = activeParticles_.begin(); it != activeParticles_.end();) { 58 std::shared_ptr<RSRenderParticle> particle = *it; 59 if (particle == nullptr || !particle->IsAlive()) { 60 it = activeParticles_.erase(it); 61 } else { 62 ++it; 63 } 64 } 65 for (auto particle : activeParticles_) { 66 if (particle != nullptr) { 67 auto particleRenderParams = particle->GetParticleRenderParams(); 68 if (particleRenderParams != nullptr) { 69 auto effect = RSRenderParticleEffector(particleRenderParams); 70 effect.ApplyEffectorToParticle(particle, deltaTime); 71 } 72 } 73 } 74 } 75 IsFinish()76bool RSRenderParticleSystem::IsFinish() 77 { 78 bool finish = true; 79 if (!activeParticles_.empty()) { 80 return false; 81 } 82 for (size_t iter = 0; iter < emitters_.size(); iter++) { 83 if (emitters_[iter] != nullptr) { 84 finish = finish && emitters_[iter]->IsEmitterFinish(); 85 } 86 } 87 return finish; 88 } 89 GetActiveParticles()90std::vector<std::shared_ptr<RSRenderParticle>> RSRenderParticleSystem::GetActiveParticles() 91 { 92 return activeParticles_; 93 } 94 Simulation(int64_t deltaTime)95std::vector<std::shared_ptr<RSRenderParticle>> RSRenderParticleSystem::Simulation(int64_t deltaTime) 96 { 97 Emit(deltaTime); 98 UpdateParticle(deltaTime); 99 return activeParticles_; 100 } 101 } // namespace Rosen 102 } // namespace OHOS 103