CMakeLists.txt typo; .gitignore
[Lilanci.git] / particle.c
blob6b672af01c81c72b131e04d169dca3b440aac4ec
1 #include "particle.h"
3 double b_x1, b_y1, b_x2, b_y2;
5 typedef struct plistitem_s{
6 Particle *p;
7 }plistitem_t;
9 #define NO_PARTS 100
10 plistitem_t parts[NO_PARTS];
12 void SetParticleBoundingBox(double x, double y, double xx, double yy)
14 b_x1 = x;
15 b_y1 = y;
16 b_x2 = x + xx;
17 b_y2 = y + yy;
20 void AddParticle(Particle *p)
22 int i;
24 for (i = 0; i < NO_PARTS; i++) {
25 if (parts[i].p == 0) {
26 parts[i].p = p;
27 return;
30 free(p);
33 void UpdateParticle(Particle *p, double dt)
35 p->x = p->x + dt * p->vx;
36 p->vx = p->vx * (1.0 - p->fx*dt) + dt * p->ax;
37 p->y = p->y + dt * p->vy;
38 p->vy = p->vy * (1.0 - p->fy*dt) + dt * p->ay;
39 p->t = p->t + dt * p->vt;
40 p->vt = p->vt * (1.0 - p->ft*dt) + dt * p->at;
43 void WhiteFunc(double temp, SDL_Color *c)
45 static SDL_Color white={255,255,255,255};
46 if (c != 0) memcpy(c, &white, sizeof(SDL_Color));
49 void QueueDrawPart(Particle *p)
51 SDL_Color color;
53 if (p->tf != 0){
54 p->tf(p->t, &color);
55 }else{
56 WhiteFunc(p->t, &color);
58 QueueDrawSpriteColorize(p->s, p->x, p->y, p->z, color);
61 void RemoveParticle(plistitem_t *a)
63 if (a == 0) return;
64 if (a->p)free(a->p);
65 a->p = 0;
68 void UpdateAndQueueDrawParticles(double dt)
70 plistitem_t *a;
71 int i;
72 for (i=0; i<NO_PARTS; i++){
73 a = &parts[i];
74 if (a->p == 0){
75 continue;
78 UpdateParticle(a->p, dt);
80 if (a->p->t <= 0.0 || a->p->x < b_x1 || a->p->y < b_y1 || (a->p->x + a->p->s->width) > b_x2 || (a->p->x + a->p->s->width) > b_x2) {
81 RemoveParticle(a);
82 continue;
85 QueueDrawPart(a->p);