Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

1.8 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
#include "Particle.hpp"

namespace esengine::particle {

ParticlePool::ParticlePool(u32 capacity) : capacity_(capacity) {
    particles_.resize(capacity);
    free_list_.reserve(capacity);
    for (u32 i = capacity; i > 0; --i) {
        free_list_.push_back(i - 1);
    }
}

Particle* ParticlePool::allocate() {
    if (free_list_.empty()) {
        return nullptr;
    }
    u32 index = free_list_.back();
    free_list_.pop_back();
    particles_[index] = Particle{};
    particles_[index].alive = true;
    if (++next_generation_ == 0) ++next_generation_;
    particles_[index].generation = next_generation_;
    alive_count_++;
    return &particles_[index];
}

void ParticlePool::deallocate(Particle* particle) {
    if (!particle || !particle->alive) {
        return;
    }
    u32 index = static_cast<u32>(particle - particles_.data());
    particle->alive = false;
    free_list_.push_back(index);
    alive_count_--;
}

void ParticlePool::clear() {
    for (auto& p : particles_) {
        p.alive = false;
    }
    free_list_.clear();
    for (u32 i = capacity_; i > 0; --i) {
        free_list_.push_back(i - 1);
    }
    alive_count_ = 0;
}

void ParticlePool::deallocateByIndex(u32 index) {
    if (index >= particles_.size() || !particles_[index].alive) {
        return;
    }
    particles_[index].alive = false;
    free_list_.push_back(index);
    alive_count_--;
}

void ParticlePool::forEachAlive(const std::function<void(Particle&)>& fn) {
    for (auto& p : particles_) {
        if (p.alive) {
            fn(p);
        }
    }
}

void ParticlePool::forEachAlive(const std::function<void(const Particle&)>& fn) const {
    for (const auto& p : particles_) {
        if (p.alive) {
            fn(p);
        }
    }
}

}  // namespace esengine::particle