diff --git a/engine/engine_conf.h b/engine/engine_conf.h index 797f314..8f0126c 100644 --- a/engine/engine_conf.h +++ b/engine/engine_conf.h @@ -9,6 +9,9 @@ #define MAX_NAME_LEN 32 #define MAX_LEVEL_PACK 4 #define N_SFX 18 +#define MAX_PARTICLE_CONF 8 +#define MAX_PARTICLE_EMITTER 16 +#define MAX_PARTICLES 10 #define MAX_TILE_TYPES 16 diff --git a/engine/particle_sys.c b/engine/particle_sys.c new file mode 100644 index 0000000..3e05d73 --- /dev/null +++ b/engine/particle_sys.c @@ -0,0 +1,32 @@ +#include "particle_sys.h" + +#include + +void init_particle_system(ParticleSystem_t* system) +{ + memset(system, 0, sizeof(ParticleSystem_t)); + sc_queue_init(&system->free_list); + for ( uint32_t i = 0; i < MAX_PARTICLE_EMITTER; ++i) + { + sc_queue_add_last(&system->free_list, i); + } +} +void add_particle_emitter(ParticleSystem_t* system, ParticleEmitter_t* emitter) +{ + 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] = *emitter; +} +void update_particle_system(ParticleSystem_t* system) +{ +} +void draw_particle_system(ParticleSystem_t* system) +{ +} +void deinit_particle_system(ParticleSystem_t* system) +{ + sc_queue_term(&system->free_list); +} diff --git a/engine/particle_sys.h b/engine/particle_sys.h new file mode 100644 index 0000000..2f3b987 --- /dev/null +++ b/engine/particle_sys.h @@ -0,0 +1,68 @@ +#ifndef _PARTICLE_SYSTEM_H +#define _PARTICLE_SYSTEM_H +#include "raylib.h" +#include "engine_conf.h" +#include "sc_queue.h" +#include +#include + +typedef enum PartEmitterType +{ + EMITTER_BURST = 0, +} PartEmitterType_t; + +typedef struct Particle +{ + Texture2D* tex; + Vector2 position; + Vector2 velocity; + Vector2 accleration; + float rotation; + bool alive; + uint32_t timer; +}Particle_t; + +typedef struct EmitterConfig +{ + Vector2 launch_range; + Vector2 speed_range; + Vector2 position; + uint32_t particle_lifetime; + Vector2 gravity; + Vector2 friction_coeff; + PartEmitterType_t type; +}EmitterConfig_t; + +typedef struct ParticleEmitter +{ + EmitterConfig_t* config; + Particle_t particles[MAX_PARTICLES]; + uint32_t n_particles; + uint32_t timer; + bool one_shot; + bool finished; + bool active; +}ParticleEmitter_t; + +typedef struct IndexList +{ + uint32_t next; +}IndexList_t; + +typedef struct ParticleSystem +{ + EmitterConfig_t emitter_configs[MAX_PARTICLE_CONF]; + ParticleEmitter_t emitters[MAX_PARTICLE_EMITTER]; + IndexList_t emitter_list[MAX_PARTICLE_EMITTER + 1]; + struct sc_queue_64 free_list; + uint32_t tail_idx; + uint32_t n_configs; + uint32_t n_emitters; +}ParticleSystem_t; + +void init_particle_system(ParticleSystem_t* system); +void add_particle_emitter(ParticleSystem_t* system, ParticleEmitter_t* emitter); +void update_particle_system(ParticleSystem_t* system); +void draw_particle_system(ParticleSystem_t* system); +void deinit_particle_system(ParticleSystem_t* system); +#endif // _PARTICLE_SYSTEM_H diff --git a/particle_test.c b/particle_test.c new file mode 100644 index 0000000..3b8ca9a --- /dev/null +++ b/particle_test.c @@ -0,0 +1,23 @@ +#include "particle_sys.h" +#include +#include +#include "raylib.h" + +int main(void) +{ + InitWindow(1280, 640, "raylib"); + SetTargetFPS(60); + static ParticleSystem_t part_sys = {0 + }; + + while(!WindowShouldClose()) + { + update_particle_system(&part_sys); + + BeginDrawing(); + ClearBackground(RAYWHITE); + draw_particle_system(&part_sys); + EndDrawing(); + } + CloseWindow(); +}