Compare commits

...

3 Commits

Author SHA1 Message Date
En Yi be75263c2c Implement stream-type particle emitter 2023-11-18 19:44:20 +08:00
En Yi cb0a93ee70 Implement feature to start and stop emitter 2023-11-18 15:48:43 +08:00
En Yi 5350c2b761 Refactor particle system for future feature
This is to support persistent emitter later on

Also, re-check the update logic
2023-11-18 12:45:57 +08:00
4 changed files with 222 additions and 59 deletions

View File

@ -12,7 +12,7 @@
#define MAX_EMITTER_CONF 8
//#define MAX_PARTICLE_EMITTER 8
#define MAX_ACTIVE_PARTICLE_EMITTER 32
#define MAX_PARTICLES 10
#define MAX_PARTICLES 32
#define MAX_TILE_TYPES 16

View File

@ -4,9 +4,6 @@
#include <stdlib.h>
#include <math.h>
// TEMPORARY VARIABLE: NEED TO FIND A WAY TO DEAL WITH THIS
#define DELTA_T 0.017
void init_particle_system(ParticleSystem_t* system)
{
memset(system, 0, sizeof(ParticleSystem_t));
@ -17,62 +14,140 @@ void init_particle_system(ParticleSystem_t* system)
}
system->tail_idx = 0;
}
void play_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter)
{
if (in_emitter == NULL) return;
if (in_emitter->config == NULL) return;
if (sc_queue_empty(&system->free_list)) return;
uint32_t idx = sc_queue_del_first(&system->free_list);
system->emitter_list[system->tail_idx].next = idx;
system->tail_idx = idx;
system->emitter_list[idx].next = 0;
uint16_t get_number_of_free_emitter(ParticleSystem_t* system)
{
return sc_queue_size(&system->free_list);
}
static inline void spawn_particle(ParticleEmitter_t* emitter, uint32_t idx)
{
uint32_t lifetime = (emitter->config->particle_lifetime[1] - emitter->config->particle_lifetime[0]);
emitter->particles[idx].timer = emitter->config->particle_lifetime[0];
emitter->particles[idx].timer += rand() % lifetime;
emitter->particles[idx].alive = true;
float angle = emitter->config->launch_range[1] - emitter->config->launch_range[0];
angle *= (float)rand() / (float)RAND_MAX;
angle += emitter->config->launch_range[0];
if(angle > 360) angle -= 360;
if(angle < -360) angle += 360;
float speed = emitter->config->speed_range[1] - emitter->config->speed_range[0];
speed *= (float)rand() / (float)RAND_MAX;
speed += emitter->config->speed_range[0];
emitter->particles[idx].velocity.x = speed * cos(angle * PI / 180);
emitter->particles[idx].velocity.y = speed * sin(angle * PI / 180);
emitter->particles[idx].position = emitter->position;
emitter->particles[idx].rotation = angle;
emitter->particles[idx].angular_vel = -10 + 20 * (float)rand() / (float)RAND_MAX;
emitter->particles[idx].size = 10 + 20 * (float)rand() / (float)RAND_MAX;
emitter->particles[idx].spawned = true;
;
}
uint16_t load_in_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter)
{
if (in_emitter == NULL) return 0;
if (in_emitter->config == NULL) return 0 ;
if (in_emitter->config->type == EMITTER_UNKNOWN) return 0;
if (sc_queue_empty(&system->free_list)) return 0;
uint16_t idx = sc_queue_del_first(&system->free_list);
system->emitters[idx] = *in_emitter;
system->emitters[idx].active = true;
if (system->emitters[idx].n_particles > MAX_PARTICLES)
{
system->emitters[idx].n_particles = MAX_PARTICLES;
}
ParticleEmitter_t* emitter = system->emitters + idx;
// Generate particles based on type
for (uint32_t i = 0; i < emitter->n_particles; ++i)
{
uint32_t lifetime = (emitter->config->particle_lifetime[1] - emitter->config->particle_lifetime[0]);
emitter->particles[i].timer = emitter->config->particle_lifetime[0];
emitter->particles[i].timer += rand() % lifetime;
emitter->particles[i].alive = true;
float angle = emitter->config->launch_range[1] - emitter->config->launch_range[0];
angle *= (float)rand() / (float)RAND_MAX;
angle += emitter->config->launch_range[0];
if(angle > 360) angle -= 360;
if(angle < -360) angle += 360;
float speed = emitter->config->speed_range[1] - emitter->config->speed_range[0];
speed *= (float)rand() / (float)RAND_MAX;
speed += emitter->config->speed_range[0];
emitter->particles[i].velocity.x = speed * cos(angle * PI / 180);
emitter->particles[i].velocity.y = speed * sin(angle * PI / 180);
emitter->particles[i].position = emitter->position;
emitter->particles[i].rotation = angle;
emitter->particles[i].angular_vel = -10 + 20 * (float)rand() / (float)RAND_MAX;
emitter->particles[i].size = 10 + 20 * (float)rand() / (float)RAND_MAX;
;
}
system->emitter_list[idx].playing = false;
return idx;
}
void play_emitter_handle(ParticleSystem_t* system, uint16_t handle)
{
if (handle == 0) return;
if (!system->emitter_list[handle].playing)
{
ParticleEmitter_t* emitter = system->emitters + handle;
if (emitter->config->type == EMITTER_BURST)
{
for (uint32_t i = 0; i < emitter->n_particles; ++i)
{
spawn_particle(emitter, i);
}
}
else if (emitter->config->type == EMITTER_STREAM)
{
// TODO: deal with stream type
//spawn_particle(emitter, 0);
uint32_t incr = 0;
for (uint32_t i = 0; i < emitter->n_particles; ++i)
{
emitter->particles[i].timer = incr;
emitter->particles[i].alive = false;
emitter->particles[i].spawned = false;
incr += emitter->config->initial_spawn_delay;
}
}
system->emitter_list[system->tail_idx].next = handle;
system->tail_idx = handle;
system->emitter_list[handle].next = 0;
system->emitter_list[handle].playing = true;
}
system->emitters[handle].active = true;
}
// 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)
{
if (handle == 0) return;
//if (!system->emitter_list[handle].playing) return;
system->emitters[handle].active = false;
}
void update_emitter_handle_position(ParticleSystem_t* system, EmitterHandle handle, Vector2 pos)
{
if (handle == 0) return;
system->emitters[handle].position = pos;
}
void unload_emitter_handle(ParticleSystem_t* system, uint16_t handle)
{
if (handle == 0) return;
system->emitters[handle].active = false;
system->emitters[handle].finished = true;
}
void 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;
play_emitter_handle(system, idx);
}
void update_particle_system(ParticleSystem_t* system)
{
uint32_t emitter_idx = system->emitter_list[0].next;
uint32_t last_idx = 0;
uint32_t prev_idx = 0;
while (emitter_idx != 0)
{
uint32_t next_idx = system->emitter_list[emitter_idx].next;
ParticleEmitter_t* emitter = system->emitters + emitter_idx;
uint32_t inactive_count = 0;
for (uint32_t i = 0; i < emitter->n_particles; ++i)
{
// TODO: If a particle is not spawned, run its timer. Spawn on zero
// Otherwise do the usual check
if (emitter->particles[i].alive)
{
if (emitter->update_func != NULL)
@ -80,33 +155,62 @@ void update_particle_system(ParticleSystem_t* system)
emitter->update_func(emitter->particles + i, emitter->user_data);
}
// Lifetime update
if (emitter->particles[i].timer > 0) emitter->particles[i].timer--;
if (emitter->particles[i].timer == 0)
}
// Lifetime update
if (emitter->particles[i].timer > 0) emitter->particles[i].timer--;
if (emitter->particles[i].timer == 0)
{
if (emitter->particles[i].spawned)
{
emitter->particles[i].alive = false;
inactive_count++;
}
else
{
emitter->particles[i].spawned = true;
}
}
else
if (!emitter->particles[i].alive)
{
inactive_count++;
if (!emitter->active)
{
inactive_count++;
}
else if (emitter->config->one_shot)
{
inactive_count++;
}
else if (emitter->particles[i].spawned)
{
// If not one shot, immediately revive the particle
spawn_particle(emitter, i);
}
}
}
if (inactive_count == emitter->n_particles)
{
emitter->active = false;
system->emitter_list[last_idx].next = system->emitter_list[emitter_idx].next;
// Stop playing only if all particles is inactive
if (!emitter->finished && emitter->config->one_shot)
{
emitter->finished = true;
}
system->emitter_list[prev_idx].next = system->emitter_list[emitter_idx].next;
system->emitter_list[emitter_idx].next = 0;
system->emitter_list[emitter_idx].playing = false;
if (system->tail_idx == emitter_idx)
{
system->tail_idx = last_idx;
system->tail_idx = prev_idx;
}
if (emitter->finished)
{
sc_queue_add_last(&system->free_list, emitter_idx);
emitter_idx = prev_idx;
}
sc_queue_add_last(&system->free_list, emitter_idx);
emitter_idx = last_idx;
}
last_idx = emitter_idx;
emitter_idx = next_idx;
prev_idx = emitter_idx;
emitter_idx = system->emitter_list[emitter_idx].next;
}
}
void draw_particle_system(ParticleSystem_t* system)

