Skip to content

Commit 2fb4475

Browse files
author
Thayer J Andrews
committed
CCEffectNode - Fix parent relative sizing
It looks like this is broken for CCRenderTexture as well but I'm intentionally not fixing it there with this change. I'll add that fix separately.
1 parent 91bd04a commit 2fb4475

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

cocos2d/CCEffectNode.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ -(void)setEffect:(CCEffect *)effect
7070
}
7171
}
7272

73+
-(void)create
74+
{
75+
CGSize pointSize = self.contentSizeInPoints;
76+
CGSize pixelSize = CGSizeMake(pointSize.width * _contentScale, pointSize.height * _contentScale);
77+
[self createTextureAndFboWithPixelSize:pixelSize];
78+
79+
CGRect rect = CGRectMake(0, 0, pointSize.width, pointSize.height);
80+
[_sprite setTextureRect:rect];
81+
82+
_projection = GLKMatrix4MakeOrtho(0.0f, pointSize.width, 0.0f, pointSize.height, -1024.0f, 1024.0f);
83+
}
84+
7385
-(void)begin
7486
{
7587
CGSize pixelSize = self.texture.contentSizeInPixels;
@@ -206,4 +218,10 @@ - (void)updateShaderUniformsFromEffect
206218
[_shaderUniforms addEntriesFromDictionary:_effect.shaderUniforms];
207219
}
208220

221+
- (void)setContentSizeType:(CCSizeType)contentSizeType
222+
{
223+
[super setContentSizeType:contentSizeType];
224+
_contentSizeChanged = YES;
225+
}
226+
209227
@end

cocos2d/CCRenderTexture.m

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,30 +168,32 @@ -(id)init
168168
}
169169

170170
-(void)create
171+
{
172+
CGSize pixelSize = CGSizeMake(_contentSize.width * _contentScale, _contentSize.height * _contentScale);
173+
[self createTextureAndFboWithPixelSize:pixelSize];
174+
}
175+
176+
-(void)createTextureAndFboWithPixelSize:(CGSize)pixelSize
171177
{
172178
glPushGroupMarkerEXT(0, "CCRenderTexture: Create");
173179

174-
int pixelW = _contentSize.width*_contentScale;
175-
int pixelH = _contentSize.height*_contentScale;
176-
177-
178180
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_oldFBO);
179181

180182
// textures must be power of two
181183
NSUInteger powW;
182184
NSUInteger powH;
183185

184186
if( [[CCConfiguration sharedConfiguration] supportsNPOT] ) {
185-
powW = pixelW;
186-
powH = pixelH;
187+
powW = pixelSize.width;
188+
powH = pixelSize.height;
187189
} else {
188-
powW = CCNextPOT(pixelW);
189-
powH = CCNextPOT(pixelH);
190+
powW = CCNextPOT(pixelSize.width);
191+
powH = CCNextPOT(pixelSize.height);
190192
}
191193

192194
void *data = calloc(powW*powH, 4);
193195

194-
CCTexture *texture = [[CCTexture alloc] initWithData:data pixelFormat:_pixelFormat pixelsWide:powW pixelsHigh:powH contentSizeInPixels:CGSizeMake(pixelW, pixelH) contentScale:_contentScale];
196+
CCTexture *texture = [[CCTexture alloc] initWithData:data pixelFormat:_pixelFormat pixelsWide:powW pixelsHigh:powH contentSizeInPixels:pixelSize contentScale:_contentScale];
195197
self.texture = texture;
196198

197199
free(data);
@@ -234,6 +236,10 @@ -(void)create
234236
CC_CHECK_GL_ERROR_DEBUG();
235237
glPopGroupMarkerEXT();
236238

239+
// XXX Thayer says: I think this is incorrect for any situations where the content
240+
// size type isn't (points, points). The call to setTextureRect below eventually arrives
241+
// at some code that assumes the supplied size is in points so, if the size is not in points,
242+
// things break.
237243
CGRect rect = CGRectMake(0, 0, _contentSize.width, _contentSize.height);
238244

239245
[self assignSpriteTexture];
@@ -618,6 +624,10 @@ -(void) setContentSize:(CGSize)size
618624
// TODO: Fix CCRenderTexture so that it correctly handles this
619625
// NSAssert(NO, @"You cannot change the content size of an already created CCRenderTexture. Recreate it");
620626
[super setContentSize:size];
627+
628+
// XXX Thayer says: I'm pretty sure this is broken since the supplied content size could
629+
// be normalized, in points, in UI points, etc. We should get the size in points then convert
630+
// to pixels and use that to make the ortho matrix.
621631
_projection = GLKMatrix4MakeOrtho(0.0f, size.width, 0.0f, size.height, -1024.0f, 1024.0f);
622632
_contentSizeChanged = YES;
623633

cocos2d/CCRenderTexture_Private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
BOOL _contentSizeChanged;
3737
}
3838

39+
-(void)createTextureAndFboWithPixelSize:(CGSize)pixelSize;
3940
-(void)destroy;
4041

4142
-(GLuint)fbo;

0 commit comments

Comments
 (0)