Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Particle.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
5#include "../core/Types.hpp"
6#include "../math/Math.hpp"
7
8#include <vector>
9
10namespace esengine::particle {
11
12struct Particle {
13 glm::vec3 position{0.0f};
14 glm::vec3 velocity{0.0f};
15 glm::vec3 added_velocity{0.0f};
16 f32 rotation = 0.0f;
18 f32 lifetime = 1.0f;
19 f32 age = 0.0f;
20 f32 size = 1.0f;
21 glm::vec4 color{1.0f};
23 f32 end_size = 0.0f;
24 glm::vec4 start_color{1.0f};
25 glm::vec4 end_color{1.0f, 1.0f, 1.0f, 0.0f};
27 bool alive = false;
30};
31
32inline f32 unitHash(u32 v) {
33 const u32 s = v * 747796405u + 2891336453u;
34 u32 w = ((s >> ((s >> 28u) + 4u)) ^ s) * 277803737u;
35 w = (w >> 22u) ^ w;
36 return static_cast<f32>(w >> 8u) * (1.0f / 16777216.0f);
37}
38
40public:
41 explicit ParticlePool(u32 capacity);
42
45 void clear();
46
47 u32 capacity() const { return capacity_; }
48 u32 aliveCount() const { return alive_count_; }
49 std::vector<Particle>& particles() { return particles_; }
50 const std::vector<Particle>& particles() const { return particles_; }
51
52 template<typename Fn>
53 void forEachAlive(Fn&& fn) {
54 for (Particle& p : particles_) {
55 if (p.alive) fn(p);
56 }
57 }
58
59 template<typename Fn>
60 void forEachAlive(Fn&& fn) const {
61 for (const Particle& p : particles_) {
62 if (p.alive) fn(p);
63 }
64 }
65
67 void deallocateByIndex(u32 index);
68
69private:
70 std::vector<Particle> particles_;
71 std::vector<u32> free_list_;
72 u32 capacity_;
73 u32 alive_count_ = 0;
74 u32 next_generation_ = 0;
75};
76
77} // namespace esengine::particle
Math utilities and GLM configuration for ESEngine.
Core type definitions and utilities for ESEngine.
std::vector< Particle > & particles()
Definition Particle.hpp:49
void deallocate(Particle *particle)
Definition Particle.cpp:29
void forEachAlive(Fn &&fn) const
Definition Particle.hpp:60
const std::vector< Particle > & particles() const
Definition Particle.hpp:50
void deallocateByIndex(u32 index)
Deallocate a particle by its pool index.
Definition Particle.cpp:50
f32 unitHash(u32 v)
Definition Particle.hpp:32
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint16_t u16
Unsigned 16-bit integer.
Definition Types.hpp:43
float f32
32-bit floating point
Definition Types.hpp:59
glm::vec3 added_velocity
Carried on top of velocity, outside gravity, damping and the speed limit.
Definition Particle.hpp:15