Skip to content

Commit

Permalink
RenderTexture: Optimized for "begin" use, and not "beginWithColor"
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoquesada committed Mar 28, 2011
1 parent 95060a7 commit 7a1bc20
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 53 deletions.
Empty file modified RELEASE_NOTES
100755 → 100644
Empty file.
Empty file modified Resources/Default.png
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Resources/Icon.png
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Resources/Info.plist
100755 → 100644
Empty file.
Empty file modified Resources/InfoWithStatusBar.plist
100755 → 100644
Empty file.
Empty file modified Resources/iTunesArtwork
100755 → 100644
Empty file.
3 changes: 0 additions & 3 deletions cocos2d/CCRenderTexture.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ enum
CCSprite* sprite_; CCSprite* sprite_;


GLenum pixelFormat_; GLenum pixelFormat_;
GLfloat clearColor_[4];
BOOL restoreColor_;

} }


/** The CCSprite being used. /** The CCSprite being used.
Expand Down
64 changes: 21 additions & 43 deletions cocos2d/CCRenderTexture.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@
#import "Support/ccUtils.h" #import "Support/ccUtils.h"
#import "Support/CCFileUtils.h" #import "Support/CCFileUtils.h"


@interface CCRenderTexture ()
- (void) saveGLColor;
- (void) restoreGLColor;
@end

@implementation CCRenderTexture @implementation CCRenderTexture


@synthesize sprite=sprite_; @synthesize sprite=sprite_;
Expand Down Expand Up @@ -115,44 +110,25 @@ -(void)dealloc


-(void)begin -(void)begin
{ {
// don't clear the frame buffer
[self beginWithClear:-1 g:-1 b:-1 a:-1];
}

-(void)beginWithClear:(float)r g:(float)g b:(float)b a:(float)a
{
if( a != -1 ) {
[self saveGLColor];
restoreColor_ = YES;
} else
restoreColor_ = NO;


// Save the current matrix // Save the current matrix
glPushMatrix(); glPushMatrix();


CGSize texSize = [texture_ contentSizeInPixels]; CGSize texSize = [texture_ contentSizeInPixels];


// Calculate the adjustment ratios based on the old and new projections // Calculate the adjustment ratios based on the old and new projections
CGSize size = [[CCDirector sharedDirector] displaySizeInPixels]; CGSize size = [[CCDirector sharedDirector] displaySizeInPixels];
float widthRatio = size.width / texSize.width; float widthRatio = size.width / texSize.width;
float heightRatio = size.height / texSize.height; float heightRatio = size.height / texSize.height;


// Adjust the orthographic propjection and viewport // Adjust the orthographic propjection and viewport
ccglOrtho((float)-1.0 / widthRatio, (float)1.0 / widthRatio, (float)-1.0 / heightRatio, (float)1.0 / heightRatio, -1,1); ccglOrtho((float)-1.0 / widthRatio, (float)1.0 / widthRatio, (float)-1.0 / heightRatio, (float)1.0 / heightRatio, -1,1);
glViewport(0, 0, texSize.width, texSize.height); glViewport(0, 0, texSize.width, texSize.height);


glGetIntegerv(CC_GL_FRAMEBUFFER_BINDING, &oldFBO_); glGetIntegerv(CC_GL_FRAMEBUFFER_BINDING, &oldFBO_);
ccglBindFramebuffer(CC_GL_FRAMEBUFFER, fbo_);//Will direct drawing to the frame buffer created above ccglBindFramebuffer(CC_GL_FRAMEBUFFER, fbo_);//Will direct drawing to the frame buffer created above


if( a != -1 ) {
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}


// Issue #1145 // Issue #1145
// There is no need to enable the default GL states here // There is no need to enable the default GL states here
Expand All @@ -166,16 +142,28 @@ -(void)beginWithClear:(float)r g:(float)g b:(float)b a:(float)a
CC_ENABLE_DEFAULT_GL_STATES(); CC_ENABLE_DEFAULT_GL_STATES();
} }


-(void)beginWithClear:(float)r g:(float)g b:(float)b a:(float)a
{
[self begin];

// save clear color
GLfloat clearColor[4];
glGetFloatv(GL_COLOR_CLEAR_VALUE,clearColor);

glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// restore clear color
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
}

-(void)end -(void)end
{ {
ccglBindFramebuffer(CC_GL_FRAMEBUFFER, oldFBO_); ccglBindFramebuffer(CC_GL_FRAMEBUFFER, oldFBO_);
// Restore the original matrix and viewport // Restore the original matrix and viewport
glPopMatrix(); glPopMatrix();
CGSize size = [[CCDirector sharedDirector] displaySizeInPixels]; CGSize size = [[CCDirector sharedDirector] displaySizeInPixels];
glViewport(0, 0, size.width, size.height); glViewport(0, 0, size.width, size.height);

if( restoreColor_ )
[self restoreGLColor];
} }


-(void)clear:(float)r g:(float)g b:(float)b a:(float)a -(void)clear:(float)r g:(float)g b:(float)b a:(float)a
Expand All @@ -184,16 +172,6 @@ -(void)clear:(float)r g:(float)g b:(float)b a:(float)a
[self end]; [self end];
} }


-(void) saveGLColor
{
glGetFloatv(GL_COLOR_CLEAR_VALUE,clearColor_);
}

- (void) restoreGLColor
{
glClearColor(clearColor_[0], clearColor_[1], clearColor_[2], clearColor_[3]);
}

#pragma mark RenderTexture - Save Image #pragma mark RenderTexture - Save Image


#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
Expand Down
3 changes: 1 addition & 2 deletions tests/RenderTextureTest.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
@end @end


@interface RenderTextureIssue937 : RenderTextureTest @interface RenderTextureIssue937 : RenderTextureTest
{ {}
}
@end @end




16 changes: 11 additions & 5 deletions tests/RenderTextureTest.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ -(id) init


// note that the render texture is a CCNode, and contains a sprite of its texture for convience, // note that the render texture is a CCNode, and contains a sprite of its texture for convience,
// so we can just parent it to the scene like any other CCNode // so we can just parent it to the scene like any other CCNode
[self addChild:target z:1]; [self addChild:target z:-1];


// create a brush image to draw into the texture with // create a brush image to draw into the texture with
brush = [[CCSprite spriteWithFile:@"fire.png"] retain]; brush = [[CCSprite spriteWithFile:@"fire.png"] retain];
Expand All @@ -152,9 +152,11 @@ -(id) init


// Save Image menu // Save Image menu
[CCMenuItemFont setFontSize:16]; [CCMenuItemFont setFontSize:16];
CCMenuItem *item = [CCMenuItemFont itemFromString:@"Save Image" target:self selector:@selector(saveImage:)]; CCMenuItem *item1 = [CCMenuItemFont itemFromString:@"Save Image" target:self selector:@selector(saveImage:)];
CCMenu *menu = [CCMenu menuWithItems:item, nil]; CCMenuItem *item2 = [CCMenuItemFont itemFromString:@"Clear" target:self selector:@selector(clearImage:)];
CCMenu *menu = [CCMenu menuWithItems:item1, item2, nil];
[self addChild:menu]; [self addChild:menu];
[menu alignItemsVertically];
[menu setPosition:ccp(s.width-80, s.height-30)]; [menu setPosition:ccp(s.width-80, s.height-30)];
} }
return self; return self;
Expand All @@ -170,6 +172,11 @@ -(NSString*) subtitle
return @"Press 'Save Image' to create an snapshot of the render texture"; return @"Press 'Save Image' to create an snapshot of the render texture";
} }


-(void) clearImage:(id)sender
{
[target clear:CCRANDOM_0_1() g:CCRANDOM_0_1() b:CCRANDOM_0_1() a:CCRANDOM_0_1()];
}

-(void) saveImage:(id)sender -(void) saveImage:(id)sender
{ {
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
Expand Down Expand Up @@ -222,6 +229,7 @@ -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
[brush setRotation:rand()%360]; [brush setRotation:rand()%360];
float r = ((float)(rand()%50)/50.f) + 0.25f; float r = ((float)(rand()%50)/50.f) + 0.25f;
[brush setScale:r]; [brush setScale:r];
[brush setColor:ccc3(CCRANDOM_0_1()*127+128, 255, 255) ];
// Call visit to draw the brush, don't call draw.. // Call visit to draw the brush, don't call draw..
[brush visit]; [brush visit];
} }
Expand Down Expand Up @@ -277,7 +285,6 @@ -(BOOL) ccMouseDragged:(NSEvent *)event
return YES; return YES;


} }

#endif // __MAC_OS_X_VERSION_MAX_ALLOWED #endif // __MAC_OS_X_VERSION_MAX_ALLOWED
@end @end


Expand Down Expand Up @@ -352,7 +359,6 @@ -(NSString*) subtitle
{ {
return @"All images should be equal..."; return @"All images should be equal...";
} }

@end @end


#pragma mark - #pragma mark -
Expand Down

0 comments on commit 7a1bc20

Please sign in to comment.