libpsys

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "pattr.h"
6 #include "psys_gl.h"
8 static void *tex_cls;
9 static unsigned int (*load_texture)(const char*, void*) = psys_gl_load_texture;
10 static void (*unload_texture)(unsigned int, void*) = psys_gl_unload_texture;
12 void psys_texture_loader(unsigned int (*load)(const char*, void*), void (*unload)(unsigned int, void*), void *cls)
13 {
14 load_texture = load;
15 unload_texture = unload;
16 tex_cls = cls;
17 }
19 int psys_init_attr(struct psys_attributes *attr)
20 {
21 memset(attr, 0, sizeof *attr);
23 if(psys_init_track3(&attr->spawn_range) == -1)
24 goto err;
25 if(psys_init_track(&attr->rate) == -1)
26 goto err;
27 if(psys_init_anm_rnd(&attr->life) == -1)
28 goto err;
29 if(psys_init_anm_rnd(&attr->size) == -1)
30 goto err;
31 if(psys_init_anm_rnd3(&attr->dir) == -1)
32 goto err;
33 if(psys_init_track3(&attr->grav) == -1)
34 goto err;
36 attr->max_particles = -1;
37 return 0;
39 err:
40 psys_destroy_attr(attr);
41 return -1;
42 }
44 void psys_destroy_attr(struct psys_attributes *attr)
45 {
46 psys_destroy_track3(&attr->spawn_range);
47 psys_destroy_track(&attr->rate);
48 psys_destroy_anm_rnd(&attr->life);
49 psys_destroy_anm_rnd(&attr->size);
50 psys_destroy_anm_rnd3(&attr->dir);
51 psys_destroy_track3(&attr->grav);
53 if(attr->tex && unload_texture) {
54 unload_texture(attr->tex, tex_cls);
55 }
56 }
58 void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm)
59 {
60 psys_eval_track3(&attr->spawn_range, tm);
61 psys_eval_track(&attr->rate, tm);
62 psys_eval_anm_rnd(&attr->life, tm);
63 psys_eval_anm_rnd(&attr->size, tm);
64 psys_eval_anm_rnd3(&attr->dir, tm);
65 psys_eval_track3(&attr->grav, tm);
66 }
68 int psys_load_attr(struct psys_attributes *attr, const char *fname)
69 {
70 FILE *fp;
71 int res;
73 if(!(fp = fopen(fname, "r"))) {
74 fprintf(stderr, "%s: failed to read file: %s: %s\n", __func__, fname, strerror(errno));
75 return -1;
76 }
77 res = psys_load_attr_stream(attr, fp);
78 fclose(fp);
79 return res;
80 }
82 int psys_load_attr_stream(struct psys_attributes *attr, FILE *fp)
83 {
84 return -1; /* TODO */
85 }
88 int psys_save_attr(struct psys_attributes *attr, const char *fname)
89 {
90 FILE *fp;
91 int res;
93 if(!(fp = fopen(fname, "w"))) {
94 fprintf(stderr, "%s: failed to write file: %s: %s\n", __func__, fname, strerror(errno));
95 return -1;
96 }
97 res = psys_save_attr_stream(attr, fp);
98 fclose(fp);
99 return res;
100 }
102 int psys_save_attr_stream(struct psys_attributes *attr, FILE *fp)
103 {
104 return -1; /* TODO */
105 }