prosperon/source/engine/particle.c

123 lines
3.5 KiB
C
Raw Normal View History

2022-12-28 16:50:54 -06:00
#include "particle.h"
#include "stb_ds.h"
2023-12-29 19:08:53 -06:00
#include "render.h"
2024-01-01 17:30:42 -06:00
#include "2dphysics.h"
2024-01-04 09:46:28 -06:00
#include "math.h"
2024-05-06 21:59:22 -05:00
#include "log.h"
2023-12-29 19:08:53 -06:00
emitter *make_emitter() {
2024-01-04 08:14:37 -06:00
emitter *e = calloc(sizeof(*e),1);
2023-12-29 19:08:53 -06:00
e->max = 20;
2024-05-06 21:59:22 -05:00
arrsetcap(e->particles, 10);
2023-12-29 19:08:53 -06:00
for (int i = 0; i < arrlen(e->particles); i++)
e->particles[i].life = 0;
e->life = 10;
2024-01-02 07:55:22 -06:00
e->tte = lerp(e->explosiveness, e->life/e->max, 0);
2024-01-01 17:30:42 -06:00
e->scale = 1;
e->speed = 20;
2024-05-06 21:59:22 -05:00
e->buffer = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(struct par_vert),
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM
});
2023-05-12 13:22:05 -05:00
return e;
2022-12-28 16:50:54 -06:00
}
2024-01-14 10:24:31 -06:00
void emitter_free(emitter *e)
2023-12-29 19:08:53 -06:00
{
arrfree(e->particles);
2024-05-06 21:59:22 -05:00
arrfree(e->verts);
free(e);
2022-12-28 16:50:54 -06:00
}
2024-01-04 09:46:28 -06:00
/* Variate a value around variance. Variance between 0 and 1. */
float variate(float val, float variance)
{
return val + val*(frand(variance)-(variance/2));
}
2024-05-06 21:59:22 -05:00
int emitter_spawn(emitter *e, transform2d *t)
2023-12-29 19:08:53 -06:00
{
2024-01-01 17:30:42 -06:00
particle p;
p.life = e->life;
2024-05-06 21:59:22 -05:00
p.pos = (HMM_Vec4){t->pos.x,t->pos.y,0,0};
float newan = t->angle * HMM_TurnToRad*(frand(e->divergence)-(e->divergence/2));
2024-01-04 09:46:28 -06:00
HMM_Vec2 norm = HMM_V2Rotate((HMM_Vec2){0,1}, newan);
p.v = HMM_MulV4F((HMM_Vec4){norm.x,norm.y,0,0}, variate(e->speed, e->variation));
2024-05-06 21:59:22 -05:00
p.angle = newan;
p.scale = variate(e->scale*t->scale.x, e->scale_var);
2024-01-01 17:30:42 -06:00
arrput(e->particles,p);
return 1;
2023-12-29 19:08:53 -06:00
}
2024-05-06 21:59:22 -05:00
void emitter_emit(emitter *e, int count, transform2d *t)
2023-12-29 19:08:53 -06:00
{
for (int i = 0; i < count; i++)
2024-05-06 21:59:22 -05:00
emitter_spawn(e, t);
2023-12-29 19:08:53 -06:00
}
2024-05-06 21:59:22 -05:00
void emitter_draw(emitter *e, sg_bindings bind)
2023-12-29 19:08:53 -06:00
{
2024-05-06 21:59:22 -05:00
if (arrlen(e->particles) == 0) return;
arrsetlen(e->verts, arrlen(e->particles));
for (int i = 0; i < arrlen(e->particles); i++) {
2024-01-04 09:46:28 -06:00
if (e->particles[i].time >= e->particles[i].life) continue;
2024-05-06 21:59:22 -05:00
particle *p = e->particles+i;
e->verts[i].pos = p->pos.xy;
e->verts[i].angle = p->angle;
e->verts[i].scale = p->scale;
/* if (p->time < e->grow_for)
e->verts[i].scale = lerp(p->time/e->grow_for, 0, p->scale);
2024-01-04 09:46:28 -06:00
else if (p->time > (p->life - e->shrink_for))
2024-05-06 21:59:22 -05:00
e->verts[i].scale = lerp((p->time-(p->life-e->shrink_for))/e->shrink_for, p->scale, 0);*/
e->verts[i].color = vec2rgba(p->color);
2024-01-03 12:29:27 -06:00
}
2024-01-03 08:38:17 -06:00
2024-05-06 21:59:22 -05:00
sg_range verts;
verts.ptr = e->verts;
verts.size = sizeof(*e->verts)*arrlen(e->verts);
if (sg_query_buffer_will_overflow(e->buffer, verts.size))
e->buffer = sg_make_buffer(&(sg_buffer_desc){
.size = verts.size,
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM
});
2024-01-03 08:38:17 -06:00
2024-05-06 21:59:22 -05:00
sg_append_buffer(e->buffer, &verts);
bind.vertex_buffers[0] = e->buffer;
sg_apply_bindings(&bind);
sg_draw(0,4,arrlen(e->verts));
2023-12-29 19:08:53 -06:00
}
2024-05-06 21:59:22 -05:00
void emitter_step(emitter *e, double dt, transform2d *t) {
HMM_Vec4 g_accel = HMM_MulV4F((HMM_Vec4){cpSpaceGetGravity(space).x, cpSpaceGetGravity(space).y, 0, 0}, dt);
for (int i = 0; i < arrlen(e->particles); i++) {
2024-01-04 09:46:28 -06:00
if (e->particles[i].time >= e->particles[i].life) continue;
2024-05-06 21:59:22 -05:00
//if (e->warp_mask & gravmask)
// e->particles[i].v = HMM_AddV4(e->particles[i].v, g_accel);
2024-01-04 08:14:37 -06:00
2024-01-03 12:29:27 -06:00
e->particles[i].pos = HMM_AddV4(e->particles[i].pos, HMM_MulV4F(e->particles[i].v, dt));
e->particles[i].angle += e->particles[i].av*dt;
2024-01-04 09:46:28 -06:00
e->particles[i].time += dt;
e->particles[i].color = sample_sampler(&e->color, e->particles[i].time/e->particles[i].life);
2024-01-03 12:29:27 -06:00
e->particles[i].scale = e->scale;
2024-05-06 21:59:22 -05:00
2024-01-04 09:46:28 -06:00
if (e->particles[i].time >= e->particles[i].life)
arrdelswap(e->particles, i);
else if (query_point(e->particles[i].pos.xy))
arrdelswap(e->particles,i);
2023-12-29 19:08:53 -06:00
}
2022-12-28 16:50:54 -06:00
2023-12-29 19:08:53 -06:00
e->tte-=dt;
if (e->tte <= 0) {
2024-05-06 21:59:22 -05:00
emitter_spawn(e, t);
2024-01-02 07:55:22 -06:00
e->tte = lerp(e->explosiveness, e->life/e->max,0);
2023-05-12 13:22:05 -05:00
}
}