particles now rotate around their center

This commit is contained in:
bill@Ixion
2009-08-30 04:33:54 -05:00
parent 5850700c8e
commit 98db275e76
+113 -112
View File
@@ -1,73 +1,73 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "ParticleSystem.h"
#include <SDL_opengl.h>
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "ParticleSystem.h"
#include <SDL_opengl.h>
#include <cmath>
#include <cstdlib>
#include <cstring>
namespace love
{
namespace graphics
{
namespace opengl
{
float calculate_variation(float inner, float outer, float var)
{
float low = inner - (outer/2.0f)*var;
float high = inner + (outer/2.0f)*var;
float r = (rand() / (float(RAND_MAX)+1));
return low*(1-r)+high*r;
}
ParticleSystem::ParticleSystem(Image * sprite, unsigned int buffer) : pStart(0), pLast(0), pEnd(0), active(true), emissionRate(0),
emitCounter(0), lifetime(-1), life(0), particleLifeMin(0), particleLifeMax(0),
direction(0), spread(0), relative(false), speedMin(0), speedMax(0), gravityMin(0),
gravityMax(0), radialAccelerationMin(0), radialAccelerationMax(0),
tangentialAccelerationMin(0), tangentialAccelerationMax(0),
sizeStart(1), sizeEnd(1), sizeVariation(0), rotationMin(0), rotationMax(0),
spinStart(0), spinEnd(0), spinVariation(0)
{
this->sprite = sprite;
sprite->retain();
memset(colorStart, 255, 4);
memset(colorEnd, 255, 4);
setBufferSize(buffer);
}
ParticleSystem::~ParticleSystem()
{
if(this->sprite != 0)
{
this->sprite->release();
this->sprite = 0;
}
if(pStart != 0)
delete [] pStart;
}
#include <cstdlib>
#include <cstring>
namespace love
{
namespace graphics
{
namespace opengl
{
float calculate_variation(float inner, float outer, float var)
{
float low = inner - (outer/2.0f)*var;
float high = inner + (outer/2.0f)*var;
float r = (rand() / (float(RAND_MAX)+1));
return low*(1-r)+high*r;
}
ParticleSystem::ParticleSystem(Image * sprite, unsigned int buffer) : pStart(0), pLast(0), pEnd(0), active(true), emissionRate(0),
emitCounter(0), lifetime(-1), life(0), particleLifeMin(0), particleLifeMax(0),
direction(0), spread(0), relative(false), speedMin(0), speedMax(0), gravityMin(0),
gravityMax(0), radialAccelerationMin(0), radialAccelerationMax(0),
tangentialAccelerationMin(0), tangentialAccelerationMax(0),
sizeStart(1), sizeEnd(1), sizeVariation(0), rotationMin(0), rotationMax(0),
spinStart(0), spinEnd(0), spinVariation(0)
{
this->sprite = sprite;
sprite->retain();
memset(colorStart, 255, 4);
memset(colorEnd, 255, 4);
setBufferSize(buffer);
}
ParticleSystem::~ParticleSystem()
{
if(this->sprite != 0)
{
this->sprite->release();
this->sprite = 0;
}
if(pStart != 0)
delete [] pStart;
}
void ParticleSystem::add()
{
if(isFull()) return;
@@ -125,33 +125,33 @@ namespace opengl
pLast++;
}
void ParticleSystem::remove(particle * p)
{
if(!isEmpty())
{
*p = *(--pLast);
}
}
void ParticleSystem::setSprite(Image * image)
{
if(this->sprite != 0)
{
this->sprite->release();
this->sprite = 0;
}
this->sprite = image;
}
void ParticleSystem::remove(particle * p)
{
if(!isEmpty())
{
*p = *(--pLast);
}
}
void ParticleSystem::setSprite(Image * image)
{
if(this->sprite != 0)
{
this->sprite->release();
this->sprite = 0;
}
this->sprite = image;
}
void ParticleSystem::setBufferSize(unsigned int size)
{
// delete previous data
delete [] pStart;
pLast = pStart = new particle[size];
pEnd = pStart + size;
{
// delete previous data
delete [] pStart;
pLast = pStart = new particle[size];
pEnd = pStart + size;
}
void ParticleSystem::setEmissionRate(int rate)
@@ -393,6 +393,7 @@ namespace opengl
glTranslatef(p->position[0],p->position[1],0.0f);
glRotatef(p->rotation * 57.29578f, 0.0f, 0.0f, 1.0f); // rad * (180 / pi)
glScalef(p->size,p->size,1.0f);
glTranslatef(sprite->getWidth()*-0.5f,sprite->getHeight()*-0.5f,0.0f);
sprite->draw(0,0, 0, 1, 1, 0, 0);
glPopMatrix();
@@ -404,20 +405,20 @@ namespace opengl
}
void ParticleSystem::update(float dt)
{
{
// Traverse all particles and update.
particle * p = pStart;
// Make some more particles.
if(active)
{
float rate = 1.0f / emissionRate; // the amount of time between each particle emit
emitCounter += dt;
while(emitCounter > rate)
{
add();
emitCounter -= rate;
}
// Make some more particles.
if(active)
{
float rate = 1.0f / emissionRate; // the amount of time between each particle emit
emitCounter += dt;
while(emitCounter > rate)
{
add();
emitCounter -= rate;
}
/*int particles = (int)(emissionRate * dt);
for(int i = 0; i != particles; i++)
add();*/
@@ -490,9 +491,9 @@ namespace opengl
if(p >= pLast)
return;
} // else
} // while
}
} // opengl
} // graphics
} // love
} // while
}
} // opengl
} // graphics
} // love