Estella
0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Particle.cpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3
#include "
Particle.hpp
"
4
5
namespace
esengine::particle
{
6
7
ParticlePool::ParticlePool
(
u32
capacity
) : capacity_(
capacity
) {
8
particles_.resize(
capacity
);
9
free_list_.reserve(
capacity
);
10
for
(
u32
i =
capacity
; i > 0; --i) {
11
free_list_.push_back(i - 1);
12
}
13
}
14
15
Particle
*
ParticlePool::allocate
() {
16
if
(free_list_.empty()) {
17
return
nullptr
;
18
}
19
u32
index = free_list_.back();
20
free_list_.pop_back();
21
particles_[index] =
Particle
{};
22
particles_[index].
alive
=
true
;
23
if
(++next_generation_ == 0) ++next_generation_;
24
particles_[index].generation = next_generation_;
25
alive_count_++;
26
return
&particles_[index];
27
}
28
29
void
ParticlePool::deallocate
(
Particle
*
particle
) {
30
if
(!
particle
|| !
particle
->alive) {
31
return
;
32
}
33
u32
index =
static_cast<
u32
>
(
particle
- particles_.data());
34
particle
->alive =
false
;
35
free_list_.push_back(index);
36
alive_count_--;
37
}
38
39
void
ParticlePool::clear
() {
40
for
(
auto
& p : particles_) {
41
p.alive =
false
;
42
}
43
free_list_.clear();
44
for
(
u32
i = capacity_; i > 0; --i) {
45
free_list_.push_back(i - 1);
46
}
47
alive_count_ = 0;
48
}
49
50
void
ParticlePool::deallocateByIndex
(
u32
index) {
51
if
(index >= particles_.size() || !particles_[index].alive) {
52
return
;
53
}
54
particles_[index].alive =
false
;
55
free_list_.push_back(index);
56
alive_count_--;
57
}
58
59
}
// namespace esengine::particle
Particle.hpp
esengine::particle::ParticlePool::deallocate
void deallocate(Particle *particle)
Definition
Particle.cpp:29
esengine::particle::ParticlePool::clear
void clear()
Definition
Particle.cpp:39
esengine::particle::ParticlePool::allocate
Particle * allocate()
Definition
Particle.cpp:15
esengine::particle::ParticlePool::ParticlePool
ParticlePool(u32 capacity)
Definition
Particle.cpp:7
esengine::particle::ParticlePool::deallocateByIndex
void deallocateByIndex(u32 index)
Deallocate a particle by its pool index.
Definition
Particle.cpp:50
esengine::particle::ParticlePool::capacity
u32 capacity() const
Definition
Particle.hpp:47
esengine::particle
Definition
Particle.cpp:5
esengine::u32
std::uint32_t u32
Unsigned 32-bit integer.
Definition
Types.hpp:45
esengine::particle::Particle
Definition
Particle.hpp:12
esengine::particle::Particle::alive
bool alive
Definition
Particle.hpp:27
src
esengine
particle
Particle.cpp
Generated by
1.15.0