Game Center windows don't scroll smoothly on OpenGL game - iphone

I have an shipped OpenGL game that I'm adding Game Center support to for a 1.1 rev. The game does not use a UIViewController and is coded as follows in the AppDelegate:
-(void)applicationDidFinishLaunching:(UIApplication *)application
{
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];
glView = [[EAGLView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[window addSubview:glView];
[window makeKeyAndVisible];
[glView performSelectorOnMainThread:#selector(mainGameLoop) withObject:nil waitUntilDone:NO];
}
Everything works great, but when Game Center brings up a window (GKAchievementViewController for example), the window animates on screen fine, but when I touch to scroll it, it doesn't have any of the momentum or bounce-back like normal windows. When I scroll the window past the top and bottom and it doesn't return with bounce back.
The window is fully functional, registers touches, it just behaves ugly.
I assume this is because some information is just not being sent down the pipe correctly because I don't have a UIViewController? I tried creating a UIViewController that sits in-between the UIWindow and the OpenGL UIView, but I get the same behavior.
This is how I'm creating the GKAchievementViewController dialog:
-(void)displayAchievements
{
achievmentsViewController = [[UIViewController alloc] init];
[glView addSubview:achievmentsViewController.view];
GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init];
if (achievements != nil)
{
achievements.achievementDelegate = self;
[achievmentsViewController presentModalViewController:achievements animated:YES];
}
[achievements release];
}
I don't use any of the UIKit in the game, all buttons, etc are done with straight OpenGL. I am also not using cocos2d or any other framworks.
In my main loop I am doing this:
while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.002, TRUE) == kCFRunLoopRunHandledSource);
Other than that, I am not making any regular calls into the OS.
Does anyone know what is going on? My game is ready to submit other than this problem and it's driving me crazy.

