Devin
Devin

Reputation: 900

EAGLContext currentContext is nil after a modal view is unwinded

This isn't really a problem since I know how to solve it, but something I'm just curious about. Below I have a screenshot of my storyboard. It's fairly simple, it initially loads a main menu, which has a button that loads a UIView via a modal segue. That UIView includes a container view controller which has a Navigation Controller whose root view controller is a GLKViewController. There are two ways to get back to the main menu. First, you can hit the pause buttons which pushes the pause view controller on top of the OpenGL view controller, and then hit the return to menu button there, triggering an unwind segue back to the main menu. Alternatively, you can wait for a timer to run down to zero and for your game to end, at which point the game over view controller is displayed via a segue, and then you can unwind back to the main menu from there. A screenshot of this storyboard is linked below:

https://i.sstatic.net/2LKVQ.png

You might notice that I have two segues hooked up to the game over view controller. Originally, I used a modal segue to display that view controller. Everything worked well, except when I unwound to the main menu and then attempted to start a new game, the [EAGLContext currentContext] was equal to nil, which I loaded in my GLKViewController subclass' viewDidLoad in the following manner.

if (![EAGLContext currentContext])
{
    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    /* Reload all my buffers/textures/shaders/etc here */

}
else
{
    self.context = [EAGLContext currentContext];
}

if (!self.context) 
{
    NSLog(@"Failed to create ES context");
}

GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
view.drawableMultisample = GLKViewDrawableMultisample4X;

[EAGLContext setCurrentContext:self.context];

Not a huge deal, at first my embedded GLKViewController just drew a black screen, but when I figured out what was happened, this just meant that I had to reload all my buffers/textures/shaders/etc that I had previously loaded into the old context and had cached for future use. However, if I unwound from a push segue, either via the pause menu or after switching to presenting the game over view via a push segue as well, the [EAGLContext currentContext] was not nil but remained equal to the context that I'd initialized the first time I'd loaded a GLKView. Therefore, I could use all my cached buffers/textures/shaders/etc and did not have to reload them.

Doing some reading, I'm not sure why unwinding from a modal segue erases the current context while doing it from a push segue does not. My understanding from doing some reading is that each thread maintains its own EAGLContext, and regardless of which way I set up my view controller segues, I always remain on the main thread. I'm curious if anyone has an idea of why this is happening.

Upvotes: 2

Views: 1344

Answers (0)

Related Questions