iPhone Game Development - Chris Craft [129]
Here is what you need to do to get started:
1. Launch Xcode.
2. Choose File⇒New Project.
3. Inside the New Project dialog box in the iPhone OS section, choose Application⇒OpenGL ES Application.
4. Name the project and click Save. We recommend you name yours GLSquare to stay consistent with the naming in this study.
5. Click Build and Go to execute your new project in the iPhone Simulator (Figure 9.14).
Figure 9.14
This is the result you will see without adding any lines of code to the OpenGL ES Application Template.
Looking at OpenGL code for the first time can feel pretty alien to first-timers. When you dig into the details, do you know what is going on here? Look back at the GLSquare project and open the Classes folder to view its contents, as shown in Figure 9.15.
FIGURE 9.15
All of the noteworthy code generated by the OpenGL ES Application Template can be found in the Classes folder.
In the Classes folder, you will find the following files:
EAGLView.h. This is the header file for the class EAGLView. This is our major class of interest as it is responsible for rendering the 3-D scene.
EAGLView.m. This is the implementation file for the class EAGLView. This is the class that implements the drawView method that renders the scene.
GLSquareAppDelegate.h. This is the header file for the class GLSquareAppDelegate. This header file defines properties for the UIWindow and the EAGLView.
GLSquareAppDelegate.m. This is the implementation file for the class GLSquareAppDelegate. This class responds to UIApplicationDelegate events in order to set the animationInterval of the EAGLView.
Open up the file EAGLView.m and take a minute to read through the code. You might see a lot of talk about buffers and other technical jargon that you may be unfamiliar with. It's important to understand at least a little about these items before the rest of it will begin to make sense to you.
Here's a short list of definitions that may help as you dig deeper into this template:
Frame buffer. The frame buffer is the last stop in the graphics pipeline. This is the buffer that everything will be rendered in.
Depth buffer. Enabling the depth buffer triggers drawing operations to check the buffer before placing pixels onto the screen. The buffer stores Z order information with each pixel to ensure the correct pixels rise to the top.
Color buffer. The color buffer is what you see on the screen. It stores a color value for each pixel on the screen.
Render buffer. A render buffer is a buffer used for off-screen rendering.
Rasterization. This is the process of converting shapes from a vector graphics format into pixels for screen display.
Now look into EAGLView.m again. You will see the following methods that are responsible for setting up and rendering the 3-D scene:
initWithCoder. You have probably seen or will see initWithCoder several times in your work as an iPhone developer. This method is called whenever the view is created indirectly as part of an Interface Builder XIB file, as in this case. In this particular class, initWithCoder is responsible for opening the OpenGL context (EAGLContext).
layoutSubviews. The method layoutSubViews is called whenever something occurs on the device that requires that subviews recalculate their positioning. This can happen when views are moved around on the screen. When layoutSubviews is triggered, it will call destroyFramebuffer, createFrameBuffer, and drawView, respectively.
createFramebuffer. As its name implies, the method createFrameBuffer creates the frame buffer. In addition, a render buffer is created and the new width and height of the render buffer are calculated.
destroyFramebuffer. This method deletes the frame buffer and the render buffer.
drawView. The method drawView is responsible for the action you