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