Skip to content

HTTPS clone URL

Subversion checkout URL

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