From f93c6edb1ffffb9dd405c6c2b17e1baee192530e Mon Sep 17 00:00:00 2001 From: TankorSmash Date: Tue, 20 Sep 2016 23:31:24 -0400 Subject: [PATCH] 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. --- cocos/renderer/CCTextureAtlas.cpp | 42 ++++++++++++++++++------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/cocos/renderer/CCTextureAtlas.cpp b/cocos/renderer/CCTextureAtlas.cpp index 8813ca0710..a7bbf99062 100644 --- a/cocos/renderer/CCTextureAtlas.cpp +++ b/cocos/renderer/CCTextureAtlas.cpp @@ -452,58 +452,65 @@ void TextureAtlas::removeAllQuads() // TextureAtlas - Resize bool TextureAtlas::resizeCapacity(ssize_t newCapacity) { - CCASSERT(newCapacity>=0, "capacity >= 0"); - if( newCapacity == _capacity ) + CCASSERT(newCapacity >= 0, "capacity >= 0"); + if (newCapacity == _capacity) { return true; } - auto oldCapactiy = _capacity; - // update capacity and totolQuads + auto oldCapacity = _capacity; + + // update capacity and totalQuads _totalQuads = MIN(_totalQuads, newCapacity); _capacity = newCapacity; V3F_C4B_T2F_Quad* tmpQuads = nullptr; GLushort* tmpIndices = 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. + + 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; } - if( ! ( tmpQuads && tmpIndices) ) { + if (!(tmpQuads && tmpIndices)) { CCLOG("cocos2d: TextureAtlas: not enough memory"); CC_SAFE_FREE(tmpQuads); CC_SAFE_FREE(tmpIndices); @@ -525,6 +532,7 @@ bool TextureAtlas::resizeCapacity(ssize_t newCapacity) return true; } + void TextureAtlas::increaseTotalQuadsWith(ssize_t amount) { CCASSERT(amount>=0, "amount >= 0");