Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
particles.hpp
Go to the documentation of this file.
1
3#pragma once
4
8#include <aegis2d/render/export.hpp>
10
11#include <cstddef>
12#include <cstdint>
13#include <numbers>
14#include <span>
15#include <vector>
16
17namespace aegis2d::render {
18
20struct Range {
22 float min = 0.0F;
24 float max = 0.0F;
25};
26
30 float rate = 50.0F;
32 Range lifetime{.min = 1.0F, .max = 1.0F};
34 Range speed{.min = 50.0F, .max = 100.0F};
37 float direction = 0.0F;
40 float spread = 2.0F * std::numbers::pi_v<float>;
42 float start_size = 4.0F;
44 float end_size = 0.0F;
46 Color start_color = rgb(0xFFD166);
48 Color end_color = rgb(0xEF476F, 0.0F);
50 std::size_t max_particles = 1000;
51};
52
54struct Particle {
56 Vec2 position{0.0F, 0.0F};
58 Vec2 velocity{0.0F, 0.0F};
60 float age = 0.0F;
62 float lifetime = 1.0F;
63};
64
80class AEGIS2D_RENDER_EXPORT ParticleEmitter {
81public:
87 explicit ParticleEmitter(const EmitterConfig& config, std::uint64_t seed = 1);
88
91 [[nodiscard]] const EmitterConfig& config() const noexcept;
92
95 void set_position(Vec2 position) noexcept;
96
99 [[nodiscard]] Vec2 position() const noexcept;
100
103 void set_emitting(bool emitting) noexcept;
104
107 [[nodiscard]] bool emitting() const noexcept;
108
112 std::size_t burst(std::size_t count);
113
117 void update(float seconds);
118
121 [[nodiscard]] std::span<const Particle> particles() const noexcept;
122
124 void clear() noexcept;
125
129 [[nodiscard]] float size(const Particle& particle) const noexcept;
130
134 [[nodiscard]] Color color(const Particle& particle) const noexcept;
135
144 void draw(IRenderer& renderer, std::int32_t layer = 0, TextureId texture = no_texture) const;
145
146private:
147 void spawn();
148
149 EmitterConfig config_;
150 core::Random random_;
151 std::vector<Particle> particles_;
152 Vec2 position_{0.0F, 0.0F};
153 float pending_ = 0.0F;
154 bool emitting_ = true;
155};
156
157} // namespace aegis2d::render
What the game draws with, whatever the graphics API behind it.
Definition renderer.hpp:81
Spawns particles at a point, moves them and removes them at the end of their life.
Definition particles.hpp:80
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.
RGBA color used by the renderer.
Vector and matrix types of the engine (GLM behind engine names).
OpenGL renderer: sprites, text and camera (client only).
Definition bitmap_font.hpp:16
aegis2d::Vec2 Vec2
2D vector of floats, in logical pixels: the engine-wide aegis2d::Vec2.
Definition renderer.hpp:17
std::uint32_t TextureId
Identifier of a texture loaded by the resource manager.
Definition renderer.hpp:32
constexpr Color rgb(std::uint32_t hex, float alpha=1.0F) noexcept
Color from its hexadecimal sRGB code, as design tools and palettes give it.
Definition color.hpp:25
Definition endpoint.hpp:85
Seeded random numbers that are the same on every platform.
Backend-independent renderer interface and the sprite it draws.
RGBA color with components in [0, 1].
Definition color.hpp:10
How an emitter spawns its particles, and how they change over their life.
Definition particles.hpp:28
Color start_color
Color at its birth.
Definition particles.hpp:46
Range lifetime
Life of each particle, in seconds.
Definition particles.hpp:32
float spread
Width of the cone of directions around direction, in radians: 0 for a straight jet,...
Definition particles.hpp:40
float rate
Particles spawned per second while emitting; 0 for bursts only.
Definition particles.hpp:30
std::size_t max_particles
Most particles alive at once; spawns past it are dropped.
Definition particles.hpp:50
float end_size
Side of a particle at the end of its life.
Definition particles.hpp:44
float direction
Middle of the directions particles leave in, in radians: 0 to the right, pi / 2 down (y grows downwar...
Definition particles.hpp:37
Color end_color
Color at the end of its life, such as transparent to fade out.
Definition particles.hpp:48
float start_size
Side of a particle at its birth, in logical pixels.
Definition particles.hpp:42
Range speed
Speed of each particle at its birth, in logical pixels per second.
Definition particles.hpp:34
One particle of an emitter.
Definition particles.hpp:54
Vec2 velocity
Velocity, in logical pixels per second.
Definition particles.hpp:58
float age
Seconds since its birth.
Definition particles.hpp:60
Vec2 position
Center, in logical pixels.
Definition particles.hpp:56
float lifetime
Seconds it lives in all.
Definition particles.hpp:62
Values picked at random between two bounds, both included.
Definition particles.hpp:20
float max
Largest value; equal to min for a fixed value.
Definition particles.hpp:24
float min
Smallest value.
Definition particles.hpp:22