libpsys
view src/psys.c @ 15:5678915dc2c7
probably done with the psys loader
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Mon, 10 Sep 2012 21:57:49 +0300 |
parents | a10f19674147 |
children | fbccc3c0d43e |
line source
1 #include <stdlib.h>
2 #include <math.h>
3 #include <assert.h>
4 #include <pthread.h>
5 #include "psys.h"
6 #include "psys_gl.h"
8 static int spawn(struct psys_emitter *em, struct psys_particle *p, void *cls);
9 static void update_particle(struct psys_emitter *em, struct psys_particle *p, float tm, float dt, void *cls);
11 /* particle pool */
12 static struct psys_particle *ppool;
13 static int ppool_size;
14 static pthread_mutex_t pool_lock = PTHREAD_MUTEX_INITIALIZER;
16 static struct psys_particle *palloc(void);
17 static void pfree(struct psys_particle *p);
19 /* --- constructors and shit --- */
21 struct psys_emitter *psys_create(void)
22 {
23 struct psys_emitter *em;
25 if(!(em = malloc(sizeof *em))) {
26 return 0;
27 }
28 if(psys_init(em) == -1) {
29 free(em);
30 return 0;
31 }
32 return em;
33 }
35 void psys_free(struct psys_emitter *em)
36 {
37 psys_destroy(em);
38 free(em);
39 }
41 int psys_init(struct psys_emitter *em)
42 {
43 memset(em, 0, sizeof *em);
45 if(anm_init_node(&em->prs) == -1) {
46 return -1;
47 }
48 if(psys_init_attr(&em->attr) == -1) {
49 anm_destroy_node(&em->prs);
50 return -1;
51 }
53 em->spawn = spawn;
54 em->update = update_particle;
56 em->draw = psys_gl_draw;
57 em->draw_start = psys_gl_draw_start;
58 em->draw_end = psys_gl_draw_end;
59 return 0;
60 }
62 void psys_destroy(struct psys_emitter *em)
63 {
64 struct psys_particle *part;
66 part = em->plist;
67 while(part) {
68 struct psys_particle *tmp = part;
69 part = part->next;
70 pfree(tmp);
71 }
73 psys_destroy_attr(&em->attr);
74 }
76 void psys_set_pos(struct psys_emitter *em, vec3_t pos, float tm)
77 {
78 anm_set_position(&em->prs, pos, ANM_SEC2TM(tm));
79 }
81 void psys_set_rot(struct psys_emitter *em, quat_t rot, float tm)
82 {
83 anm_set_rotation(&em->prs, rot, ANM_SEC2TM(tm));
84 }
86 void psys_set_pivot(struct psys_emitter *em, vec3_t pivot)
87 {
88 anm_set_pivot(&em->prs, pivot);
89 }
91 void psys_clear_collision_planes(struct psys_emitter *em)
92 {
93 struct psys_plane *plane;
95 plane = em->planes;
96 while(plane) {
97 struct psys_plane *tmp = plane;
98 plane = plane->next;
99 free(tmp);
100 }
101 }
103 int psys_add_collision_plane(struct psys_emitter *em, plane_t plane, float elast)
104 {
105 struct psys_plane *node;
107 if(!(node = malloc(sizeof *node))) {
108 return -1;
109 }
110 node->p = plane;
111 node->elasticity = elast;
112 node->next = em->planes;
113 em->planes = node;
114 return 0;
115 }
117 void psys_add_particle(struct psys_emitter *em, struct psys_particle *p)
118 {
119 p->next = em->plist;
120 em->plist = p;
122 em->pcount++;
123 }
125 void psys_spawn_func(struct psys_emitter *em, psys_spawn_func_t func, void *cls)
126 {
127 em->spawn = func;
128 em->spawn_cls = cls;
129 }
131 void psys_update_func(struct psys_emitter *em, psys_update_func_t func, void *cls)
132 {
133 em->update = func;
134 em->upd_cls = cls;
135 }
137 void psys_draw_func(struct psys_emitter *em, psys_draw_func_t draw,
138 psys_draw_start_func_t start, psys_draw_end_func_t end, void *cls)
139 {
140 em->draw = draw;
141 em->draw_start = start;
142 em->draw_end = end;
143 em->draw_cls = cls;
144 }
146 /* --- update and render --- */
148 void psys_update(struct psys_emitter *em, float tm)
149 {
150 float dt, spawn_dt, spawn_tm;
151 int i, spawn_count;
152 struct psys_particle *p, pdummy;
153 anm_time_t atm = ANM_SEC2TM(tm);
155 assert(em->spawn && em->update);
157 dt = tm - em->last_update;
158 if(dt <= 0.0) {
159 return;
160 }
162 psys_eval_attr(&em->attr, atm);
164 /* how many particles to spawn for this interval ? */
165 em->spawn_acc += psys_get_cur_value(&em->attr.rate) * dt;
166 if(em->spawn_acc >= 1.0) {
167 spawn_count = em->spawn_acc;
168 em->spawn_acc = fmod(em->spawn_acc, 1.0);
169 } else {
170 spawn_count = 0;
171 }
173 spawn_dt = dt / (float)spawn_count;
174 spawn_tm = em->last_update;
175 for(i=0; i<spawn_count; i++) {
176 if(em->attr.max_particles >= 0 && em->pcount >= em->attr.max_particles) {
177 break;
178 }
180 /* update emitter position for this spawning */
181 em->cur_pos = anm_get_position(&em->prs, ANM_SEC2TM(spawn_tm));
183 if(!(p = palloc())) {
184 return;
185 }
186 if(em->spawn(em, p, em->spawn_cls) == -1) {
187 pfree(p);
188 }
189 spawn_tm += spawn_dt;
190 }
192 /* update all particles */
193 p = em->plist;
194 while(p) {
195 em->update(em, p, tm, dt, em->upd_cls);
196 p = p->next;
197 }
199 /* cleanup dead particles */
200 pdummy.next = em->plist;
201 p = &pdummy;
202 while(p->next) {
203 if(p->next->life <= 0) {
204 struct psys_particle *tmp = p->next;
205 p->next = p->next->next;
206 pfree(tmp);
207 em->pcount--;
208 } else {
209 p = p->next;
210 }
211 }
212 em->plist = pdummy.next;
214 em->last_update = tm;
216 printf("particles: %5d\r", em->pcount);
217 }
219 void psys_draw(struct psys_emitter *em)
220 {
221 struct psys_particle *p;
223 if(em->draw_start) {
224 em->draw_start(em, em->draw_cls);
225 }
227 p = em->plist;
228 while(p) {
229 em->draw(em, p, em->draw_cls);
230 p = p->next;
231 }
233 if(em->draw_end) {
234 em->draw_end(em, em->draw_cls);
235 }
236 }
238 static int spawn(struct psys_emitter *em, struct psys_particle *p, void *cls)
239 {
240 struct psys_rnd3 rpos;
241 rpos.value = em->cur_pos;
242 rpos.range = psys_get_cur_value3(&em->attr.spawn_range);
244 p->pos = psys_eval_rnd3(&rpos);
245 p->vel = psys_eval_anm_rnd3(&em->attr.dir, PSYS_EVAL_CUR);
246 p->base_size = psys_eval_anm_rnd(&em->attr.size, PSYS_EVAL_CUR);
247 p->max_life = p->life = psys_eval_anm_rnd(&em->attr.life, PSYS_EVAL_CUR);
249 p->pattr = &em->attr.part_attr;
251 psys_add_particle(em, p);
252 return 0;
253 }
255 static void update_particle(struct psys_emitter *em, struct psys_particle *p, float tm, float dt, void *cls)
256 {
257 vec3_t accel, grav;
258 anm_time_t t;
260 grav = psys_get_cur_value3(&em->attr.grav);
262 accel.x = grav.x - p->vel.x * em->attr.drag;
263 accel.y = grav.y - p->vel.y * em->attr.drag;
264 accel.z = grav.z - p->vel.z * em->attr.drag;
266 p->vel.x += accel.x * dt;
267 p->vel.y += accel.y * dt;
268 p->vel.z += accel.z * dt;
270 p->pos.x += p->vel.x * dt;
271 p->pos.y += p->vel.y * dt;
272 p->pos.z += p->vel.z * dt;
274 /* update particle attributes */
275 t = (anm_time_t)(1000.0 * (p->max_life - p->life) / p->max_life);
277 p->color = psys_get_value3(&p->pattr->color, t);
278 p->alpha = psys_get_value(&p->pattr->alpha, t);
279 p->size = p->base_size * psys_get_value(&p->pattr->size, t);
281 p->life -= dt;
282 }
284 /* --- particle allocation pool --- */
286 static struct psys_particle *palloc(void)
287 {
288 struct psys_particle *p;
290 pthread_mutex_lock(&pool_lock);
291 if(ppool) {
292 p = ppool;
293 ppool = ppool->next;
294 ppool_size--;
295 } else {
296 p = malloc(sizeof *p);
297 }
298 pthread_mutex_unlock(&pool_lock);
300 return p;
301 }
303 static void pfree(struct psys_particle *p)
304 {
305 pthread_mutex_lock(&pool_lock);
306 p->next = ppool;
307 ppool = p;
308 ppool_size++;
309 pthread_mutex_unlock(&pool_lock);
310 }