128 lines
3.2 KiB
C
128 lines
3.2 KiB
C
#include "Particle.h"
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include "report.h"
|
|
|
|
struct Particle *newParticle() {
|
|
struct Particle *particle = malloc(sizeof(struct Particle));
|
|
if (particle == NULL) {
|
|
report(ERROR, "newParticle", "could not malloc Particle");
|
|
return NULL;
|
|
}
|
|
struct Phys phys;
|
|
cleanPhys(&phys);
|
|
particle->phys = phys;
|
|
particle->l_phys = phys;
|
|
cleanAnim(&particle->animation);
|
|
cleanVector(&particle->target);
|
|
particle->time = 10000;
|
|
particle->r = 1.0f;
|
|
particle->g = 1.0f;
|
|
particle->b = 1.0f;
|
|
particle->a = 1.0f;
|
|
particle->flags = PART_DIE;
|
|
return particle;
|
|
}
|
|
struct Particle *freeParticle(struct Particle *particle) {
|
|
free(particle);
|
|
return NULL;
|
|
}
|
|
int setParticleAnim(struct Particle *particle, struct AnimData* data) {
|
|
if (data == NULL) return 1;
|
|
particle->animation.anim = data;
|
|
if (particle->animation.anim->sets[0] != NULL) {
|
|
particle->animation.set = particle->animation.anim->sets[0];
|
|
particle->animation.set_id = 0;
|
|
if (particle->animation.set->faces[0] != NULL) {
|
|
particle->animation.face = particle->animation.set->faces[0];
|
|
particle->animation.face_id = 0;
|
|
particle->animation.sheet = particle->animation.face->sheet;
|
|
if (particle->animation.face->frames[0] != NULL) {
|
|
particle->animation.frame = particle->animation.face->frames[0];
|
|
particle->animation.f = 0;
|
|
} else {
|
|
return 1;
|
|
}
|
|
} else {
|
|
return 1;
|
|
}
|
|
} else {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
struct Particles *newParticles(int l_size) {
|
|
struct Particles *p = malloc(sizeof(struct Particles));
|
|
if (p == NULL) {
|
|
report(ERROR, "newParticles", "could not malloc Particles");
|
|
}
|
|
int i;
|
|
// live
|
|
p->l_count = 0;
|
|
p->l_size = l_size;
|
|
p->live = malloc(sizeof(struct Particle*)*l_size);
|
|
if (p->live == NULL) {
|
|
report(ERROR, "newParticles", "could not malloc live array");
|
|
}
|
|
for (i = 0; i < l_size; i++) {
|
|
p->live[i] = NULL;
|
|
}
|
|
return p;
|
|
}
|
|
struct Particles *freeParticles(struct Particles *p) {
|
|
int i;
|
|
// free live particles
|
|
for (i = 0; i < p->l_size; i++) {
|
|
if (p->live[i] != NULL) {
|
|
delParticle(p, i);
|
|
}
|
|
}
|
|
free(p->live);
|
|
// free particles
|
|
free(p);
|
|
return NULL;
|
|
}
|
|
|
|
int addParticle(struct Particles *ps, struct Particle *p) {
|
|
int i;
|
|
// resize if needed
|
|
while(ps->l_count+1 > ps->l_size) {
|
|
printf("resizing\n");
|
|
ps->live = realloc(ps->live, (ps->l_size+16)*sizeof(struct Particles*));
|
|
for (i = ps->l_size; i < ps->l_size+16; i++) {
|
|
printf("nulled %d\n", i);
|
|
ps->live[i] = NULL;
|
|
}
|
|
ps->l_size += 16;
|
|
}
|
|
// add!
|
|
for (i = 0; i < ps->l_size; i++) {
|
|
if (ps->live[i] == NULL) {
|
|
ps->live[i] = p;
|
|
break;
|
|
}
|
|
}
|
|
ps->l_count++;
|
|
return 0;
|
|
}
|
|
|
|
int remParticle(struct Particles *ps, int id) {
|
|
ps->live[id] = NULL;
|
|
ps->l_count--;
|
|
return 0;
|
|
}
|
|
int delParticle(struct Particles *ps, int id) {
|
|
freeParticle(ps->live[id]);
|
|
ps->live[id] = NULL;
|
|
ps->l_count--;
|
|
printf("deleted %d\n", id);
|
|
return 0;
|
|
}
|
|
|
|
// TODO: implement templates system
|
|
int spawnParticle(struct Particles *ps, struct AnimData *anim, int time, float x, float y) {
|
|
struct Particle *particle = newParticle();
|
|
particle->animation.anim = anim;
|
|
}
|