# HG changeset patch # User John Tsiombikas # Date 1317098574 -10800 # Node ID d774738f50f6009eba30204f58fa4f5580c75626 # Parent 613d2bf3ea1fb97dd80de802ed8c5b05e77c56a7 almost finished with the reorg (2) diff -r 613d2bf3ea1f -r d774738f50f6 src/pattr.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pattr.c Tue Sep 27 07:42:54 2011 +0300 @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include "pattr.h" +#include "psys_gl.h" + +static void *tex_cls; +static unsigned int (*load_texture)(const char*, void*) = psys_gl_load_texture; +static void (*unload_texture)(unsigned int, void*) = psys_gl_unload_texture; + +void psys_texture_loader(unsigned int (*load)(const char*, void*), void (*unload)(unsigned int, void*), void *cls) +{ + load_texture = load; + unload_texture = unload; + tex_cls = cls; +} + +int psys_init_attr(struct psys_attributes *attr) +{ + memset(attr, 0, sizeof *attr); + + if(psys_init_track3(&attr->spawn_range) == -1) + goto err; + if(psys_init_track(&attr->rate) == -1) + goto err; + if(psys_init_anm_rnd(&attr->life) == -1) + goto err; + if(psys_init_anm_rnd(&attr->size) == -1) + goto err; + if(psys_init_anm_rnd3(&attr->dir) == -1) + goto err; + if(psys_init_track3(&attr->grav) == -1) + goto err; + + attr->max_particles = -1; + return 0; + +err: + psys_destroy_attr(attr); + return -1; +} + +void psys_destroy_attr(struct psys_attributes *attr) +{ + psys_destroy_track3(&attr->spawn_range); + psys_destroy_track(&attr->rate); + psys_destroy_anm_rnd(&attr->life); + psys_destroy_anm_rnd(&attr->size); + psys_destroy_anm_rnd3(&attr->dir); + psys_destroy_track3(&attr->grav); + + if(attr->tex && unload_texture) { + unload_texture(attr->tex, tex_cls); + } +} + +void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm) +{ + psys_eval_track3(&attr->spawn_range, tm); + psys_eval_track(&attr->rate, tm); + psys_eval_anm_rnd(&attr->life, tm); + psys_eval_anm_rnd(&attr->size, tm); + psys_eval_anm_rnd3(&attr->dir, tm); + psys_eval_track3(&attr->grav, tm); +} + +int psys_load_attr(struct psys_attributes *attr, const char *fname) +{ + FILE *fp; + int res; + + if(!(fp = fopen(fname, "r"))) { + fprintf(stderr, "%s: failed to read file: %s: %s\n", __func__, fname, strerror(errno)); + return -1; + } + res = psys_load_attr_stream(attr, fp); + fclose(fp); + return res; +} + +int psys_load_attr_stream(struct psys_attributes *attr, FILE *fp) +{ + return -1; /* TODO */ +} + + +int psys_save_attr(struct psys_attributes *attr, const char *fname) +{ + FILE *fp; + int res; + + if(!(fp = fopen(fname, "w"))) { + fprintf(stderr, "%s: failed to write file: %s: %s\n", __func__, fname, strerror(errno)); + return -1; + } + res = psys_save_attr_stream(attr, fp); + fclose(fp); + return res; +} + +int psys_save_attr_stream(struct psys_attributes *attr, FILE *fp) +{ + return -1; /* TODO */ +} diff -r 613d2bf3ea1f -r d774738f50f6 src/pattr.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pattr.h Tue Sep 27 07:42:54 2011 +0300 @@ -0,0 +1,37 @@ +#ifndef PATTR_H_ +#define PATTR_H_ + +#include "pstrack.h" +#include "rndval.h" + +struct psys_attributes { + unsigned int tex; /* OpenGL texture to use for the billboard */ + + struct psys_track3 spawn_range; /* radius of emmiter */ + struct psys_track rate; /* spawn rate particles per sec */ + struct psys_anm_rnd life; /* particle life in seconds */ + struct psys_anm_rnd size; /* base particle size */ + struct psys_anm_rnd3 dir; /* particle shoot direction */ + + struct psys_track3 grav; /* external force (usually gravity) */ + + float drag; /* I don't think this needs to animate */ + + /* limits */ + int max_particles; +}; + +void psys_texture_loader(unsigned int (*load)(const char*, void*), void (*unload)(unsigned int, void*), void *cls); + +int psys_init_attr(struct psys_attributes *attr); +void psys_destroy_attr(struct psys_attributes *attr); + +void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm); + +int psys_load_attr(struct psys_attributes *attr, const char *fname); +int psys_load_attr_stream(struct psys_attributes *attr, FILE *fp); + +int psys_save_attr(struct psys_attributes *attr, const char *fname); +int psys_save_attr_stream(struct psys_attributes *attr, FILE *fp); + +#endif diff -r 613d2bf3ea1f -r d774738f50f6 src/psys_gl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/psys_gl.h Tue Sep 27 07:42:54 2011 +0300 @@ -0,0 +1,14 @@ +#ifndef PSYS_GL_H_ +#define PSYS_GL_H_ + +struct psys_particle; +struct psys_emitter; + +void psys_gl_draw_start(struct psys_emitter *em, void *cls); +void psys_gl_draw(struct psys_emitter *em, struct psys_particle *p, void *cls); +void psys_gl_draw_end(struct psys_emitter *em, void *cls); + +unsigned int psys_gl_load_texture(const char *fname, void *cls); +void psys_gl_unload_texture(unsigned int tex, void *cls); + +#endif /* PSYS_GL_H_ */