libpsys

view src/pattr.c @ 10:0f83e882fd04

install target
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 29 Aug 2012 03:16:58 +0300
parents a10f19674147
children 1b0db53d5b5b
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);
11 static void *tex_cls;
12 static unsigned int (*load_texture)(const char*, void*) = psys_gl_load_texture;
13 static void (*unload_texture)(unsigned int, void*) = psys_gl_unload_texture;
15 void psys_texture_loader(unsigned int (*load)(const char*, void*), void (*unload)(unsigned int, void*), void *cls)
16 {
17 load_texture = load;
18 unload_texture = unload;
19 tex_cls = cls;
20 }
22 int psys_init_attr(struct psys_attributes *attr)
23 {
24 memset(attr, 0, sizeof *attr);
26 if(psys_init_track3(&attr->spawn_range) == -1)
27 goto err;
28 if(psys_init_track(&attr->rate) == -1)
29 goto err;
30 if(psys_init_anm_rnd(&attr->life) == -1)
31 goto err;
32 if(psys_init_anm_rnd(&attr->size) == -1)
33 goto err;
34 if(psys_init_anm_rnd3(&attr->dir) == -1)
35 goto err;
36 if(psys_init_track3(&attr->grav) == -1)
37 goto err;
39 if(init_particle_attr(&attr->part_attr) == -1)
40 goto err;
42 attr->max_particles = -1;
44 anm_set_track_default(&attr->size.value.trk, 1.0);
45 anm_set_track_default(&attr->life.value.trk, 1.0);
47 return 0;
49 err:
50 psys_destroy_attr(attr);
51 return -1;
52 }
55 static int init_particle_attr(struct psys_particle_attributes *pattr)
56 {
57 if(psys_init_track3(&pattr->color) == -1) {
58 return -1;
59 }
60 if(psys_init_track(&pattr->alpha) == -1) {
61 psys_destroy_track3(&pattr->color);
62 return -1;
63 }
64 if(psys_init_track(&pattr->size) == -1) {
65 psys_destroy_track3(&pattr->color);
66 psys_destroy_track(&pattr->alpha);
67 return -1;
68 }
70 anm_set_track_default(&pattr->color.x, 1.0);
71 anm_set_track_default(&pattr->color.y, 1.0);
72 anm_set_track_default(&pattr->color.z, 1.0);
73 anm_set_track_default(&pattr->alpha.trk, 1.0);
74 anm_set_track_default(&pattr->size.trk, 1.0);
75 return 0;
76 }
79 void psys_destroy_attr(struct psys_attributes *attr)
80 {
81 psys_destroy_track3(&attr->spawn_range);
82 psys_destroy_track(&attr->rate);
83 psys_destroy_anm_rnd(&attr->life);
84 psys_destroy_anm_rnd(&attr->size);
85 psys_destroy_anm_rnd3(&attr->dir);
86 psys_destroy_track3(&attr->grav);
88 destroy_particle_attr(&attr->part_attr);
90 if(attr->tex && unload_texture) {
91 unload_texture(attr->tex, tex_cls);
92 }
93 }
95 static void destroy_particle_attr(struct psys_particle_attributes *pattr)
96 {
97 psys_destroy_track3(&pattr->color);
98 psys_destroy_track(&pattr->alpha);
99 psys_destroy_track(&pattr->size);
100 }
102 void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm)
103 {
104 psys_eval_track3(&attr->spawn_range, tm);
105 psys_eval_track(&attr->rate, tm);
106 psys_eval_anm_rnd(&attr->life, tm);
107 psys_eval_anm_rnd(&attr->size, tm);
108 psys_eval_anm_rnd3(&attr->dir, tm);
109 psys_eval_track3(&attr->grav, tm);
110 }
112 int psys_load_attr(struct psys_attributes *attr, const char *fname)
113 {
114 FILE *fp;
115 int res;
117 if(!(fp = fopen(fname, "r"))) {
118 fprintf(stderr, "%s: failed to read file: %s: %s\n", __func__, fname, strerror(errno));
119 return -1;
120 }
121 res = psys_load_attr_stream(attr, fp);
122 fclose(fp);
123 return res;
124 }
126 int psys_load_attr_stream(struct psys_attributes *attr, FILE *fp)
127 {
128 return -1; /* TODO */
129 }
132 int psys_save_attr(struct psys_attributes *attr, const char *fname)
133 {
134 FILE *fp;
135 int res;
137 if(!(fp = fopen(fname, "w"))) {
138 fprintf(stderr, "%s: failed to write file: %s: %s\n", __func__, fname, strerror(errno));
139 return -1;
140 }
141 res = psys_save_attr_stream(attr, fp);
142 fclose(fp);
143 return res;
144 }
146 int psys_save_attr_stream(struct psys_attributes *attr, FILE *fp)
147 {
148 return -1; /* TODO */
149 }