Permalink
Browse files

RenderTexture: Optimized for "begin" use, and not "beginWithColor"

  • Loading branch information...
ricardoquesada committed Mar 28, 2011
1 parent 95060a7 commit 7a1bc200c292f4875b2cd4ea6c9ae9f09cd2bdad
View
0 RELEASE_NOTES 100755 → 100644
No changes.
View
0 Resources/Default.png 100755 → 100644
No changes.
View
0 Resources/Icon.png 100755 → 100644
No changes.
View
0 Resources/Info.plist 100755 → 100644
No changes.
View
0 Resources/InfoWithStatusBar.plist 100755 → 100644
No changes.
View
0 Resources/iTunesArtwork 100755 → 100644
No changes.
@@ -59,9 +59,6 @@ enum
CCSprite* sprite_;
GLenum pixelFormat_;
- GLfloat clearColor_[4];
- BOOL restoreColor_;
-
}
/** The CCSprite being used.
View
@@ -30,11 +30,6 @@
#import "Support/ccUtils.h"
#import "Support/CCFileUtils.h"
-@interface CCRenderTexture ()
-- (void) saveGLColor;
-- (void) restoreGLColor;
-@end
-
@implementation CCRenderTexture
@synthesize sprite=sprite_;
@@ -115,44 +110,25 @@ -(void)dealloc
-(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
glPushMatrix();
CGSize texSize = [texture_ contentSizeInPixels];
-
-
+
+
// Calculate the adjustment ratios based on the old and new projections
CGSize size = [[CCDirector sharedDirector] displaySizeInPixels];
float widthRatio = size.width / texSize.width;
float heightRatio = size.height / texSize.height;
-
-
+
+
// 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);
glViewport(0, 0, texSize.width, texSize.height);
-
-
+
+
glGetIntegerv(CC_GL_FRAMEBUFFER_BINDING, &oldFBO_);
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
// There is no need to enable the default GL states here
@@ -166,16 +142,28 @@ -(void)beginWithClear:(float)r g:(float)g b:(float)b a:(float)a
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
{
ccglBindFramebuffer(CC_GL_FRAMEBUFFER, oldFBO_);
// Restore the original matrix and viewport
glPopMatrix();
CGSize size = [[CCDirector sharedDirector] displaySizeInPixels];
glViewport(0, 0, size.width, size.height);
-
- if( restoreColor_ )
- [self restoreGLColor];
}
-(void)clear:(float)r g:(float)g b:(float)b a:(float)a
@@ -184,16 +172,6 @@ -(void)clear:(float)r g:(float)g b:(float)b a:(float)a
[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
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
@@ -44,8 +44,7 @@
@end
@interface RenderTextureIssue937 : RenderTextureTest
-{
-}
+{}
@end
View
@@ -138,7 +138,7 @@ -(id) init
// 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
- [self addChild:target z:1];
+ [self addChild:target z:-1];
// create a brush image to draw into the texture with
brush = [[CCSprite spriteWithFile:@"fire.png"] retain];
@@ -152,9 +152,11 @@ -(id) init
// Save Image menu
[CCMenuItemFont setFontSize:16];
- CCMenuItem *item = [CCMenuItemFont itemFromString:@"Save Image" target:self selector:@selector(saveImage:)];
- CCMenu *menu = [CCMenu menuWithItems:item, nil];
+ CCMenuItem *item1 = [CCMenuItemFont itemFromString:@"Save Image" target:self selector:@selector(saveImage:)];
+ CCMenuItem *item2 = [CCMenuItemFont itemFromString:@"Clear" target:self selector:@selector(clearImage:)];
+ CCMenu *menu = [CCMenu menuWithItems:item1, item2, nil];
[self addChild:menu];
+ [menu alignItemsVertically];
[menu setPosition:ccp(s.width-80, s.height-30)];
}
return self;
@@ -170,6 +172,11 @@ -(NSString*) subtitle
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
{
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
@@ -222,6 +229,7 @@ -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
[brush setRotation:rand()%360];
float r = ((float)(rand()%50)/50.f) + 0.25f;
[brush setScale:r];
+ [brush setColor:ccc3(CCRANDOM_0_1()*127+128, 255, 255) ];
// Call visit to draw the brush, don't call draw..
[brush visit];
}
@@ -277,7 +285,6 @@ -(BOOL) ccMouseDragged:(NSEvent *)event
return YES;
}
-
#endif // __MAC_OS_X_VERSION_MAX_ALLOWED
@end
@@ -352,7 +359,6 @@ -(NSString*) subtitle
{
return @"All images should be equal...";
}
-
@end
#pragma mark -

0 comments on commit 7a1bc20

Please sign in to comment.