/
particle.c
224 lines (192 loc) · 6.5 KB
/
particle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/************************************************************************
* This file is part of Wizznic. *
* Copyright 2009-2012 Jimmy Christensen <dusted@dusted.dk> *
* Wizznic is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* Wizznic is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with Wizznic. If not, see <http://www.gnu.org/licenses/>. *
************************************************************************/
#include "particles.h"
#include "settings.h"
#define GRAVITYCONSTANT 2
static listItem* pSystems; //All particle systems are added to this list
static SDL_Surface* screen;
//Spawn particle system
void spawnParticleSystem(psysSet_t* settings)
{
if(!setting()->particles) return;
int i; //Gotta have a counter.
//Create a particle system
pSystem_t* tSystem=malloc( sizeof(pSystem_t) );
//Copy settings
tSystem->settings=(*settings);
//Setup particles
uint32_t col;
uint8_t r,g,b;
SDL_Surface* img;
//Should we use info's from a surface?
if(tSystem->settings.srcImg)
{
//number of particles
tSystem->settings.numParticles=tSystem->settings.srcRect.w*tSystem->settings.srcRect.h;
//Create array of particles
tSystem->particles = malloc( sizeof(particle_t)*(tSystem->settings.numParticles));
for(i=0; i<tSystem->settings.numParticles;i++)
{
tSystem->particles[i].life=tSystem->settings.life - (rand()%tSystem->settings.lifeVar);
img=tSystem->settings.srcImg;
tSystem->particles[i].x = i%tSystem->settings.srcRect.w;
tSystem->particles[i].y = i/tSystem->settings.srcRect.w;
col=freadPixel(img,tSystem->particles[i].x+tSystem->settings.srcRect.x,tSystem->particles[i].y+tSystem->settings.srcRect.y);
r = ((col & img->format->Rmask) >> img->format->Rshift);
g = ((col & img->format->Gmask) >> img->format->Gshift);
b = ((col & img->format->Bmask) >> img->format->Bshift);
if( !isAlpha(r,g,b) )
{
tSystem->particles[i].color = col;
} else {
tSystem->particles[i].color = col;
tSystem->particles[i].life=0;
}
if(tSystem->settings.vel)
{
tSystem->particles[i].velx = (rand()%(tSystem->settings.vel*2))-(tSystem->settings.vel);
tSystem->particles[i].vely = (rand()%(tSystem->settings.vel*2))-(tSystem->settings.vel);
}
tSystem->particles[i].x *= 100;
tSystem->particles[i].y *= 100;
tSystem->particles[i].x += tSystem->settings.x*100;
tSystem->particles[i].y += tSystem->settings.y*100;
}
} else {
//Create array of particles
tSystem->particles = malloc( sizeof(particle_t)*(tSystem->settings.numParticles));
for(i=0; i<tSystem->settings.numParticles;i++)
{
tSystem->particles[i].life=tSystem->settings.life - (rand()%tSystem->settings.lifeVar);
tSystem->particles[i].velx = (rand()%(tSystem->settings.vel*2))-(tSystem->settings.vel);
tSystem->particles[i].vely = (rand()%(tSystem->settings.vel*2))-(tSystem->settings.vel);
tSystem->particles[i].x = tSystem->settings.x*100;
tSystem->particles[i].y = tSystem->settings.y*100;
if(tSystem->settings.color == PARTICLECOLORRANDOM)
{
tSystem->particles[i].color = SDL_MapRGB( screen->format, rand()%256,rand()%256,rand()%256);
} else {
tSystem->particles[i].color = tSystem->settings.color;
}
}
}
//Add to list of systems.
listAddData(pSystems, (void*)tSystem);
}
//Just easier than having to deal with it down in the loop
inline void updateParticle(particle_t* p, SDL_Surface* screen,psysSet_t* s)
{
//Move
p->x += p->velx;
p->y += p->vely;
//Gravity
if(s->gravity)
p->vely += GRAVITYCONSTANT;
//Color?
//Fade?
//Bounce/Edge detection
if(p->x > (SCREENW*100) )
{
if(s->bounce)
{
p->x=(SCREENW*100);
p->velx *= -1;
p->velx -= p->velx/4; //Only lose 1/4 inertia
} else {
p->life=0;
}
}
if(p->y > (SCREENH*100) )
{
if(s->bounce)
{
p->y=(SCREENH*100);
p->vely *= -1;
p->vely -= p->vely/3; //lose 1/3 inertia
} else {
p->life=0;
}
}
//Age
p->life -= getTicks();
if(p->life<0) p->life=0;
}
//This runs/draws all particle systems, and emitters.
void runParticles(SDL_Surface* screen)
{
runParticlesLayer(screen, PSYS_LAYER_TOP);
}
//This will not enforce PSYS_LAYER_NODRAW (it should not, no particle systems should be created if they are not to be drawn).
void runParticlesLayer(SDL_Surface* screen, int layer)
{
if(!setting()->particles) return;
listItem* it = pSystems;
pSystem_t* p; //psystem
int i;
//Loop through systems
while( (it=it->next) )
{
p=(pSystem_t*)it->data;
if(p->settings.layer==layer)
{
//Draw, then update
for( i=0; i < p->settings.numParticles; i++ )
{
if( p->particles[i].life )
{
//Draw particle
plotPixel( screen, p->particles[i].x/100,p->particles[i].y/100, p->particles[i].color );
//Update particle
updateParticle(&p->particles[i], screen, &p->settings);
}
}
//System life
p->settings.life -= getTicks();
if(p->settings.life<0)
{
//Remove system
clearSystem(p);
//Remove from list. (removeItem returns the item just before current, if any)
it=listRemoveItem(pSystems, it);
}
} //System is on correct layer
}
}
//Frees one system
inline void clearSystem(pSystem_t* s)
{
//Free particles
free(s->particles);
//Free the system
free(s);
}
//Frees all resources and removes all systems and emitters.
void clearParticles()
{
listItem* it = pSystems;
//Loop through systems
while( (it=it->next) )
{
clearSystem( (pSystem_t*)it->data );
it=listRemoveItem(pSystems, it);
}
}
void initParticles(SDL_Surface* scr)
{
pSystems = initList();
screen=scr;
}