Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "ParticleManager.h" 21 : 22 : #include "ps/Filesystem.h" 23 : #include "ps/Profile.h" 24 : #include "renderer/backend/IDevice.h" 25 : #include "renderer/Scene.h" 26 : 27 : #include <algorithm> 28 : #include <cstddef> 29 : #include <memory> 30 : #include <unordered_map> 31 : #include <utility> 32 : #include <vector> 33 : 34 : class CFrustum; 35 : 36 0 : static Status ReloadChangedFileCB(void* param, const VfsPath& path) 37 : { 38 0 : return static_cast<CParticleManager*>(param)->ReloadChangedFile(path); 39 : } 40 : 41 10 : CParticleManager::CParticleManager(Renderer::Backend::IDevice& device) 42 20 : : m_UseInstancing{device.GetCapabilities().instancing} 43 : { 44 10 : RegisterFileReloadFunc(ReloadChangedFileCB, this); 45 10 : } 46 : 47 10 : CParticleManager::~CParticleManager() 48 : { 49 10 : UnregisterFileReloadFunc(ReloadChangedFileCB, this); 50 10 : } 51 : 52 0 : CParticleEmitterType* CParticleManager::LoadEmitterType(const VfsPath& path) 53 : { 54 0 : if (auto it{m_EmitterTypes.find(path)}; it != m_EmitterTypes.end()) 55 0 : return it->second.get(); 56 : 57 0 : std::unique_ptr<CParticleEmitterType> emitterType{ 58 0 : std::make_unique<CParticleEmitterType>(*this)}; 59 0 : if (!emitterType->Load(path)) 60 : return nullptr; 61 : 62 0 : return m_EmitterTypes.emplace(path, std::move(emitterType)).first->second.get(); 63 0 : } 64 : 65 0 : void CParticleManager::AddUnattachedEmitter(std::unique_ptr<CParticleEmitter> emitter) 66 : { 67 0 : emitter->m_Active = false; 68 0 : m_UnattachedEmitters.emplace_back(std::move(emitter)); 69 0 : } 70 : 71 0 : void CParticleManager::ClearUnattachedEmitters() 72 : { 73 0 : m_UnattachedEmitters.clear(); 74 0 : } 75 : 76 0 : void CParticleManager::Interpolate(const float simFrameLength) 77 : { 78 0 : m_CurrentTime += simFrameLength; 79 0 : } 80 : 81 : struct ParticleAlive 82 : { 83 0 : bool operator()(const SParticle& particle) const 84 : { 85 0 : return particle.age < particle.maxAge; 86 : } 87 : }; 88 : 89 : struct EmitterHasNoParticles 90 : { 91 0 : bool operator()(const std::unique_ptr<CParticleEmitter>& emitter) const 92 : { 93 0 : return std::none_of(emitter->m_Particles.begin(), emitter->m_Particles.end(), ParticleAlive{}); 94 : } 95 : }; 96 : 97 0 : void CParticleManager::RenderSubmit(SceneCollector& collector, const CFrustum&) 98 : { 99 0 : PROFILE("submit unattached particles"); 100 : 101 : // Delete any unattached emitters that have no particles left 102 0 : std::erase_if(m_UnattachedEmitters, EmitterHasNoParticles()); 103 : 104 : // TODO: should do some frustum culling 105 : 106 0 : for (std::unique_ptr<CParticleEmitter>& emitter : m_UnattachedEmitters) 107 0 : collector.Submit(emitter.get()); 108 0 : } 109 : 110 0 : Status CParticleManager::ReloadChangedFile(const VfsPath& path) 111 : { 112 0 : if (auto it = m_EmitterTypes.find(path); it != m_EmitterTypes.end()) 113 : { 114 : // We can't erase an emitter type because emitters might hold 115 : // a reference to it. 116 0 : it->second->Load(path); 117 : } 118 0 : return INFO::OK; 119 : }