Cocos2dx image loading
// Data: Image file data dataLen: file length bool Image: initWithImageData (const unsigned char * data, ssize_t dataLen) {bool ret = false; do {CC_BREAK_IF (! Data | dataLen <= 0); unsigned char * unpackedData = nullptr; ssize_t unpackedLen = 0; // decompress pvr. ccz Format Image // detecgt and unzip the compress file if (ZipUtils: isCCZBuffer (data, dataLen) {unpackedLen = ZipUtils: inflateCCZBuffer (data, dataLen, & unpackedData) ;}// uncompress the image else if (ZipUtils: isGZipBuffer (data, dataLen) in pvr.gz format) {unpackedLen = ZipUtils: inflateMemory (const_cast
(Data), dataLen, & unpackedData);} else {unpackedData = const_cast
(Data); unpackedLen = dataLen;} // identifies the file type _ fileType = detectFormat (unpackedData, unpackedLen); switch (_ fileType) {case Format: PNG: ret = initWithPngData (unpackedData, unpackedLen); break ;... // initialize the image decoding based on the file type.} while (0); return ret ;}
Here, we will first introduce the process of decoding, loading, and then displaying slice, and then decode each format of image in detail.
Texture2D * TextureCache: addImage (const std: string & path ){... // create an Image object image = new (std: nothrow) Image (); CC_BREAK_IF (nullptr = image ); // image decoding bool bRet = image-> initWithImageFile (fullpath); CC_BREAK_IF (! BRet); // create a 2D texture = new (std: nothrow) Texture2D (); // start to initialize the texture if (texture & texture-> initWithImage (image )){...}} // use the specified pixel format for initialization (auto by default, determined based on the Image decoding result) bool Texture2D: initWithImage (image * Image, PixelFormat format ){... // obtain the current OpenGL environment Configuration * conf = Configuration: getInstance (); // determine whether the texture size exceeds the limit int maxTextureSize = conf-> getMaxTextureSize ();... // obtain the number of mipmap maps if (image-> getNumbe ROfMipmaps ()> 1) {CCLOG (cocos2d: WARNING: This image has more than 1 mipmaps and we will not convert the data format ); // load the mipmap texture initWithMipmaps (image-> getMipmaps (), image-> getNumberOfMipmaps (), image-> getRenderFormat (), imageWidth, imageHeight); return true ;} else if (image-> isCompressed ()){... initWithData (...)... return true;} else {... initWithData (...)... return true;} bool Textu Re2D: initWithMipmaps (MipmapInfo * mipmaps, int mipmapsNum, PixelFormat pixelFormat, int pixelsWide, int pixelsHigh ){... // set the row-byte alignment of pixels to Improve the Performance under certain platforms, will cause the system function in glTexImage to read out of bounds // Set the row align only when mipmapsNum = 1 and the data is uncompressed if (mipmapsNum = 1 &&! Info. compressed) {unsigned int bytesPerRow = pixelsWide * info. bpp/8; if (bytesPerRow % 8 = 0) {glPixelStorei (GL_UNPACK_ALIGNMENT, 8);} else if (bytesPerRow % 4 = 0) {glPixelStorei (GL_UNPACK_ALIGNMENT, 4);} else if (bytesPerRow % 2 = 0) {glPixelStorei (bytes, 2);} else {glPixelStorei (GL_UNPACK_ALIGNMENT, 1) ;}} else {glelstorei (bytes, 1 );}... // generate the glGe texture Index NTextures (1, & _ name); // In the bindTexture2 function, glActiveTexture and glBindTexture are called to specify the Texture unit and bind the texture. GL: bindTexture2D (_ name ); // select the texture filtering method based on the number of mipmap maps and whether or not to set anti-aliasing. The selection of texture filtering will be further analyzed if (mipmapsNum = 1) {glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _ antialiasEnabled? GL_LINEAR: GL_NEAREST);} else {glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _ antialiasEnabled? GL_LINEAR_MIPMAP_NEAREST: GL_NEAREST_MIPMAP_NEAREST);} glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _ antialiasEnabled? GL_LINEAR: GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, callback, callback); glTexParameteri (GL_TEXTURE_2D, callback, callback); for (int I = 0; I <mipmapsNum; ++ I) {unsigned char * data = mipmaps [I]. address; GLsizei datalen = mipmaps [I]. len; // texture maps a part of a specified texture image to each element with texture ing enabled. The texture is enabled when the current segment colorant or vertex colorant uses the built-in texture search function. If (info. compressed) {// The texture glCompressedTexImage2D (GL_TEXTURE_2D, I, info. internalFormat, (GLsizei) width, (GLsizei) height, 0, datalen, data);} else {// generate a 2D Texture glTexImage2D (GL_TEXTURE_2D, I, info. internalFormat, (GLsizei) width, (GLsizei) height, 0, info. format, info. type, data);} if (I> 0 & (width! = Height | ccNextPOT (width )! = Width) {CCLOG (cocos2d: Texture2D. WARNING. Mipmap level % u is not squared. Texture won't render correctly. width = % d! = Height = % d, I, width, height);} GLenum err = glGetError (); if (err! = GL_NO_ERROR) {CCLOG (cocos2d: Texture2D: Error uploading compressed texture level: % u. glError: 0x % 04X, I, err); return false;} // mipmap width = MAX (width> 1, 1) of the 1/4 size ); height = MAX (height> 1, 1);} _ contentSize = Size (float) pixelsWide, (float) pixelsHigh); _ pixelsWide = pixelsWide; _ pixelsHigh = pixelsHigh; _ pixelFormat = pixelFormat; _ maxS = 1; _ maxT = 1; // turn off the alpha gradient _ hasPremultipliedAlpha = false; _ hasMipmaps = mipmapsNum> 1; // shader setGLProgram (GLProgramCache :: getInstance ()-> getGLProgram (GLProgram: SHADER_NAME_POSITION_TEXTURE ));}
You can initialize a non-mipmaps texture directly in the form of "mipmapsNum" 1 ". After texture rendering is complete, the texture will be added to the display queue. Of course, this is just a brief introduction, the rendering process will be added after I finish the decoding part of the image ~