View File

@ -7,10 +7,13 @@
#include <stdint.h>
#include <stdbool.h>
typedef uint16_t EmitterHandle;
typedef enum PartEmitterType
{
EMITTER_BURST = 0,
EMITTER_UNKNOWN,
EMITTER_UNKNOWN = 0,
EMITTER_BURST,
EMITTER_STREAM,
} PartEmitterType_t;
typedef struct Particle
@ -23,6 +26,7 @@ typedef struct Particle
float size;
uint32_t timer;
bool alive;
bool spawned;
}Particle_t;
typedef void (*particle_update_func_t)(Particle_t* part, void* user_data);
@ -32,6 +36,7 @@ typedef struct EmitterConfig
float launch_range[2];
float speed_range[2];
uint32_t particle_lifetime[2];
uint32_t initial_spawn_delay;
PartEmitterType_t type;
bool one_shot;
}EmitterConfig_t;
@ -53,6 +58,7 @@ typedef struct ParticleEmitter
typedef struct IndexList
{
uint32_t next;
bool playing;
}IndexList_t;
typedef struct ParticleSystem
@ -66,7 +72,17 @@ typedef struct ParticleSystem
}ParticleSystem_t;
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 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 update_emitter_handle_position(ParticleSystem_t* system, EmitterHandle handle, Vector2 pos);
void unload_emitter_handle(ParticleSystem_t* system, EmitterHandle handle);
void update_particle_system(ParticleSystem_t* system);
void draw_particle_system(ParticleSystem_t* system);
void deinit_particle_system(ParticleSystem_t* system);

