dungeon_crawler

annotate prototype/psys/psys.c @ 75:b05ab29cd17d

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