Created
February 27, 2013 19:33
-
-
Save quique123/5050916 to your computer and use it in GitHub Desktop.
Cocos2d Image Size Bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment