Alpha Blending With Apple UI on iPhone - iphone

I am trying to place an OpenGL view on top of a standard UIView with a UIImageView. When I draw anything into the OpenGL view, I see slight alpha blending issues that result in the white lines seen below:
CCDirector *director = [CCDirector sharedDirector];
EAGLView *glView = [EAGLView viewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
pixelFormat:kEAGLColorFormatRGBA8 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
[glView setMultipleTouchEnabled:YES];
[director setOpenGLView:glView];
if(![director enableRetinaDisplay:YES]) NSLog(#"Retina Display Not supported");
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setAnimationInterval:1.0/60];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[[CCDirector sharedDirector] runWithScene:[PPAvatarEditorScene scene]];
[[CCDirector sharedDirector] resume];
[glView setOpaque:NO];
[avatarImage addSubview:glView];
I am using Cocos2D to draw into the OpenGL view. Is there an issue with the Alpha Pixel Format or Texture Format?

Try setting the glClearColor to black:
glClearColor(0, 0, 0, 0);

Related

MPMoviePlayerController in background doesn't leave page when we change scene

I am using MPMoviePlayerController to play a video in the background of a CCMenu. So I have a video playing behind some buttons. When I switch views/scenes, I want the moviePlayer to leave the scene the same as the buttons/images on that layer/scene.
The code to play the movie in the background is as follows:
-(void)playMainMenuVideo{
NSString *path=[[NSBundle mainBundle] pathForResource:#"MYMMainMenu" ofType:#"mov"];
MPMoviePlayerViewController * player=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
player.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[player view] setFrame:[[[CCDirector sharedDirector]view] bounds]];
player.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[[[CCDirector sharedDirector]view] addSubview:[player view]];
[[[CCDirector sharedDirector]view] sendSubviewToBack:player.view];
UIView* glView = [CCDirector sharedDirector].view; // attention
[glView.superview insertSubview:player.view atIndex:0]; // attention
glView.opaque = NO; // attention
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // attention
}
This works perfect, In the app delegate I also changed
CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds]
pixelFormat:kEAGLColorFormatRGB565 // To kEAGLColorFormatRGBA8
Lastly I want to do a page turn like so:
[[CCDirector sharedDirector] replaceScene: [CCTransitionPageTurn transitionWithDuration:1.0 scene:[FirstPageViewController scene]]];
But only the sprites will leave the scene, the video stays playing in the background. How can I fix this? Thanks in advance
I figured it out.
1) Setup notification for when video is done playing.
2) When its done playing, place an image of the last frame of the video, over the video.
3) Then set these properties back to original
UIView* glView = [CCDirector sharedDirector].view; // attention
glView.opaque = YES; // attention
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
4)Remove MPMoviePlayerController
Happy Coding everyone :)

CCSprite doesn't display on iPhone 4

