Skip to content

Commit 98955aa

Browse files
committed
Private CCDirector.viewportRect property.
Former-commit-id: 782d106
1 parent 25c79fe commit 98955aa

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

cocos2d/CCDirector.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,29 @@ -(CGSize)viewSizeInPixels
444444
return _winSizeInPixels;
445445
}
446446

447+
-(CGRect)viewportRect
448+
{
449+
// TODO It's _possible_ that a user will use a non-axis aligned projection. Weird, but possible.
450+
kmMat4 projection;
451+
kmGLGetMatrix(KM_GL_PROJECTION, &projection);
452+
453+
kmMat4 projectionInv;
454+
kmMat4Inverse(&projectionInv, &projection);
455+
456+
// Calculate z=0 using -> transform*[0, 0, 0, 1]/w
457+
kmScalar zClip = projection.mat[14]/projection.mat[15];
458+
459+
// Bottom left and top right coordinates of viewport in clip coords.
460+
kmVec3 clipBL = {-1.0, -1.0, zClip};
461+
kmVec3 clipTR = { 1.0, 1.0, zClip};
462+
463+
kmVec3 glBL, glTR;
464+
kmVec3TransformCoord(&glBL, &clipBL, &projectionInv);
465+
kmVec3TransformCoord(&glTR, &clipTR, &projectionInv);
466+
467+
return CGRectMake(glBL.x, glBL.y, glTR.x - glBL.x, glTR.y - glBL.y);
468+
}
469+
447470
-(CGSize)designSize
448471
{
449472
// Return the viewSize unless designSize has been set.

cocos2d/CCDirector_Private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
*/
4747
@property (nonatomic,readwrite,strong) CCActionManager *actionManager;
4848

49+
/// Rect of the visible screen area in GL coordinates.
50+
@property(nonatomic, readonly) CGRect viewportRect;
51+
4952
/* Sets the glViewport*/
5053
-(void) setViewport;
5154

0 commit comments

Comments
 (0)