libpsys

view src/pattr.c @ 8:a10f19674147

ha!
author John Tsiombikas <nuclear@mutantstargoat.com>
date Tue, 27 Sep 2011 21:47:27 +0300
parents d774738f50f6
children 9c24273f211b
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;
38 anm_set_track_default(&attr->size.value.trk, 1.0);
39 anm_set_track_default(&attr->life.value.trk, 1.0);
41 return 0;
43 err:
44 psys_destroy_attr(attr);
45 return -1;
46 }
48 void psys_destroy_attr(struct psys_attributes *attr)
49 {
50 psys_destroy_track3(&attr->spawn_range);
51 psys_destroy_track(&attr->rate);
52 psys_destroy_anm_rnd(&attr->life);
53 psys_destroy_anm_rnd(&attr->size);
54 psys_destroy_anm_rnd3(&attr->dir);
55 psys_destroy_track3(&attr->grav);
57 if(attr->tex && unload_texture) {
58 unload_texture(attr->tex, tex_cls);
59 }
60 }
62 void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm)
63 {
64 psys_eval_track3(&attr->spawn_range, tm);
65 psys_eval_track(&attr->rate, tm);
66 psys_eval_anm_rnd(&attr->life, tm);
67 psys_eval_anm_rnd(&attr->size, tm);
68 psys_eval_anm_rnd3(&attr->dir, tm);
69 psys_eval_track3(&attr->grav, tm);
70 }
72 int psys_load_attr(struct psys_attributes *attr, const char *fname)
73 {
74 FILE *fp;
75 int res;
77 if(!(fp = fopen(fname, "r"))) {
78 fprintf(stderr, "%s: failed to read file: %s: %s\n", __func__, fname, strerror(errno));
79 return -1;
80 }
81 res = psys_load_attr_stream(attr, fp);
82 fclose(fp);
83 return res;
84 }
86 int psys_load_attr_stream(struct psys_attributes *attr, FILE *fp)
87 {
88 return -1; /* TODO */
89 }
92 int psys_save_attr(struct psys_attributes *attr, const char *fname)
93 {
94 FILE *fp;
95 int res;
97 if(!(fp = fopen(fname, "w"))) {
98 fprintf(stderr, "%s: failed to write file: %s: %s\n", __func__, fname, strerror(errno));
99 return -1;
100 }
101 res = psys_save_attr_stream(attr, fp);
102 fclose(fp);
103 return res;
104 }
106 int psys_save_attr_stream(struct psys_attributes *attr, FILE *fp)
107 {
108 return -1; /* TODO */
109 }