OK, I think I found the answer to this so I'm going to answer my own question in the hopes of saving others a lot of frustration.
The problem is CFRunLoopRunInMode. It doesn't play nice with UIKit. I switched to using CADisplayLink and all is well.
displayLink = [NSClassFromString(#"CADisplayLink") displayLinkWithTarget:self selector:#selector(mainLoopBody)];
[displayLink setFrameInterval:1];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
I've read that CADisplayLink is causing some games problems and there is a lot of debate about using it vs CFRunLoopRunInMode vs NSTimer. I'll use this for a while and see if it causes any problems, but windows scroll right again.
My one concern with CADisplayLink is if the frame rate starts to bog down. Under my old CFRunLoopRunInMode system, if the frames took longer the delta time reflected that. With CADisplayLink, I'm not sure if that is still the case.
Oh the fun of programming.

Related

admob click after return it wrong layout

I use cocos2d combined with AdMob , nomarl my app work great, but after hit the advertising and return the game, it wrong layout, my game is landscape!
Things changes after AdMob is involved. AdMob has a AD type "movie". After clicking the ad, it popups a full screen mode, and play a movie, and then back to the game. The game changes, originally, it is landscape mode, and after back from ad, it seems to be portrait mode, and the game is scaled, everything looks bad. I have tried to update the view controller / CCDirector after back from ad, but it doesn't work.
A workaround found: just rotate the device to the counterpart landscape mode and everything goes well again.
Just wonder if there is any way to prevent telling customers rotate it manually?
Have you encountered this before?
_viewController = [[UIViewController alloc] init];
_viewController.view = [[CCDirector sharedDirector] openGLView];
_admobView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
_admobView.adUnitID = ADMOB_PUBLISHER_ID;
_admobView.delegate = self;
GADRequest *request = [GADRequest request];
request.testing = YES;
[_admobView setRootViewController:_viewController];
[_admobView loadRequest:request];
[_viewController.view addSubview:_admobView];
I had a similar issue before, and the cause was that I created a new UIViewController and set it as rootViewController for AdMob view - just like what you are doing:
_viewController = [[UIViewController alloc] init];
_viewController.view = [[CCDirector sharedDirector] openGLView];
...
[_admobView setRootViewController:_viewController];
This leads to layout problems when returning from some kinds of advertisement. What I am doing now in my app is like:
adMobView.rootViewController = [RootViewController sharedInstance];
[[[CCDirector sharedDirector] openGLView] addSubview:adMobView];
where [RootViewController sharedInstance] is a class method which returns the only instance of RootViewController in the app. For details please refer to my answer to another question: https://stackoverflow.com/a/10222956/1241690
(For cocos2d 2.x, the second line should be:
[[[CCDirector sharedDirector] view] addSubview:adMobView];
)

ViewController doesn't show until I quit and relaunch app

I have just made my app universal by combining separate iPhone and iPad projects. Everything seems to be working but there is one major bug.
When the iPad app launches it just displays a black screen (presumably the window) and status bar. When I press the home button I suddenly see the SplitViewController as it disappears. When I open the app up again the SplitViewController is displayed.
I can't figure out why the controller only displays after I close and reopen the app. Any ideas?
(I don't have any idea what is causing this so if you need code samples from specific places let me know).
Thanks.
Edit:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Appirater appLaunched:YES];
// Registers this class as the delegate of the audio session.
[[AVAudioSession sharedInstance] setDelegate: self];
// Allow the app sound to continue to play when the screen is locked.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
justOpened = YES;
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
{deleted}
} else {
[self.window addSubview:self.splitViewController.view];
[self.window makeKeyAndVisible];
CGRect rect = CGRectMake(-2, self.window.frame.size.height-29, self.window.frame.size.width+2, 29);
imgBar = [[UIImageView alloc] initWithFrame:rect];
imgBar.contentMode = UIViewContentModeScaleToFill;
imgBar.image = [UIImage imageNamed:#"wood_btm.png"];
imgBar.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
self.splitViewController.showsMasterInPortrait = YES;
self.splitViewController.title = #"Exams";
self.splitViewController.splitPosition=280;
}
return YES;
}
Update:
By messing around with the window's background color I discovered that for some reason it was at the top of the view hierarchy. I then made the window's background color clear and I could see the SplitViewController. Strangely I can also interact with it. So essentially I have solved the problem by making the window clear. This is obviously not the ideal solution though so if anyone can think of a cause let me know.
[self.window addSubview:self.splitViewController.view];
[self.window makeKeyAndVisible];
return YES;
should be at the end of -applicationDidFinishLaunching:WithOptions: method.
By messing around with the window's background color I discovered that for some reason it was at the top of the view hierarchy. I then made the window's background color clear and I could see the SplitViewController. Strangely I can also interact with it. So essentially I have solved the problem by making the window clear.

game center not working on ipod/iphone

Hi all
Working on a universal build for an iphone/ipod/ipad app. I was trying to get game center working within this app. I now have the game center working on the ipad but for some reason it still wont work on the ipod/iphone and i have no idea why.
highScoreVC=[[UIViewController alloc] init];
GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];
if (leaderboardController != nil)
{
leaderboardController.leaderboardDelegate = self;
[[[CCDirector sharedDirector] openGLView] addSubview:highScoreVC.view];
[highScoreVC presentModalViewController:leaderboardController animated: YES];
}
this is my code for displaying the leaderboard and it does work everytime for ipad but as i said not for other devices. the code is still being called the same as for ipad so i have no clue why it is not displaying. any ideas why this would be? cheers
As far as I'm concerned, [GKLeaderboardViewController init] will return nil if the device is not authenticated with Game Center by that point. Are you sure you are correctly authenticated on the devices causing problems?
I suspect there may be some issues with how your UIKit items for Game Center are being displayed if they are working fine on the iPad but not iPhone/iPod. I experienced similar sounding issues when converting an app to universal.
If you create a new standalone iPhone/iPod project just for testing does game center display fine?
ok i got it working in the end.
cocos2d 0.99.5 has indeed made some changes that to how this works. the following code displays the leaderboard correctly
-(UIViewController*) getRootViewController{
return (UIViewController*)([(BubbleDreamsAppDelegate*) [[UIApplication sharedApplication] delegate] viewController]);
}
and then
highScoreVC = [self getRootViewController];
GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];
if (leaderboardController != nil)
{
leaderboardController.leaderboardDelegate = self;
[highScoreVC presentModalViewController:leaderboardController animated: YES];
}
hope this helps anyone that gets stuck with same problem

NSOperation for animation loop causes strange scrolling behaviour

