Implement initial particle system
Changelog: - Can only to one-shot burst emitter - Add test for itscene_man
parent
290dafdf86
commit
7c86e0b3c5
|
@ -139,6 +139,19 @@ target_link_libraries(assets_test
|
||||||
${GAME_LIBS}
|
${GAME_LIBS}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_executable(particle_test
|
||||||
|
particle_test.c
|
||||||
|
)
|
||||||
|
target_include_directories(particle_test
|
||||||
|
PRIVATE
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}
|
||||||
|
)
|
||||||
|
target_compile_options(particle_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||||
|
target_link_options(particle_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||||
|
target_link_libraries(particle_test
|
||||||
|
${GAME_LIBS}
|
||||||
|
)
|
||||||
|
|
||||||
if (BUILD_TESTING)
|
if (BUILD_TESTING)
|
||||||
find_package(cmocka 1.1.0 REQUIRED)
|
find_package(cmocka 1.1.0 REQUIRED)
|
||||||
add_subdirectory(tests)
|
add_subdirectory(tests)
|
||||||
|
|
|
@ -8,6 +8,7 @@ add_library(lib_engine STATIC
|
||||||
rres.c
|
rres.c
|
||||||
mempool.c
|
mempool.c
|
||||||
entManager.c
|
entManager.c
|
||||||
|
particle_sys.c
|
||||||
${LIBZSTD_DIR}/lib/libzstd.a
|
${LIBZSTD_DIR}/lib/libzstd.a
|
||||||
)
|
)
|
||||||
target_include_directories(lib_engine
|
target_include_directories(lib_engine
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include "collisions.h"
|
#include "collisions.h"
|
||||||
#include "sc/array/sc_array.h"
|
#include "sc/array/sc_array.h"
|
||||||
#include "assets.h"
|
#include "assets.h"
|
||||||
|
#include "particle_sys.h"
|
||||||
|
|
||||||
typedef struct Scene Scene_t;
|
typedef struct Scene Scene_t;
|
||||||
|
|
||||||
|
@ -21,6 +22,7 @@ typedef struct GameEngine {
|
||||||
unsigned int curr_scene;
|
unsigned int curr_scene;
|
||||||
Assets_t assets;
|
Assets_t assets;
|
||||||
SFXList_t sfx_list;
|
SFXList_t sfx_list;
|
||||||
|
ParticleSystem_t part_sys;
|
||||||
// Maintain own queue to handle key presses
|
// Maintain own queue to handle key presses
|
||||||
struct sc_queue_32 key_buffer;
|
struct sc_queue_32 key_buffer;
|
||||||
} GameEngine_t;
|
} GameEngine_t;
|
||||||
|
|
|
@ -1,30 +1,124 @@
|
||||||
#include "particle_sys.h"
|
#include "particle_sys.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#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)
|
void init_particle_system(ParticleSystem_t* system)
|
||||||
{
|
{
|
||||||
memset(system, 0, sizeof(ParticleSystem_t));
|
memset(system, 0, sizeof(ParticleSystem_t));
|
||||||
sc_queue_init(&system->free_list);
|
sc_queue_init(&system->free_list);
|
||||||
for ( uint32_t i = 0; i < MAX_PARTICLE_EMITTER; ++i)
|
for ( uint32_t i = 1; i <= MAX_PARTICLE_EMITTER; ++i)
|
||||||
{
|
{
|
||||||
sc_queue_add_last(&system->free_list, i);
|
sc_queue_add_last(&system->free_list, i);
|
||||||
}
|
}
|
||||||
|
system->tail_idx = 0;
|
||||||
}
|
}
|
||||||
void add_particle_emitter(ParticleSystem_t* system, ParticleEmitter_t* emitter)
|
void add_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter)
|
||||||
{
|
{
|
||||||
if (sc_queue_empty(&system->free_list)) return;
|
if (sc_queue_empty(&system->free_list)) return;
|
||||||
uint32_t idx = sc_queue_del_first(&system->free_list);
|
uint32_t idx = sc_queue_del_first(&system->free_list);
|
||||||
system->emitter_list[system->tail_idx].next = idx;
|
system->emitter_list[system->tail_idx].next = idx;
|
||||||
system->tail_idx = idx;
|
system->tail_idx = idx;
|
||||||
system->emitter_list[idx].next = 0;
|
system->emitter_list[idx].next = 0;
|
||||||
system->emitters[idx] = *emitter;
|
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;
|
||||||
|
angle *= PI / 180;
|
||||||
|
|
||||||
|
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);
|
||||||
|
emitter->particles[i].velocity.y = speed * sin(angle);
|
||||||
|
emitter->particles[i].position = emitter->position;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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 last_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)
|
||||||
|
{
|
||||||
|
if (emitter->particles[i].alive)
|
||||||
|
{
|
||||||
|
if (emitter->config.update_func != NULL)
|
||||||
|
{
|
||||||
|
emitter->config.update_func(emitter->particles + i, emitter->config.user_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lifetime update
|
||||||
|
if (emitter->particles[i].timer > 0) emitter->particles[i].timer--;
|
||||||
|
if (emitter->particles[i].timer == 0)
|
||||||
|
{
|
||||||
|
emitter->particles[i].alive = false;
|
||||||
|
inactive_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
inactive_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (inactive_count == emitter->n_particles)
|
||||||
|
{
|
||||||
|
emitter->active = false;
|
||||||
|
system->emitter_list[last_idx].next = system->emitter_list[emitter_idx].next;
|
||||||
|
system->emitter_list[emitter_idx].next = 0;
|
||||||
|
if (system->tail_idx == emitter_idx)
|
||||||
|
{
|
||||||
|
system->tail_idx = last_idx;
|
||||||
|
}
|
||||||
|
sc_queue_add_last(&system->free_list, emitter_idx);
|
||||||
|
emitter_idx = last_idx;
|
||||||
|
}
|
||||||
|
last_idx = emitter_idx;
|
||||||
|
emitter_idx = next_idx;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void draw_particle_system(ParticleSystem_t* system)
|
void draw_particle_system(ParticleSystem_t* system)
|
||||||
{
|
{
|
||||||
|
uint32_t emitter_idx = system->emitter_list[0].next;
|
||||||
|
|
||||||
|
while (emitter_idx != 0)
|
||||||
|
{
|
||||||
|
ParticleEmitter_t* emitter = system->emitters + emitter_idx;
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < emitter->n_particles; ++i)
|
||||||
|
{
|
||||||
|
if (emitter->particles[i].alive)
|
||||||
|
{
|
||||||
|
DrawCircleV(emitter->particles[i].position, 5, BLACK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
emitter_idx = system->emitter_list[emitter_idx].next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void deinit_particle_system(ParticleSystem_t* system)
|
void deinit_particle_system(ParticleSystem_t* system)
|
||||||
{
|
{
|
||||||
|
|
|
@ -18,24 +18,27 @@ typedef struct Particle
|
||||||
Vector2 velocity;
|
Vector2 velocity;
|
||||||
Vector2 accleration;
|
Vector2 accleration;
|
||||||
float rotation;
|
float rotation;
|
||||||
bool alive;
|
float size;
|
||||||
uint32_t timer;
|
uint32_t timer;
|
||||||
|
bool alive;
|
||||||
}Particle_t;
|
}Particle_t;
|
||||||
|
|
||||||
|
typedef void (*particle_update_func_t)(Particle_t* part, void* user_data);
|
||||||
|
|
||||||
typedef struct EmitterConfig
|
typedef struct EmitterConfig
|
||||||
{
|
{
|
||||||
Vector2 launch_range;
|
float launch_range[2];
|
||||||
Vector2 speed_range;
|
float speed_range[2];
|
||||||
Vector2 position;
|
uint32_t particle_lifetime[2];
|
||||||
uint32_t particle_lifetime;
|
|
||||||
Vector2 gravity;
|
|
||||||
Vector2 friction_coeff;
|
|
||||||
PartEmitterType_t type;
|
PartEmitterType_t type;
|
||||||
|
void* user_data;
|
||||||
|
particle_update_func_t update_func;
|
||||||
}EmitterConfig_t;
|
}EmitterConfig_t;
|
||||||
|
|
||||||
typedef struct ParticleEmitter
|
typedef struct ParticleEmitter
|
||||||
{
|
{
|
||||||
EmitterConfig_t* config;
|
EmitterConfig_t config;
|
||||||
|
Vector2 position;
|
||||||
Particle_t particles[MAX_PARTICLES];
|
Particle_t particles[MAX_PARTICLES];
|
||||||
uint32_t n_particles;
|
uint32_t n_particles;
|
||||||
uint32_t timer;
|
uint32_t timer;
|
||||||
|
@ -51,8 +54,7 @@ typedef struct IndexList
|
||||||
|
|
||||||
typedef struct ParticleSystem
|
typedef struct ParticleSystem
|
||||||
{
|
{
|
||||||
EmitterConfig_t emitter_configs[MAX_PARTICLE_CONF];
|
ParticleEmitter_t emitters[MAX_PARTICLE_EMITTER + 1];
|
||||||
ParticleEmitter_t emitters[MAX_PARTICLE_EMITTER];
|
|
||||||
IndexList_t emitter_list[MAX_PARTICLE_EMITTER + 1];
|
IndexList_t emitter_list[MAX_PARTICLE_EMITTER + 1];
|
||||||
struct sc_queue_64 free_list;
|
struct sc_queue_64 free_list;
|
||||||
uint32_t tail_idx;
|
uint32_t tail_idx;
|
||||||
|
@ -61,7 +63,7 @@ typedef struct ParticleSystem
|
||||||
}ParticleSystem_t;
|
}ParticleSystem_t;
|
||||||
|
|
||||||
void init_particle_system(ParticleSystem_t* system);
|
void init_particle_system(ParticleSystem_t* system);
|
||||||
void add_particle_emitter(ParticleSystem_t* system, ParticleEmitter_t* emitter);
|
void add_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter);
|
||||||
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);
|
||||||
|
|
|
@ -2,16 +2,80 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
#include "raymath.h"
|
||||||
|
#include "constants.h"
|
||||||
|
static const Vector2 GRAVITY = {0, GRAV_ACCEL};
|
||||||
|
|
||||||
|
void simple_particle_system_update(Particle_t* part, void* user_data)
|
||||||
|
{
|
||||||
|
|
||||||
|
float delta_time = DELTA_T; // TODO: Will need to think about delta time handling
|
||||||
|
part->velocity =
|
||||||
|
Vector2Add(
|
||||||
|
part->velocity,
|
||||||
|
Vector2Scale(GRAVITY, delta_time)
|
||||||
|
);
|
||||||
|
|
||||||
|
float mag = Vector2Length(part->velocity);
|
||||||
|
part->velocity = Vector2Scale(
|
||||||
|
Vector2Normalize(part->velocity),
|
||||||
|
(mag > PLAYER_MAX_SPEED)? PLAYER_MAX_SPEED:mag
|
||||||
|
);
|
||||||
|
// 3 dp precision
|
||||||
|
if (fabs(part->velocity.x) < 1e-3) part->velocity.x = 0;
|
||||||
|
if (fabs(part->velocity.y) < 1e-3) part->velocity.y = 0;
|
||||||
|
|
||||||
|
part->position = Vector2Add(
|
||||||
|
part->position,
|
||||||
|
Vector2Scale(part->velocity, delta_time)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Level boundary collision
|
||||||
|
{
|
||||||
|
|
||||||
|
if(
|
||||||
|
part->position.x + part->size < 0 || part->position.x - part->size > 1280
|
||||||
|
|| part->position.y + part->size < 0 || part->position.y - part->size > 640
|
||||||
|
)
|
||||||
|
{
|
||||||
|
part->timer = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
InitWindow(1280, 640, "raylib");
|
InitWindow(1280, 640, "raylib");
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
static ParticleSystem_t part_sys = {0
|
static ParticleSystem_t part_sys = {0};
|
||||||
|
|
||||||
|
init_particle_system(&part_sys);
|
||||||
|
|
||||||
|
EmitterConfig_t conf ={
|
||||||
|
.launch_range = {0, 360},
|
||||||
|
.speed_range = {400, 2000},
|
||||||
|
.particle_lifetime = {30, 110},
|
||||||
|
.update_func = &simple_particle_system_update
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ParticleEmitter_t emitter = {
|
||||||
|
.config = conf,
|
||||||
|
.n_particles = MAX_PARTICLES,
|
||||||
|
.one_shot = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
bool key_press = false;
|
||||||
while(!WindowShouldClose())
|
while(!WindowShouldClose())
|
||||||
{
|
{
|
||||||
|
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
||||||
|
{
|
||||||
|
key_press = true;
|
||||||
|
}
|
||||||
|
else if (key_press && IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
|
||||||
|
{
|
||||||
|
emitter.position = GetMousePosition();
|
||||||
|
add_particle_emitter(&part_sys, &emitter);
|
||||||
|
key_press = false;
|
||||||
|
}
|
||||||
update_particle_system(&part_sys);
|
update_particle_system(&part_sys);
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
|
|
Loading…
Reference in New Issue