2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
// cocos2d
|
|
|
|
#include "CCTextureAtlas.h"
|
|
|
|
#include "CCTextureCache.h"
|
2010-12-31 14:56:24 +08:00
|
|
|
#include "ccMacros.h"
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
// support
|
|
|
|
#include "CCTexture2D.h"
|
|
|
|
|
2010-11-03 10:59:07 +08:00
|
|
|
#include <stdlib.h>
|
2010-08-02 10:58:00 +08:00
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
//According to some tests GL_TRIANGLE_STRIP is slower, MUCH slower. Probably I'm doing something very wrong
|
|
|
|
|
|
|
|
// implementation CCTextureAtlas
|
|
|
|
|
2010-11-12 17:26:01 +08:00
|
|
|
namespace cocos2d {
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
CCTextureAtlas::CCTextureAtlas()
|
2010-09-06 14:15:06 +08:00
|
|
|
:m_pTexture(NULL)
|
|
|
|
,m_pIndices(NULL)
|
|
|
|
,m_pQuads(NULL)
|
|
|
|
{}
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
CCTextureAtlas::~CCTextureAtlas()
|
|
|
|
{
|
2010-11-03 10:59:07 +08:00
|
|
|
// CCLOGINFO("cocos2d: deallocing CCTextureAtlas.");
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_FREE(m_pQuads)
|
|
|
|
CC_SAFE_FREE(m_pIndices)
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
#if CC_USES_VBO
|
2010-07-16 16:28:11 +08:00
|
|
|
glDeleteBuffers(2, m_pBuffersVBO);
|
2010-12-30 17:30:11 +08:00
|
|
|
#endif // CC_USES_VBO
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_RELEASE(m_pTexture);
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
unsigned int CCTextureAtlas::getTotalQuads()
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
|
|
|
return m_uTotalQuads;
|
|
|
|
}
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
unsigned int CCTextureAtlas::getCapacity()
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
|
|
|
return m_uCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCTexture2D* CCTextureAtlas::getTexture()
|
|
|
|
{
|
|
|
|
return m_pTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCTextureAtlas::setTexture(CCTexture2D * var)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_RETAIN(var);
|
|
|
|
CC_SAFE_RELEASE(m_pTexture);
|
2010-07-16 16:28:11 +08:00
|
|
|
m_pTexture = var;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccV3F_C4B_T2F_Quad* CCTextureAtlas::getQuads()
|
|
|
|
{
|
|
|
|
return m_pQuads;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCTextureAtlas::setQuads(ccV3F_C4B_T2F_Quad *var)
|
|
|
|
{
|
|
|
|
m_pQuads = var;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextureAtlas - alloc & init
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
CCTextureAtlas * CCTextureAtlas::textureAtlasWithFile(const char* file, unsigned int capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
|
|
|
CCTextureAtlas * pTextureAtlas = new CCTextureAtlas();
|
2010-09-04 12:02:52 +08:00
|
|
|
if(pTextureAtlas && pTextureAtlas->initWithFile(file, capacity))
|
|
|
|
{
|
|
|
|
pTextureAtlas->autorelease();
|
|
|
|
return pTextureAtlas;
|
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pTextureAtlas);
|
2010-09-04 12:02:52 +08:00
|
|
|
return NULL;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
CCTextureAtlas * CCTextureAtlas::textureAtlasWithTexture(CCTexture2D *texture, unsigned int capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
|
|
|
CCTextureAtlas * pTextureAtlas = new CCTextureAtlas();
|
2010-09-04 12:02:52 +08:00
|
|
|
if (pTextureAtlas && pTextureAtlas->initWithTexture(texture, capacity))
|
|
|
|
{
|
|
|
|
pTextureAtlas->autorelease();
|
|
|
|
return pTextureAtlas;
|
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pTextureAtlas);
|
2010-09-04 12:02:52 +08:00
|
|
|
return NULL;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
bool CCTextureAtlas::initWithFile(const char * file, unsigned int capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
|
|
|
// retained in property
|
2010-12-30 17:30:11 +08:00
|
|
|
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(file);
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
if (texture)
|
|
|
|
{
|
|
|
|
return initWithTexture(texture, capacity);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Could not open file: %s", file);
|
|
|
|
delete this;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
bool CCTextureAtlas::initWithTexture(CCTexture2D *texture, unsigned int capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2010-09-04 12:02:52 +08:00
|
|
|
assert(texture != NULL);
|
2010-07-16 16:28:11 +08:00
|
|
|
m_uCapacity = capacity;
|
|
|
|
m_uTotalQuads = 0;
|
|
|
|
|
|
|
|
// retained in property
|
2010-08-06 14:32:32 +08:00
|
|
|
this->m_pTexture = texture;
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_RETAIN(m_pTexture);
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
m_pQuads = (ccV3F_C4B_T2F_Quad*)calloc( sizeof(ccV3F_C4B_T2F_Quad) * m_uCapacity, 1 );
|
|
|
|
m_pIndices = (GLushort *)calloc( sizeof(GLushort) * m_uCapacity * 6, 1 );
|
|
|
|
|
|
|
|
if( ! ( m_pQuads && m_pIndices) ) {
|
2010-11-03 10:59:07 +08:00
|
|
|
//CCLOG("cocos2d: CCTextureAtlas: not enough memory");
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_FREE(m_pQuads)
|
|
|
|
CC_SAFE_FREE(m_pIndices)
|
2010-09-04 12:02:52 +08:00
|
|
|
return false;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
#if CC_USES_VBO
|
2010-07-16 16:28:11 +08:00
|
|
|
// initial binding
|
|
|
|
glGenBuffers(2, &m_pBuffersVBO[0]);
|
2010-12-30 17:30:11 +08:00
|
|
|
#endif // CC_USES_VBO
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
this->initIndices();
|
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2010-08-06 16:05:19 +08:00
|
|
|
char * CCTextureAtlas::description()
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2010-08-06 16:05:19 +08:00
|
|
|
char *ret = new char[100];
|
2010-09-03 11:49:10 +08:00
|
|
|
sprintf(ret, "<CCTextureAtlas | totalQuads = %u>", m_uTotalQuads);
|
2010-07-16 16:28:11 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CCTextureAtlas::initIndices()
|
|
|
|
{
|
2010-08-02 10:58:00 +08:00
|
|
|
for( unsigned int i=0; i < m_uCapacity; i++)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
|
|
|
#if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
|
|
|
|
m_pIndices[i*6+0] = i*4+0;
|
|
|
|
m_pIndices[i*6+1] = i*4+0;
|
|
|
|
m_pIndices[i*6+2] = i*4+2;
|
|
|
|
m_pIndices[i*6+3] = i*4+1;
|
|
|
|
m_pIndices[i*6+4] = i*4+3;
|
|
|
|
m_pIndices[i*6+5] = i*4+3;
|
|
|
|
#else
|
|
|
|
m_pIndices[i*6+0] = i*4+0;
|
|
|
|
m_pIndices[i*6+1] = i*4+1;
|
|
|
|
m_pIndices[i*6+2] = i*4+2;
|
|
|
|
|
|
|
|
// inverted index. issue #179
|
|
|
|
m_pIndices[i*6+3] = i*4+3;
|
|
|
|
m_pIndices[i*6+4] = i*4+2;
|
|
|
|
m_pIndices[i*6+5] = i*4+1;
|
|
|
|
// m_pIndices[i*6+3] = i*4+2;
|
|
|
|
// m_pIndices[i*6+4] = i*4+3;
|
|
|
|
// m_pIndices[i*6+5] = i*4+1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
#if CC_USES_VBO
|
2010-07-16 16:28:11 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]);
|
2010-07-21 11:13:32 +08:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(m_pQuads[0]) * m_uCapacity, m_pQuads, GL_DYNAMIC_DRAW);
|
2010-07-16 16:28:11 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);
|
2010-07-21 11:13:32 +08:00
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_pIndices[0]) * m_uCapacity * 6, m_pIndices, GL_STATIC_DRAW);
|
2010-07-16 16:28:11 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
2010-12-30 17:30:11 +08:00
|
|
|
#endif // CC_USES_VBO
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TextureAtlas - Update, Insert, Move & Remove
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
void CCTextureAtlas::updateQuad(ccV3F_C4B_T2F_Quad *quad, unsigned int index)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert( index >= 0 && index < m_uCapacity, "updateQuadWithTexture: Invalid index");
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
m_uTotalQuads = max( index+1, m_uTotalQuads);
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
m_pQuads[index] = *quad;
|
|
|
|
}
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
void CCTextureAtlas::insertQuad(ccV3F_C4B_T2F_Quad *quad, unsigned int index)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert( index < m_uCapacity, "insertQuadWithTexture: Invalid index");
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
m_uTotalQuads++;
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert( m_uTotalQuads <= m_uCapacity, "invalid totalQuads");
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
// issue #575. index can be > totalQuads
|
2010-12-30 17:30:11 +08:00
|
|
|
unsigned int remaining = (m_uTotalQuads-1) - index;
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
// last object doesn't need to be moved
|
|
|
|
if( remaining > 0) {
|
2010-08-06 14:32:32 +08:00
|
|
|
// texture coordinates
|
2010-07-21 11:13:32 +08:00
|
|
|
memmove( &m_pQuads[index+1],&m_pQuads[index], sizeof(m_pQuads[0]) * remaining );
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_pQuads[index] = *quad;
|
|
|
|
}
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
void CCTextureAtlas::insertQuadFromIndex(unsigned int oldIndex, unsigned int newIndex)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert( newIndex >= 0 && newIndex < m_uTotalQuads, "insertQuadFromIndex:atIndex: Invalid index");
|
|
|
|
CCAssert( oldIndex >= 0 && oldIndex < m_uTotalQuads, "insertQuadFromIndex:atIndex: Invalid index");
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
if( oldIndex == newIndex )
|
|
|
|
return;
|
|
|
|
|
2010-11-12 17:26:01 +08:00
|
|
|
// because it is ambigious in iphone, so we implement abs ourself
|
|
|
|
// unsigned int howMany = abs( oldIndex - newIndex);
|
|
|
|
unsigned int howMany = (oldIndex - newIndex) >= 0 ? (oldIndex - newIndex) : (newIndex - oldIndex);
|
2010-12-30 17:30:11 +08:00
|
|
|
unsigned int dst = oldIndex;
|
|
|
|
unsigned int src = oldIndex + 1;
|
2010-07-16 16:28:11 +08:00
|
|
|
if( oldIndex > newIndex) {
|
|
|
|
dst = newIndex+1;
|
|
|
|
src = newIndex;
|
|
|
|
}
|
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
// texture coordinates
|
2010-07-16 16:28:11 +08:00
|
|
|
ccV3F_C4B_T2F_Quad quadsBackup = m_pQuads[oldIndex];
|
2010-07-21 11:13:32 +08:00
|
|
|
memmove( &m_pQuads[dst],&m_pQuads[src], sizeof(m_pQuads[0]) * howMany );
|
2010-07-16 16:28:11 +08:00
|
|
|
m_pQuads[newIndex] = quadsBackup;
|
|
|
|
}
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
void CCTextureAtlas::removeQuadAtIndex(unsigned int index)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert( index < m_uTotalQuads, "removeQuadAtIndex: Invalid index");
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
unsigned int remaining = (m_uTotalQuads-1) - index;
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
// last object doesn't need to be moved
|
|
|
|
if( remaining ) {
|
2010-08-06 14:32:32 +08:00
|
|
|
// texture coordinates
|
2010-07-21 11:13:32 +08:00
|
|
|
memmove( &m_pQuads[index],&m_pQuads[index+1], sizeof(m_pQuads[0]) * remaining );
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_uTotalQuads--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCTextureAtlas::removeAllQuads()
|
|
|
|
{
|
|
|
|
m_uTotalQuads = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextureAtlas - Resize
|
2010-08-02 10:58:00 +08:00
|
|
|
bool CCTextureAtlas::resizeCapacity(unsigned int newCapacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
|
|
|
if( newCapacity == m_uCapacity )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// update capacity and totolQuads
|
2010-08-02 10:58:00 +08:00
|
|
|
m_uTotalQuads = min(m_uTotalQuads, newCapacity);
|
2010-07-16 16:28:11 +08:00
|
|
|
m_uCapacity = newCapacity;
|
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
void * tmpQuads = realloc( m_pQuads, sizeof(m_pQuads[0]) * m_uCapacity );
|
|
|
|
void * tmpIndices = realloc( m_pIndices, sizeof(m_pIndices[0]) * m_uCapacity * 6 );
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
if( ! ( tmpQuads && tmpIndices) ) {
|
2010-11-03 10:59:07 +08:00
|
|
|
//CCLOG("cocos2d: CCTextureAtlas: not enough memory");
|
2010-07-16 16:28:11 +08:00
|
|
|
if( tmpQuads )
|
|
|
|
free(tmpQuads);
|
|
|
|
else
|
|
|
|
free(m_pQuads);
|
|
|
|
|
|
|
|
if( tmpIndices )
|
|
|
|
free(tmpIndices);
|
|
|
|
else
|
|
|
|
free(m_pIndices);
|
|
|
|
|
|
|
|
m_pQuads = NULL;
|
|
|
|
m_pIndices = NULL;
|
|
|
|
m_uCapacity = m_uTotalQuads = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pQuads = (ccV3F_C4B_T2F_Quad *)tmpQuads;
|
|
|
|
m_pIndices = (GLushort *)tmpIndices;
|
|
|
|
|
|
|
|
this->initIndices();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextureAtlas - Drawing
|
|
|
|
|
|
|
|
void CCTextureAtlas::drawQuads()
|
|
|
|
{
|
|
|
|
this->drawNumberOfQuads(m_uTotalQuads);
|
|
|
|
}
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
void CCTextureAtlas::drawNumberOfQuads(unsigned int n)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2010-12-30 17:30:11 +08:00
|
|
|
// 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, GL_TEXTURE_COORD_ARRAY
|
|
|
|
// Unneeded states: -
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_pTexture->getName());
|
2010-07-21 17:40:10 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
#define kQuadSize sizeof(m_pQuads[0].bl)
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
#if CC_USES_VBO
|
2011-01-10 17:54:44 +08:00
|
|
|
|
2011-01-11 13:57:13 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]);
|
|
|
|
|
2011-01-10 17:54:44 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTTURE_DATA
|
2011-01-11 13:57:13 +08:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(m_pQuads[0]) * m_uCapacity, m_pQuads, GL_DYNAMIC_DRAW);
|
2011-01-10 17:54:44 +08:00
|
|
|
#endif
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
// XXX: update is done in draw... perhaps it should be done in a timer
|
2010-07-21 11:13:32 +08:00
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(m_pQuads[0]) * n, m_pQuads);
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
// vertices
|
2010-12-30 17:30:11 +08:00
|
|
|
glVertexPointer(3, GL_FLOAT, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, vertices));
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
// colors
|
2010-12-30 17:30:11 +08:00
|
|
|
glColorPointer(4, GL_UNSIGNED_BYTE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, colors));
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
// texture coords
|
2010-12-30 17:30:11 +08:00
|
|
|
glTexCoordPointer(2, GL_FLOAT, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, texCoords));
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);
|
2011-01-11 13:57:13 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTTURE_DATA
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_pIndices[0]) * m_uCapacity * 6, m_pIndices, GL_STATIC_DRAW);
|
|
|
|
#endif
|
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
#if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
|
2010-12-30 17:30:11 +08:00
|
|
|
glDrawElements(GL_TRIANGLE_STRIP, n*6, GL_UNSIGNED_SHORT, (GLvoid*)0);
|
2010-07-16 16:28:11 +08:00
|
|
|
#else
|
2010-12-30 17:30:11 +08:00
|
|
|
glDrawElements(GL_TRIANGLES, n*6, GL_UNSIGNED_SHORT, (GLvoid*)0);
|
|
|
|
#endif // CC_USES_VBO
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
#else // ! CC_USES_VBO
|
2010-07-21 17:40:10 +08:00
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
unsigned int offset = (unsigned int)m_pQuads;
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
// vertex
|
2010-12-30 17:30:11 +08:00
|
|
|
unsigned int diff = offsetof( ccV3F_C4B_T2F, vertices);
|
|
|
|
glVertexPointer(3, GL_FLOAT, kQuadSize, (GLvoid*) (offset + diff) );
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
// color
|
|
|
|
diff = offsetof( ccV3F_C4B_T2F, colors);
|
2010-12-30 17:30:11 +08:00
|
|
|
glColorPointer(4, GL_UNSIGNED_BYTE, kQuadSize, (GLvoid*)(offset + diff));
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2010-08-06 14:32:32 +08:00
|
|
|
// texture coords
|
2010-07-16 16:28:11 +08:00
|
|
|
diff = offsetof( ccV3F_C4B_T2F, texCoords);
|
2010-12-30 17:30:11 +08:00
|
|
|
glTexCoordPointer(2, GL_FLOAT, kQuadSize, (GLvoid*)(offset + diff));
|
2010-07-16 16:28:11 +08:00
|
|
|
|
|
|
|
#if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
|
|
|
|
glDrawElements(GL_TRIANGLE_STRIP, n*6, GL_UNSIGNED_SHORT, m_pIndices);
|
|
|
|
#else
|
|
|
|
glDrawElements(GL_TRIANGLES, n*6, GL_UNSIGNED_SHORT, m_pIndices);
|
|
|
|
#endif
|
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
#endif // CC_USES_VBO
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
|
2010-11-12 17:26:01 +08:00
|
|
|
}//namespace cocos2d
|