From 1d23f1a2b4925ab935b82caa8aca268173463482 Mon Sep 17 00:00:00 2001 From: silverscania Date: Sun, 19 May 2013 18:53:59 +0300 Subject: [PATCH] The real cause of the problem for the texture corruption was when using textures with no alpha. The function bitsPerPixelForFormat returns the wrong number for RGB_888. --- cocos2dx/textures/CCTexture2D.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/cocos2dx/textures/CCTexture2D.cpp b/cocos2dx/textures/CCTexture2D.cpp index ad66468acf..894332948d 100644 --- a/cocos2dx/textures/CCTexture2D.cpp +++ b/cocos2dx/textures/CCTexture2D.cpp @@ -174,18 +174,28 @@ bool CCTexture2D::hasPremultipliedAlpha() bool CCTexture2D::initWithData(const void *data, CCTexture2DPixelFormat pixelFormat, unsigned int pixelsWide, unsigned int pixelsHigh, const CCSize& contentSize) { - unsigned int bytesPerRow = pixelsWide * bitsPerPixelForFormat(pixelFormat) / 8; - bool powerOf2 = ccNextPOT(pixelsWide) == pixelsWide; + unsigned int bitsPerPixel; + //Hack: bitsPerPixelForFormat returns wrong number for RGB_888 textures. See function. + if(pixelFormat == kCCTexture2DPixelFormat_RGB888) + { + bitsPerPixel = 24; + } + else + { + bitsPerPixel = bitsPerPixelForFormat(pixelFormat); + } - if(bytesPerRow % 8 == 0 && powerOf2) + unsigned int bytesPerRow = pixelsWide * bitsPerPixel / 8; + + if(bytesPerRow % 8 == 0) { glPixelStorei(GL_UNPACK_ALIGNMENT, 8); } - else if(bytesPerRow % 4 == 0 && powerOf2) + else if(bytesPerRow % 4 == 0) { glPixelStorei(GL_UNPACK_ALIGNMENT, 4); } - else if(bytesPerRow % 2 == 0 && powerOf2) + else if(bytesPerRow % 2 == 0) { glPixelStorei(GL_UNPACK_ALIGNMENT, 2); }