public
Created

Cocos2d Image Size Bug

  • Download Gist
AppDelegate.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
//
// AppDelegate.m
// Honduras
//
// Created by Marcio Valenzuela on 7/8/11.
// Copyright Personal 2011. All rights reserved.
//
 
#import "cocos2d.h"
 
#import "AppDelegate.h"
#import "GameConfig.h"
#import "HelloWorldLayer.h"
#import "RootViewController.h"
#import "SimpleAudioEngine.h"
#import "Level.h"
#import "GameOverScene.h"
#import "NewLevelScene.h"
#import "GameScene.h"
#import "GameManager.h"
#import "Appirater.h"
 
#import "MZIAPHelper.h"
 
@implementation AppDelegate
 
@synthesize window;
@synthesize mainScene = _mainScene;
@synthesize gameOverScene = _gameOverScene;
@synthesize newLevelScene = _newLevelScene;
@synthesize levels = _levels;
@synthesize curLevelIndex = _curLevelIndex;
 
- (void) removeStartupFlicker
{
//
// THIS CODE REMOVES THE STARTUP FLICKER
//
// Uncomment the following code if you Application only supports landscape mode
//
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
#endif
}
 
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
//IAPS
[MZIAPHelper sharedInstance];
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
// Init the View Controller
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
// Create the EAGLView manually
// 1. Create a RGB565 format. Alternative: RGBA8
// 2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
//
//
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
// attach the openglView to the director
[director setOpenGLView:glView];
// // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director enableRetinaDisplay:YES] )
CCLOG(@"Retina Display Not supported");
//
// VERY IMPORTANT:
// If the rotation is going to be controlled by a UIViewController
// then the device orientation should be "Portrait".
//
// IMPORTANT:
// By default, this template only supports Landscape orientations.
// Edit the RootViewController.m file to edit the supported orientations.
//
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:YES];
// make the OpenGLView a child of the view controller
[viewController setView:glView];
// make the View Controller a child of the main window
[window setRootViewController:viewController];
//[window addSubview: viewController.view];
[window makeKeyAndVisible];
// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change anytime.
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
// Removes the startup flicker
[self removeStartupFlicker];
//***INSERT LEVEL CODE
//
// Load levels
self.levels = [[[NSMutableArray alloc] init] autorelease];
Level *level1 = [[[Level alloc] initWithLevelNum:1 spawnRate:2 bgImageName:@"bg.png"] autorelease];
Level *level2 = [[[Level alloc] initWithLevelNum:2 spawnRate:1 bgImageName:@"bg2.png"] autorelease];
[_levels addObject:level1];
[_levels addObject:level2];
self.curLevelIndex = 0;
 
// Run the intro Scene
//[[CCDirector sharedDirector] runWithScene:kMainScene];
[[GameManager sharedGameManager] setupAudioEngine];
[[GameManager sharedGameManager] runSceneWithID:kMainMenuScene];
[Appirater appLaunched:YES];
 
}
// Supported orientations: Landscape. Customize it for your own needs
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
 
 
- (Level *)curLevel {
return [_levels objectAtIndex:_curLevelIndex];
}
 
- (void)applicationWillResignActive:(UIApplication *)application {
[[CCDirector sharedDirector] pause];
}
 
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[CCDirector sharedDirector] resume];
}
 
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[CCDirector sharedDirector] purgeCachedData];
[TestFlight passCheckpoint:@"appDidReceiveMemoryWarning"];
TFLog(@"memory warning");
}
 
-(void) applicationDidEnterBackground:(UIApplication*)application {
[[CCDirector sharedDirector] stopAnimation];
}
 
-(void) applicationWillEnterForeground:(UIApplication*)application {
[[CCDirector sharedDirector] startAnimation];
[Appirater appEnteredForeground:YES];
}
 
- (void)applicationWillTerminate:(UIApplication *)application {
CCDirector *director = [CCDirector sharedDirector];
[[director openGLView] removeFromSuperview];
[viewController release];
[window release];
[director end];
}
 
