libpsys

view src/psys.c @ 1:874a942853ad

foobar
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sat, 24 Sep 2011 20:44:42 +0300
parents 1c8eb90a6989
children 6e5342a2529a
line source
1 #include <stdlib.h>
2 #include <math.h>
3 #include <assert.h>
4 #include <pthread.h>
5 #include <vmath.h>
6 #include "psys_impl.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 static int init_v3track(struct v3track *v3t);
12 static void destroy_v3track(struct v3track *v3t);
13 static void set_v3value(struct v3track *v3t, anm_time_t tm, vec3_t v);
14 static vec3_t get_v3value(struct v3track *v3t, anm_time_t tm);
16 /* particle pool */
17 static struct psys_particle *ppool;
18 static int ppool_size;
19 static pthread_mutex_t pool_lock = PTHREAD_MUTEX_INITIALIZER;
21 static struct psys_particle *palloc(void);
22 static void pfree(struct psys_particle *p);
24 /* --- constructors and shit --- */
26 struct psys_emitter *psys_create(void)
27 {
28 struct psys_emitter *em;
30 if(!(em = malloc(sizeof *em))) {
31 return 0;
32 }
33 if(psys_init(em) == -1) {
34 free(em);
35 return 0;
36 }
37 return em;
38 }
40 void psys_free(struct psys_emitter *em)
41 {
42 psys_destroy(em);
43 free(em);
44 }
46 int psys_init(struct psys_emitter *em)
47 {
48 memset(em, 0, sizeof *em);
50 if(anm_init_node(&em->prs) == -1) {
51 psys_destroy(em);
52 return -1;
53 }
54 if(anm_init_track(&em->rate) == -1) {
55 psys_destroy(em);
56 return -1;
57 }
58 if(anm_init_track(&em->life) == -1) {
59 psys_destroy(em);
60 return -1;
61 }
62 if(init_v3track(&em->dir) == -1) {
63 psys_destroy(em);
64 return -1;
65 }
66 if(init_v3track(&em->grav) == -1) {
67 psys_destroy(em);
68 return -1;
69 }
71 em->spawn = spawn;
72 em->update = update_particle;
74 em->draw = psys_gl_draw;
75 em->draw_start = psys_gl_draw_start;
76 em->draw_end = psys_gl_draw_end;
78 return 0;
79 }
81 void psys_destroy(struct psys_emitter *em)
82 {
83 struct psys_particle *part;
85 part = em->plist;
86 while(part) {
87 struct psys_particle *tmp = part;
88 part = part->next;
89 pfree(tmp);
90 }
92 anm_destroy_node(&em->prs);
93 anm_destroy_track(&em->rate);
94 destroy_v3track(&em->dir);
95 }
97 void psys_set_pos(struct psys_emitter *em, vec3_t pos, float tm)
98 {
99 anm_set_position(&em->prs, pos, ANM_SEC2TM(tm));
100 }
102 void psys_set_rot(struct psys_emitter *em, quat_t rot, float tm)
103 {
104 anm_set_rotation(&em->prs, rot, ANM_SEC2TM(tm));
105 }
107 void psys_set_pivot(struct psys_emitter *em, vec3_t pivot)
108 {
109 anm_set_pivot(&em->prs, pivot);
110 }
112 void psys_set_rate(struct psys_emitter *em, float rate, float tm)
113 {
114 anm_set_value(&em->rate, ANM_SEC2TM(tm), rate);
115 }
117 void psys_set_life(struct psys_emitter *em, float life, float tm)
118 {
119 anm_set_value(&em->life, ANM_SEC2TM(tm), life);
120 }
122 void psys_set_dir(struct psys_emitter *em, vec3_t dir, float tm)
123 {
124 set_v3value(&em->dir, ANM_SEC2TM(tm), dir);
125 }
127 void psys_set_grav(struct psys_emitter *em, vec3_t grav, float tm)
128 {
129 set_v3value(&em->grav, ANM_SEC2TM(tm), grav);
130 }
133 void psys_clear_collision_planes(struct psys_emitter *em)
134 {
135 struct col_plane *plane;
137 plane = em->planes;
138 while(plane) {
139 struct col_plane *tmp = plane;
140 plane = plane->next;
141 free(tmp);
142 }
143 }
145 int psys_add_collision_plane(struct psys_emitter *em, plane_t plane, float elast)
146 {
147 struct col_plane *node;
149 if(!(node = malloc(sizeof *node))) {
150 return -1;
151 }
152 node->p = plane;
153 node->elasticity = elast;
154 node->next = em->planes;
155 em->planes = node;
156 return 0;
157 }
159 void psys_add_particle(struct psys_emitter *em, struct psys_particle *p)
160 {
161 p->next = em->plist;
162 em->plist = p;
163 }
165 void psys_spawn_func(struct psys_emitter *em, psys_spawn_func_t func, void *cls)
166 {
167 em->spawn = func;
168 em->spawn_cls = cls;
169 }
171 void psys_update_func(struct psys_emitter *em, psys_update_func_t func, void *cls)
172 {
173 em->update = func;
174 em->upd_cls = cls;
175 }
177 void psys_draw_func(struct psys_emitter *em, psys_draw_func_t draw,
178 psys_draw_start_func_t start, psys_draw_end_func_t end, void *cls)
179 {
180 em->draw = draw;
181 em->draw_start = start;
182 em->draw_end = end;
183 em->draw_cls = cls;
184 }
186 /* --- query current state --- */
187 vec3_t psys_get_pos(struct psys_emitter *em)
188 {
189 return em->cur_pos;
190 }
192 quat_t psys_get_rot(struct psys_emitter *em)
193 {
194 return em->cur_rot;
195 }
197 float psys_get_rate(struct psys_emitter *em)
198 {
199 return em->cur_rate;
200 }
202 float psys_get_life(struct psys_emitter *em)
203 {
204 return em->cur_life;
205 }
207 vec3_t psys_get_dir(struct psys_emitter *em)
208 {
209 return em->cur_dir;
210 }
212 vec3_t psys_get_grav(struct psys_emitter *em)
213 {
214 return em->cur_grav;
215 }
217 /* --- update and render --- */
219 void psys_update(struct psys_emitter *em, float tm)
220 {
221 float dt, spawn_dt;
222 int i, spawn_count;
223 struct psys_particle *p, pdummy;
224 anm_time_t atm;
226 assert(em->spawn && em->update);
228 atm = ANM_SEC2TM(tm);
230 em->cur_rate = anm_get_value(&em->rate, atm);
231 dt = tm - em->last_update;
233 /* how many particles to spawn for this interval ? */
234 spawn_count = em->cur_rate * dt;
236 #ifndef SUB_UPDATE_POS
237 em->cur_pos = anm_get_position(&em->prs, atm);
238 #endif
239 em->cur_dir = get_v3value(&em->dir, atm);
240 em->cur_life = anm_get_value(&em->life, atm);
241 em->cur_grav = get_v3value(&em->grav, atm);
243 spawn_dt = dt / (float)spawn_count;
244 for(i=0; i<spawn_count; i++) {
245 #ifdef SUB_UPDATE_POS
246 /* update emitter position for this spawning */
247 em->cur_pos = anm_get_position(&em->prs, ANM_SEC2TM(em->last_update + spawn_dt));
248 #endif
250 if(!(p = palloc())) {
251 return;
252 }
253 if(em->spawn(em, p, em->spawn_cls) == -1) {
254 pfree(p);
255 }
256 }
258 /* update all particles */
259 p = em->plist;
260 while(p) {
261 em->update(em, p, tm, dt, em->upd_cls);
262 p = p->next;
263 }
265 /* cleanup dead particles */
266 pdummy.next = em->plist;
267 p = &pdummy;
268 while(p->next) {
269 if(p->next->life <= 0) {
270 struct psys_particle *tmp = p->next;
271 p->next = p->next->next;
272 pfree(tmp);
273 } else {
274 p = p->next;
275 }
276 }
277 em->plist = pdummy.next;
278 }
280 void psys_draw(struct psys_emitter *em)
281 {
282 struct psys_particle *p;
284 if(em->draw_start) {
285 em->draw_start(em, em->draw_cls);
286 }
288 p = em->plist;
289 while(p) {
290 em->draw(em, p, em->draw_cls);
291 p = p->next;
292 }
294 if(em->draw_end) {
295 em->draw_end(em, em->draw_cls);
296 }
297 }
299 static int spawn(struct psys_emitter *em, struct psys_particle *p, void *cls)
300 {
301 p->pos = em->cur_pos;
302 p->vel = em->cur_dir;
303 p->size = 1.0;
304 p->life = em->cur_life;
306 psys_add_particle(em, p);
307 return 0;
308 }
310 static void update_particle(struct psys_emitter *em, struct psys_particle *p, float tm, float dt, void *cls)
311 {
312 vec3_t accel;
314 accel.x = em->cur_grav.x - p->vel.x * em->drag;
315 accel.y = em->cur_grav.y - p->vel.y * em->drag;
316 accel.z = em->cur_grav.z - p->vel.z * em->drag;
318 p->vel.x += accel.x * dt;
319 p->vel.y += accel.y * dt;
320 p->vel.z += accel.z * dt;
322 p->pos.x += p->vel.x * dt;
323 p->pos.y += p->vel.y * dt;
324 p->pos.z += p->vel.z * dt;
325 }
327 /* --- v3track helper --- */
329 int init_v3track(struct v3track *v3t)
330 {
331 if(anm_init_track(&v3t->x) == -1) {
332 return -1;
333 }
334 if(anm_init_track(&v3t->y) == -1) {
335 anm_destroy_track(&v3t->x);
336 return -1;
337 }
338 if(anm_init_track(&v3t->z) == -1) {
339 anm_destroy_track(&v3t->x);
340 anm_destroy_track(&v3t->y);
341 return -1;
342 }
343 return 0;
344 }
346 static void destroy_v3track(struct v3track *v3t)
347 {
348 anm_destroy_track(&v3t->x);
349 anm_destroy_track(&v3t->y);
350 anm_destroy_track(&v3t->z);
351 }
353 static void set_v3value(struct v3track *v3t, anm_time_t tm, vec3_t v)
354 {
355 anm_set_value(&v3t->x, tm, v.x);
356 anm_set_value(&v3t->y, tm, v.y);
357 anm_set_value(&v3t->z, tm, v.z);
358 }
360 static vec3_t get_v3value(struct v3track *v3t, anm_time_t tm)
361 {
362 vec3_t v;
363 v.x = anm_get_value(&v3t->x, tm);
364 v.y = anm_get_value(&v3t->y, tm);
365 v.z = anm_get_value(&v3t->z, tm);
366 return v;
367 }
369 /* --- particle allocation pool --- */
371 static struct psys_particle *palloc(void)
372 {
373 struct psys_particle *p;
375 pthread_mutex_lock(&pool_lock);
376 if(ppool) {
377 p = ppool;
378 ppool = ppool->next;
379 ppool_size--;
380 } else {
381 p = malloc(sizeof *p);
382 }
383 pthread_mutex_unlock(&pool_lock);
385 if(p) {
386 memset(p, 0, sizeof *p);
387 /*reset_pattr(&p->attr);*/
388 }
389 return p;
390 }
392 static void pfree(struct psys_particle *p)
393 {
394 pthread_mutex_lock(&pool_lock);
395 p->next = ppool;
396 ppool = p;
397 ppool_size++;
398 pthread_mutex_unlock(&pool_lock);
399 }