I have a an app using cocos2D for a mini-game.
When I push a button, I launch a cocos2D scene with the game.
I try to load just a image in background (bg.png) in my cocos2D scene. I'm using sprites.
It works on iPhone 5 and iPhone 4S (iOS 6), but it doesn't work on the iPhone 4.
On the iPhone 4 I have a black screen. I don't understand why.
sprites-hd.png does 3762 × 1252, 1,4 Mo. Is it too big for the iPhone 4 ?
In my GameViewController_iPhone.m :
-(IBAction) playGame {
CCDirector *director = [CCDirector sharedDirector];
CCGLView *glView = [CCGLView viewWithFrame:[[ISAMAppDelegate_iPhone sharedISAMAppDelegate].window bounds]
pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
// attach the openglView to the director
[director setView:glView];
if( ! [director enableRetinaDisplay:YES] )
MyLog(#"Retina Display Not supported");
[[CCDirector sharedDirector] setAnimationInterval:1.0/60];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[self.view addSubview:glView];
[[CCDirector sharedDirector] runWithScene:[Game sceneWithGameViewController:self]];
}
In my Game.m :
+ (CCScene *)sceneWithGameViewController:(GameViewController_iPhone*) gvc
{
CCScene *game = [CCScene node];
Game *layer = [[Game alloc] initWithGameViewController:gvc];
[game addChild:layer];
return game;
}
- (id) initWithGameViewController:(GameViewController_iPhone*) gvc {
if(![super init]) return nil;
if((self = [super init])) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"sprites.plist"];
CCSpriteBatchNode* batchNode = [CCSpriteBatchNode batchNodeWithFile:#"sprites.png" capacity:50];
[self addChild:batchNode];
CCSprite *someSprite = [CCSprite spriteWithSpriteFrameName:#"bg.png"];
[batchNode addChild:someSprite];
}
}
You can't load textures larger than 2048x2048 in iPhone 4, so you may split your current sprite atlas.

cocos2d CCScene background orientation

I'm trying to add a background image for my scene using the following code:
CCSprite* background = [CCSprite spriteWithFile:#"background-hd.png"];
background.tag = 1;
background.anchorPoint = CGPointMake(0,0);
[self addChild:background];
My apps orientation is landscape and the image's orientation is landscape as well, however I get the image orientated in portrait:
I think this is the solution with Layer which set the scene with orientation...
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
StartLayer *layer = [StartLayer node];
CCSprite *background = [CCSprite spriteWithFile:#"background-hd.png"];
background.anchorPoint = ccp(0,0);
[layer addChild:background z:-1];
[scene addChild: layer];
return scene;
}
See this link for More information Cocos2d-Scenes-and-Layers....
Hope this helpful to you...

Game Scene crashes when I restart the game

I am developing a game using cocos2D game engine for IOS6. I use the following code for starting the game for the first time :
-(void)addGameScene
{
CCDirector *director = [CCDirector sharedDirector];
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeMainLoop];
else {
[CCDirector setDirectorType:kCCDirectorTypeDisplayLink];
}
// Init the View Controller
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
if(!glView)
glView = [[EAGLView alloc] initWithFrame:[window bounds]];
[director setOpenGLView:glView];
[director setOpenGLView:glView];
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
[viewController setView:glView];
[window setRootViewController:viewController];
[window addSubview: viewController.view];
[window makeKeyAndVisible];
[[CCDirector sharedDirector] runWithScene: [HelloWorldLayer scene]];
}
When i go out of the game scene i do not end the cocos2D game engine, instead i just stop the animations and hide the glView. I use this code.
[[CCDirector sharedDirector] stopAnimation];
CATransition *animation3 = [CATransition animation];
[animation3 setDuration:0.5f];
[animation3 setType:kCATransitionFade];
[animation3 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[[CCDirector sharedDirector].openGLView layer] addAnimation:animation3 forKey:#"SwitchToView"];
[[CCDirector sharedDirector].openGLView setHidden:YES];
When i again start the game to be played, I use this code :
[[CCDirector sharedDirector].openGLView setHidden:NO];
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];
[[CCDirector sharedDirector] startAnimation];
It works fine.
But when I start the game for the first time, and the i go back from the game scene, and then I exit the application by pressing the home button of device, then I again start the application, and then I restart the game, I get a crash in this scenario.
The console prints :
2012-12-12 15:53:24.847 CasinoApp[2856:12203] -[HelloWorldLayer init] : Screen width 480.00 screen height 320.00
2012-12-12 15:53:24.848 CasinoApp[2856:12203] 10.000000
2012-12-12 15:53:24.849 CasinoApp[2856:12203] -[CCTexture2D(Image) initWithImage:resolutionType:] : cocos2d: CCTexture2D. Can't create Texture. UIImage is nil
2012-12-12 15:53:24.850 CasinoApp[2856:12203] -[CCTextureCache addImage:] : cocos2d: Couldn't add image:lines in CCTextureCache
2012-12-12 15:53:24.850 CasinoApp[2856:12203] *** Assertion failure in -[CCDirectorTimer startAnimation], /Users/rakesh/Desktop/Ryan/Code/CasinoApp/CasinoApp/libs/cocos2d/Platforms/iOS/CCDirectorIOS.m:498
Can Anyone tell me the reason for this. It would be appreciated.. Thanks.
When you can the scene method of a CCScene class, the class gets reinitialised and the whole scene is constructed again. therefore, you do not have to start the animations again if you are replacing the scene with the same scene, also it you are hiding the glView you can try pausing the director and resume it again before you unhide the glView.
[[CCDirector shareDirector]pause];
[[CCDirector sharedDirector].openGLView setHidden:YES];
and
[[CCDirector shareDirector]resume];
[[CCDirector sharedDirector].openGLView setHidden:YES];
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];
Try
[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] pause];
and
[[CCDirector sharedDirector] stopAnimation]; // call this to make sure you don't start a second display link
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
Hope this helps!

White screen to load cocos2d scene

I am new in cocos 2d game development and developing a game where I need to reload scenes from view controller many times.For this i remove the scene and run it again.After 2 or more times the scene loads but white screen occurs and in console the error "OpenGL error 0x0506 in -[EAGLView swapBuffers]" is shows.
here is my code to add the scene-
if ([[CCDirector sharedDirector] runningScene] == NULL)
{
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:YES
numberOfSamples:4];
[director setOpenGLView:glView];
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setAnimationInterval:1.0/60];
[window addSubview:glView];
[[CCDirector sharedDirector] runWithScene: [HelloWorldLayer node]];
}
and code for remove the scene-
[[CCDirector sharedDirector].openGLView removeFromSuperview];
[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] end];
[[CCDirector sharedDirector] release];
Please help me I am not getting where is the problem.
Thanks.
Two things of note:
don't release CCDirector! All you need to do is call stopAnimation and later startAnimation
don't remove the openGLView from its super view. Instead, just hide it: [CCDirector sharedDirector].openGLView hidden:YES]