vrshoot
view libs/psys/pattr.c @ 1:e7ca128b8713
looks nice :)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Feb 2014 00:35:22 +0200 |
parents | |
children |
line source
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <ctype.h>
8 #ifdef _MSC_VER
9 #include <malloc.h>
10 #else
11 #include <alloca.h>
12 #endif
14 #include "pattr.h"
16 enum {
17 OPT_STR,
18 OPT_NUM,
19 OPT_NUM_RANGE,
20 OPT_VEC,
21 OPT_VEC_RANGE
22 };
24 struct cfgopt {
25 char *name;
26 int type;
27 long tm;
28 char *valstr;
29 vec3_t val, valrng;
30 };
32 static int init_particle_attr(struct psys_particle_attributes *pattr);
33 static void destroy_particle_attr(struct psys_particle_attributes *pattr);
34 static struct cfgopt *get_cfg_opt(const char *line);
35 static void release_cfg_opt(struct cfgopt *opt);
36 static char *stripspace(char *str);
38 static void *tex_cls;
39 static unsigned int (*load_texture)(const char*, void*);
40 static void (*unload_texture)(unsigned int, void*);
43 void psys_texture_loader(unsigned int (*load)(const char*, void*), void (*unload)(unsigned int, void*), void *cls)
44 {
45 load_texture = load;
46 unload_texture = unload;
47 tex_cls = cls;
48 }
50 struct psys_attributes *psys_create_attr(void)
51 {
52 struct psys_attributes *attr = malloc(sizeof *attr);
53 if(attr) {
54 if(psys_init_attr(attr) == -1) {
55 free(attr);
56 attr = 0;
57 }
58 }
59 return attr;
60 }
62 void psys_free_attr(struct psys_attributes *attr)
63 {
64 psys_destroy_attr(attr);
65 free(attr);
66 }
68 int psys_init_attr(struct psys_attributes *attr)
69 {
70 memset(attr, 0, sizeof *attr);
72 if(psys_init_track3(&attr->spawn_range) == -1)
73 goto err;
74 if(psys_init_track(&attr->rate) == -1)
75 goto err;
76 if(psys_init_anm_rnd(&attr->life) == -1)
77 goto err;
78 if(psys_init_anm_rnd(&attr->size) == -1)
79 goto err;
80 if(psys_init_anm_rnd3(&attr->dir) == -1)
81 goto err;
82 if(psys_init_track3(&attr->grav) == -1)
83 goto err;
85 if(init_particle_attr(&attr->part_attr) == -1)
86 goto err;
88 attr->max_particles = -1;
90 anm_set_track_default(&attr->size.value.trk, 1.0);
91 anm_set_track_default(&attr->life.value.trk, 1.0);
93 return 0;
95 err:
96 psys_destroy_attr(attr);
97 return -1;
98 }
101 static int init_particle_attr(struct psys_particle_attributes *pattr)
102 {
103 if(psys_init_track3(&pattr->color) == -1) {
104 return -1;
105 }
106 if(psys_init_track(&pattr->alpha) == -1) {
107 psys_destroy_track3(&pattr->color);
108 return -1;
109 }
110 if(psys_init_track(&pattr->size) == -1) {
111 psys_destroy_track3(&pattr->color);
112 psys_destroy_track(&pattr->alpha);
113 return -1;
114 }
116 anm_set_track_default(&pattr->color.x, 1.0);
117 anm_set_track_default(&pattr->color.y, 1.0);
118 anm_set_track_default(&pattr->color.z, 1.0);
119 anm_set_track_default(&pattr->alpha.trk, 1.0);
120 anm_set_track_default(&pattr->size.trk, 1.0);
121 return 0;
122 }
125 void psys_destroy_attr(struct psys_attributes *attr)
126 {
127 psys_destroy_track3(&attr->spawn_range);
128 psys_destroy_track(&attr->rate);
129 psys_destroy_anm_rnd(&attr->life);
130 psys_destroy_anm_rnd(&attr->size);
131 psys_destroy_anm_rnd3(&attr->dir);
132 psys_destroy_track3(&attr->grav);
134 destroy_particle_attr(&attr->part_attr);
136 if(attr->tex && unload_texture) {
137 unload_texture(attr->tex, tex_cls);
138 }
139 }
141 static void destroy_particle_attr(struct psys_particle_attributes *pattr)
142 {
143 psys_destroy_track3(&pattr->color);
144 psys_destroy_track(&pattr->alpha);
145 psys_destroy_track(&pattr->size);
146 }
148 void psys_copy_attr(struct psys_attributes *dest, const struct psys_attributes *src)
149 {
150 dest->tex = src->tex;
152 psys_copy_track3(&dest->spawn_range, &src->spawn_range);
153 psys_copy_track(&dest->rate, &src->rate);
155 psys_copy_anm_rnd(&dest->life, &src->life);
156 psys_copy_anm_rnd(&dest->size, &src->size);
157 psys_copy_anm_rnd3(&dest->dir, &src->dir);
159 psys_copy_track3(&dest->grav, &src->grav);
161 dest->drag = src->drag;
162 dest->max_particles = src->max_particles;
164 /* also copy the particle attributes */
165 psys_copy_track3(&dest->part_attr.color, &src->part_attr.color);
166 psys_copy_track(&dest->part_attr.alpha, &src->part_attr.alpha);
167 psys_copy_track(&dest->part_attr.size, &src->part_attr.size);
168 }
170 void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm)
171 {
172 psys_eval_track3(&attr->spawn_range, tm);
173 psys_eval_track(&attr->rate, tm);
174 psys_eval_anm_rnd(&attr->life, tm);
175 psys_eval_anm_rnd(&attr->size, tm);
176 psys_eval_anm_rnd3(&attr->dir, tm);
177 psys_eval_track3(&attr->grav, tm);
178 }
180 int psys_load_attr(struct psys_attributes *attr, const char *fname)
181 {
182 FILE *fp;
183 int res;
185 if(!fname) {
186 return -1;
187 }
189 if(!(fp = fopen(fname, "r"))) {
190 fprintf(stderr, "%s: failed to read file: %s: %s\n", __FUNCTION__, fname, strerror(errno));
191 return -1;
192 }
193 res = psys_load_attr_stream(attr, fp);
194 fclose(fp);
195 return res;
196 }
198 int psys_load_attr_stream(struct psys_attributes *attr, FILE *fp)
199 {
200 int lineno = 0;
201 char buf[512];
202 struct cfgopt *opt = 0;
204 psys_init_attr(attr);
206 while(fgets(buf, sizeof buf, fp)) {
208 lineno++;
210 if(!(opt = get_cfg_opt(buf))) {
211 continue;
212 }
214 if(strcmp(opt->name, "texture") == 0) {
215 if(opt->type != OPT_STR) {
216 goto err;
217 }
218 if(!load_texture) {
219 fprintf(stderr, "particle system requests a texture, but no texture loader available!\n");
220 goto err;
221 }
222 if(!(attr->tex = load_texture(opt->valstr, tex_cls))) {
223 fprintf(stderr, "failed to load texture: %s\n", opt->valstr);
224 goto err;
225 }
227 release_cfg_opt(opt);
228 continue;
229 } else if(opt->type == OPT_STR) {
230 fprintf(stderr, "invalid particle config: '%s'\n", opt->name);
231 goto err;
232 }
234 if(strcmp(opt->name, "spawn_range") == 0) {
235 psys_set_value3(&attr->spawn_range, opt->tm, opt->val);
236 } else if(strcmp(opt->name, "rate") == 0) {
237 psys_set_value(&attr->rate, opt->tm, opt->val.x);
238 } else if(strcmp(opt->name, "life") == 0) {
239 psys_set_anm_rnd(&attr->life, opt->tm, opt->val.x, opt->valrng.x);
240 } else if(strcmp(opt->name, "size") == 0) {
241 psys_set_anm_rnd(&attr->size, opt->tm, opt->val.x, opt->valrng.x);
242 } else if(strcmp(opt->name, "dir") == 0) {
243 psys_set_anm_rnd3(&attr->dir, opt->tm, opt->val, opt->valrng);
244 } else if(strcmp(opt->name, "grav") == 0) {
245 psys_set_value3(&attr->grav, opt->tm, opt->val);
246 } else if(strcmp(opt->name, "drag") == 0) {
247 attr->drag = opt->val.x;
248 } else if(strcmp(opt->name, "pcolor") == 0) {
249 psys_set_value3(&attr->part_attr.color, opt->tm, opt->val);
250 } else if(strcmp(opt->name, "palpha") == 0) {
251 psys_set_value(&attr->part_attr.alpha, opt->tm, opt->val.x);
252 } else if(strcmp(opt->name, "psize") == 0) {
253 psys_set_value(&attr->part_attr.size, opt->tm, opt->val.x);
254 } else {
255 fprintf(stderr, "unrecognized particle config option: %s\n", opt->name);
256 goto err;
257 }
259 release_cfg_opt(opt);
260 }
262 return 0;
264 err:
265 fprintf(stderr, "Line %d: error parsing particle definition\n", lineno);
266 release_cfg_opt(opt);
267 return -1;
268 }
270 static struct cfgopt *get_cfg_opt(const char *line)
271 {
272 char *buf, *tmp;
273 struct cfgopt *opt;
275 /* allocate a working buffer on the stack that could fit the current line */
276 buf = alloca(strlen(line) + 1);
278 line = stripspace((char*)line);
279 if(line[0] == '#' || !line[0]) {
280 return 0; /* skip empty lines and comments */
281 }
283 if(!(opt = malloc(sizeof *opt))) {
284 return 0;
285 }
287 if(!(opt->valstr = strchr(line, '='))) {
288 release_cfg_opt(opt);
289 return 0;
290 }
291 *opt->valstr++ = 0;
292 opt->valstr = stripspace(opt->valstr);
294 strcpy(buf, line);
295 buf = stripspace(buf);
297 /* parse the keyframe time specifier if it exists */
298 if((tmp = strchr(buf, '('))) {
299 char *endp;
300 float tval;
302 *tmp++ = 0;
303 opt->name = malloc(strlen(buf) + 1);
304 strcpy(opt->name, buf);
306 tval = strtod(tmp, &endp);
307 if(endp == tmp) { /* nada ... */
308 opt->tm = 0;
309 } else if(*endp == 's') { /* seconds suffix */
310 opt->tm = (long)(tval * 1000.0f);
311 } else {
312 opt->tm = (long)tval;
313 }
314 } else {
315 opt->name = malloc(strlen(buf) + 1);
316 strcpy(opt->name, buf);
317 opt->tm = 0;
318 }
320 if(sscanf(opt->valstr, "[%f %f %f] ~ [%f %f %f]", &opt->val.x, &opt->val.y, &opt->val.z,
321 &opt->valrng.x, &opt->valrng.y, &opt->valrng.z) == 6) {
322 /* value is a vector range */
323 opt->type = OPT_VEC_RANGE;
325 } else if(sscanf(opt->valstr, "%f ~ %f", &opt->val.x, &opt->valrng.x) == 2) {
326 /* value is a number range */
327 opt->type = OPT_NUM_RANGE;
328 opt->val.y = opt->val.z = opt->val.x;
329 opt->valrng.y = opt->valrng.z = opt->valrng.x;
331 } else if(sscanf(opt->valstr, "[%f %f %f]", &opt->val.x, &opt->val.y, &opt->val.z) == 3) {
332 /* value is a vector */
333 opt->type = OPT_VEC;
334 opt->valrng.x = opt->valrng.y = opt->valrng.z = 0.0f;
336 } else if(sscanf(opt->valstr, "%f", &opt->val.x) == 1) {
337 /* value is a number */
338 opt->type = OPT_NUM;
339 opt->val.y = opt->val.z = opt->val.x;
340 opt->valrng.x = opt->valrng.y = opt->valrng.z = 0.0f;
342 } else if(sscanf(opt->valstr, "\"%s\"", buf) == 1) {
343 /* just a string... strip the quotes */
344 if(buf[strlen(buf) - 1] == '\"') {
345 buf[strlen(buf) - 1] = 0;
346 }
347 opt->type = OPT_STR;
348 opt->valstr = strdup(buf);
349 } else {
350 /* fuck it ... */
351 release_cfg_opt(opt);
352 return 0;
353 }
355 return opt;
356 }
358 static void release_cfg_opt(struct cfgopt *opt)
359 {
360 if(opt) {
361 free(opt->name);
362 opt->name = 0;
363 }
364 opt = 0;
365 }
368 int psys_save_attr(const struct psys_attributes *attr, const char *fname)
369 {
370 FILE *fp;
371 int res;
373 if(!(fp = fopen(fname, "w"))) {
374 fprintf(stderr, "%s: failed to write file: %s: %s\n", __FUNCTION__, fname, strerror(errno));
375 return -1;
376 }
377 res = psys_save_attr_stream(attr, fp);
378 fclose(fp);
379 return res;
380 }
382 int psys_save_attr_stream(const struct psys_attributes *attr, FILE *fp)
383 {
384 return -1; /* TODO */
385 }
388 static char *stripspace(char *str)
389 {
390 char *end;
392 while(*str && isspace(*str)) {
393 str++;
394 }
396 end = str + strlen(str) - 1;
397 while(end >= str && isspace(*end)) {
398 *end-- = 0;
399 }
400 return str;
401 }