- (void)applicationSignificantTimeChange:(UIApplication *)application {
[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}
 
#pragma scene management to be relinquished
 
- (void)loadGameOverScene {
[_gameOverScene.layer.label setString:@"You Lose :["];
[_gameOverScene.layer reset];
[[CCDirector sharedDirector] replaceScene:_gameOverScene];
}
 
- (void)loadWinScene {
[_gameOverScene.layer.label setString:@"You Win!"];
[_gameOverScene.layer reset];
[[CCDirector sharedDirector] replaceScene:_gameOverScene];
}
 
- (void)loadNewLevelScene{
[_newLevelScene.layer reset];
[[CCDirector sharedDirector] replaceScene:_newLevelScene];
}
 
- (void)nextLevel {
//[_mainScene.layer reset];
//this previous method must call...OriginalScene reset method which:
// resets arrays
// cleans all sprites
// remove old backgrounds
// calls new background and sets it via delegate
// resets stats and timers
// resets scores
 
[[CCDirector sharedDirector] replaceScene:_mainScene];
}
 
- (void)restartGame {
_curLevelIndex = 0;
[self nextLevel];
}
 
- (void)levelComplete {
_curLevelIndex++;
if (_curLevelIndex >= [_levels count]) {
_curLevelIndex = 0;
[self loadWinScene];
} else {
[self loadNewLevelScene];
}
}
 
- (void)dealloc {
self.mainScene = nil;
self.gameOverScene = nil;
[[CCDirector sharedDirector] end];
[window release];
[super dealloc];
}
 
@end
MainMenuLayer.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
//
// MainMenuLayer.m
//
// Created by Marcio Valenzuela on 8/26/11.
// Copyright 2011 Personal. All rights reserved.
//
 
#import "MainMenuLayer.h"
#import "MZIAPHelper.h"
#import "IAPProduct.h"
#import <StoreKit/StoreKit.h>
#import "MZIAPHelper.h"
 
@interface MainMenuLayer()
 
-(void)displayMainMenu;
-(void)displaySceneSelection;
 
@end
 
@implementation MainMenuLayer {
//for IAPs
NSArray * _products;
NSNumberFormatter * _priceFormatter;
}
 
-(void)playScene:(CCMenuItemFont*)itemPassedIn {
if ([itemPassedIn tag] == 1) {
[[GameManager sharedGameManager] runSceneWithID:kFirstScene];
} else if ([itemPassedIn tag] == 2) {
[[GameManager sharedGameManager] runSceneWithID:kSecondScene];
} else if ([itemPassedIn tag] == 3) {
[[GameManager sharedGameManager] runSceneWithID:kThirdScene];
} else if ([itemPassedIn tag] == 4) {
[[GameManager sharedGameManager] runSceneWithID:kFourthScene];
} else if ([itemPassedIn tag] == 5) {
[[GameManager sharedGameManager] runSceneWithID:kCreditsScene];
} else {
CCLOG(@"Unexpected item. Tag was: %d", [itemPassedIn tag]);
}
}
 
-(void)buyLevel{
CCLOG(@"Show the Options screen");
[[GameManager sharedGameManager] runSceneWithID:kBuyLevelScene];
}
 
-(void)displayMainMenu {
CGSize screenSize = [CCDirector sharedDirector].winSize;
if (sceneSelectMenu != nil) {
[sceneSelectMenu removeFromParentAndCleanup:YES];
}
//RETINA TEST FIRST
if ([[UIScreen mainScreen]scale] == 1){
//NONRETINA CODE
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPAD NONRETINA
CCMenuItemImage *playGameButton = [CCMenuItemImage
itemFromNormalImage:@"mzhattrace-iPad.png"
selectedImage:@"mzhattrace-iPad.png"
disabledImage:nil
target:self
selector:@selector(displaySceneSelection)];
CCMenuItemImage *buyLevelsButton = [CCMenuItemImage
itemFromNormalImage:@"coin-iPad.png"
selectedImage:@"coin-iPad.png"
disabledImage:nil
target:self
selector:@selector(buyLevel)];
mainMenu = [CCMenu menuWithItems:playGameButton,buyLevelsButton,nil];
} else {
// iPHONE NON RETINA
CCMenuItemImage *playGameButton = [CCMenuItemImage
itemFromNormalImage:@"mzhattrace.png"
selectedImage:@"mzhattrace.png"
disabledImage:nil
target:self
selector:@selector(displaySceneSelection)];
CCMenuItemImage *buyLevelsButton = [CCMenuItemImage
itemFromNormalImage:@"coin.png"
selectedImage:@"coin.png"
disabledImage:nil
target:self
selector:@selector(buyLevel)];
mainMenu = [CCMenu menuWithItems:playGameButton,buyLevelsButton,nil];
}
 
} else if ([[UIScreen mainScreen]scale] == 2){
//RETINA CODE
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//iPAD RETINA
CCMenuItemImage *playGameButton = [CCMenuItemImage
itemFromNormalImage:@"mzhattrace-iPad-hd.png"
selectedImage:@"mzhattrace-iPad-hd.png"
disabledImage:nil
target:self
selector:@selector(displaySceneSelection)];
CCMenuItemImage *buyLevelsButton = [CCMenuItemImage
itemFromNormalImage:@"coin-iPad-hd.png"
selectedImage:@"coin-iPad-hd.png"
disabledImage:nil
target:self
selector:@selector(buyLevel)];
mainMenu = [CCMenu menuWithItems:playGameButton,buyLevelsButton,nil];
} else {
//iPHONE RETINA
CCMenuItemImage *playGameButton = [CCMenuItemImage
itemFromNormalImage:@"mzhattrace-hd.png"
selectedImage:@"mzhattrace-hd.png"
disabledImage:nil
target:self
selector:@selector(displaySceneSelection)];
CCMenuItemImage *buyLevelsButton = [CCMenuItemImage
itemFromNormalImage:@"coin-hd.png"
selectedImage:@"coin-hd.png"
disabledImage:nil
target:self
selector:@selector(buyLevel)];
mainMenu = [CCMenu menuWithItems:playGameButton,buyLevelsButton,nil];
}
 
}
//Continue with menu setup
 
