libpsys

view src/pattr.c @ 13:1b0db53d5b5b

started writting config file for the particle system attributes
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 06 Sep 2012 02:32:58 +0300
parents 9c24273f211b
children 4f36bbbcc82f
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 int init_particle_attr(struct psys_particle_attributes *pattr);
9 static void destroy_particle_attr(struct psys_particle_attributes *pattr);
10 static char *stripspace(char *str);
12 static void *tex_cls;
13 static unsigned int (*load_texture)(const char*, void*) = psys_gl_load_texture;
14 static void (*unload_texture)(unsigned int, void*) = psys_gl_unload_texture;
16 void psys_texture_loader(unsigned int (*load)(const char*, void*), void (*unload)(unsigned int, void*), void *cls)
17 {
18 load_texture = load;
19 unload_texture = unload;
20 tex_cls = cls;
21 }
23 int psys_init_attr(struct psys_attributes *attr)
24 {
25 memset(attr, 0, sizeof *attr);
27 if(psys_init_track3(&attr->spawn_range) == -1)
28 goto err;
29 if(psys_init_track(&attr->rate) == -1)
30 goto err;
31 if(psys_init_anm_rnd(&attr->life) == -1)
32 goto err;
33 if(psys_init_anm_rnd(&attr->size) == -1)
34 goto err;
35 if(psys_init_anm_rnd3(&attr->dir) == -1)
36 goto err;
37 if(psys_init_track3(&attr->grav) == -1)
38 goto err;
40 if(init_particle_attr(&attr->part_attr) == -1)
41 goto err;
43 attr->max_particles = -1;
45 anm_set_track_default(&attr->size.value.trk, 1.0);
46 anm_set_track_default(&attr->life.value.trk, 1.0);
48 return 0;
50 err:
51 psys_destroy_attr(attr);
52 return -1;
53 }
56 static int init_particle_attr(struct psys_particle_attributes *pattr)
57 {
58 if(psys_init_track3(&pattr->color) == -1) {
59 return -1;
60 }
61 if(psys_init_track(&pattr->alpha) == -1) {
62 psys_destroy_track3(&pattr->color);
63 return -1;
64 }
65 if(psys_init_track(&pattr->size) == -1) {
66 psys_destroy_track3(&pattr->color);
67 psys_destroy_track(&pattr->alpha);
68 return -1;
69 }
71 anm_set_track_default(&pattr->color.x, 1.0);
72 anm_set_track_default(&pattr->color.y, 1.0);
73 anm_set_track_default(&pattr->color.z, 1.0);
74 anm_set_track_default(&pattr->alpha.trk, 1.0);
75 anm_set_track_default(&pattr->size.trk, 1.0);
76 return 0;
77 }
80 void psys_destroy_attr(struct psys_attributes *attr)
81 {
82 psys_destroy_track3(&attr->spawn_range);
83 psys_destroy_track(&attr->rate);
84 psys_destroy_anm_rnd(&attr->life);
85 psys_destroy_anm_rnd(&attr->size);
86 psys_destroy_anm_rnd3(&attr->dir);
87 psys_destroy_track3(&attr->grav);
89 destroy_particle_attr(&attr->part_attr);
91 if(attr->tex && unload_texture) {
92 unload_texture(attr->tex, tex_cls);
93 }
94 }
96 static void destroy_particle_attr(struct psys_particle_attributes *pattr)
97 {
98 psys_destroy_track3(&pattr->color);
99 psys_destroy_track(&pattr->alpha);
100 psys_destroy_track(&pattr->size);
101 }
103 void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm)
104 {
105 psys_eval_track3(&attr->spawn_range, tm);
106 psys_eval_track(&attr->rate, tm);
107 psys_eval_anm_rnd(&attr->life, tm);
108 psys_eval_anm_rnd(&attr->size, tm);
109 psys_eval_anm_rnd3(&attr->dir, tm);
110 psys_eval_track3(&attr->grav, tm);
111 }
113 int psys_load_attr(struct psys_attributes *attr, const char *fname)
114 {
115 FILE *fp;
116 int res;
118 if(!(fp = fopen(fname, "r"))) {
119 fprintf(stderr, "%s: failed to read file: %s: %s\n", __func__, fname, strerror(errno));
120 return -1;
121 }
122 res = psys_load_attr_stream(attr, fp);
123 fclose(fp);
124 return res;
125 }
127 int psys_load_attr_stream(struct psys_attributes *attr, FILE *fp)
128 {
129 int lineno = 0;
130 char buf[512];
132 psys_init_attr(attr);
134 while(fgets(buf, sizeof buf, fp)) {
135 char *key, *valstr;
137 lineno++;
139 key = stripspace(buf);
140 if(key[0] == '#' || !key[0]) {
141 continue; // skip empty lines and comments
142 }
144 if(!(valstr = strchr(buf, '='))) {
145 fprintf(stderr "%s: invalid input: %s\n", __func__, line);
146 return -1;
147 }
148 *valstr++ = 0;
149 valstr = stripspace(valstr);
152 }
153 }
156 int psys_save_attr(struct psys_attributes *attr, const char *fname)
157 {
158 FILE *fp;
159 int res;
161 if(!(fp = fopen(fname, "w"))) {
162 fprintf(stderr, "%s: failed to write file: %s: %s\n", __func__, fname, strerror(errno));
163 return -1;
164 }
165 res = psys_save_attr_stream(attr, fp);
166 fclose(fp);
167 return res;
168 }
170 int psys_save_attr_stream(struct psys_attributes *attr, FILE *fp)
171 {
172 return -1; /* TODO */
173 }
176 static char *stripspace(char *str)
177 {
178 char *end;
180 while(*str && isspace(*str)) {
181 str++;
182 }
184 end = str + strlen(str);
185 while(end >= str && isspace(*end)) {
186 *end-- = 0;
187 }
188 return str;
189 }