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

@ -457,8 +457,9 @@ bool TextureAtlas::resizeCapacity(ssize_t newCapacity)
{
return true;
}
auto oldCapactiy = _capacity;
// update capacity and totolQuads
auto oldCapacity = _capacity;
// update capacity and totalQuads
_totalQuads = MIN(_totalQuads, newCapacity);
_capacity = newCapacity;
@ -467,38 +468,44 @@ bool TextureAtlas::resizeCapacity(ssize_t newCapacity)
// 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.
ssize_t _quads_size = sizeof(_quads[0]);
ssize_t new_quads_size = _capacity * _quads_size;
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)
{
memset(tmpQuads, 0, _capacity * sizeof(_quads[0]) );
memset(tmpQuads, 0, new_quads_size);
}
}
else
{
tmpQuads = (V3F_C4B_T2F_Quad*)realloc( _quads, sizeof(_quads[0]) * _capacity );
if (tmpQuads != nullptr && _capacity > oldCapactiy)
tmpQuads = (V3F_C4B_T2F_Quad*)realloc(_quads, new_quads_size);
if (tmpQuads != nullptr && _capacity > oldCapacity)
{
memset(tmpQuads+oldCapactiy, 0, (_capacity - oldCapactiy)*sizeof(_quads[0]) );
memset(tmpQuads + oldCapacity, 0, (_capacity - oldCapacity)*_quads_size);
}
_quads = nullptr;
}
ssize_t _indices_size = sizeof(_indices[0]);
ssize_t new_size = _capacity * 6 * _indices_size;
if (_indices == nullptr)
{
tmpIndices = (GLushort*)malloc( _capacity * 6 * sizeof(_indices[0]) );
tmpIndices = (GLushort*)malloc(new_size);
if (tmpIndices != nullptr)
{
memset( tmpIndices, 0, _capacity * 6 * sizeof(_indices[0]) );
memset(tmpIndices, 0, new_size);
}
}
else
{
tmpIndices = (GLushort*)realloc( _indices, sizeof(_indices[0]) * _capacity * 6 );
if (tmpIndices != nullptr && _capacity > oldCapactiy)
tmpIndices = (GLushort*)realloc(_indices, new_size);
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;
}
@ -525,6 +532,7 @@ bool TextureAtlas::resizeCapacity(ssize_t newCapacity)
return true;
}
void TextureAtlas::increaseTotalQuadsWith(ssize_t amount)
{
CCASSERT(amount>=0, "amount >= 0");