[mainMenu alignItemsVerticallyWithPadding:screenSize.height * 0.059f];
[mainMenu setPosition:ccp(screenSize.width * 2,screenSize.height / 2)];
id moveAction = [CCMoveTo actionWithDuration:1.2f position:ccp(screenSize.width * 0.85f,
(screenSize.height / 2) )];
id moveEffect = [CCEaseIn actionWithAction:moveAction rate:1.0f];
[mainMenu runAction:moveEffect];
[self addChild:mainMenu z:0 tag:kMainMenuTagValue];
}
 
-(void)displaySceneSelection {
CGSize screenSize = [CCDirector sharedDirector].winSize;
if (mainMenu != nil) {
[mainMenu removeFromParentAndCleanup:YES];
}
//FIRST LABEL
CCLabelTTF *playScene1Label = [CCLabelTTF labelWithString:@"See Mel Eat" dimensions:CGSizeMake(175,50) alignment:NSTextAlignmentCenter fontName:@"StencilRegular" fontSize:32];
playScene1Label.color = ccc3(0, 0, 0);
//CCLabelBMFont *playScene1Label = [CCLabelBMFont labelWithString:@"See Mel Eat" fntFile:@"Arial.fnt"];
CCMenuItemLabel *playScene1 = [CCMenuItemLabel itemWithLabel:playScene1Label target:self selector:@selector(playScene:)];
[playScene1 setTag:1];
//SECOND LABEL
CCLabelTTF *playScene2Label = [CCLabelTTF labelWithString:@"See Mel Fly" dimensions:CGSizeMake(175,50) alignment:NSTextAlignmentCenter fontName:@"StencilRegular" fontSize:32];
playScene2Label.color = ccc3(0,0,0);
CCMenuItemLabel *playScene2 = [CCMenuItemLabel itemWithLabel:playScene2Label target:self selector:@selector(playScene:)];
[playScene2 setTag:2];
CCLabelTTF *playScene3Label = [CCLabelTTF labelWithString:@"See Mel Ride" dimensions:CGSizeMake(200,50) alignment:NSTextAlignmentCenter fontName:@"StencilRegular" fontSize:32];
playScene3Label.color = ccc3(0,0,0);
CCMenuItemLabel *playScene3 = [CCMenuItemLabel itemWithLabel:playScene3Label target:self selector:@selector(playScene:)];
[playScene3 setTag:3];
//CCLabelBMFont *playScene4Label = [CCLabelBMFont labelWithString:@"See Mel Vote" fntFile:@"Arial.fnt"];
CCLabelTTF *playScene4Label = [CCLabelTTF labelWithString:@"See Mel Vote" dimensions:CGSizeMake(200,50) alignment:NSTextAlignmentCenter fontName:@"StencilRegular" fontSize:32];
playScene4Label.color = ccc3(0,0,0);
CCMenuItemLabel *playScene4 = [CCMenuItemLabel itemWithLabel:playScene4Label target:self selector:@selector(playScene:)];
[playScene4 setTag:4];
//CCLabelBMFont *playScene5Label = [CCLabelBMFont labelWithString:@"Some other2" fntFile:@"Arial.fnt"];
CCLabelTTF *playScene5Label = [CCLabelTTF labelWithString:@"Credits" dimensions:CGSizeMake(100,50) alignment:NSTextAlignmentCenter fontName:@"StencilRegular" fontSize:32];
playScene5Label.color = ccc3(0,0,0);
CCMenuItemLabel *playScene5 = [CCMenuItemLabel itemWithLabel:playScene5Label target:self selector:@selector(playScene:)];
[playScene5 setTag:5];
//CCLabelBMFont *backButtonLabel = [CCLabelBMFont labelWithString:@"Back" fntFile:@"Arial.fnt"];
CCLabelTTF *backButtonLabel = [CCLabelTTF labelWithString:@"Back" dimensions:CGSizeMake(100,50) alignment:NSTextAlignmentCenter fontName:@"StencilRegular" fontSize:32];
backButtonLabel.color = ccc3(0,0,0);
CCMenuItemLabel *backButton = [CCMenuItemLabel itemWithLabel:backButtonLabel target:self selector:@selector(displayMainMenu)];
sceneSelectMenu = [CCMenu menuWithItems:playScene1, playScene2,playScene3,playScene4,playScene5,backButton,nil];
[sceneSelectMenu alignItemsVerticallyWithPadding:screenSize.height * 0.009f];
[sceneSelectMenu setPosition:ccp(screenSize.width * 2,screenSize.height / 2)];
id moveAction = [CCMoveTo actionWithDuration:0.5f position:ccp(screenSize.width * 0.75f,screenSize.height/2)];
id moveEffect = [CCEaseIn actionWithAction:moveAction rate:1.0f];
[sceneSelectMenu runAction:moveEffect];
[self addChild:sceneSelectMenu z:3 tag:kSceneMenuTagValue];
}
/**
# pragma mark - IAP methods
 
-(void)fetchIAPs{
_priceFormatter = [[NSNumberFormatter alloc] init];
[_priceFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[self reload];
//self.refreshControl = [[UIRefreshControl alloc] init];
//[self.refreshControl addTarget:self action:@selector(reload) forControlEvents:UIControlEventValueChanged];
//[self reload];
//[self.refreshControl beginRefreshing];
}
 
- (void)reload {
_products = nil;
//[self.tableView reloadData];
[[MZIAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
if (success) {
_products = products;
//[self.tableView reloadData];
}
//[self.refreshControl endRefreshing];
}];
NSLog(@"IAPProducts reloaded");
}
**/
# pragma mark - INIT
-(id)init {
if( (self=[super initWithColor:ccc4(255,255,255,255)]) ) {
}
return self;
}
 
 
-(void)onEnter {
[super onEnter];
[[GameManager sharedGameManager] playBackgroundTrack:BACKGROUND_TRACK_MAIN_MENU];
CGSize screenSize = [CCDirector sharedDirector].winSize;
 
//MAIN MENU
//RETINA TEST FIRST
if ([[UIScreen mainScreen]scale] == 1){
//NONRETINA CODE
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CCSprite *background = nil;
 
background = [CCSprite spriteWithFile:@"MainMenu-iPad.png"];
[background setPosition:ccp(screenSize.width/2,screenSize.height/2)];
[self addChild:background];
[self displayMainMenu];
} else {
CCSprite *background = nil;
 
background = [CCSprite spriteWithFile:@"MainMenu.png"];
[background setPosition:ccp(screenSize.width/2,screenSize.height/2)];
[self addChild:background];
[self displayMainMenu];
}
} else if ([[UIScreen mainScreen]scale] == 2){
//RETINA CODE
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CCSprite *background = nil;
 
background = [CCSprite spriteWithFile:@"MainMenu-iPad-hd.png"];
[background setPosition:ccp(screenSize.width/2,screenSize.height/2)];
[self addChild:background];
[self displayMainMenu];
} else {
CCSprite *background = nil;
 
background = [CCSprite spriteWithFile:@"MainMenu-hd.png"];
[background setPosition:ccp(screenSize.width/2,screenSize.height/2)];
[self addChild:background];
[self displayMainMenu];
}
}
//MAIN FIGURE
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CCSprite *character = [CCSprite spriteWithFile:@"melzelaya-iPad.png"];
[character setPosition:ccp(screenSize.width * 0.25f, screenSize.height * 0.25f)];
[character setScale:0.8];
[self addChild:character];
id rotateAction = [CCEaseElasticInOut actionWithAction:[CCRotateBy actionWithDuration:5.5f angle:360]];
id scaleUp = [CCScaleTo actionWithDuration:2.0f scale:1.5f];
id scaleDown = [CCScaleTo actionWithDuration:2.0f scale:0.5f];
[character runAction:[CCRepeatForever actionWithAction:[CCSequence actions:scaleUp,scaleDown,nil]]];
[character runAction:[CCRepeatForever actionWithAction:rotateAction]];
} else {
CCSprite *character = [CCSprite spriteWithFile:@"melzelaya.png"];
[character setPosition:ccp(screenSize.width * 0.25f, screenSize.height * 0.25f)];
[character setScale:0.8];
[self addChild:character];
id rotateAction = [CCEaseElasticInOut actionWithAction:[CCRotateBy actionWithDuration:5.5f angle:360]];
id scaleUp = [CCScaleTo actionWithDuration:2.0f scale:1.5f];
id scaleDown = [CCScaleTo actionWithDuration:2.0f scale:0.5f];
[character runAction:[CCRepeatForever actionWithAction:[CCSequence actions:scaleUp,scaleDown,nil]]];
[character runAction:[CCRepeatForever actionWithAction:rotateAction]];
}
//get products from store
//[self fetchIAPs];
 
}
 
