Mercurial > love-experiments
annotate src/modules/graphics/opengl/ParticleSystem.cpp @ 1408:107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
| author | Alex Szpakowski <slime73@gmail.com> |
|---|---|
| date | Wed, 27 Mar 2013 01:09:53 -0300 |
| parents | f0795108d4c0 |
| children | e952fc68069d |
| rev | line source |
|---|---|
| 1096 | 1 /** |
|
1282
b7f097053d74
Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents:
1230
diff
changeset
|
2 * Copyright (c) 2006-2013 LOVE Development Team |
| 1096 | 3 * |
| 4 * This software is provided 'as-is', without any express or implied | |
| 5 * warranty. In no event will the authors be held liable for any damages | |
| 6 * arising from the use of this software. | |
| 7 * | |
| 8 * Permission is granted to anyone to use this software for any purpose, | |
| 9 * including commercial applications, and to alter it and redistribute it | |
| 10 * freely, subject to the following restrictions: | |
| 11 * | |
| 12 * 1. The origin of this software must not be misrepresented; you must not | |
| 13 * claim that you wrote the original software. If you use this software | |
| 14 * in a product, an acknowledgment in the product documentation would be | |
| 15 * appreciated but is not required. | |
| 16 * 2. Altered source versions must be plainly marked as such, and must not be | |
| 17 * misrepresented as being the original software. | |
| 18 * 3. This notice may not be removed or altered from any source distribution. | |
| 19 **/ | |
| 20 | |
| 21 #include "ParticleSystem.h" | |
| 22 | |
| 23 #include "common/math.h" | |
| 1369 | 24 #include "modules/math/MathModule.h" |
| 1096 | 25 |
|
1216
8ae3ebeeda85
opengl graphics module files now always include OpenGL.h rather than GLee.h
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
26 #include "OpenGL.h" |
| 1096 | 27 #include <cmath> |
| 28 #include <cstdlib> | |
| 29 | |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
30 using love::math::Math; |
|
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
31 |
| 1096 | 32 namespace love |
| 33 { | |
| 34 namespace graphics | |
| 35 { | |
| 36 namespace opengl | |
| 37 { | |
| 38 | |
| 39 namespace | |
| 40 { | |
| 41 Colorf colorToFloat(const Color &c) | |
| 42 { | |
| 43 return Colorf((GLfloat)c.r/255.0f, (GLfloat)c.g/255.0f, (GLfloat)c.b/255.0f, (GLfloat)c.a/255.0f); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 float calculate_variation(float inner, float outer, float var) | |
| 48 { | |
| 49 float low = inner - (outer/2.0f)*var; | |
| 50 float high = inner + (outer/2.0f)*var; | |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
51 float r = Math::instance.random(); |
| 1096 | 52 return low*(1-r)+high*r; |
| 53 } | |
| 54 | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
55 StringMap<ParticleSystem::AreaSpreadDistribution, ParticleSystem::DISTRIBUTION_MAX_ENUM>::Entry ParticleSystem::distributionsEntries[] = { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
56 { "none", ParticleSystem::DISTRIBUTION_NONE }, |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
57 { "uniform", ParticleSystem::DISTRIBUTION_UNIFORM }, |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
58 { "normal", ParticleSystem::DISTRIBUTION_NORMAL }, |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
59 }; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
60 |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
61 StringMap<ParticleSystem::AreaSpreadDistribution, ParticleSystem::DISTRIBUTION_MAX_ENUM> ParticleSystem::distributions(ParticleSystem::distributionsEntries, sizeof(ParticleSystem::distributionsEntries)); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
62 |
| 1096 | 63 |
| 64 ParticleSystem::ParticleSystem(Image *sprite, unsigned int buffer) | |
| 65 : pStart(0) | |
| 66 , pLast(0) | |
| 67 , pEnd(0) | |
| 1147 | 68 , particleVerts(0) |
|
1307
1c61424c6024
Cleaned up exception handling code when creating new fonts, spritebatches, and shaders
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
69 , sprite(sprite) |
| 1096 | 70 , active(true) |
| 71 , emissionRate(0) | |
| 72 , emitCounter(0) | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
73 , areaSpreadDistribution(DISTRIBUTION_NONE) |
| 1096 | 74 , lifetime(-1) |
| 75 , life(0) | |
| 76 , particleLifeMin(0) | |
| 77 , particleLifeMax(0) | |
| 78 , direction(0) | |
| 79 , spread(0) | |
| 80 , relative(false) | |
| 81 , speedMin(0) | |
| 82 , speedMax(0) | |
| 83 , gravityMin(0) | |
| 84 , gravityMax(0) | |
| 85 , radialAccelerationMin(0) | |
| 86 , radialAccelerationMax(0) | |
| 87 , tangentialAccelerationMin(0) | |
| 88 , tangentialAccelerationMax(0) | |
| 89 , sizeVariation(0) | |
| 90 , rotationMin(0) | |
| 91 , rotationMax(0) | |
| 92 , spinStart(0) | |
| 93 , spinEnd(0) | |
| 94 , spinVariation(0) | |
| 95 , offsetX(sprite->getWidth()*0.5f) | |
| 96 , offsetY(sprite->getHeight()*0.5f) | |
| 97 { | |
| 98 sizes.push_back(1.0f); | |
| 99 colors.push_back(Colorf(1.0f, 1.0f, 1.0f, 1.0f)); | |
| 100 setBufferSize(buffer); | |
|
1307
1c61424c6024
Cleaned up exception handling code when creating new fonts, spritebatches, and shaders
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
101 sprite->retain(); |
| 1096 | 102 } |
| 103 | |
| 104 ParticleSystem::~ParticleSystem() | |
| 105 { | |
|
1353
c3c9d6ed3e21
ParticleSystem:setQuads can now accept a table argument, cleaned up the ParticleSystem quad code
Alex Szpakowski <slime73@gmail.com>
parents:
1352
diff
changeset
|
106 for (size_t i = 0; i < quads.size(); i++) |
|
c3c9d6ed3e21
ParticleSystem:setQuads can now accept a table argument, cleaned up the ParticleSystem quad code
Alex Szpakowski <slime73@gmail.com>
parents:
1352
diff
changeset
|
107 quads[i]->release(); |
|
c3c9d6ed3e21
ParticleSystem:setQuads can now accept a table argument, cleaned up the ParticleSystem quad code
Alex Szpakowski <slime73@gmail.com>
parents:
1352
diff
changeset
|
108 |
| 1096 | 109 if (this->sprite != 0) |
| 110 this->sprite->release(); | |
| 111 | |
| 112 if (pStart != 0) | |
| 113 delete [] pStart; | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
114 |
| 1147 | 115 if (particleVerts != 0) |
| 116 delete [] particleVerts; | |
| 1096 | 117 } |
| 118 | |
| 119 void ParticleSystem::add() | |
| 120 { | |
| 121 if (isFull()) return; | |
| 122 | |
| 123 float min,max; | |
| 124 | |
| 125 min = particleLifeMin; | |
| 126 max = particleLifeMax; | |
| 127 if (min == max) | |
| 128 pLast->life = min; | |
| 129 else | |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
130 pLast->life = Math::instance.random(min, max); |
| 1096 | 131 pLast->lifetime = pLast->life; |
| 132 | |
| 133 pLast->position[0] = position.getX(); | |
| 134 pLast->position[1] = position.getY(); | |
| 135 | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
136 switch (areaSpreadDistribution) |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
137 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
138 case DISTRIBUTION_UNIFORM: |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
139 pLast->position[0] += Math::instance.random(-areaSpread.getX(), areaSpread.getX()); |
|
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
140 pLast->position[1] += Math::instance.random(-areaSpread.getY(), areaSpread.getY()); |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
141 break; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
142 case DISTRIBUTION_NORMAL: |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
143 pLast->position[0] += Math::instance.randnormal(areaSpread.getX()); |
|
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
144 pLast->position[1] += Math::instance.randnormal(areaSpread.getY()); |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
145 break; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
146 case DISTRIBUTION_NONE: |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
147 default: |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
148 break; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
149 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
150 |
| 1096 | 151 min = direction - spread/2.0f; |
| 152 max = direction + spread/2.0f; | |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
153 pLast->direction = Math::instance.random(min, max); |
| 1096 | 154 |
| 155 pLast->origin = position; | |
| 156 | |
| 157 min = speedMin; | |
| 158 max = speedMax; | |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
159 float speed = Math::instance.random(min, max); |
| 1096 | 160 pLast->speed = love::Vector(cos(pLast->direction), sin(pLast->direction)); |
| 161 pLast->speed *= speed; | |
| 162 | |
| 163 min = gravityMin; | |
| 164 max = gravityMax; | |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
165 pLast->gravity = Math::instance.random(min, max); |
| 1096 | 166 |
| 167 min = radialAccelerationMin; | |
| 168 max = radialAccelerationMax; | |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
169 pLast->radialAcceleration = Math::instance.random(min, max); |
| 1096 | 170 |
| 171 min = tangentialAccelerationMin; | |
| 172 max = tangentialAccelerationMax; | |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
173 pLast->tangentialAcceleration = Math::instance.random(min, max); |
| 1096 | 174 |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
175 pLast->sizeOffset = Math::instance.random(sizeVariation); // time offset for size change |
|
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
176 pLast->sizeIntervalSize = (1.0f - Math::instance.random(sizeVariation)) - pLast->sizeOffset; |
| 1096 | 177 pLast->size = sizes[(size_t)(pLast->sizeOffset - .5f) * (sizes.size() - 1)]; |
| 178 | |
| 179 min = rotationMin; | |
| 180 max = rotationMax; | |
| 181 pLast->spinStart = calculate_variation(spinStart, spinEnd, spinVariation); | |
| 182 pLast->spinEnd = calculate_variation(spinEnd, spinStart, spinVariation); | |
|
1368
ffd3c8f2709f
Make love.math a singleton. Remove code duplication. Add love.math.triangulate.
vrld <vrld@vrld.org>
parents:
1360
diff
changeset
|
183 pLast->rotation = Math::instance.random(min, max); |
| 1096 | 184 |
| 185 pLast->color = colors[0]; | |
| 186 | |
| 187 pLast++; | |
| 188 } | |
| 189 | |
| 190 void ParticleSystem::remove(particle *p) | |
| 191 { | |
| 192 if (!isEmpty()) | |
| 193 { | |
| 194 *p = *(--pLast); | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 void ParticleSystem::setSprite(Image *image) | |
| 199 { | |
| 200 if (sprite != 0) | |
| 201 sprite->release(); | |
| 202 | |
| 203 sprite = image; | |
| 204 sprite->retain(); | |
| 205 } | |
| 206 | |
| 207 void ParticleSystem::setBufferSize(unsigned int size) | |
| 208 { | |
| 209 // delete previous data | |
| 1147 | 210 if (pStart != 0) |
| 211 delete [] pStart; | |
| 1096 | 212 |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
213 pLast = pStart = new particle[size](); |
| 1096 | 214 |
| 215 pEnd = pStart + size; | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
216 |
| 1147 | 217 if (particleVerts != 0) |
| 218 delete [] particleVerts; | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
219 |
| 1147 | 220 // each particle has 4 vertices |
| 221 particleVerts = new vertex[size*4]; | |
| 1096 | 222 } |
| 223 | |
| 224 void ParticleSystem::setEmissionRate(int rate) | |
| 225 { | |
| 226 emissionRate = rate; | |
| 227 } | |
| 228 | |
| 229 void ParticleSystem::setLifetime(float life) | |
| 230 { | |
| 231 this->life = lifetime = life; | |
| 232 } | |
| 233 | |
| 234 void ParticleSystem::setParticleLife(float min, float max) | |
| 235 { | |
| 236 particleLifeMin = min; | |
| 237 if (max == 0) | |
| 238 particleLifeMax = min; | |
| 239 else | |
| 240 particleLifeMax = max; | |
| 241 } | |
| 242 | |
| 243 void ParticleSystem::setPosition(float x, float y) | |
| 244 { | |
| 245 position = love::Vector(x, y); | |
| 246 } | |
| 247 | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
248 |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
249 void ParticleSystem::setAreaSpread(AreaSpreadDistribution distribution, float x, float y) |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
250 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
251 areaSpread = love::Vector(x, y); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
252 areaSpreadDistribution = distribution; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
253 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
254 |
| 1096 | 255 void ParticleSystem::setDirection(float direction) |
| 256 { | |
| 257 this->direction = direction; | |
| 258 } | |
| 259 | |
| 260 void ParticleSystem::setSpread(float spread) | |
| 261 { | |
| 262 this->spread = spread; | |
| 263 } | |
| 264 | |
| 265 void ParticleSystem::setRelativeDirection(bool relative) | |
| 266 { | |
| 267 this->relative = relative; | |
| 268 } | |
| 269 | |
| 270 void ParticleSystem::setSpeed(float speed) | |
| 271 { | |
| 272 speedMin = speedMax = speed; | |
| 273 } | |
| 274 | |
| 275 void ParticleSystem::setSpeed(float min, float max) | |
| 276 { | |
| 277 speedMin = min; | |
| 278 speedMax = max; | |
| 279 } | |
| 280 | |
| 281 void ParticleSystem::setGravity(float gravity) | |
| 282 { | |
| 283 gravityMin = gravityMax = gravity; | |
| 284 } | |
| 285 | |
| 286 void ParticleSystem::setGravity(float min, float max) | |
| 287 { | |
| 288 gravityMin = min; | |
| 289 gravityMax = max; | |
| 290 } | |
| 291 | |
| 292 void ParticleSystem::setRadialAcceleration(float acceleration) | |
| 293 { | |
| 294 radialAccelerationMin = radialAccelerationMax = acceleration; | |
| 295 } | |
| 296 | |
| 297 void ParticleSystem::setRadialAcceleration(float min, float max) | |
| 298 { | |
| 299 radialAccelerationMin = min; | |
| 300 radialAccelerationMax = max; | |
| 301 } | |
| 302 | |
| 303 void ParticleSystem::setTangentialAcceleration(float acceleration) | |
| 304 { | |
| 305 tangentialAccelerationMin = tangentialAccelerationMax = acceleration; | |
| 306 } | |
| 307 | |
| 308 void ParticleSystem::setTangentialAcceleration(float min, float max) | |
| 309 { | |
| 310 tangentialAccelerationMin = min; | |
| 311 tangentialAccelerationMax = max; | |
| 312 } | |
| 313 | |
| 314 void ParticleSystem::setSize(float size) | |
| 315 { | |
| 316 sizes.resize(1); | |
| 317 sizes[0] = size; | |
| 318 } | |
| 319 | |
| 320 void ParticleSystem::setSize(const std::vector<float> &newSizes, float variation) | |
| 321 { | |
| 322 sizes = newSizes; | |
| 323 sizeVariation = variation; | |
| 324 } | |
| 325 | |
| 326 void ParticleSystem::setSizeVariation(float variation) | |
| 327 { | |
| 328 sizeVariation = variation; | |
| 329 } | |
| 330 | |
| 331 void ParticleSystem::setRotation(float rotation) | |
| 332 { | |
| 333 rotationMin = rotationMax = rotation; | |
| 334 } | |
| 335 | |
| 336 void ParticleSystem::setRotation(float min, float max) | |
| 337 { | |
| 338 rotationMin = min; | |
| 339 rotationMax = max; | |
| 340 } | |
| 341 | |
| 342 void ParticleSystem::setSpin(float spin) | |
| 343 { | |
| 344 spinStart = spin; | |
| 345 spinEnd = spin; | |
| 346 } | |
| 347 | |
| 348 void ParticleSystem::setSpin(float start, float end) | |
| 349 { | |
| 350 spinStart = start; | |
| 351 spinEnd = end; | |
| 352 } | |
| 353 | |
| 354 void ParticleSystem::setSpin(float start, float end, float variation) | |
| 355 { | |
| 356 spinStart = start; | |
| 357 spinEnd = end; | |
| 358 spinVariation = variation; | |
| 359 } | |
| 360 | |
| 361 void ParticleSystem::setSpinVariation(float variation) | |
| 362 { | |
| 363 spinVariation = variation; | |
| 364 } | |
| 365 | |
| 366 void ParticleSystem::setColor(const Color &color) | |
| 367 { | |
| 368 colors.resize(1); | |
| 369 colors[0] = colorToFloat(color); | |
| 370 } | |
| 371 | |
| 372 void ParticleSystem::setColor(const std::vector<Color> &newColors) | |
| 373 { | |
| 374 colors.resize(newColors.size()); | |
| 375 for (size_t i = 0; i < newColors.size(); ++i) | |
| 376 colors[i] = colorToFloat(newColors[i]); | |
| 377 } | |
| 378 | |
|
1360
f1c353a5067d
Cleaned up some ParticleSystem and Quad code
Alex Szpakowski <slime73@gmail.com>
parents:
1353
diff
changeset
|
379 void ParticleSystem::setQuads(const std::vector<Quad *> &newQuads) |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
380 { |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
381 for (size_t i = 0; i < quads.size(); i++) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
382 quads[i]->release(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
383 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
384 quads.resize(newQuads.size()); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
385 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
386 for (size_t i = 0; i < newQuads.size(); i++) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
387 { |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
388 quads[i] = newQuads[i]; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
389 quads[i]->retain(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
390 } |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
391 } |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
392 |
|
1353
c3c9d6ed3e21
ParticleSystem:setQuads can now accept a table argument, cleaned up the ParticleSystem quad code
Alex Szpakowski <slime73@gmail.com>
parents:
1352
diff
changeset
|
393 void ParticleSystem::setQuads() |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
394 { |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
395 for (size_t i = 0; i < quads.size(); i++) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
396 quads[i]->release(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
397 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
398 quads.resize(0); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
399 } |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
400 |
| 1096 | 401 void ParticleSystem::setOffset(float x, float y) |
| 402 { | |
| 403 offsetX = x; | |
| 404 offsetY = y; | |
| 405 } | |
| 406 | |
| 407 float ParticleSystem::getX() const | |
| 408 { | |
| 409 return position.getX(); | |
| 410 } | |
| 411 | |
| 412 float ParticleSystem::getY() const | |
| 413 { | |
| 414 return position.getY(); | |
| 415 } | |
| 416 | |
| 417 const love::Vector &ParticleSystem::getPosition() const | |
| 418 { | |
| 419 return position; | |
| 420 } | |
| 421 | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
422 ParticleSystem::AreaSpreadDistribution ParticleSystem::getAreaSpreadDistribution() const |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
423 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
424 return areaSpreadDistribution; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
425 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
426 |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
427 const love::Vector &ParticleSystem::getAreaSpreadParameters() const |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
428 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
429 return areaSpread; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
430 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
431 |
| 1096 | 432 float ParticleSystem::getDirection() const |
| 433 { | |
| 434 return direction; | |
| 435 } | |
| 436 | |
| 437 float ParticleSystem::getSpread() const | |
| 438 { | |
| 439 return spread; | |
| 440 } | |
| 441 | |
| 442 float ParticleSystem::getOffsetX() const | |
| 443 { | |
| 444 return offsetX; | |
| 445 } | |
| 446 | |
| 447 float ParticleSystem::getOffsetY() const | |
| 448 { | |
| 449 return offsetY; | |
| 450 } | |
| 451 | |
| 452 int ParticleSystem::count() const | |
| 453 { | |
| 454 return (int)(pLast - pStart); | |
| 455 } | |
| 456 | |
| 457 void ParticleSystem::start() | |
| 458 { | |
| 459 active = true; | |
| 460 } | |
| 461 | |
| 462 void ParticleSystem::stop() | |
| 463 { | |
| 464 active = false; | |
| 465 life = lifetime; | |
| 466 emitCounter = 0; | |
| 467 } | |
| 468 | |
| 469 void ParticleSystem::pause() | |
| 470 { | |
| 471 active = false; | |
| 472 } | |
| 473 | |
| 474 void ParticleSystem::reset() | |
| 475 { | |
| 476 pLast = pStart; | |
| 477 life = lifetime; | |
| 478 emitCounter = 0; | |
| 479 } | |
| 480 | |
|
1408
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
481 void ParticleSystem::emit(int num) |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
482 { |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
483 if (!active) |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
484 return; |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
485 |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
486 for (int i = 0; i < num; i++) |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
487 { |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
488 if (isFull()) |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
489 return; |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
490 |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
491 add(); |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
492 } |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
493 } |
|
107d80a59bca
Added ParticleSystem:emit(num) (issue #577)
Alex Szpakowski <slime73@gmail.com>
parents:
1369
diff
changeset
|
494 |
| 1096 | 495 bool ParticleSystem::isActive() const |
| 496 { | |
| 497 return active; | |
| 498 } | |
| 499 | |
| 500 bool ParticleSystem::isEmpty() const | |
| 501 { | |
| 502 return pStart == pLast; | |
| 503 } | |
| 504 | |
| 505 bool ParticleSystem::isFull() const | |
| 506 { | |
| 507 return pLast == pEnd; | |
| 508 } | |
| 509 | |
| 510 void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const | |
| 511 { | |
| 512 if (sprite == 0) return; // just in case of failure | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
513 |
| 1147 | 514 int numParticles = count(); |
| 515 if (numParticles == 0) return; // don't bother if there's nothing to do | |
| 1096 | 516 |
| 517 glPushMatrix(); | |
| 518 glPushAttrib(GL_CURRENT_BIT); | |
| 519 | |
| 1147 | 520 static Matrix t; |
| 1096 | 521 t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky); |
| 522 glMultMatrixf((const GLfloat *)t.getElements()); | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
523 |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
524 const vertex *imageVerts = sprite->getVertices(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
525 const vertex *tVerts; |
|
1353
c3c9d6ed3e21
ParticleSystem:setQuads can now accept a table argument, cleaned up the ParticleSystem quad code
Alex Szpakowski <slime73@gmail.com>
parents:
1352
diff
changeset
|
526 |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
527 size_t numQuads = quads.size(); |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
528 |
| 1147 | 529 // set the vertex data for each particle (transformation, texcoords, color) |
| 530 for (int i = 0; i < numParticles; i++) | |
| 1096 | 531 { |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
532 particle *p = pStart + i; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
533 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
534 if (numQuads > 0) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
535 { |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
536 // Make sure the quad index is valid |
|
1353
c3c9d6ed3e21
ParticleSystem:setQuads can now accept a table argument, cleaned up the ParticleSystem quad code
Alex Szpakowski <slime73@gmail.com>
parents:
1352
diff
changeset
|
537 size_t quadIndex = (p->quadIndex >= numQuads) ? numQuads - 1 : p->quadIndex; |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
538 tVerts = quads[quadIndex]->getVertices(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
539 } |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
540 else |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
541 tVerts = imageVerts; |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
542 |
| 1147 | 543 // particle vertices are sprite vertices transformed by particle information |
| 544 t.setTransformation(p->position[0], p->position[1], p->rotation, p->size, p->size, offsetX, offsetY, 0.0f, 0.0f); | |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
545 t.transform(&particleVerts[i*4], &tVerts[0], 4); |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
546 |
| 1147 | 547 // set the texture coordinate and color data for particle vertices |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
548 for (int v = 0; v < 4; v++) |
|
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
549 { |
| 1147 | 550 int vi = (i * 4) + v; // current vertex index for particle |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
551 |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
552 particleVerts[vi].s = tVerts[v].s; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
553 particleVerts[vi].t = tVerts[v].t; |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
554 |
| 1147 | 555 // particle colors are stored as floats (0-1) but vertex colors are stored as unsigned bytes (0-255) |
| 556 particleVerts[vi].r = p->color.r*255; | |
| 557 particleVerts[vi].g = p->color.g*255; | |
| 558 particleVerts[vi].b = p->color.b*255; | |
| 559 particleVerts[vi].a = p->color.a*255; | |
| 560 } | |
| 1096 | 561 } |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
562 |
| 1147 | 563 sprite->bind(); |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
564 |
| 1147 | 565 glEnableClientState(GL_COLOR_ARRAY); |
| 566 glEnableClientState(GL_VERTEX_ARRAY); | |
| 567 glEnableClientState(GL_TEXTURE_COORD_ARRAY); | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
568 |
| 1147 | 569 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(vertex), (GLvoid *)&particleVerts[0].r); |
| 570 glVertexPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&particleVerts[0].x); | |
| 571 glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&particleVerts[0].s); | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
572 |
| 1147 | 573 glDrawArrays(GL_QUADS, 0, numParticles*4); |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
574 |
| 1147 | 575 glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 576 glDisableClientState(GL_VERTEX_ARRAY); | |
| 577 glDisableClientState(GL_COLOR_ARRAY); | |
| 1096 | 578 |
| 579 glPopAttrib(); | |
| 580 glPopMatrix(); | |
| 581 } | |
| 582 | |
| 583 void ParticleSystem::update(float dt) | |
| 584 { | |
| 585 // Traverse all particles and update. | |
| 586 particle *p = pStart; | |
| 587 | |
| 588 // Make some more particles. | |
| 589 if (active) | |
| 590 { | |
| 591 float rate = 1.0f / emissionRate; // the amount of time between each particle emit | |
| 592 emitCounter += dt; | |
| 593 while (emitCounter > rate) | |
| 594 { | |
| 595 add(); | |
| 596 emitCounter -= rate; | |
| 597 } | |
| 598 /*int particles = (int)(emissionRate * dt); | |
| 599 for (int i = 0; i != particles; i++) | |
| 600 add();*/ | |
| 601 | |
| 602 life -= dt; | |
| 603 if (lifetime != -1 && life < 0) | |
| 604 stop(); | |
| 605 } | |
| 606 | |
| 607 while (p != pLast) | |
| 608 { | |
| 609 // Decrease lifespan. | |
| 610 p->life -= dt; | |
| 611 | |
| 612 if (p->life > 0) | |
| 613 { | |
| 614 | |
| 615 // Temp variables. | |
| 616 love::Vector radial, tangential, gravity(0, p->gravity); | |
| 617 love::Vector ppos(p->position[0], p->position[1]); | |
| 618 | |
| 619 // Get vector from particle center to particle. | |
| 620 radial = ppos - p->origin; | |
| 621 radial.normalize(); | |
| 622 tangential = radial; | |
| 623 | |
| 624 // Resize radial acceleration. | |
| 625 radial *= p->radialAcceleration; | |
| 626 | |
| 627 // Calculate tangential acceleration. | |
| 628 { | |
| 629 float a = tangential.getX(); | |
| 630 tangential.setX(-tangential.getY()); | |
| 631 tangential.setY(a); | |
| 632 } | |
| 633 | |
| 634 // Resize tangential. | |
| 635 tangential *= p->tangentialAcceleration; | |
| 636 | |
| 637 // Update position. | |
| 638 p->speed += (radial+tangential+gravity)*dt; | |
| 639 | |
| 640 // Modify position. | |
| 641 ppos += p->speed * dt; | |
| 642 | |
| 643 p->position[0] = ppos.getX(); | |
| 644 p->position[1] = ppos.getY(); | |
| 645 | |
| 646 const float t = 1.0f - p->life / p->lifetime; | |
| 647 | |
| 648 // Rotate. | |
| 649 p->rotation += (p->spinStart * (1.0f - t) + p->spinEnd * t)*dt; | |
| 650 | |
| 651 // Change size according to given intervals: | |
| 652 // i = 0 1 2 3 n-1 | |
| 653 // |-------|-------|------|--- ... ---| | |
| 654 // t = 0 1/(n-1) 3/(n-1) 1 | |
| 655 // | |
| 656 // `s' is the interpolation variable scaled to the current | |
| 657 // interval width, e.g. if n = 5 and t = 0.3, then the current | |
| 658 // indices are 1,2 and s = 0.3 - 0.25 = 0.05 | |
| 659 float s = p->sizeOffset + t * p->sizeIntervalSize; // size variation | |
| 660 s *= (float)(sizes.size() - 1); // 0 <= s < sizes.size() | |
| 661 size_t i = (size_t)s; | |
| 662 size_t k = (i == sizes.size() - 1) ? i : i + 1; // boundary check (prevents failing on t = 1.0f) | |
| 663 s -= (float)i; // transpose s to be in interval [0:1]: i <= s < i + 1 ~> 0 <= s < 1 | |
| 664 p->size = sizes[i] * (1.0f - s) + sizes[k] * s; | |
| 665 | |
| 666 // Update color according to given intervals (as above) | |
| 667 s = t * (float)(colors.size() - 1); | |
| 668 i = (size_t)s; | |
| 669 k = (i == colors.size() - 1) ? i : i + 1; | |
| 670 s -= (float)i; // 0 <= s <= 1 | |
| 671 p->color = colors[i] * (1.0f - s) + colors[k] * s; | |
| 672 | |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
673 // Update quad index |
|
1353
c3c9d6ed3e21
ParticleSystem:setQuads can now accept a table argument, cleaned up the ParticleSystem quad code
Alex Szpakowski <slime73@gmail.com>
parents:
1352
diff
changeset
|
674 k = quads.size(); |
|
c3c9d6ed3e21
ParticleSystem:setQuads can now accept a table argument, cleaned up the ParticleSystem quad code
Alex Szpakowski <slime73@gmail.com>
parents:
1352
diff
changeset
|
675 if (k > 0) |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
676 { |
|
1353
c3c9d6ed3e21
ParticleSystem:setQuads can now accept a table argument, cleaned up the ParticleSystem quad code
Alex Szpakowski <slime73@gmail.com>
parents:
1352
diff
changeset
|
677 s = t * (float) k; // [0:numquads-1] |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
678 i = (s > 0) ? (size_t) s : 0; |
|
1353
c3c9d6ed3e21
ParticleSystem:setQuads can now accept a table argument, cleaned up the ParticleSystem quad code
Alex Szpakowski <slime73@gmail.com>
parents:
1352
diff
changeset
|
679 p->quadIndex = (i < k) ? i : k - 1; |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
680 } |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
681 else |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
682 p->quadIndex = 0; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
683 |
| 1096 | 684 // Next particle. |
| 685 p++; | |
| 686 } | |
| 687 else | |
| 688 { | |
| 689 remove(p); | |
| 690 | |
| 691 if (p >= pLast) | |
| 692 return; | |
| 693 } // else | |
| 694 } // while | |
| 695 } | |
| 696 | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
697 bool ParticleSystem::getConstant(const char *in, AreaSpreadDistribution &out) |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
698 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
699 return distributions.find(in, out); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
700 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
701 |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
702 bool ParticleSystem::getConstant(AreaSpreadDistribution in, const char *&out) |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
703 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
704 return distributions.find(in, out); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
705 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
706 |
| 1096 | 707 } // opengl |
| 708 } // graphics | |
| 709 } // love |