I've created an animation loop which I run as an operation in order to keep the rest of my interface responsive. Whilst almost there, there is still one remaining issue. My UIScrollViews don't seem to be reliably picking up when a user touch ends. What this means is, for example, if a user drags down on a scroll view, when they lift their fingers the scrollview doesn't bounce back into place and the scrollbar remains visible. As if the finger hasn't left the screen. It takes another tap on the scrollview for it to snap to its correct position and the scrollbar to fade away...
Here's the loop I created in a subclassed NSOperation:
- (void)main
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
_displayLink = [[CADisplayLink displayLinkWithTarget: self selector: #selector(animationLoop:)] retain];
[_displayLink setFrameInterval: 1.0f];
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes];
while (![self isCancelled])
{
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
[runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
[loopPool drain];
}
[_displayLink invalidate];
[pool release];
}
It seems that whilst this operation is running things go a little weird...
DOes anyone have any idea what might be going on here, and even better how to fix it...
Thanks!
I do not understand why are you using a so complicated solution for your need which seems very classic.
In my opinion you'd better use the Quartz basic features to create your animation and let the OS choose when to compose for rendering. You can also use the setNeedsLayout and setNeedDisplay method to notify the rendering loop you've modify something instead of implementing your own loop.
Or maybe you have a specific need I did not understand.

iPhone - Splash Screen with progress bar

I tried to create a SplashView which display the Default.png in the background and a UIProgressBar in front. But the splash screen is not being updated...
Inside my view controller I load first the splash view with a parameter how many steps my initialisation has and then I start a second thread via NSTimer and after each initialisation step I tell the SplashView to display the new progress value.
All looks good in theory, but when running this app the progress bar is not being updated (the method of the splash screen receives the values, I can see it in the logs). I also tried to add usleep(10000); in between to give the view updates a bit time and also instead of using the progress bar I drew directly on the view and called [self setNeedsDisplay]; but all didn't work :/
What am I doing wrong?
Thanks for your help!
Tom
Here is some code:
SPLASHSCREEN:
- (id)initWithFrame:(CGRect)frame withStepCount:(int)stepCount {
if (self = [super initWithFrame:frame]) {
// Initialization code
background = [[UIImageView alloc] initWithFrame: [self bounds]];
[background setImage: [UIImage imageWithContentsOfFile: [NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], #"Default.png"]]];
[self addSubview: background];
progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
[progressView setFrame:CGRectMake(60.0f, 222.0f, 200.0f, 20.0f)];
[progressView setProgress: 0.0f];
stepValue = 1.0f / (float)stepCount;
[self addSubview:progressView];
}
return self;
}
- (void)tick {
value += stepValue;
[progressView setProgress: value];
}
VIEWCONTROLLER:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
splashView = [[SplashView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f) withStepCount:9];
[self setView: splashView];
NSTimer* delayTimer;
delayTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(finishInitialization) userInfo:nil repeats:NO];
}
return self;
}
- (void)finishInitialization {
// do some stuff, like allocation, opening a db, creating views, heavy stuff...
[splashView tick]; // this should update the progress bar...
// do some stuff, like allocation, opening a db, creating views, heavy stuff...
[splashView tick]; // this should update the progress bar...
// init done... set the right view and release the SplashView
}
As mentioned in another answer, for some finite amount of time, as your app is being launched, Default.png is displayed and you have no control over it. However, if in your AppDelegate, you create a new view that displays the same Default.png, you can create a seamless transition from the original Default.png to a view that you can add a progress bar to.
Now, presumably, you have created a view or similar and you are updating a progress bar every so often in order to give the user some feedback. The challenge here is that your view is only drawn when it gets called to do a drawRect. If, however, you go from AppDelegate to some initialization code to a viewcontroller's viewDidLoad, without the run loop getting a chance to figure out which views need to have drawRect called on, then your view will never display its status bar.
Therefore in order to accomplish what you want, you have to either make sure that drawRect gets called, such as by pushing off a lot of your initialization code into other threads or timer tasks, or you can force the drawing by calling drawRect yourself, after setting up contexts and such.
If you go with the background tasks, then make sure your initialization code is thread-safe.
Default.png is just a graphic, a static image shown while the application is launching. If you want to show further progress, you'll have to show everything at the applicationDidLaunch phase. Show your modal "Splash Screen" there first (Create a view controller, add its view as a subview of your main window) and dismiss it when you are done whatever additional loading you needed to do.
Also, you need to do update your progress bar in a seperate thread. Updating your GUI in the same thread where a lot of business is going on is (in my opinion, but I could be wrong) a bad idea.
The main thread is, as far as I know, the only one that can safely do GUI things, and its event loop (that is, the main application thread's) is the one that does the actual displaying after you've called -setNeedsDisplay. Spawn a new thread to do your loading, and update the progress on the main thread.