@end
RootViewController.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
//
// RootViewController.m
// MZ
//
// Created by Marcio Valenzuela on 7/11/11.
// Copyright Personal 2011. All rights reserved.
//
 
//
// RootViewController + iAd
// If you want to support iAd, use this class as the controller of your iAd
//
 
#import "cocos2d.h"
 
#import "RootViewController.h"
#import "GameConfig.h"
 
@implementation RootViewController
 
 
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//
// There are 2 ways to support auto-rotation:
// - The OpenGL / cocos2d way
// - Faster, but doesn't rotate the UIKit objects
// - The ViewController way
// - A bit slower, but the UiKit objects are placed in the right place
//
#if GAME_AUTOROTATION==kGameAutorotationNone
//
// EAGLView won't be autorotated.
// Since this method should return YES in at least 1 orientation,
// we return YES only in the Portrait orientation
//
return ( interfaceOrientation == UIInterfaceOrientationPortrait );
#elif GAME_AUTOROTATION==kGameAutorotationCCDirector
//
// EAGLView will be rotated by cocos2d
//
// Sample: Autorotate only in landscape mode
//
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight];
} else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
}
// Since this method should return YES in at least 1 orientation,
// we return YES only in the Portrait orientation
return ( interfaceOrientation == UIInterfaceOrientationPortrait );
#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
//
// EAGLView will be rotated by the UIViewController
//
// Sample: Autorotate only in landscpe mode
//
// return YES for the supported orientations
return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
#else
#error Unknown value in GAME_AUTOROTATION
#endif // GAME_AUTOROTATION
// Shold not happen
return NO;
}
 
//
// This callback only will be called when GAME_AUTOROTATION == kGameAutorotationUIViewController
//
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//
// Assuming that the main window has the size of the screen
// BUG: This won't work if the EAGLView is not fullscreen
///
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect rect = CGRectZero;
 
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
rect = screenRect;
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
CCDirector *director = [CCDirector sharedDirector];
EAGLView *glView = [director openGLView];
float contentScaleFactor = [director contentScaleFactor];
if( contentScaleFactor != 1 ) {
rect.size.width *= contentScaleFactor;
rect.size.height *= contentScaleFactor;
}
glView.frame = rect;
}
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController
 
 
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
 
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
 
 
- (void)dealloc {
[super dealloc];
}
 
 
@end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.