libpsys

annotate src/psys.c @ 6:d774738f50f6

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