libpsys

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