Merge pull request #855 from minggo/iss1150_rendertexture_crash

use texture format kCCTexture2DPixelFormat_RGBA8888 to save image
This commit is contained in:
minggo 2012-04-19 02:05:04 -07:00
commit 4e4a26a2db
2 changed files with 47 additions and 46 deletions

View File

@ -28,14 +28,14 @@ THE SOFTWARE.
#include "CCDirector.h"
#include "platform/platform.h"
#include "CCImage.h"
#include "CCGLProgram.h"
#include "ccGLStateCache.h"
#include "CCGLProgram.h"
#include "ccGLStateCache.h"
#include "CCConfiguration.h"
#include "support/ccUtils.h"
#include "CCTextureCache.h"
#include "CCFileUtils.h"
#include "CCGL.h"
// extern
// extern
#include "kazmath/GL/matrix.h"
NS_CC_BEGIN
@ -115,14 +115,14 @@ bool CCRenderTexture::initWithWidthAndHeight(int w, int h, CCTexture2DPixelForma
// textures must be power of two squared
unsigned int powW = 0;
unsigned int powH = 0;
if( CCConfiguration::sharedConfiguration()->isSupportsNPOT() ) {
powW = w;
powH = h;
} else {
powW = ccNextPOT(w);
powH = ccNextPOT(h);
unsigned int powH = 0;
if( CCConfiguration::sharedConfiguration()->isSupportsNPOT() ) {
powW = w;
powH = h;
} else {
powW = ccNextPOT(w);
powH = ccNextPOT(h);
}
void *data = malloc((int)(powW * powH * 4));
@ -178,21 +178,21 @@ void CCRenderTexture::begin()
float widthRatio = size.width / texSize.width;
float heightRatio = size.height / texSize.height;
// Adjust the orthographic projection and viewport
glViewport(0, 0, (GLsizei)(texSize.width * CC_CONTENT_SCALE_FACTOR()), (GLsizei)(texSize.height * CC_CONTENT_SCALE_FACTOR()));
// special viewport for 3d projection + retina display
if ( director->getProjection() == kCCDirectorProjection3D && CC_CONTENT_SCALE_FACTOR() != 1 )
{
glViewport((GLsizei)(-texSize.width/2), (GLsizei)(-texSize.height/2), (GLsizei)(texSize.width * CC_CONTENT_SCALE_FACTOR()), (GLsizei)(texSize.height * CC_CONTENT_SCALE_FACTOR()));
}
kmMat4 orthoMatrix;
kmMat4OrthographicProjection(&orthoMatrix, (float)-1.0 / widthRatio, (float)1.0 / widthRatio,
(float)-1.0 / heightRatio, (float)1.0 / heightRatio, -1,1 );
kmGLMultMatrix(&orthoMatrix);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &m_nOldFBO);
// Adjust the orthographic projection and viewport
glViewport(0, 0, (GLsizei)(texSize.width * CC_CONTENT_SCALE_FACTOR()), (GLsizei)(texSize.height * CC_CONTENT_SCALE_FACTOR()));
// special viewport for 3d projection + retina display
if ( director->getProjection() == kCCDirectorProjection3D && CC_CONTENT_SCALE_FACTOR() != 1 )
{
glViewport((GLsizei)(-texSize.width/2), (GLsizei)(-texSize.height/2), (GLsizei)(texSize.width * CC_CONTENT_SCALE_FACTOR()), (GLsizei)(texSize.height * CC_CONTENT_SCALE_FACTOR()));
}
kmMat4 orthoMatrix;
kmMat4OrthographicProjection(&orthoMatrix, (float)-1.0 / widthRatio, (float)1.0 / widthRatio,
(float)-1.0 / heightRatio, (float)1.0 / heightRatio, -1,1 );
kmGLMultMatrix(&orthoMatrix);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &m_nOldFBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_uFBO);
}
@ -214,21 +214,21 @@ void CCRenderTexture::beginWithClear(float r, float g, float b, float a)
void CCRenderTexture::end(bool bIsTOCacheTexture)
{
glBindFramebuffer(GL_FRAMEBUFFER, m_nOldFBO);
kmGLPopMatrix();
CCDirector *director = CCDirector::sharedDirector();
CCSize size = director->getWinSizeInPixels();
// restore viewport
glViewport(0, 0, GLsizei(size.width * CC_CONTENT_SCALE_FACTOR()), GLsizei(size.height * CC_CONTENT_SCALE_FACTOR()));
// special viewport for 3d projection + retina display
if ( director->getProjection() == kCCDirectorProjection3D && CC_CONTENT_SCALE_FACTOR() != 1 )
{
glViewport((GLsizei)(-size.width/2), (GLsizei)(-size.height/2), (GLsizei)(size.width * CC_CONTENT_SCALE_FACTOR()), (GLsizei)(size.height * CC_CONTENT_SCALE_FACTOR()));
}
kmGLPopMatrix();
CCDirector *director = CCDirector::sharedDirector();
CCSize size = director->getWinSizeInPixels();
// restore viewport
glViewport(0, 0, GLsizei(size.width * CC_CONTENT_SCALE_FACTOR()), GLsizei(size.height * CC_CONTENT_SCALE_FACTOR()));
// special viewport for 3d projection + retina display
if ( director->getProjection() == kCCDirectorProjection3D && CC_CONTENT_SCALE_FACTOR() != 1 )
{
glViewport((GLsizei)(-size.width/2), (GLsizei)(-size.height/2), (GLsizei)(size.width * CC_CONTENT_SCALE_FACTOR()), (GLsizei)(size.height * CC_CONTENT_SCALE_FACTOR()));
}
//TODO: Does this line take effect? director->setProjection(director->getProjection());
#if CC_ENABLE_CACHE_TEXTTURE_DATA
@ -238,10 +238,9 @@ void CCRenderTexture::end(bool bIsTOCacheTexture)
// to get the rendered texture data
const CCSize& s = m_pTexture->getContentSizeInPixels();
int tx = (int)s.width;
int ty = (int)s.height;
m_pUITextureImage = new CCImage;
if (m_pUITextureImage == newCCImage())
m_pUITextureImage = newCCImage();
if (m_pUITextureImage)
{
VolatileTexture::addDataTexture(m_pTexture, m_pUITextureImage->getData(), kTexture2DPixelFormat_RGBA8888, s);
}

View File

@ -378,8 +378,10 @@ RenderTextureIssue937::RenderTextureIssue937()
spr_nonpremulti->setPosition(ccp(16,16));
/* A2 & B2 setup */
CCRenderTexture *rend = CCRenderTexture::renderTextureWithWidthAndHeight(32, 64, kCCTexture2DPixelFormat_RGBA4444);
CCRenderTexture *rend = CCRenderTexture::renderTextureWithWidthAndHeight(32, 64, kCCTexture2DPixelFormat_RGBA8888);
if (NULL == rend)
{