Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::render::ParticleEmitter Class Reference

Spawns particles at a point, moves them and removes them at the end of their life. More...

#include <aegis2d/render/particles.hpp>

Public Member Functions

 ParticleEmitter (const EmitterConfig &config, std::uint64_t seed=1)
 Creates an emitter at (0, 0), emitting, with no particle yet.
 
const EmitterConfig & config () const noexcept
 How particles are spawned and change.
 
void set_position (Vec2 position) noexcept
 Moves the point new particles leave from; those alive keep their course.
 
Vec2 position () const noexcept
 Point new particles leave from.
 
void set_emitting (bool emitting) noexcept
 Starts or stops spawning at the rate; the particles alive live on.
 
bool emitting () const noexcept
 Tells whether the emitter spawns at its rate.
 
std::size_t burst (std::size_t count)
 Spawns particles at once.
 
void update (float seconds)
 Ages and moves the particles, removes those at the end of their life, then spawns the new ones of the step.
 
std::span< const Particle > particles () const noexcept
 Particles alive, in no particular order.
 
void clear () noexcept
 Removes every particle.
 
float size (const Particle &particle) const noexcept
 Side of a particle now.
 
Color color (const Particle &particle) const noexcept
 Color of a particle now.
 
void draw (IRenderer &renderer, std::int32_t layer=0, TextureId texture=no_texture) const
 Draws every particle into the current frame's sprite batch.
 

Detailed Description

Spawns particles at a point, moves them and removes them at the end of their life.

While emitting, rate particles appear each second, the fraction of a frame carried over to the next, so the count is the same at any frame rate; burst() adds many at once, as for an explosion. Each particle leaves the emitter's position in a direction within the spread and at a speed within its range, moves in a straight line, and its size and color go from their start to their end values over its life. The draw is left to the renderer: size() and color() give what to draw for each particle. Particles are picked from a seed, so the same seed and steps give the same particles.

render::ParticleEmitter sparks{{.rate = 0.0F, .lifetime = {0.3F, 0.6F}}};
sparks.set_position(explosion);
sparks.burst(40);
sparks.update(step_seconds); // once per tick
Spawns particles at a point, moves them and removes them at the end of their life.
Definition particles.hpp:80
void set_position(Vec2 position) noexcept
Moves the point new particles leave from; those alive keep their course.

Constructor & Destructor Documentation

◆ ParticleEmitter()

aegis2d::render::ParticleEmitter::ParticleEmitter ( const EmitterConfig &  config,
std::uint64_t  seed = 1 
)
explicit

Creates an emitter at (0, 0), emitting, with no particle yet.

Parameters
configHow particles are spawned and change.
seedSeed of the particles' random values.
Exceptions
std::invalid_argumentif the rate is negative or not finite, a lifetime is not above 0, a range has its min above its max, or max_particles is 0.

Member Function Documentation

◆ burst()

std::size_t aegis2d::render::ParticleEmitter::burst ( std::size_t  count)

Spawns particles at once.

Parameters
countNumber of particles, fewer if max_particles is reached.
Returns
The number spawned.

◆ clear()

void aegis2d::render::ParticleEmitter::clear ( )
noexcept

Removes every particle.

◆ color()

Color aegis2d::render::ParticleEmitter::color ( const Particle &  particle) const
noexcept

Color of a particle now.

Parameters
particleA particle of this emitter.
Returns
From start_color at its birth to end_color at the end of its life.

◆ config()

const EmitterConfig & aegis2d::render::ParticleEmitter::config ( ) const
noexcept

How particles are spawned and change.

Returns
The configuration given to the constructor.

◆ draw()

void aegis2d::render::ParticleEmitter::draw ( IRenderer &  renderer,
std::int32_t  layer = 0,
TextureId  texture = no_texture 
) const

Draws every particle into the current frame's sprite batch.

One sprite per particle, centered on its position, a square of its size() tinted with its color() now. The particles share the texture and the layer, so the batch draws them all in one draw call per layer (see IRenderer).

Parameters
rendererRenderer of the current frame.
layerDrawing order of the particles, as Sprite::layer.
textureTexture of each particle, or no_texture for plain squares.
Exceptions
std::logic_erroroutside a frame.

◆ emitting()

bool aegis2d::render::ParticleEmitter::emitting ( ) const
noexcept

Tells whether the emitter spawns at its rate.

Returns
true by default.

◆ particles()

std::span< const Particle > aegis2d::render::ParticleEmitter::particles ( ) const
noexcept

Particles alive, in no particular order.

Returns
A view valid until the next call of a non-const function.

◆ position()

Vec2 aegis2d::render::ParticleEmitter::position ( ) const
noexcept

Point new particles leave from.

Returns
The last position set; (0, 0) by default.

◆ set_emitting()

void aegis2d::render::ParticleEmitter::set_emitting ( bool  emitting)
noexcept

Starts or stops spawning at the rate; the particles alive live on.

Parameters
emittingfalse to stop, as when a trail's source stops moving.

◆ set_position()

void aegis2d::render::ParticleEmitter::set_position ( Vec2  position)
noexcept

Moves the point new particles leave from; those alive keep their course.

Parameters
positionNew position, in logical pixels.

◆ size()

float aegis2d::render::ParticleEmitter::size ( const Particle &  particle) const
noexcept

Side of a particle now.

Parameters
particleA particle of this emitter.
Returns
From start_size at its birth to end_size at the end of its life.

◆ update()

void aegis2d::render::ParticleEmitter::update ( float  seconds)

Ages and moves the particles, removes those at the end of their life, then spawns the new ones of the step.

Parameters
secondsTime of the step; values not above 0 or not finite do nothing.

The documentation for this class was generated from the following file: