When I launch my game I want the screen to show a loading image while loading resources in the background. I do this by loading a simple UIImageView component and adding a “spinner” to it, to give the user feedback, that the device is loading something in the background.
While this image is showing I load all my images and textures and set up my OpenGL view and let it render.
When the 2nd frame gets rendered I want to hide the ImageView and only show the OpenGL view. I don’t want the OpenGL view to show on the first frame because it takes awfully long to render.
However, I have some problems with loading all my resources and setting up the DisplayLink for the OpenGL view to enter the render loop in a new thread and then showing the OpenGL view when loading is finished. The render loop doesn’t seem to start.
Here is the loadView method of my View Controller
- (void)loadView
{
CGRect mainScreenFrame = [[UIScreen mainScreen] applicationFrame];
// Set up the image view
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"Startscreen" ofType:@"png"]];
_imageView = [[UIImageView alloc] initWithFrame:mainScreenFrame];
_imageView.image = img;
// Set up the spinner
_spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[_spinner setCenter:CGPointMake(mainScreenFrame.size.height/3.0*1.85, mainScreenFrame.size.width/10.0*7.55)];
[_imageView addSubview:_spinner];
[_spinner startAnimating];
// Show the loading image
self.view = _imageView;
/* Load resources in a new thread -- this shows the spinner during loading */
[NSThread detachNewThreadSelector:@selector(loadGLView) toTarget:self withObject:nil];
}
loadGLView only does the following and initializes the OpenGL View and starts the loading process.
_glView = [[JungleOpenGLView alloc] initWithFrame:mainScreenFrame];
This is how I set up the DisplayLink in the OpenGL View.
CADisplayLink* displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(setupRender:)];
[displayLink setFrameInterval:2];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
When the 2nd frame gets rendered I send a notification and the ViewController then sets
self.view = _glView;
I found my mistake.
Instead of creating a new thread for the loading method like this:
I used:
That does the trick. Good read on this subject (creating threads) can be found here: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html