Skip to content

Commit 238e715

Browse files
committed
Merge pull request #7 from spritebuilder/headless-develop
Added Android support for CCFxiedScreenMode and CCFlexibleScreenMode
2 parents 77cda08 + ac96e3b commit 238e715

File tree

12 files changed

+193
-322
lines changed

12 files changed

+193
-322
lines changed

cocos2d-tests-ios.xcodeproj/project.pbxproj

Lines changed: 0 additions & 236 deletions
Large diffs are not rendered by default.

cocos2d/CCConfiguration.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@
2828

2929
#import "Platforms/CCGL.h"
3030

31+
extern NSString* const CCSetupPixelFormat;
32+
extern NSString* const CCSetupScreenMode;
33+
extern NSString* const CCSetupScreenOrientation;
34+
extern NSString* const CCSetupAnimationInterval;
35+
extern NSString* const CCSetupFixedUpdateInterval;
36+
extern NSString* const CCSetupShowDebugStats;
37+
extern NSString* const CCSetupTabletScale2X;
38+
39+
extern NSString* const CCSetupDepthFormat;
40+
extern NSString* const CCSetupPreserveBackbuffer;
41+
extern NSString* const CCSetupMultiSampling;
42+
extern NSString* const CCSetupNumberOfSamples;
43+
44+
// Landscape screen orientation. Used with CCSetupScreenOrientation.
45+
extern NSString* const CCScreenOrientationLandscape;
46+
47+
// Portrait screen orientation. Used with CCSetupScreenOrientation.
48+
extern NSString* const CCScreenOrientationPortrait;
49+
50+
// Support all screen orientations. Used with CCSetupScreenOrientation.
51+
extern NSString* const CCScreenOrientationAll;
52+
53+
54+
// The flexible screen mode is Cocos2d's default. It will give you an area that can vary slightly in size. In landscape mode the height will be 320 points for mobiles and 384 points for tablets. The width of the area can vary from 480 to 568 points.
55+
extern NSString* const CCScreenModeFlexible;
56+
57+
// The fixed screen mode will setup the working area to be 568 x 384 points. Depending on the device, the outer edges may be cropped. The safe area, that will be displayed on all sorts of devices, is 480 x 320 points and placed in the center of the working area.
58+
extern NSString* const CCScreenModeFixed;
59+
3160
/** OS version definitions. Includes both iOS and Mac OS versions
3261
*/
3362
typedef NS_ENUM(NSUInteger, CCSystemVersion){

cocos2d/CCConfiguration.m

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@
3838
#import "ccConfig.h"
3939
#import "cocos2d.h"
4040

41+
NSString* const CCSetupPixelFormat = @"CCSetupPixelFormat";
42+
NSString* const CCSetupScreenMode = @"CCSetupScreenMode";
43+
NSString* const CCSetupScreenOrientation = @"CCSetupScreenOrientation";
44+
NSString* const CCSetupAnimationInterval = @"CCSetupAnimationInterval";
45+
NSString* const CCSetupFixedUpdateInterval = @"CCSetupFixedUpdateInterval";
46+
NSString* const CCSetupShowDebugStats = @"CCSetupShowDebugStats";
47+
NSString* const CCSetupTabletScale2X = @"CCSetupTabletScale2X";
48+
49+
NSString* const CCSetupDepthFormat = @"CCSetupDepthFormat";
50+
NSString* const CCSetupPreserveBackbuffer = @"CCSetupPreserveBackbuffer";
51+
NSString* const CCSetupMultiSampling = @"CCSetupMultiSampling";
52+
NSString* const CCSetupNumberOfSamples = @"CCSetupNumberOfSamples";
53+
54+
NSString* const CCScreenOrientationLandscape = @"CCScreenOrientationLandscape";
55+
NSString* const CCScreenOrientationPortrait = @"CCScreenOrientationPortrait";
56+
NSString* const CCScreenOrientationAll = @"CCScreenOrientationAll";
57+
58+
NSString* const CCScreenModeFlexible = @"CCScreenModeFlexible";
59+
NSString* const CCScreenModeFixed = @"CCScreenModeFixed";
60+
61+
4162
@interface CCConfiguration ()
4263
-(void) getOpenGLvariables;
4364
@end
@@ -123,20 +144,16 @@ -(NSInteger) runningDevice
123144
{
124145
NSInteger ret=-1;
125146

126-
/*
127-
#if defined(APPORTABLE)
128-
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
129-
{
130-
ret = ([UIScreen mainScreen].scale > 1) ? CCDeviceiPadRetinaDisplay : CCDeviceiPad;
131-
}
132-
else if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone )
133-
{
134-
if( [UIScreen mainScreen].scale > 1 ) {
135-
ret = CCDeviceiPhoneRetinaDisplay;
136-
} else
137-
ret = CCDeviceiPhone;
138-
}
139-
#el*/#if __CC_PLATFORM_IOS
147+
#if __CC_PLATFORM_ANDROID
148+
if([CCDirector sharedDirector].contentScaleFactor > 1.0)
149+
{
150+
ret = CCDeviceiPhoneRetinaDisplay;
151+
}
152+
else
153+
{
154+
ret = CCDeviceiPhone;
155+
}
156+
#elif __CC_PLATFORM_IOS
140157

141158
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
142159
{

cocos2d/Platforms/Android/CCActivity.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
#import <BridgeKitV3/BridgeKit.h>
1313
#import "../../Platforms/CCGL.h"
14+
#import "CCProtocols.h"
1415

1516
@class CCScene;
1617
@class AndroidRelativeLayout;
1718
BRIDGE_CLASS("org.cocos2d.CCActivity")
18-
@interface CCActivity : AndroidActivity <AndroidSurfaceHolderCallback>
19+
@interface CCActivity : AndroidActivity <AndroidSurfaceHolderCallback, CCDirectorDelegate>
1920
@property (readonly, nonatomic) AndroidRelativeLayout *layout;
2021
+ (instancetype)currentActivity;
2122

cocos2d/Platforms/Android/CCActivity.m

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ @implementation CCActivity {
4545
NSThread *_thread;
4646
BOOL _running;
4747
NSRunLoop *_gameLoop;
48+
NSMutableDictionary *_cocos2dSetupConfig;
4849
}
4950
@synthesize layout=_layout;
5051

@@ -87,7 +88,24 @@ - (void)run
8788
_layout = [[AndroidRelativeLayout alloc] initWithContext:self];
8889
AndroidDisplayMetrics *metrics = [[AndroidDisplayMetrics alloc] init];
8990
[self.windowManager.defaultDisplay getMetrics:metrics];
90-
_glView = [[CCGLView alloc] initWithContext:self scaleFactor:metrics.density];
91+
92+
// Configure Cocos2d with the options set in SpriteBuilder
93+
// TODO: repalce Published-iOS with Published-Android
94+
NSString* configPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Published-iOS"];
95+
96+
configPath = [configPath stringByAppendingPathComponent:@"configCocos2d.plist"];
97+
98+
_cocos2dSetupConfig = [NSMutableDictionary dictionaryWithContentsOfFile:configPath];
99+
100+
enum CCAndroidScreenMode screenMode = CCNativeScreenMode;
101+
102+
if([_cocos2dSetupConfig[CCSetupScreenMode] isEqual:CCScreenModeFlexible] ||
103+
[_cocos2dSetupConfig[CCSetupScreenMode] isEqual:CCScreenModeFixed])
104+
{
105+
screenMode = CCScreenScaledAspectFitEmulationMode;
106+
}
107+
108+
_glView = [[CCGLView alloc] initWithContext:self screenMode:screenMode scaleFactor:metrics.density];
91109
[metrics release];
92110
[_glView.holder addCallback:self];
93111
[self.layout addView:_glView];
@@ -117,7 +135,7 @@ - (void)surfaceChanged:(JavaObject<AndroidSurfaceHolder> *)holder format:(int)fo
117135
if(_glView == nil)
118136
return;
119137

120-
_glView.bounds = CGRectMake(0, 0, width/_glView.contentScaleFactor, height/_glView.contentScaleFactor);
138+
_glView.bounds = CGRectMake(0, 0, width/_glView.contentScaleFactor, height/_glView.contentScaleFactor);
121139

122140
#if USE_MAIN_THREAD
123141
[self reshape:[NSValue valueWithCGSize:CGSizeMake(width, height)]];
@@ -147,10 +165,23 @@ - (void)startGL:(JavaObject<AndroidSurfaceHolder> *)holder
147165
[self setupPaths];
148166

149167
CCDirectorAndroid *director = (CCDirectorAndroid*)[CCDirector sharedDirector];
168+
director.delegate = self;
150169
director.contentScaleFactor = _glView.contentScaleFactor;
151170
[CCTexture setDefaultAlphaPixelFormat:CCTexturePixelFormat_RGBA8888];
152171
[director setView:_glView];
153172

173+
if([_cocos2dSetupConfig[CCSetupScreenMode] isEqual:CCScreenModeFixed])
174+
{
175+
CGSize fixed = {568, 384};
176+
if([_cocos2dSetupConfig[CCSetupScreenOrientation] isEqualToString:CCScreenOrientationPortrait])
177+
{
178+
CC_SWAP(fixed.width, fixed.height);
179+
}
180+
181+
director.designSize = fixed;
182+
[director setProjection:CCDirectorProjectionCustom];
183+
}
184+
154185
[director runWithScene:[self startScene]];
155186
[director setAnimationInterval:1.0/60.0];
156187
[director startAnimation];
@@ -270,6 +301,20 @@ - (void)popApplicationContext:(EGLContext)ctx
270301
}
271302
}
272303

304+
#pragma mark CCDirector Delegate
305+
306+
// Projection delegate is only used if the fixed resolution mode is enabled
307+
-(GLKMatrix4)updateProjection
308+
{
309+
CGSize sizePoint = [CCDirector sharedDirector].viewSize;
310+
CGSize fixed = [CCDirector sharedDirector].designSize;
311+
312+
// Half of the extra size that will be cut off
313+
CGPoint offset = ccpMult(ccp(fixed.width - sizePoint.width, fixed.height - sizePoint.height), 0.5);
314+
315+
return CCMatrix4MakeOrtho(offset.x, sizePoint.width + offset.x, offset.y, sizePoint.height + offset.y, -1024, 1024);
316+
}
317+
273318
@end
274319

275320
#endif

cocos2d/Platforms/Android/CCGLView.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,32 @@
1717

1818
#import "../../Platforms/CCGL.h"
1919

20+
enum CCAndroidScreenMode {
21+
22+
/*
23+
NOTE: Emulation modes are not ideal and are only inteded for quick prototyping,
24+
because emulation modes force a an aspect ratio that is not intended for the device.
25+
*/
26+
27+
/*
28+
Keeps true to the device resolution and calculates a content scale
29+
based on AndroidDisplayMetrics density property
30+
*/
31+
CCNativeScreenMode,
32+
33+
/*
34+
Provides a screen that is 320pt wide and has the aspect ratio of the
35+
device. The screen size in pixels matches the native
36+
resolution.
37+
*/
38+
CCScreenScaledAspectFitEmulationMode
39+
};
40+
2041
BRIDGE_CLASS("org.cocos2d.CCGLView")
2142
@interface CCGLView : AndroidSurfaceView
2243

2344
- (id)initWithContext:(AndroidContext *)context;
24-
- (id)initWithContext:(AndroidContext *)context scaleFactor:(float)scaleFactor;
45+
- (id)initWithContext:(AndroidContext *)context screenMode:(enum CCAndroidScreenMode)screenMode scaleFactor:(float)scaleFactor;
2546
- (BOOL)onTouchEvent:(AndroidMotionEvent *)event;
2647

2748
- (BOOL)setupView:(ANativeWindow*)window;
@@ -33,6 +54,7 @@ BRIDGE_CLASS("org.cocos2d.CCGLView")
3354
@property (nonatomic, readonly) EGLSurface eglSurface;
3455
@property (nonatomic, readonly) EGLContext eglContext;
3556
@property (nonatomic, readonly) EGLConfig eglConfiguration;
57+
@property (nonatomic, readonly) enum CCAndroidScreenMode screenMode;
3658

3759
- (void)addGestureDetector:(AndroidGestureDetector *)detector;
3860
- (void)removeGestureDetector:(AndroidGestureDetector *)detector;

cocos2d/Platforms/Android/CCGLView.m

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Oleg Osin on 5/22/14.
66
//
77
//
8+
89
#import "cocos2d.h"
910
#import "CCGLView.h"
1011

@@ -19,6 +20,8 @@
1920
#import "CCTouchAndroid.h"
2021
#import <CoreGraphics/CGGeometry.h>
2122

23+
#define IPHONE3G_WIDTH 320.0f
24+
2225
static NSMutableDictionary *touches = nil;
2326
static CCTouchEvent *currentEvent = nil;
2427

@@ -29,12 +32,13 @@ @implementation CCGLView {
2932
@bridge (constructor) initWithContext:;
3033
@bridge (callback) onTouchEvent: = onTouchEvent;
3134

32-
- (id)initWithContext:(AndroidContext *)context scaleFactor:(float)scaleFactor
35+
- (id)initWithContext:(AndroidContext *)context screenMode:(enum CCAndroidScreenMode)screenMode scaleFactor:(float)scaleFactor
3336
{
3437
self = [self initWithContext:context];
3538
if (self)
3639
{
3740
_contentScaleFactor = scaleFactor;
41+
_screenMode = screenMode;
3842
}
3943
return self;
4044
}
@@ -503,9 +507,35 @@ - (BOOL)setupView:(ANativeWindow*)window
503507
return NO;
504508
}
505509

506-
width /= _contentScaleFactor;
507-
height /= _contentScaleFactor;
508-
//
510+
switch (_screenMode)
511+
{
512+
case CCNativeScreenMode:
513+
{
514+
width /= _contentScaleFactor;
515+
height /= _contentScaleFactor;
516+
}
517+
break;
518+
519+
case CCScreenScaledAspectFitEmulationMode:
520+
{
521+
CGSize size = CGSizeMake(width, height);
522+
if (width > height)
523+
size = CGSizeMake(height, width);
524+
525+
_contentScaleFactor = size.width / IPHONE3G_WIDTH;
526+
527+
width /= _contentScaleFactor;
528+
height /= _contentScaleFactor;
529+
}
530+
break;
531+
532+
533+
default:
534+
CCLOGWARN(@"WARNING: Failed to identify screen mode");
535+
break;
536+
537+
}
538+
509539
// ANativeWindow_setBuffersGeometry(window, width, height, format);
510540

511541
if(eglGetError() != EGL_SUCCESS) { NSLog(@"EGL ERROR: %i", eglGetError()); };

cocos2d/Platforms/iOS/CCAppDelegate.h

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,6 @@
3030
#import <UIKit/UIKit.h>
3131
#import "CCDirectorIOS.h"
3232

33-
extern NSString* const CCSetupPixelFormat;
34-
extern NSString* const CCSetupScreenMode;
35-
extern NSString* const CCSetupScreenOrientation;
36-
extern NSString* const CCSetupAnimationInterval;
37-
extern NSString* const CCSetupFixedUpdateInterval;
38-
extern NSString* const CCSetupShowDebugStats;
39-
extern NSString* const CCSetupTabletScale2X;
40-
41-
extern NSString* const CCSetupDepthFormat;
42-
extern NSString* const CCSetupPreserveBackbuffer;
43-
extern NSString* const CCSetupMultiSampling;
44-
extern NSString* const CCSetupNumberOfSamples;
45-
46-
// Landscape screen orientation. Used with CCSetupScreenOrientation.
47-
extern NSString* const CCScreenOrientationLandscape;
48-
49-
// Portrait screen orientation. Used with CCSetupScreenOrientation.
50-
extern NSString* const CCScreenOrientationPortrait;
51-
52-
// Support all screen orientations. Used with CCSetupScreenOrientation.
53-
extern NSString* const CCScreenOrientationAll;
54-
55-
56-
// The flexible screen mode is Cocos2d's default. It will give you an area that can vary slightly in size. In landscape mode the height will be 320 points for mobiles and 384 points for tablets. The width of the area can vary from 480 to 568 points.
57-
extern NSString* const CCScreenModeFlexible;
58-
59-
// The fixed screen mode will setup the working area to be 568 x 384 points. Depending on the device, the outer edges may be cropped. The safe area, that will be displayed on all sorts of devices, is 480 x 320 points and placed in the center of the working area.
60-
extern NSString* const CCScreenModeFixed;
61-
62-
6333
@class CCAppDelegate;
6434
@class CCScene;
6535

cocos2d/Platforms/iOS/CCAppDelegate.m

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,6 @@
3434

3535
#import "OALSimpleAudio.h"
3636

37-
NSString* const CCSetupPixelFormat = @"CCSetupPixelFormat";
38-
NSString* const CCSetupScreenMode = @"CCSetupScreenMode";
39-
NSString* const CCSetupScreenOrientation = @"CCSetupScreenOrientation";
40-
NSString* const CCSetupAnimationInterval = @"CCSetupAnimationInterval";
41-
NSString* const CCSetupFixedUpdateInterval = @"CCSetupFixedUpdateInterval";
42-
NSString* const CCSetupShowDebugStats = @"CCSetupShowDebugStats";
43-
NSString* const CCSetupTabletScale2X = @"CCSetupTabletScale2X";
44-
45-
NSString* const CCSetupDepthFormat = @"CCSetupDepthFormat";
46-
NSString* const CCSetupPreserveBackbuffer = @"CCSetupPreserveBackbuffer";
47-
NSString* const CCSetupMultiSampling = @"CCSetupMultiSampling";
48-
NSString* const CCSetupNumberOfSamples = @"CCSetupNumberOfSamples";
49-
50-
NSString* const CCScreenOrientationLandscape = @"CCScreenOrientationLandscape";
51-
NSString* const CCScreenOrientationPortrait = @"CCScreenOrientationPortrait";
52-
NSString* const CCScreenOrientationAll = @"CCScreenOrientationAll";
53-
54-
NSString* const CCScreenModeFlexible = @"CCScreenModeFlexible";
55-
NSString* const CCScreenModeFixed = @"CCScreenModeFixed";
56-
5737
// Fixed size. As wide as iPhone 5 at 2x and as high as the iPad at 2x.
5838
const CGSize FIXED_SIZE = {568, 384};
5939

0 commit comments

Comments
 (0)