libpsys
changeset 6:d774738f50f6
almost finished with the reorg (2)
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Tue, 27 Sep 2011 07:42:54 +0300 |
parents | 613d2bf3ea1f |
children | 3c0a306c5f01 |
files | src/pattr.c src/pattr.h src/psys_gl.h |
diffstat | 3 files changed, 156 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/pattr.c Tue Sep 27 07:42:54 2011 +0300 1.3 @@ -0,0 +1,105 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <errno.h> 1.8 +#include "pattr.h" 1.9 +#include "psys_gl.h" 1.10 + 1.11 +static void *tex_cls; 1.12 +static unsigned int (*load_texture)(const char*, void*) = psys_gl_load_texture; 1.13 +static void (*unload_texture)(unsigned int, void*) = psys_gl_unload_texture; 1.14 + 1.15 +void psys_texture_loader(unsigned int (*load)(const char*, void*), void (*unload)(unsigned int, void*), void *cls) 1.16 +{ 1.17 + load_texture = load; 1.18 + unload_texture = unload; 1.19 + tex_cls = cls; 1.20 +} 1.21 + 1.22 +int psys_init_attr(struct psys_attributes *attr) 1.23 +{ 1.24 + memset(attr, 0, sizeof *attr); 1.25 + 1.26 + if(psys_init_track3(&attr->spawn_range) == -1) 1.27 + goto err; 1.28 + if(psys_init_track(&attr->rate) == -1) 1.29 + goto err; 1.30 + if(psys_init_anm_rnd(&attr->life) == -1) 1.31 + goto err; 1.32 + if(psys_init_anm_rnd(&attr->size) == -1) 1.33 + goto err; 1.34 + if(psys_init_anm_rnd3(&attr->dir) == -1) 1.35 + goto err; 1.36 + if(psys_init_track3(&attr->grav) == -1) 1.37 + goto err; 1.38 + 1.39 + attr->max_particles = -1; 1.40 + return 0; 1.41 + 1.42 +err: 1.43 + psys_destroy_attr(attr); 1.44 + return -1; 1.45 +} 1.46 + 1.47 +void psys_destroy_attr(struct psys_attributes *attr) 1.48 +{ 1.49 + psys_destroy_track3(&attr->spawn_range); 1.50 + psys_destroy_track(&attr->rate); 1.51 + psys_destroy_anm_rnd(&attr->life); 1.52 + psys_destroy_anm_rnd(&attr->size); 1.53 + psys_destroy_anm_rnd3(&attr->dir); 1.54 + psys_destroy_track3(&attr->grav); 1.55 + 1.56 + if(attr->tex && unload_texture) { 1.57 + unload_texture(attr->tex, tex_cls); 1.58 + } 1.59 +} 1.60 + 1.61 +void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm) 1.62 +{ 1.63 + psys_eval_track3(&attr->spawn_range, tm); 1.64 + psys_eval_track(&attr->rate, tm); 1.65 + psys_eval_anm_rnd(&attr->life, tm); 1.66 + psys_eval_anm_rnd(&attr->size, tm); 1.67 + psys_eval_anm_rnd3(&attr->dir, tm); 1.68 + psys_eval_track3(&attr->grav, tm); 1.69 +} 1.70 + 1.71 +int psys_load_attr(struct psys_attributes *attr, const char *fname) 1.72 +{ 1.73 + FILE *fp; 1.74 + int res; 1.75 + 1.76 + if(!(fp = fopen(fname, "r"))) { 1.77 + fprintf(stderr, "%s: failed to read file: %s: %s\n", __func__, fname, strerror(errno)); 1.78 + return -1; 1.79 + } 1.80 + res = psys_load_attr_stream(attr, fp); 1.81 + fclose(fp); 1.82 + return res; 1.83 +} 1.84 + 1.85 +int psys_load_attr_stream(struct psys_attributes *attr, FILE *fp) 1.86 +{ 1.87 + return -1; /* TODO */ 1.88 +} 1.89 + 1.90 + 1.91 +int psys_save_attr(struct psys_attributes *attr, const char *fname) 1.92 +{ 1.93 + FILE *fp; 1.94 + int res; 1.95 + 1.96 + if(!(fp = fopen(fname, "w"))) { 1.97 + fprintf(stderr, "%s: failed to write file: %s: %s\n", __func__, fname, strerror(errno)); 1.98 + return -1; 1.99 + } 1.100 + res = psys_save_attr_stream(attr, fp); 1.101 + fclose(fp); 1.102 + return res; 1.103 +} 1.104 + 1.105 +int psys_save_attr_stream(struct psys_attributes *attr, FILE *fp) 1.106 +{ 1.107 + return -1; /* TODO */ 1.108 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/pattr.h Tue Sep 27 07:42:54 2011 +0300 2.3 @@ -0,0 +1,37 @@ 2.4 +#ifndef PATTR_H_ 2.5 +#define PATTR_H_ 2.6 + 2.7 +#include "pstrack.h" 2.8 +#include "rndval.h" 2.9 + 2.10 +struct psys_attributes { 2.11 + unsigned int tex; /* OpenGL texture to use for the billboard */ 2.12 + 2.13 + struct psys_track3 spawn_range; /* radius of emmiter */ 2.14 + struct psys_track rate; /* spawn rate particles per sec */ 2.15 + struct psys_anm_rnd life; /* particle life in seconds */ 2.16 + struct psys_anm_rnd size; /* base particle size */ 2.17 + struct psys_anm_rnd3 dir; /* particle shoot direction */ 2.18 + 2.19 + struct psys_track3 grav; /* external force (usually gravity) */ 2.20 + 2.21 + float drag; /* I don't think this needs to animate */ 2.22 + 2.23 + /* limits */ 2.24 + int max_particles; 2.25 +}; 2.26 + 2.27 +void psys_texture_loader(unsigned int (*load)(const char*, void*), void (*unload)(unsigned int, void*), void *cls); 2.28 + 2.29 +int psys_init_attr(struct psys_attributes *attr); 2.30 +void psys_destroy_attr(struct psys_attributes *attr); 2.31 + 2.32 +void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm); 2.33 + 2.34 +int psys_load_attr(struct psys_attributes *attr, const char *fname); 2.35 +int psys_load_attr_stream(struct psys_attributes *attr, FILE *fp); 2.36 + 2.37 +int psys_save_attr(struct psys_attributes *attr, const char *fname); 2.38 +int psys_save_attr_stream(struct psys_attributes *attr, FILE *fp); 2.39 + 2.40 +#endif
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/psys_gl.h Tue Sep 27 07:42:54 2011 +0300 3.3 @@ -0,0 +1,14 @@ 3.4 +#ifndef PSYS_GL_H_ 3.5 +#define PSYS_GL_H_ 3.6 + 3.7 +struct psys_particle; 3.8 +struct psys_emitter; 3.9 + 3.10 +void psys_gl_draw_start(struct psys_emitter *em, void *cls); 3.11 +void psys_gl_draw(struct psys_emitter *em, struct psys_particle *p, void *cls); 3.12 +void psys_gl_draw_end(struct psys_emitter *em, void *cls); 3.13 + 3.14 +unsigned int psys_gl_load_texture(const char *fname, void *cls); 3.15 +void psys_gl_unload_texture(unsigned int tex, void *cls); 3.16 + 3.17 +#endif /* PSYS_GL_H_ */