Images displayed by a game eventually become GPU textures. Understanding how Cocos2d-x creates and caches those textures helps explain loading time, memory use, filtering artifacts, and why removing a sprite does not always release its image immediately.
This article uses the older CC-prefixed class names found in Cocos2d-x 2.x. In newer versions, the same roles are generally represented by Texture2D, Image, and TextureCache.
CCTexture2D
CCTexture2D wraps a GPU texture object and records its dimensions, content size, pixel format, scale, and texture coordinates. Initialization normally follows this path:
- decode compressed image data into pixels;
- choose or convert to a supported pixel format;
- create and bind an OpenGL texture;
- upload pixels with
glTexImage2D; - configure filtering and wrapping;
- retain metadata used by sprites and labels.
Common formats trade quality for memory. RGBA8888 uses four bytes per pixel and preserves full color and alpha. RGB565 and RGBA4444 use two bytes per pixel but reduce precision. A 2048 × 2048 RGBA8888 texture consumes about 16 MiB before mipmaps and other overhead, regardless of how small its PNG file is.
Filtering parameters determine how a texture looks when scaled:
- nearest-neighbor filtering keeps pixel-art edges sharp;
- linear filtering smooths enlarged or reduced images;
- mipmaps improve minification at the cost of additional memory and generation time.
Repeat wrapping also requires dimensions and GPU capabilities supported by the target engine and device.
PVR texture loading
CCTexturePVR parses PVR containers, validates headers, identifies pixel or compressed formats, and uploads one or more mip levels. Hardware-supported compressed formats can reduce texture memory and upload bandwidth, but format support differs across GPU families. Always provide an appropriate asset variant or fallback for every target platform.
Premultiplied alpha matters when blending. If the loader and shader disagree about whether RGB channels already include alpha, edges may show dark or bright halos.
CCTextureCache
The texture cache maps a normalized asset key to a texture object. Repeated requests reuse the same GPU resource instead of decoding and uploading the file again:
1 | auto texture = CCTextureCache::sharedTextureCache() |
Asynchronous loading moves decoding away from the main update path, but final GPU upload may still need engine-managed synchronization. A callback must also account for scenes or nodes that have been destroyed while loading was in progress.
Removing a sprite releases one reference; it does not necessarily evict the cached texture. Use the cache’s targeted removal or unused-texture cleanup at deliberate transition points. Avoid clearing the whole cache during active rendering because live sprites may need those resources.
Profile actual device memory, atlas dimensions, draw calls, and load stalls before changing formats. Texture optimization is most effective when image dimensions, compression, atlas layout, mipmaps, filtering, and cache lifetime are designed together.