Compare commits

..

No commits in common. "be75263c2c38f3557ca7f521d108629298ae1164" and "e1bef49d06d255adcd2e24582a644e0267f508e9" have entirely different histories.

4 changed files with 56 additions and 219 deletions

View File

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

View File

@ -4,6 +4,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.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) void init_particle_system(ParticleSystem_t* system)
{ {
memset(system, 0, sizeof(ParticleSystem_t)); memset(system, 0, sizeof(ParticleSystem_t));
@ -14,140 +17,62 @@ void init_particle_system(ParticleSystem_t* system)
} }
system->tail_idx = 0; system->tail_idx = 0;
} }
void play_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter)
uint16_t get_number_of_free_emitter(ParticleSystem_t* system)
{ {
return sc_queue_size(&system->free_list); if (in_emitter == NULL) return;
} if (in_emitter->config == NULL) return;
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);
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;
system->emitters[idx] = *in_emitter; system->emitters[idx] = *in_emitter;
system->emitters[idx].active = true; system->emitters[idx].active = true;
if (system->emitters[idx].n_particles > MAX_PARTICLES) if (system->emitters[idx].n_particles > MAX_PARTICLES)
{ {
system->emitters[idx].n_particles = MAX_PARTICLES; system->emitters[idx].n_particles = MAX_PARTICLES;
} }
system->emitter_list[idx].playing = false; ParticleEmitter_t* emitter = system->emitters + idx;
return idx; // Generate particles based on type
} for (uint32_t i = 0; i < emitter->n_particles; ++i)
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; uint32_t lifetime = (emitter->config->particle_lifetime[1] - emitter->config->particle_lifetime[0]);
if (emitter->config->type == EMITTER_BURST) emitter->particles[i].timer = emitter->config->particle_lifetime[0];
{ emitter->particles[i].timer += rand() % lifetime;
for (uint32_t i = 0; i < emitter->n_particles; ++i) emitter->particles[i].alive = true;
{
spawn_particle(emitter, i); float angle = emitter->config->launch_range[1] - emitter->config->launch_range[0];
} angle *= (float)rand() / (float)RAND_MAX;
} angle += emitter->config->launch_range[0];
else if (emitter->config->type == EMITTER_STREAM) if(angle > 360) angle -= 360;
{ if(angle < -360) angle += 360;
// TODO: deal with stream type
//spawn_particle(emitter, 0); float speed = emitter->config->speed_range[1] - emitter->config->speed_range[0];
uint32_t incr = 0; speed *= (float)rand() / (float)RAND_MAX;
for (uint32_t i = 0; i < emitter->n_particles; ++i) speed += emitter->config->speed_range[0];
{
emitter->particles[i].timer = incr; emitter->particles[i].velocity.x = speed * cos(angle * PI / 180);
emitter->particles[i].alive = false; emitter->particles[i].velocity.y = speed * sin(angle * PI / 180);
emitter->particles[i].spawned = false; emitter->particles[i].position = emitter->position;
incr += emitter->config->initial_spawn_delay; 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[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) void update_particle_system(ParticleSystem_t* system)
{ {
uint32_t emitter_idx = system->emitter_list[0].next; uint32_t emitter_idx = system->emitter_list[0].next;
uint32_t prev_idx = 0; uint32_t last_idx = 0;
while (emitter_idx != 0) while (emitter_idx != 0)
{ {
uint32_t next_idx = system->emitter_list[emitter_idx].next;
ParticleEmitter_t* emitter = system->emitters + emitter_idx; ParticleEmitter_t* emitter = system->emitters + emitter_idx;
uint32_t inactive_count = 0; uint32_t inactive_count = 0;
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
// Otherwise do the usual check
if (emitter->particles[i].alive) if (emitter->particles[i].alive)
{ {
if (emitter->update_func != NULL) if (emitter->update_func != NULL)
@ -155,62 +80,33 @@ void update_particle_system(ParticleSystem_t* system)
emitter->update_func(emitter->particles + i, emitter->user_data); emitter->update_func(emitter->particles + i, emitter->user_data);
} }
} // Lifetime update
// Lifetime update if (emitter->particles[i].timer > 0) emitter->particles[i].timer--;
if (emitter->particles[i].timer > 0) emitter->particles[i].timer--; if (emitter->particles[i].timer == 0)
if (emitter->particles[i].timer == 0)
{
if (emitter->particles[i].spawned)
{ {
emitter->particles[i].alive = false; emitter->particles[i].alive = false;
} inactive_count++;
else
{
emitter->particles[i].spawned = true;
} }
} }
else
if (!emitter->particles[i].alive)
{ {
if (!emitter->active) inactive_count++;
{
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) if (inactive_count == emitter->n_particles)
{ {
// Stop playing only if all particles is inactive emitter->active = false;
if (!emitter->finished && emitter->config->one_shot) system->emitter_list[last_idx].next = system->emitter_list[emitter_idx].next;
{
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].next = 0;
system->emitter_list[emitter_idx].playing = false;
if (system->tail_idx == emitter_idx) if (system->tail_idx == emitter_idx)
{ {
system->tail_idx = prev_idx; system->tail_idx = last_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;
prev_idx = emitter_idx; emitter_idx = next_idx;
emitter_idx = system->emitter_list[emitter_idx].next;
} }
} }
void draw_particle_system(ParticleSystem_t* system) void draw_particle_system(ParticleSystem_t* system)

View File

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

View File

@ -69,7 +69,6 @@ int main(void)
.launch_range = {0, 360}, .launch_range = {0, 360},
.speed_range = {400, 2000}, .speed_range = {400, 2000},
.particle_lifetime = {30, 110}, .particle_lifetime = {30, 110},
.type = EMITTER_BURST,
}; };
ParticleEmitter_t emitter = { ParticleEmitter_t emitter = {
@ -79,66 +78,24 @@ int main(void)
.spr = (tex.width == 0) ? NULL : &spr, .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; bool key_press = false;
uint8_t key2_press = 0;
char text_buffer[32];
while(!WindowShouldClose()) while(!WindowShouldClose())
{ {
Vector2 mouse_pos = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{ {
key_press = true; key_press = true;
} }
else if (key_press && IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) else if (key_press && IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{ {
emitter.position = mouse_pos; emitter.position = GetMousePosition();
play_particle_emitter(&part_sys, &emitter); play_particle_emitter(&part_sys, &emitter);
key_press = false; key_press = false;
} }
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); update_particle_system(&part_sys);
sprintf(text_buffer, "free: %u", get_number_of_free_emitter(&part_sys));
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
draw_particle_system(&part_sys); draw_particle_system(&part_sys);
DrawText(text_buffer, 0, 0, 16, BLACK);
EndDrawing(); EndDrawing();
} }
UnloadTexture(tex); UnloadTexture(tex);