diff --git a/engine/particle_sys.c b/engine/particle_sys.c index fc241a2..b231cc0 100644 --- a/engine/particle_sys.c +++ b/engine/particle_sys.c @@ -103,7 +103,7 @@ void play_emitter_handle(ParticleSystem_t* system, uint16_t handle) // An emitter cannot be unloaded or paused mid-way when particles to still // emitting, so defer into update function to do so -void pause_emitter_handle(ParticleSystem_t* system, uint16_t handle) +void stop_emitter_handle(ParticleSystem_t* system, EmitterHandle handle) { if (handle == 0) return; //if (!system->emitter_list[handle].playing) return; @@ -118,7 +118,7 @@ void update_emitter_handle_position(ParticleSystem_t* system, EmitterHandle hand system->emitters[handle].position = pos; } -void unload_emitter_handle(ParticleSystem_t* system, uint16_t handle) +void unload_emitter_handle(ParticleSystem_t* system, EmitterHandle handle) { if (handle == 0) return; @@ -126,12 +126,13 @@ void unload_emitter_handle(ParticleSystem_t* system, uint16_t handle) system->emitters[handle].finished = true; } -void play_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter) +EmitterHandle play_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter) { - uint16_t idx = load_in_particle_emitter(system, in_emitter); - if (idx == 0) return; + EmitterHandle idx = load_in_particle_emitter(system, in_emitter); + if (idx == 0) return 0 ; play_emitter_handle(system, idx); + return idx; } void update_particle_system(ParticleSystem_t* system) @@ -143,6 +144,12 @@ void update_particle_system(ParticleSystem_t* system) { ParticleEmitter_t* emitter = system->emitters + emitter_idx; uint32_t inactive_count = 0; + + if (emitter->emitter_update_func != NULL && emitter->active) + { + emitter->active = emitter->emitter_update_func(emitter); + } + for (uint32_t i = 0; i < emitter->n_particles; ++i) { // TODO: If a particle is not spawned, run its timer. Spawn on zero @@ -187,10 +194,11 @@ void update_particle_system(ParticleSystem_t* system) } } } + if (inactive_count == emitter->n_particles) { // Stop playing only if all particles is inactive - if (!emitter->finished && emitter->config->one_shot) + if (!emitter->finished) { emitter->finished = true; } @@ -213,6 +221,7 @@ void update_particle_system(ParticleSystem_t* system) emitter_idx = system->emitter_list[emitter_idx].next; } } + void draw_particle_system(ParticleSystem_t* system) { uint32_t emitter_idx = system->emitter_list[0].next; diff --git a/engine/particle_sys.h b/engine/particle_sys.h index 5452929..f8b36e4 100644 --- a/engine/particle_sys.h +++ b/engine/particle_sys.h @@ -29,7 +29,10 @@ typedef struct Particle bool spawned; }Particle_t; +typedef struct ParticleEmitter ParticleEmitter_t; + typedef void (*particle_update_func_t)(Particle_t* part, void* user_data); +typedef bool (*emitter_check_func_t)(const ParticleEmitter_t* emitter); typedef struct EmitterConfig { @@ -41,7 +44,7 @@ typedef struct EmitterConfig bool one_shot; }EmitterConfig_t; -typedef struct ParticleEmitter +struct ParticleEmitter { const EmitterConfig_t* config; Sprite_t* spr; @@ -53,7 +56,8 @@ typedef struct ParticleEmitter bool active; void* user_data; particle_update_func_t update_func; -}ParticleEmitter_t; + emitter_check_func_t emitter_update_func; +}; typedef struct IndexList { @@ -75,11 +79,11 @@ void init_particle_system(ParticleSystem_t* system); uint16_t get_number_of_free_emitter(ParticleSystem_t* system); // For one-shots -void play_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter); +EmitterHandle play_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter); EmitterHandle load_in_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter); void play_emitter_handle(ParticleSystem_t* system, EmitterHandle handle); -void pause_emitter_handle(ParticleSystem_t* system, EmitterHandle handle); +void stop_emitter_handle(ParticleSystem_t* system, EmitterHandle handle); void update_emitter_handle_position(ParticleSystem_t* system, EmitterHandle handle, Vector2 pos); void unload_emitter_handle(ParticleSystem_t* system, EmitterHandle handle); diff --git a/particle_test.c b/particle_test.c index cdf364d..cacac11 100644 --- a/particle_test.c +++ b/particle_test.c @@ -45,6 +45,12 @@ void simple_particle_system_update(Particle_t* part, void* user_data) } } } + +static bool check_mouse_click(const ParticleEmitter_t* emitter) +{ + return IsMouseButtonDown(MOUSE_RIGHT_BUTTON); +} + int main(void) { InitWindow(1280, 640, "raylib"); @@ -76,6 +82,7 @@ int main(void) .config = &conf, .n_particles = MAX_PARTICLES, .update_func = &simple_particle_system_update, + .emitter_update_func = NULL, .spr = (tex.width == 0) ? NULL : &spr, }; @@ -92,15 +99,14 @@ int main(void) .config = &conf2, .n_particles = MAX_PARTICLES, .update_func = &simple_particle_system_update, + .emitter_update_func = &check_mouse_click, .spr = (tex.width == 0) ? NULL : &spr, }; - EmitterHandle han = load_in_particle_emitter(&part_sys, &emitter2); - assert(han != 0); - bool key_press = false; uint8_t key2_press = 0; char text_buffer[32]; + EmitterHandle han = 0; while(!WindowShouldClose()) { Vector2 mouse_pos = GetMousePosition(); @@ -121,8 +127,9 @@ int main(void) if (key2_press == 0b01) { + han = play_particle_emitter(&part_sys, &emitter2); update_emitter_handle_position(&part_sys, han, mouse_pos); - play_emitter_handle(&part_sys, han); + //play_emitter_handle(&part_sys, han); } else if(key2_press == 0b11) { @@ -130,7 +137,8 @@ int main(void) } else if (key2_press == 0b10) { - pause_emitter_handle(&part_sys, han); + stop_emitter_handle(&part_sys, han); + han = 0; } update_particle_system(&part_sys);