View File

@ -69,6 +69,7 @@ int main(void)
.launch_range = {0, 360},
.speed_range = {400, 2000},
.particle_lifetime = {30, 110},
.type = EMITTER_BURST,
};
ParticleEmitter_t emitter = {
@ -78,24 +79,66 @@ int main(void)
.spr = (tex.width == 0) ? NULL : &spr,
};
EmitterConfig_t conf2 ={
.one_shot = false,
.launch_range = {45, 135},
.speed_range = {300, 800},
.particle_lifetime = {15, 30},
.initial_spawn_delay = 5,
.type = EMITTER_STREAM,
};
ParticleEmitter_t emitter2 = {
.config = &conf2,
.n_particles = MAX_PARTICLES,
.update_func = &simple_particle_system_update,
.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];
while(!WindowShouldClose())
{
Vector2 mouse_pos = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
key_press = true;
}
else if (key_press && IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{
emitter.position = GetMousePosition();
emitter.position = mouse_pos;
play_particle_emitter(&part_sys, &emitter);
key_press = false;
}
update_particle_system(&part_sys);
key2_press <<= 1;
key2_press |= IsMouseButtonDown(MOUSE_RIGHT_BUTTON)? 1: 0;
key2_press &= 0b11;
if (key2_press == 0b01)
{
update_emitter_handle_position(&part_sys, han, mouse_pos);
play_emitter_handle(&part_sys, han);
}
else if(key2_press == 0b11)
{
update_emitter_handle_position(&part_sys, han, mouse_pos);
}
else if (key2_press == 0b10)
{
pause_emitter_handle(&part_sys, han);
}
update_particle_system(&part_sys);
sprintf(text_buffer, "free: %u", get_number_of_free_emitter(&part_sys));
BeginDrawing();
ClearBackground(RAYWHITE);
draw_particle_system(&part_sys);
DrawText(text_buffer, 0, 0, 16, BLACK);
EndDrawing();
}
UnloadTexture(tex);