Rework stream handling function

Changelog:
- Add callback function to stop the emitter
    - This is use to automatically stop the emitter when the condition
      is met
- Update particle test program
scene_man
En Yi 2023-11-20 20:48:41 +08:00
parent 8ae999ccc8
commit 8165cd41c2
3 changed files with 36 additions and 15 deletions

View File

@ -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 // An emitter cannot be unloaded or paused mid-way when particles to still
// emitting, so defer into update function to do so // 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 (handle == 0) return;
//if (!system->emitter_list[handle].playing) 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; 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; if (handle == 0) return;
@ -126,12 +126,13 @@ void unload_emitter_handle(ParticleSystem_t* system, uint16_t handle)
system->emitters[handle].finished = true; 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); EmitterHandle idx = load_in_particle_emitter(system, in_emitter);
if (idx == 0) return; if (idx == 0) return 0 ;
play_emitter_handle(system, idx); play_emitter_handle(system, idx);
return idx;
} }
void update_particle_system(ParticleSystem_t* system) 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; ParticleEmitter_t* emitter = system->emitters + emitter_idx;
uint32_t inactive_count = 0; 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) for (uint32_t i = 0; i < emitter->n_particles; ++i)
{ {
// TODO: If a particle is not spawned, run its timer. Spawn on zero // 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) if (inactive_count == emitter->n_particles)
{ {
// Stop playing only if all particles is inactive // Stop playing only if all particles is inactive
if (!emitter->finished && emitter->config->one_shot) if (!emitter->finished)
{ {
emitter->finished = true; emitter->finished = true;
} }
@ -213,6 +221,7 @@ void update_particle_system(ParticleSystem_t* system)
emitter_idx = system->emitter_list[emitter_idx].next; emitter_idx = system->emitter_list[emitter_idx].next;
} }
} }
void draw_particle_system(ParticleSystem_t* system) void draw_particle_system(ParticleSystem_t* system)
{ {
uint32_t emitter_idx = system->emitter_list[0].next; uint32_t emitter_idx = system->emitter_list[0].next;

View File

@ -29,7 +29,10 @@ typedef struct Particle
bool spawned; bool spawned;
}Particle_t; }Particle_t;
typedef struct ParticleEmitter ParticleEmitter_t;
typedef void (*particle_update_func_t)(Particle_t* part, void* user_data); 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 typedef struct EmitterConfig
{ {
@ -41,7 +44,7 @@ typedef struct EmitterConfig
bool one_shot; bool one_shot;
}EmitterConfig_t; }EmitterConfig_t;
typedef struct ParticleEmitter struct ParticleEmitter
{ {
const EmitterConfig_t* config; const EmitterConfig_t* config;
Sprite_t* spr; Sprite_t* spr;
@ -53,7 +56,8 @@ typedef struct ParticleEmitter
bool active; bool active;
void* user_data; void* user_data;
particle_update_func_t update_func; particle_update_func_t update_func;
}ParticleEmitter_t; emitter_check_func_t emitter_update_func;
};
typedef struct IndexList typedef struct IndexList
{ {
@ -75,11 +79,11 @@ void init_particle_system(ParticleSystem_t* system);
uint16_t get_number_of_free_emitter(ParticleSystem_t* system); uint16_t get_number_of_free_emitter(ParticleSystem_t* system);
// For one-shots // 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); EmitterHandle load_in_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter);
void play_emitter_handle(ParticleSystem_t* system, EmitterHandle handle); 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 update_emitter_handle_position(ParticleSystem_t* system, EmitterHandle handle, Vector2 pos);
void unload_emitter_handle(ParticleSystem_t* system, EmitterHandle handle); void unload_emitter_handle(ParticleSystem_t* system, EmitterHandle handle);

View File

@ -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) int main(void)
{ {
InitWindow(1280, 640, "raylib"); InitWindow(1280, 640, "raylib");
@ -76,6 +82,7 @@ int main(void)
.config = &conf, .config = &conf,
.n_particles = MAX_PARTICLES, .n_particles = MAX_PARTICLES,
.update_func = &simple_particle_system_update, .update_func = &simple_particle_system_update,
.emitter_update_func = NULL,
.spr = (tex.width == 0) ? NULL : &spr, .spr = (tex.width == 0) ? NULL : &spr,
}; };
@ -92,15 +99,14 @@ int main(void)
.config = &conf2, .config = &conf2,
.n_particles = MAX_PARTICLES, .n_particles = MAX_PARTICLES,
.update_func = &simple_particle_system_update, .update_func = &simple_particle_system_update,
.emitter_update_func = &check_mouse_click,
.spr = (tex.width == 0) ? NULL : &spr, .spr = (tex.width == 0) ? NULL : &spr,
}; };
EmitterHandle han = load_in_particle_emitter(&part_sys, &emitter2);
assert(han != 0);
bool key_press = false; bool key_press = false;
uint8_t key2_press = 0; uint8_t key2_press = 0;
char text_buffer[32]; char text_buffer[32];
EmitterHandle han = 0;
while(!WindowShouldClose()) while(!WindowShouldClose())
{ {
Vector2 mouse_pos = GetMousePosition(); Vector2 mouse_pos = GetMousePosition();
@ -121,8 +127,9 @@ int main(void)
if (key2_press == 0b01) if (key2_press == 0b01)
{ {
han = play_particle_emitter(&part_sys, &emitter2);
update_emitter_handle_position(&part_sys, han, mouse_pos); 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) else if(key2_press == 0b11)
{ {
@ -130,7 +137,8 @@ int main(void)
} }
else if (key2_press == 0b10) else if (key2_press == 0b10)
{ {
pause_emitter_handle(&part_sys, han); stop_emitter_handle(&part_sys, han);
han = 0;
} }
update_particle_system(&part_sys); update_particle_system(&part_sys);