axmol/cocos2dx/platform/CCParticleSystemPoint_mobil...

186 lines
5.8 KiB
C++

/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCParticleSystemPoint.h"
#include "platform/CCGL.h"
namespace cocos2d {
//implementation CCParticleSystemPoint
bool CCParticleSystemPoint::initWithTotalParticles(int numberOfParticles)
{
if( CCParticleSystem::initWithTotalParticles(numberOfParticles) )
{
m_pVertices = new ccPointSprite[m_nTotalParticles];
if( ! m_pVertices )
{
CCLOG("cocos2d: Particle system: not enough memory");
this->release();
return false;
}
#if CC_USES_VBO
glGenBuffers(1, &m_uVerticesID);
// initial binding
glBindBuffer(GL_ARRAY_BUFFER, m_uVerticesID);
glBufferData(GL_ARRAY_BUFFER, sizeof(ccPointSprite)*m_nTotalParticles, m_pVertices, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif
return true;
}
return false;
}
CCParticleSystemPoint::~CCParticleSystemPoint()
{
CCX_SAFE_DELETE(m_pVertices);
#if CC_USES_VBO
glDeleteBuffers(1, &m_uVerticesID);
#endif
}
void CCParticleSystemPoint::updateQuadWithParticle(tCCParticle* particle, CGPoint newPosition)
{
// place vertices and colos in array
m_pVertices[m_nParticleIdx].pos = vertex2(newPosition.x, newPosition.y);
m_pVertices[m_nParticleIdx].size = particle->size;
m_pVertices[m_nParticleIdx].colors = particle->color;
}
void CCParticleSystemPoint::postStep()
{
#if CC_USES_VBO
glBindBuffer(GL_ARRAY_BUFFER, m_uVerticesID);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(ccPointSprite)*m_nParticleCount, m_pVertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif
}
void CCParticleSystemPoint::draw()
{
if (m_nParticleIdx==0)
{
return;
}
// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
// Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY
// Unneeded states: GL_TEXTURE_COORD_ARRAY
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, m_pTexture->getName());
glEnable(GL_POINT_SPRITE_OES);
glTexEnvi( GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE );
#define kPointSize sizeof(m_pVertices[0])
#if CC_USES_VBO
glBindBuffer(GL_ARRAY_BUFFER, m_uVerticesID);
#if CC_ENABLE_CACHE_TEXTTURE_DATA
glBufferData(GL_ARRAY_BUFFER, sizeof(ccPointSprite)*m_nTotalParticles, m_pVertices, GL_DYNAMIC_DRAW);
#endif
glVertexPointer(2,GL_FLOAT,kPointSize,0);
glColorPointer(4, GL_FLOAT, kPointSize,(GLvoid*) offsetof(ccPointSprite,colors) );
glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
glPointSizePointerOES(GL_FLOAT,kPointSize,(GLvoid*) offsetof(ccPointSprite,size) );
#else // Uses Vertex Array List
int offset = (int)m_pVertices;
glVertexPointer(2,GL_FLOAT, kPointSize, (GLvoid*) offset);
int diff = offsetof(ccPointSprite, colors);
glColorPointer(4, GL_FLOAT, kPointSize, (GLvoid*) (offset+diff));
glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
diff = offsetof(ccPointSprite, size);
glPointSizePointerOES(GL_FLOAT, kPointSize, (GLvoid*) (offset+diff));
#endif
bool newBlend = (m_tBlendFunc.src != CC_BLEND_SRC || m_tBlendFunc.dst != CC_BLEND_DST) ? true : false;
if( newBlend )
{
glBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );
}
glDrawArrays(GL_POINTS, 0, m_nParticleIdx);
// restore blend state
if( newBlend )
glBlendFunc( CC_BLEND_SRC, CC_BLEND_DST);
#if CC_USES_VBO
// unbind VBO buffer
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif
glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
glDisable(GL_POINT_SPRITE_OES);
// restore GL default state
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
// Non supported properties
//
// SPIN IS NOT SUPPORTED
//
void CCParticleSystemPoint::setStartSpin(float var)
{
NSAssert(var == 0, "PointParticleSystem doesn't support spinning");
CCParticleSystem::setStartSpin(var);
}
void CCParticleSystemPoint::setStartSpinVar(float var)
{
NSAssert(var == 0, "PointParticleSystem doesn't support spinning");
CCParticleSystem::setStartSpinVar(var);
}
void CCParticleSystemPoint::setEndSpin(float var)
{
NSAssert(var == 0, "PointParticleSystem doesn't support spinning");
CCParticleSystem::setEndSpin(var);
}
void CCParticleSystemPoint::setEndSpinVar(float var)
{
NSAssert(var == 0, "PointParticleSystem doesn't support spinning");
CCParticleSystem::setEndSpinVar(var);
}
//
// SIZE > 64 IS NOT SUPPORTED
//
void CCParticleSystemPoint::setStartSize(float size)
{
NSAssert(size >= 0 && size <= CC_MAX_PARTICLE_SIZE, "PointParticleSystem only supports 0 <= size <= 64");
CCParticleSystem::setStartSize(size);
}
void CCParticleSystemPoint::setEndSize(float size)
{
NSAssert( (size == kCCParticleStartSizeEqualToEndSize) ||
( size >= 0 && size <= CC_MAX_PARTICLE_SIZE), "PointParticleSystem only supports 0 <= size <= 64");
CCParticleSystem::setEndSize(size);
}
}// namespace cocos2d