Monday, March 26, 2012

Part 1: The template

This post is mostly to get you up and running (almost) from scratch. Remember that I'm using iOS 5. This means automatic reference counting and storyboards, which are both amazing features that make your life easier. After this post, you'll have a basic template to use when you want to make OpenGL applications.

Start by creating a Single View Application. This is mostly because the OpenGL Game template comes with a bunch of crap that we don't need (like code to make two cubes float around...), but that the Single View Application template comes with a storyboard and some basic View lifecycle methods that are somewhat useful.

We're gonna do everything in portrait mode, so change the Supported Device Orientations.

You can also make the application full screen by adding the following key in the Info panel, which is usually what I do.

Don't forget to add the two most important frameworks for these tutorials: OpenglES and GLKIT.

When it's done, go in the Storyboard, delete the current Controller/View and drop a GLKit View Controller instead. Open the identity inspector and change the class of the controller to be the one in your application.

When this is done, go in your Controller's .h file, import GLKit, and change its parent so that it inherits GLKViewController instead.

This is what I like my Controller's .m file to look like when I start from scratch. It's clean and you can easily navigate through the code.

You have the viewDidLoad method which creates creates an EAGLContext for OpenGL ES 2.0, and also sets it as the current context. Then you give that context to the view to initialize it. When that's done, the view will will start calling its delegate's (the controller) drawInRect so that you can update the display with whatever you want to draw.

update will also be called so that you can update your (game's) model/objects. This is where you will change an object's position for instance, and the next time drawInRect is called, you will use that position to do the actual drawing of the object. So there you go! When you build and run the application, this is what you should get:

No comments: