2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2011-07-05 10:47:25 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2010-08-02 10:58:00 +08:00
|
|
|
|
|
|
|
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"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "shaders/CCGLProgram.h"
|
|
|
|
#include "shaders/ccGLStateCache.h"
|
2012-07-19 17:22:36 +08:00
|
|
|
#include "support/CCNotificationCenter.h"
|
2012-04-24 15:02:18 +08:00
|
|
|
#include "CCEventType.h"
|
2012-11-14 18:05:15 +08:00
|
|
|
#include "CCGL.h"
|
2010-07-16 16:28:11 +08:00
|
|
|
// support
|
|
|
|
#include "CCTexture2D.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCString.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
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation TextureAtlas
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-03-26 18:44:28 +08:00
|
|
|
NS_CC_BEGIN
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureAtlas::TextureAtlas()
|
2013-06-15 14:03:30 +08:00
|
|
|
:_indices(NULL)
|
|
|
|
,_dirty(false)
|
|
|
|
,_texture(NULL)
|
|
|
|
,_quads(NULL)
|
2010-09-06 14:15:06 +08:00
|
|
|
{}
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureAtlas::~TextureAtlas()
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOGINFO("cocos2d: TextureAtlas deallocing %p.", this);
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_quads);
|
|
|
|
CC_SAFE_FREE(_indices);
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteBuffers(2, _buffersVBO);
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-03-26 18:44:28 +08:00
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteVertexArrays(1, &_VAOname);
|
2013-07-13 10:51:42 +08:00
|
|
|
ccGLBindVAO(0);
|
2012-03-26 18:44:28 +08:00
|
|
|
#endif
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_texture);
|
2012-04-24 15:02:18 +08:00
|
|
|
|
2013-07-17 13:53:16 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2013-07-12 06:24:23 +08:00
|
|
|
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
|
2013-07-17 13:53:16 +08:00
|
|
|
#endif
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
int TextureAtlas::getTotalQuads() const
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _totalQuads;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
int TextureAtlas::getCapacity() const
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _capacity;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 14:18:35 +08:00
|
|
|
Texture2D* TextureAtlas::getTexture() const
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _texture;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureAtlas::setTexture(Texture2D * var)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RETAIN(var);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_texture);
|
|
|
|
_texture = var;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad* TextureAtlas::getQuads()
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
//if someone accesses the quads directly, presume that changes will be made
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
|
|
|
return _quads;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 14:18:35 +08:00
|
|
|
void TextureAtlas::setQuads(V3F_C4B_T2F_Quad* quads)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-07-23 14:18:35 +08:00
|
|
|
_quads = quads;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TextureAtlas - alloc & init
|
2012-06-27 14:21:29 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
TextureAtlas * TextureAtlas::create(const char* file, int capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-07-23 14:18:35 +08:00
|
|
|
TextureAtlas * textureAtlas = new TextureAtlas();
|
|
|
|
if(textureAtlas && textureAtlas->initWithFile(file, capacity))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-23 14:18:35 +08:00
|
|
|
textureAtlas->autorelease();
|
|
|
|
return textureAtlas;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-23 14:18:35 +08:00
|
|
|
CC_SAFE_DELETE(textureAtlas);
|
2012-04-19 14:35:52 +08:00
|
|
|
return NULL;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
TextureAtlas * TextureAtlas::createWithTexture(Texture2D *texture, int capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureAtlas * pTextureAtlas = new TextureAtlas();
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pTextureAtlas && pTextureAtlas->initWithTexture(texture, capacity))
|
|
|
|
{
|
|
|
|
pTextureAtlas->autorelease();
|
|
|
|
return pTextureAtlas;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pTextureAtlas);
|
|
|
|
return NULL;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
bool TextureAtlas::initWithFile(const char * file, int capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// retained in property
|
2013-07-12 06:24:23 +08:00
|
|
|
Texture2D *texture = TextureCache::getInstance()->addImage(file);
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (texture)
|
|
|
|
{
|
2010-12-30 17:30:11 +08:00
|
|
|
return initWithTexture(texture, capacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Could not open file: %s", file);
|
2012-09-06 02:30:44 +08:00
|
|
|
return false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
bool TextureAtlas::initWithTexture(Texture2D *texture, int capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(capacity>=0, "Capacity must be >= 0");
|
|
|
|
|
|
|
|
// CCASSERT(texture != NULL, "texture should not be null");
|
2013-06-15 14:03:30 +08:00
|
|
|
_capacity = capacity;
|
|
|
|
_totalQuads = 0;
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// retained in property
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_texture = texture;
|
|
|
|
CC_SAFE_RETAIN(_texture);
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Re-initialization is not allowed
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_quads == NULL && _indices == NULL, "");
|
2011-07-05 10:47:25 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
_quads = (V3F_C4B_T2F_Quad*)malloc( _capacity * sizeof(V3F_C4B_T2F_Quad) );
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices = (GLushort *)malloc( _capacity * 6 * sizeof(GLushort) );
|
2012-04-07 12:02:40 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( ! ( _quads && _indices) && _capacity > 0)
|
2012-04-07 12:02:40 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
//CCLOG("cocos2d: TextureAtlas: not enough memory");
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_quads);
|
|
|
|
CC_SAFE_FREE(_indices);
|
2011-05-06 18:09:31 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// release texture, should set it to null, because the destruction will
|
|
|
|
// release it too. see cocos2d-x issue #484
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
memset( _quads, 0, _capacity * sizeof(V3F_C4B_T2F_Quad) );
|
2013-06-15 14:03:30 +08:00
|
|
|
memset( _indices, 0, _capacity * 6 * sizeof(GLushort) );
|
2012-04-24 15:02:18 +08:00
|
|
|
|
2013-07-17 13:53:16 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2012-04-24 15:02:18 +08:00
|
|
|
// listen the event when app go to background
|
2013-07-12 06:24:23 +08:00
|
|
|
NotificationCenter::getInstance()->addObserver(this,
|
2013-06-20 14:13:12 +08:00
|
|
|
callfuncO_selector(TextureAtlas::listenBackToForeground),
|
2012-04-24 15:02:18 +08:00
|
|
|
EVNET_COME_TO_FOREGROUND,
|
|
|
|
NULL);
|
2013-07-17 13:53:16 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-17 17:55:26 +08:00
|
|
|
this->setupIndices();
|
|
|
|
|
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
2012-04-19 14:35:52 +08:00
|
|
|
setupVBOandVAO();
|
|
|
|
#else
|
2012-04-17 17:55:26 +08:00
|
|
|
setupVBO();
|
2012-03-26 18:44:28 +08:00
|
|
|
#endif
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureAtlas::listenBackToForeground(Object *obj)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
|
|
|
setupVBOandVAO();
|
|
|
|
#else
|
|
|
|
setupVBO();
|
|
|
|
#endif
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// set _dirty to true to force it rebinding buffer
|
|
|
|
_dirty = true;
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
const char* TextureAtlas::description() const
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return String::createWithFormat("<TextureAtlas | totalQuads = %u>", _totalQuads)->getCString();
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureAtlas::setupIndices()
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_capacity == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
2011-11-10 16:44:58 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
for( int i=0; i < _capacity; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2010-07-16 16:28:11 +08:00
|
|
|
#if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices[i*6+0] = i*4+0;
|
|
|
|
_indices[i*6+1] = i*4+0;
|
|
|
|
_indices[i*6+2] = i*4+2;
|
|
|
|
_indices[i*6+3] = i*4+1;
|
|
|
|
_indices[i*6+4] = i*4+3;
|
|
|
|
_indices[i*6+5] = i*4+3;
|
2010-07-16 16:28:11 +08:00
|
|
|
#else
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices[i*6+0] = i*4+0;
|
|
|
|
_indices[i*6+1] = i*4+1;
|
|
|
|
_indices[i*6+2] = i*4+2;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// inverted index. issue #179
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices[i*6+3] = i*4+3;
|
|
|
|
_indices[i*6+4] = i*4+2;
|
|
|
|
_indices[i*6+5] = i*4+1;
|
2012-04-19 14:35:52 +08:00
|
|
|
#endif
|
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
//TextureAtlas - VAO / VBO specific
|
|
|
|
|
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureAtlas::setupVBOandVAO()
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glGenVertexArrays(1, &_VAOname);
|
|
|
|
ccGLBindVAO(_VAOname);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
#define kQuadSize sizeof(_quads[0].bl)
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glGenBuffers(2, &_buffersVBO[0]);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _capacity, _quads, GL_DYNAMIC_DRAW);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
// vertices
|
2013-07-17 13:53:16 +08:00
|
|
|
glEnableVertexAttribArray(kVertexAttrib_Position);
|
2013-07-05 16:49:22 +08:00
|
|
|
glVertexAttribPointer(kVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, vertices));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
// colors
|
2013-07-17 13:53:16 +08:00
|
|
|
glEnableVertexAttribArray(kVertexAttrib_Color);
|
2013-07-05 16:49:22 +08:00
|
|
|
glVertexAttribPointer(kVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
// tex coords
|
2013-07-17 13:53:16 +08:00
|
|
|
glEnableVertexAttribArray(kVertexAttrib_TexCoords);
|
2013-07-05 16:49:22 +08:00
|
|
|
glVertexAttribPointer(kVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _capacity * 6, _indices, GL_STATIC_DRAW);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
// Must unbind the VAO before changing the element buffer.
|
|
|
|
ccGLBindVAO(0);
|
2012-04-17 17:55:26 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
#else // CC_TEXTURE_ATLAS_USE_VAO
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureAtlas::setupVBO()
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glGenBuffers(2, &_buffersVBO[0]);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
mapBuffers();
|
|
|
|
}
|
|
|
|
#endif // ! // CC_TEXTURE_ATLAS_USE_VAO
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureAtlas::mapBuffers()
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
// Avoid changing the element buffer for whatever VAO might be bound.
|
|
|
|
ccGLBindVAO(0);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _capacity, _quads, GL_DYNAMIC_DRAW);
|
2012-04-19 14:35:52 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _capacity * 6, _indices, GL_STATIC_DRAW);
|
2012-04-19 14:35:52 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TextureAtlas - Update, Insert, Move & Remove
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::updateQuad(V3F_C4B_T2F_Quad *quad, int index)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( index >= 0 && index < _capacity, "updateQuadWithTexture: Invalid index");
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalQuads = MAX( index+1, _totalQuads);
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_quads[index] = *quad;
|
2011-07-05 10:47:25 +08:00
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::insertQuad(V3F_C4B_T2F_Quad *quad, int index)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( index>=0 && index<_capacity, "insertQuadWithTexture: Invalid index");
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalQuads++;
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( _totalQuads <= _capacity, "invalid totalQuads");
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// issue #575. index can be > totalQuads
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int remaining = (_totalQuads-1) - index;
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// last object doesn't need to be moved
|
|
|
|
if( remaining > 0)
|
2012-03-26 22:37:09 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// texture coordinates
|
2013-06-15 14:03:30 +08:00
|
|
|
memmove( &_quads[index+1],&_quads[index], sizeof(_quads[0]) * remaining );
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_quads[index] = *quad;
|
2011-07-05 10:47:25 +08:00
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::insertQuads(V3F_C4B_T2F_Quad* quads, int index, int amount)
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(index>=0 && amount>=0 && index+amount<=_capacity, "insertQuadWithTexture: Invalid index + amount");
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalQuads += amount;
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( _totalQuads <= _capacity, "invalid totalQuads");
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// issue #575. index can be > totalQuads
|
2013-06-15 14:03:30 +08:00
|
|
|
int remaining = (_totalQuads-1) - index - amount;
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// last object doesn't need to be moved
|
|
|
|
if( remaining > 0)
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// tex coordinates
|
2013-06-15 14:03:30 +08:00
|
|
|
memmove( &_quads[index+amount],&_quads[index], sizeof(_quads[0]) * remaining );
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
int max = index + amount;
|
|
|
|
int j = 0;
|
|
|
|
for (int i = index; i < max ; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_quads[index] = quads[j];
|
2012-04-19 14:35:52 +08:00
|
|
|
index++;
|
|
|
|
j++;
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::insertQuadFromIndex(int oldIndex, int newIndex)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( newIndex >= 0 && newIndex < _totalQuads, "insertQuadFromIndex:atIndex: Invalid index");
|
|
|
|
CCASSERT( oldIndex >= 0 && oldIndex < _totalQuads, "insertQuadFromIndex:atIndex: Invalid index");
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( oldIndex == newIndex )
|
2012-03-26 22:37:09 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
2012-03-26 22:37:09 +08:00
|
|
|
}
|
2012-09-17 15:02:24 +08:00
|
|
|
// because it is ambiguous in iphone, so we implement abs ourselves
|
2012-04-19 14:35:52 +08:00
|
|
|
// unsigned int howMany = abs( oldIndex - newIndex);
|
2013-07-20 13:01:27 +08:00
|
|
|
int howMany = (oldIndex - newIndex) > 0 ? (oldIndex - newIndex) : (newIndex - oldIndex);
|
|
|
|
int dst = oldIndex;
|
|
|
|
int src = oldIndex + 1;
|
2012-04-19 14:35:52 +08:00
|
|
|
if( oldIndex > newIndex)
|
2012-03-26 22:37:09 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
dst = newIndex+1;
|
|
|
|
src = newIndex;
|
|
|
|
}
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// texture coordinates
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad quadsBackup = _quads[oldIndex];
|
2013-06-15 14:03:30 +08:00
|
|
|
memmove( &_quads[dst],&_quads[src], sizeof(_quads[0]) * howMany );
|
|
|
|
_quads[newIndex] = quadsBackup;
|
2011-07-05 10:47:25 +08:00
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::removeQuadAtIndex(int index)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( index>=0 && index<_totalQuads, "removeQuadAtIndex: Invalid index");
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
int remaining = (_totalQuads-1) - index;
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// last object doesn't need to be moved
|
|
|
|
if( remaining )
|
2012-03-26 22:37:09 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// texture coordinates
|
2013-06-15 14:03:30 +08:00
|
|
|
memmove( &_quads[index],&_quads[index+1], sizeof(_quads[0]) * remaining );
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalQuads--;
|
2011-07-05 10:47:25 +08:00
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::removeQuadsAtIndex(int index, int amount)
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(index>=0 && amount>=0 && index+amount<=_totalQuads, "removeQuadAtIndex: index + amount out of bounds");
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
int remaining = (_totalQuads) - (index + amount);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalQuads -= amount;
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( remaining )
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
memmove( &_quads[index], &_quads[index+amount], sizeof(_quads[0]) * remaining );
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureAtlas::removeAllQuads()
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalQuads = 0;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TextureAtlas - Resize
|
2013-07-20 13:01:27 +08:00
|
|
|
bool TextureAtlas::resizeCapacity(int newCapacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(newCapacity>=0, "capacity >= 0");
|
2013-06-15 14:03:30 +08:00
|
|
|
if( newCapacity == _capacity )
|
2012-03-26 22:37:09 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-03-26 22:37:09 +08:00
|
|
|
}
|
2013-07-20 13:01:27 +08:00
|
|
|
int oldCapactiy = _capacity;
|
2012-04-19 14:35:52 +08:00
|
|
|
// update capacity and totolQuads
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalQuads = MIN(_totalQuads, newCapacity);
|
|
|
|
_capacity = newCapacity;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad* tmpQuads = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
GLushort* tmpIndices = NULL;
|
|
|
|
|
|
|
|
// when calling initWithTexture(fileName, 0) on bada device, calloc(0, 1) will fail and return NULL,
|
2013-06-15 14:03:30 +08:00
|
|
|
// so here must judge whether _quads and _indices is NULL.
|
|
|
|
if (_quads == NULL)
|
2012-03-21 21:53:03 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
tmpQuads = (V3F_C4B_T2F_Quad*)malloc( _capacity * sizeof(_quads[0]) );
|
2012-04-07 12:02:40 +08:00
|
|
|
if (tmpQuads != NULL)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
memset(tmpQuads, 0, _capacity * sizeof(_quads[0]) );
|
2012-04-07 12:02:40 +08:00
|
|
|
}
|
2012-03-21 21:53:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
tmpQuads = (V3F_C4B_T2F_Quad*)realloc( _quads, sizeof(_quads[0]) * _capacity );
|
2013-07-20 13:01:27 +08:00
|
|
|
if (tmpQuads != NULL && _capacity > oldCapactiy)
|
2012-04-07 12:02:40 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
memset(tmpQuads+oldCapactiy, 0, (_capacity - oldCapactiy)*sizeof(_quads[0]) );
|
2012-04-07 12:02:40 +08:00
|
|
|
}
|
2012-03-21 21:53:03 +08:00
|
|
|
}
|
2011-11-10 16:44:58 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_indices == NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
tmpIndices = (GLushort*)malloc( _capacity * 6 * sizeof(_indices[0]) );
|
2012-04-07 12:02:40 +08:00
|
|
|
if (tmpIndices != NULL)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
memset( tmpIndices, 0, _capacity * 6 * sizeof(_indices[0]) );
|
2012-04-07 12:02:40 +08:00
|
|
|
}
|
|
|
|
|
2012-03-21 21:53:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
tmpIndices = (GLushort*)realloc( _indices, sizeof(_indices[0]) * _capacity * 6 );
|
2013-07-20 13:01:27 +08:00
|
|
|
if (tmpIndices != NULL && _capacity > oldCapactiy)
|
2012-04-07 12:02:40 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
memset( tmpIndices+oldCapactiy, 0, (_capacity-oldCapactiy) * 6 * sizeof(_indices[0]) );
|
2012-04-07 12:02:40 +08:00
|
|
|
}
|
2012-03-21 21:53:03 +08:00
|
|
|
}
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( ! ( tmpQuads && tmpIndices) ) {
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOG("cocos2d: TextureAtlas: not enough memory");
|
2012-03-21 21:53:03 +08:00
|
|
|
CC_SAFE_FREE(tmpQuads);
|
|
|
|
CC_SAFE_FREE(tmpIndices);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_quads);
|
|
|
|
CC_SAFE_FREE(_indices);
|
|
|
|
_capacity = _totalQuads = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_quads = tmpQuads;
|
|
|
|
_indices = tmpIndices;
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2012-03-26 18:44:28 +08:00
|
|
|
setupIndices();
|
2012-04-19 14:35:52 +08:00
|
|
|
mapBuffers();
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2011-07-05 10:47:25 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::increaseTotalQuadsWith(int amount)
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(amount>=0, "amount >= 0");
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalQuads += amount;
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::moveQuadsFromIndex(int oldIndex, int amount, int newIndex)
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(oldIndex>=0 && amount>=0 && newIndex>=0, "values must be >= 0");
|
|
|
|
CCASSERT(newIndex + amount <= _totalQuads, "insertQuadFromIndex:atIndex: Invalid index");
|
|
|
|
CCASSERT(oldIndex < _totalQuads, "insertQuadFromIndex:atIndex: Invalid index");
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( oldIndex == newIndex )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//create buffer
|
2013-07-05 16:49:22 +08:00
|
|
|
size_t quadSize = sizeof(V3F_C4B_T2F_Quad);
|
|
|
|
V3F_C4B_T2F_Quad* tempQuads = (V3F_C4B_T2F_Quad*)malloc( quadSize * amount);
|
2013-06-15 14:03:30 +08:00
|
|
|
memcpy( tempQuads, &_quads[oldIndex], quadSize * amount );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (newIndex < oldIndex)
|
|
|
|
{
|
|
|
|
// move quads from newIndex to newIndex + amount to make room for buffer
|
2013-06-15 14:03:30 +08:00
|
|
|
memmove( &_quads[newIndex], &_quads[newIndex+amount], (oldIndex-newIndex)*quadSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// move quads above back
|
2013-06-15 14:03:30 +08:00
|
|
|
memmove( &_quads[oldIndex], &_quads[oldIndex+amount], (newIndex-oldIndex)*quadSize);
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
memcpy( &_quads[newIndex], tempQuads, amount*quadSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
free(tempQuads);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::moveQuadsFromIndex(int index, int newIndex)
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(index>=0 && newIndex>=0, "values must be >= 0");
|
|
|
|
CCASSERT(newIndex + (_totalQuads - index) <= _capacity, "moveQuadsFromIndex move is out of bounds");
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
memmove(_quads + newIndex,_quads + index, (_totalQuads - index) * sizeof(_quads[0]));
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::fillWithEmptyQuadsFromIndex(int index, int amount)
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(index>=0 && amount>=0, "values must be >= 0");
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad quad;
|
2012-04-17 17:55:26 +08:00
|
|
|
memset(&quad, 0, sizeof(quad));
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
int to = index + amount;
|
|
|
|
for (int i = index ; i < to ; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_quads[i] = quad;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2010-07-16 16:28:11 +08:00
|
|
|
// TextureAtlas - Drawing
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureAtlas::drawQuads()
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->drawNumberOfQuads(_totalQuads, 0);
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::drawNumberOfQuads(int numberOfQuads)
|
2011-07-05 10:47:25 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(numberOfQuads>=0, "numberOfQuads must be >= 0");
|
|
|
|
this->drawNumberOfQuads(numberOfQuads, 0);
|
2011-07-05 10:47:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void TextureAtlas::drawNumberOfQuads(int numberOfQuads, int start)
|
|
|
|
{
|
|
|
|
CCASSERT(numberOfQuads>=0 && start>=0, "numberOfQuads and start must be >= 0");
|
|
|
|
|
|
|
|
if(!numberOfQuads)
|
2012-04-24 15:02:18 +08:00
|
|
|
return;
|
2013-07-20 13:01:27 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ccGLBindTexture2D(_texture->getName());
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
|
|
|
|
|
|
|
//
|
|
|
|
// Using VBO and VAO
|
|
|
|
//
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// XXX: update is done in draw... perhaps it should be done in a timer
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dirty)
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
|
2012-11-14 18:05:15 +08:00
|
|
|
// option 1: subdata
|
2013-06-15 14:03:30 +08:00
|
|
|
//glBufferSubData(GL_ARRAY_BUFFER, sizeof(_quads[0])*start, sizeof(_quads[0]) * n , &_quads[start] );
|
2012-11-14 18:05:15 +08:00
|
|
|
|
|
|
|
// option 2: data
|
|
|
|
// glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * (n-start), &quads_[start], GL_DYNAMIC_DRAW);
|
|
|
|
|
|
|
|
// option 3: orphaning + glMapBuffer
|
2013-07-20 13:01:27 +08:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * (numberOfQuads-start), NULL, GL_DYNAMIC_DRAW);
|
2012-11-14 18:05:15 +08:00
|
|
|
void *buf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
2013-07-20 13:01:27 +08:00
|
|
|
memcpy(buf, _quads, sizeof(_quads[0])* (numberOfQuads-start));
|
2012-11-14 18:05:15 +08:00
|
|
|
glUnmapBuffer(GL_ARRAY_BUFFER);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ccGLBindVAO(_VAOname);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
#if CC_REBIND_INDICES_BUFFER
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
|
2012-04-17 17:55:26 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
|
2013-07-20 13:01:27 +08:00
|
|
|
glDrawElements(GL_TRIANGLE_STRIP, (GLsizei) numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0])) );
|
2012-04-17 17:55:26 +08:00
|
|
|
#else
|
2013-07-20 13:01:27 +08:00
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei) numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0])) );
|
2012-04-17 17:55:26 +08:00
|
|
|
#endif // CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
|
|
|
|
|
|
|
|
#if CC_REBIND_INDICES_BUFFER
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
#endif
|
|
|
|
|
2013-01-26 14:20:24 +08:00
|
|
|
// glBindVertexArray(0);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
#else // ! CC_TEXTURE_ATLAS_USE_VAO
|
|
|
|
|
|
|
|
//
|
|
|
|
// Using VBO without VAO
|
|
|
|
//
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
#define kQuadSize sizeof(_quads[0].bl)
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
// XXX: update is done in draw... perhaps it should be done in a timer
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dirty)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2013-07-21 19:10:22 +08:00
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, sizeof(_quads[0])*start, sizeof(_quads[0]) * numberOfQuads , &_quads[start] );
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = false;
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
2013-07-25 15:04:13 +08:00
|
|
|
ccGLEnableVertexAttribs(VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
// vertices
|
2013-07-05 16:49:22 +08:00
|
|
|
glVertexAttribPointer(kVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, vertices));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
// colors
|
2013-07-05 16:49:22 +08:00
|
|
|
glVertexAttribPointer(kVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
// tex coords
|
2013-07-05 16:49:22 +08:00
|
|
|
glVertexAttribPointer(kVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
#if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
|
2013-07-21 19:10:22 +08:00
|
|
|
glDrawElements(GL_TRIANGLE_STRIP, (GLsizei)numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0])));
|
2012-04-17 17:55:26 +08:00
|
|
|
#else
|
2013-07-21 19:10:22 +08:00
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei)numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0])));
|
2012-04-17 17:55:26 +08:00
|
|
|
#endif // CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
#endif // CC_TEXTURE_ATLAS_USE_VAO
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWS(1);
|
2012-04-19 14:35:52 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
|
2012-03-26 18:44:28 +08:00
|
|
|
NS_CC_END
|
|
|
|
|