libpsys
diff src/pattr.c @ 6:d774738f50f6
almost finished with the reorg (2)
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Tue, 27 Sep 2011 07:42:54 +0300 |
parents | |
children | a10f19674147 |
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 +}