Beginning OpenGL ES 2.0 with GLKit Part 1

A tutorial to get you up-to-speed with the basics of using OpenGL with GLKit, even if you have no experience whatsoever. By Ray Wenderlich.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

GLKViewController and Pausing

Now that we're all nicely set up in a custom GLKViewController subclass, let's play around with one of the neat features of GLKViewController - pausing!

To see how it works, just add the following to the bottom of HelloGLKitViewController.m:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.paused = !self.paused;
}

Compile and run the app, and now whenever you tap the animation stops! Behind the scenes, GLKViewController stops calling your update method and your draw method. This is a really handy way to implement a pause button in your game.

In addition to that, GLKViewController has a pauseOnWillResignActive property that is by default set to YES. This means when the user hits the home button or receives an interruption such as a phone call, your game will be automatically paused! Similarly, it has a resumeOnDidBecomeActive property that is by default set to YES, which means when the user comes back to your app, it will automatically unpause. Handy, that!

We've covered almost every property of GLKViewController by now, except for the extra time info properties I discussed earlier:

  • timeSinceLastDraw gives you the elapsed time since the last call to the draw method. Note this might be different than timeSinceLastUpdate, since your update method takes time! :]
  • timeSinceFirstResume gives you the elapsed time since the first time GLKViewController resumed sending updates. This often means the time since your app launched, if your GLKViewController is the first thing that shows up.
  • timeSinceLastResume gives you the elapsed time since the last time GLKViewController resumed sending updates. This often means the last time your game was unpaused.

Let's add some code to try these out. Add the following code to the top of touchesBegan:

NSLog(@"timeSinceLastUpdate: %f", self.timeSinceLastUpdate);
NSLog(@"timeSinceLastDraw: %f", self.timeSinceLastDraw);
NSLog(@"timeSinceFirstResume: %f", self.timeSinceFirstResume);
NSLog(@"timeSinceLastResume: %f", self.timeSinceLastResume);

Play around with it so you're familiar with how they work. As you can see, these can be pretty convenient!

Where To Go From Here?

At this point, you know the basics of getting a project set up and running with GLKView and GLKViewController, and what they can do for you.

Now you're ready for part two of the tutorial, where we'll finally start drawing something to the screen with OpenGL commands and the new GLKBaseEffect class!

This "Beginning OpenGL ES 2.0 with GLKit" series is one of the chapters in our new iOS 5 By Tutorials book. If you like what you see here, check out the book - there's an entire second chapter on Intermediate OpenGL ES 2.0 with GLKit, above and beyond what we're posting for free here! :]

If you have any questions or comments on this tutorial or on OpenGL ES 2.0 or GLKit in general, please join the forum discussion below!