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