sandbox
Loading...
Searching...
No Matches
particle_emitter.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_PARTICLES_PARTICLE_EMITTER_HPP_
3#define LIBSBX_PARTICLES_PARTICLE_EMITTER_HPP_
4
5#include <cstdint>
6
7#include <libsbx/math/vector2.hpp>
8#include <libsbx/math/vector3.hpp>
10#include <libsbx/math/volume.hpp>
12
13#include <libsbx/graphics/images/image2d.hpp>
14
15namespace sbx::particles {
16
17enum class emitter_state : std::uint8_t {
18 playing,
19 paused,
20 stopped
21}; // enum class emitter_state
22
24 emitter_state state{emitter_state::playing};
25 bool loop{true};
26
27 std::float_t duration{5.0f};
28 std::float_t elapsed{0.0f};
29 std::float_t emission_accumulator{0.0f};
30
31 std::uint32_t max_particles{10000};
32 std::float_t emission_rate{100.0f};
33 math::volume emission_shape{{-1.0f, 0.0f, -1.0f}, {1.0f, 0.0f, 1.0f}};
34 math::vector2 initial_speed{5.0f, 10.0f};
35 math::vector2 initial_lifetime{1.0f, 3.0f};
36 math::vector2 initial_size{0.1f, 0.5f};
37 math::vector2 initial_rotation{0.0f, math::two_pi};
38 math::color initial_color{1.0f, 1.0f, 1.0f, 1.0f};
39
40 math::vector3 gravity{0.0f, -9.81f, 0.0f};
41 std::float_t drag{0.1f};
42
43 math::color end_color{1.0f, 1.0f, 1.0f, 0.0f};
44 std::float_t end_size_scale{0.0f};
45
46 // graphics::image2d_handle texture{};
47 std::vector<graphics::image2d_handle> images{};
48
49 std::uint32_t burst_count{0};
50
51 auto play() -> void {
52 state = emitter_state::playing;
53 }
54
55 auto pause() -> void {
56 state = emitter_state::paused;
57 }
58
59 auto stop() -> void {
60 state = emitter_state::stopped;
61 elapsed = 0.0f;
62 emission_accumulator = 0.0f;
63 }
64
65 auto burst(std::uint32_t count) -> void {
66 burst_count += count;
67 }
68
69 [[nodiscard]] auto is_playing() const -> bool {
70 return state == emitter_state::playing;
71 }
72
73 [[nodiscard]] auto is_paused() const -> bool {
74 return state == emitter_state::paused;
75 }
76
77 [[nodiscard]] auto is_stopped() const -> bool {
78 return state == emitter_state::stopped;
79 }
80
81 [[nodiscard]] auto is_finished() const -> bool {
82 return !loop && elapsed >= duration && state == emitter_state::stopped;
83 }
84
85}; // struct particle_emitter
86
87} // namespace sbx::particles
88
89#endif // LIBSBX_PARTICLES_PARTICLE_EMITTER_HPP_
A vector in two-dimensional space.
Definition: vector2.hpp:28
Definition: vector3.hpp:23
Definition: volume.hpp:14
RGBA color value type.
Definition: color.hpp:48
RGBA color representation and utilities.
Mathematical constants.
Definition: particle_emitter.hpp:23