2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-05 10:47:25 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureAtlas.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCEventType.h"
|
|
|
|
#include "base/CCDirector.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "renderer/CCGLProgram.h"
|
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
|
|
|
#include "CCGL.h"
|
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
|
2014-04-09 22:30:49 +08:00
|
|
|
#include "deprecated/CCString.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-12-18 17:47:20 +08:00
|
|
|
:_indices(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
,_dirty(false)
|
2013-12-18 17:47:20 +08:00
|
|
|
,_texture(nullptr)
|
|
|
|
,_quads(nullptr)
|
2013-12-31 11:02:01 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2014-07-09 23:03:04 +08:00
|
|
|
,_rendererRecreatedListener(nullptr)
|
2013-12-31 11:02:01 +08:00
|
|
|
#endif
|
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-08-22 11:05:06 +08:00
|
|
|
CCLOGINFO("deallocing TextureAtlas: %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
|
|
|
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
glDeleteVertexArrays(1, &_VAOname);
|
|
|
|
GL::bindVAO(0);
|
|
|
|
}
|
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
|
2014-07-09 23:03:04 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_rendererRecreatedListener);
|
2013-07-17 13:53:16 +08:00
|
|
|
#endif
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t 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-12-12 12:07:20 +08:00
|
|
|
ssize_t 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-12-24 15:49:58 +08:00
|
|
|
TextureAtlas * TextureAtlas::create(const std::string& file, ssize_t capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TextureAtlas * textureAtlas = new (std::nothrow) TextureAtlas();
|
2013-07-23 14:18:35 +08:00
|
|
|
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);
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
TextureAtlas * TextureAtlas::createWithTexture(Texture2D *texture, ssize_t capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TextureAtlas * textureAtlas = new (std::nothrow) TextureAtlas();
|
2013-07-26 06:53:24 +08:00
|
|
|
if (textureAtlas && textureAtlas->initWithTexture(texture, capacity))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
textureAtlas->autorelease();
|
|
|
|
return textureAtlas;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-26 06:53:24 +08:00
|
|
|
CC_SAFE_DELETE(textureAtlas);
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2010-07-16 16:28:11 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
bool TextureAtlas::initWithFile(const std::string& file, ssize_t capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// retained in property
|
2013-11-07 19:11:09 +08:00
|
|
|
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(file);
|
2010-07-16 16:28:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (texture)
|
2013-07-31 07:41:26 +08:00
|
|
|
{
|
2010-12-30 17:30:11 +08:00
|
|
|
return initWithTexture(texture, capacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-24 10:51:47 +08:00
|
|
|
CCLOG("cocos2d: Could not open file: %s", file.c_str());
|
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-12-12 12:07:20 +08:00
|
|
|
bool TextureAtlas::initWithTexture(Texture2D *texture, ssize_t capacity)
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(capacity>=0, "Capacity must be >= 0");
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
// CCASSERT(texture != nullptr, "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-12-18 17:47:20 +08:00
|
|
|
CCASSERT(_quads == nullptr && _indices == nullptr, "");
|
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
|
2014-07-09 23:03:04 +08:00
|
|
|
/** listen the event that renderer was recreated on Android/WP8 */
|
|
|
|
_rendererRecreatedListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(TextureAtlas::listenRendererRecreated, this));
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_rendererRecreatedListener, -1);
|
2013-07-17 13:53:16 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-17 17:55:26 +08:00
|
|
|
this->setupIndices();
|
|
|
|
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
setupVBOandVAO();
|
|
|
|
}
|
2013-11-13 14:52:16 +08:00
|
|
|
else
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
setupVBO();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-07-09 23:03:04 +08:00
|
|
|
void TextureAtlas::listenRendererRecreated(EventCustom* event)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
setupVBOandVAO();
|
|
|
|
}
|
2013-11-13 14:52:16 +08:00
|
|
|
else
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
setupVBO();
|
|
|
|
}
|
2012-04-24 15:02:18 +08:00
|
|
|
|
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-12-13 06:38:12 +08:00
|
|
|
std::string TextureAtlas::getDescription() const
|
2010-07-16 16:28:11 +08:00
|
|
|
{
|
2014-01-05 11:59:32 +08:00
|
|
|
return StringUtils::format("<TextureAtlas | totalQuads = %d>", static_cast<int>(_totalQuads));
|
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
|
|
|
{
|
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
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
//TextureAtlas - VAO / VBO specific
|
|
|
|
|
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);
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindVAO(_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-25 17:48:22 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, vertices));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
// colors
|
2013-07-25 17:48:22 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
// tex coords
|
2014-05-09 03:34:26 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 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.
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindVAO(0);
|
2012-04-17 17:55:26 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
2013-11-12 20:18:04 +08:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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.
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindVAO(0);
|
2012-11-14 18:05:15 +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-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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::updateQuad(V3F_C4B_T2F_Quad *quad, ssize_t 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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::insertQuad(V3F_C4B_T2F_Quad *quad, ssize_t 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-12-05 17:19:01 +08:00
|
|
|
auto 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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::insertQuads(V3F_C4B_T2F_Quad* quads, ssize_t index, ssize_t 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-12-05 17:19:01 +08:00
|
|
|
auto 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-12-05 17:19:01 +08:00
|
|
|
auto max = index + amount;
|
2013-07-20 13:01:27 +08:00
|
|
|
int j = 0;
|
2013-12-12 12:07:20 +08:00
|
|
|
for (ssize_t 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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::insertQuadFromIndex(ssize_t oldIndex, ssize_t 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-12-05 17:19:01 +08:00
|
|
|
auto howMany = (oldIndex - newIndex) > 0 ? (oldIndex - newIndex) : (newIndex - oldIndex);
|
|
|
|
auto dst = oldIndex;
|
|
|
|
auto 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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::removeQuadAtIndex(ssize_t 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-12-05 17:19:01 +08:00
|
|
|
auto 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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::removeQuadsAtIndex(ssize_t index, ssize_t 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-12-05 17:19:01 +08:00
|
|
|
auto 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-12-12 12:07:20 +08:00
|
|
|
bool TextureAtlas::resizeCapacity(ssize_t 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-12-05 17:19:01 +08:00
|
|
|
auto 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-12-18 17:47:20 +08:00
|
|
|
V3F_C4B_T2F_Quad* tmpQuads = nullptr;
|
|
|
|
GLushort* tmpIndices = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
// when calling initWithTexture(fileName, 0) on bada device, calloc(0, 1) will fail and return nullptr,
|
|
|
|
// so here must judge whether _quads and _indices is nullptr.
|
|
|
|
if (_quads == nullptr)
|
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]) );
|
2013-12-18 17:47:20 +08:00
|
|
|
if (tmpQuads != nullptr)
|
2012-04-07 12:02:40 +08:00
|
|
|
{
|
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-12-18 17:47:20 +08:00
|
|
|
if (tmpQuads != nullptr && _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
|
|
|
}
|
2014-04-09 20:41:09 +08:00
|
|
|
_quads = nullptr;
|
2012-03-21 21:53:03 +08:00
|
|
|
}
|
2011-11-10 16:44:58 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if (_indices == nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
tmpIndices = (GLushort*)malloc( _capacity * 6 * sizeof(_indices[0]) );
|
2013-12-18 17:47:20 +08:00
|
|
|
if (tmpIndices != nullptr)
|
2012-04-07 12:02:40 +08:00
|
|
|
{
|
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-12-18 17:47:20 +08:00
|
|
|
if (tmpIndices != nullptr && _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
|
|
|
}
|
2014-04-09 20:41:09 +08:00
|
|
|
_indices = nullptr;
|
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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::increaseTotalQuadsWith(ssize_t 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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::moveQuadsFromIndex(ssize_t oldIndex, ssize_t amount, ssize_t 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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::moveQuadsFromIndex(ssize_t index, ssize_t 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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::fillWithEmptyQuadsFromIndex(ssize_t index, ssize_t 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-12-05 17:19:01 +08:00
|
|
|
auto to = index + amount;
|
2013-12-12 12:07:20 +08:00
|
|
|
for (ssize_t 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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::drawNumberOfQuads(ssize_t 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-12-12 12:07:20 +08:00
|
|
|
void TextureAtlas::drawNumberOfQuads(ssize_t numberOfQuads, ssize_t start)
|
2013-07-20 13:01:27 +08:00
|
|
|
{
|
|
|
|
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-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D(_texture->getName());
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
//
|
|
|
|
// Using VBO and VAO
|
|
|
|
//
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:: update is done in draw... perhaps it should be done in a timer
|
2013-11-12 20:18:04 +08:00
|
|
|
if (_dirty)
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
|
|
|
|
// option 1: subdata
|
2014-01-17 13:35:58 +08:00
|
|
|
// glBufferSubData(GL_ARRAY_BUFFER, sizeof(_quads[0])*start, sizeof(_quads[0]) * n , &_quads[start] );
|
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
// option 2: data
|
2014-01-17 13:35:58 +08:00
|
|
|
// glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * (n-start), &quads_[start], GL_DYNAMIC_DRAW);
|
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
// option 3: orphaning + glMapBuffer
|
2014-08-29 10:06:44 +08:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _capacity, nullptr, GL_DYNAMIC_DRAW);
|
2013-11-12 20:18:04 +08:00
|
|
|
void *buf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
2014-08-29 10:06:44 +08:00
|
|
|
memcpy(buf, _quads, sizeof(_quads[0])* _totalQuads);
|
2013-11-12 20:18:04 +08:00
|
|
|
glUnmapBuffer(GL_ARRAY_BUFFER);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
_dirty = false;
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
GL::bindVAO(_VAOname);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
#if CC_REBIND_INDICES_BUFFER
|
2013-11-12 20:18:04 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
|
2012-04-17 17:55:26 +08:00
|
|
|
#endif
|
|
|
|
|
2013-11-12 20:18:04 +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
|
|
|
|
|
|
|
#if CC_REBIND_INDICES_BUFFER
|
2013-11-12 20:18:04 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
2012-04-17 17:55:26 +08:00
|
|
|
#endif
|
|
|
|
|
2013-01-26 14:20:24 +08:00
|
|
|
// glBindVertexArray(0);
|
2013-11-12 20:18:04 +08:00
|
|
|
}
|
2013-11-13 14:52:16 +08:00
|
|
|
else
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
//
|
|
|
|
// Using VBO without VAO
|
|
|
|
//
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
#define kQuadSize sizeof(_quads[0].bl)
|
2013-11-12 20:18:04 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:: update is done in draw... perhaps it should be done in a timer
|
2013-11-12 20:18:04 +08:00
|
|
|
if (_dirty)
|
|
|
|
{
|
2014-08-29 10:06:44 +08:00
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(_quads[0]) * _totalQuads , &_quads[0] );
|
2013-11-12 20:18:04 +08:00
|
|
|
_dirty = false;
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
// vertices
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, vertices));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
// colors
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
// tex coords
|
2014-05-09 03:34:26 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-11-12 20:18:04 +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
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,numberOfQuads*6);
|
|
|
|
|
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
|
|
|
|
|