tidy up TextureAtlas::resizeCapacity (#16588)

My intent is to reduce clutter and improve readability, so that going forward it's a little easier to edit this.

No behaviour change.
This commit is contained in:
TankorSmash 2016-09-20 23:31:24 -04:00 committed by Ricardo Quesada
parent e8c3082229
commit f93c6edb1f
1 changed files with 25 additions and 17 deletions

View File

@ -452,13 +452,14 @@ void TextureAtlas::removeAllQuads()
// TextureAtlas - Resize // TextureAtlas - Resize
bool TextureAtlas::resizeCapacity(ssize_t newCapacity) bool TextureAtlas::resizeCapacity(ssize_t newCapacity)
{ {
CCASSERT(newCapacity>=0, "capacity >= 0"); CCASSERT(newCapacity >= 0, "capacity >= 0");
if( newCapacity == _capacity ) if (newCapacity == _capacity)
{ {
return true; return true;
} }
auto oldCapactiy = _capacity; auto oldCapacity = _capacity;
// update capacity and totolQuads
// update capacity and totalQuads
_totalQuads = MIN(_totalQuads, newCapacity); _totalQuads = MIN(_totalQuads, newCapacity);
_capacity = newCapacity; _capacity = newCapacity;
@ -467,43 +468,49 @@ bool TextureAtlas::resizeCapacity(ssize_t newCapacity)
// when calling initWithTexture(fileName, 0) on bada device, calloc(0, 1) will fail and return nullptr, // 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. // so here must judge whether _quads and _indices is nullptr.
ssize_t _quads_size = sizeof(_quads[0]);
ssize_t new_quads_size = _capacity * _quads_size;
if (_quads == nullptr) if (_quads == nullptr)
{ {
tmpQuads = (V3F_C4B_T2F_Quad*)malloc( _capacity * sizeof(_quads[0]) ); tmpQuads = (V3F_C4B_T2F_Quad*)malloc(new_quads_size);
if (tmpQuads != nullptr) if (tmpQuads != nullptr)
{ {
memset(tmpQuads, 0, _capacity * sizeof(_quads[0]) ); memset(tmpQuads, 0, new_quads_size);
} }
} }
else else
{ {
tmpQuads = (V3F_C4B_T2F_Quad*)realloc( _quads, sizeof(_quads[0]) * _capacity ); tmpQuads = (V3F_C4B_T2F_Quad*)realloc(_quads, new_quads_size);
if (tmpQuads != nullptr && _capacity > oldCapactiy) if (tmpQuads != nullptr && _capacity > oldCapacity)
{ {
memset(tmpQuads+oldCapactiy, 0, (_capacity - oldCapactiy)*sizeof(_quads[0]) ); memset(tmpQuads + oldCapacity, 0, (_capacity - oldCapacity)*_quads_size);
} }
_quads = nullptr; _quads = nullptr;
} }
ssize_t _indices_size = sizeof(_indices[0]);
ssize_t new_size = _capacity * 6 * _indices_size;
if (_indices == nullptr) if (_indices == nullptr)
{ {
tmpIndices = (GLushort*)malloc( _capacity * 6 * sizeof(_indices[0]) ); tmpIndices = (GLushort*)malloc(new_size);
if (tmpIndices != nullptr) if (tmpIndices != nullptr)
{ {
memset( tmpIndices, 0, _capacity * 6 * sizeof(_indices[0]) ); memset(tmpIndices, 0, new_size);
} }
} }
else else
{ {
tmpIndices = (GLushort*)realloc( _indices, sizeof(_indices[0]) * _capacity * 6 ); tmpIndices = (GLushort*)realloc(_indices, new_size);
if (tmpIndices != nullptr && _capacity > oldCapactiy) if (tmpIndices != nullptr && _capacity > oldCapacity)
{ {
memset( tmpIndices+oldCapactiy, 0, (_capacity-oldCapactiy) * 6 * sizeof(_indices[0]) ); memset(tmpIndices + oldCapacity, 0, (_capacity - oldCapacity) * 6 * _indices_size);
} }
_indices = nullptr; _indices = nullptr;
} }
if( ! ( tmpQuads && tmpIndices) ) { if (!(tmpQuads && tmpIndices)) {
CCLOG("cocos2d: TextureAtlas: not enough memory"); CCLOG("cocos2d: TextureAtlas: not enough memory");
CC_SAFE_FREE(tmpQuads); CC_SAFE_FREE(tmpQuads);
CC_SAFE_FREE(tmpIndices); CC_SAFE_FREE(tmpIndices);
@ -525,6 +532,7 @@ bool TextureAtlas::resizeCapacity(ssize_t newCapacity)
return true; return true;
} }
void TextureAtlas::increaseTotalQuadsWith(ssize_t amount) void TextureAtlas::increaseTotalQuadsWith(ssize_t amount)
{ {
CCASSERT(amount>=0, "amount >= 0"); CCASSERT(amount>=0, "amount >= 0");