Implementation

We’ll begin by creating a class named RingsState that inherits from GLESGameState3D. Since we plan to receive accelerometer input, we will also conform to the UIAccelerometerDelegate protocol and add an array to store the input. We can start rendering by using a Sprite3D to represent our space ship:

//RingsState.h
@interface RingsState : GLESGameState3D <UIAccelerometerDelegate> {
    UIAccelerationValue        accel[3];
    Sprite3D *ship;
}

Remember that we’ll be using PVRT code, which requires C++ compatibility, so our implementation file will have to be in .mm format.

First, we need to hook into the UIAccelerometer in our initWithFrame: function:

//RingsState.mm
-(id) initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager;
{
    [super initWithFrame:frame andManager:pManager];
    [self setupWorld];
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 100.0)];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    return self;
}

Next, we initialize the ship in the setupWorld: function. If you are using the example code, the ship.pod and littlespaceship02DiffuseMap.pvr files should already have been added to the project in the same manner we added textures in the preceding chapter:

- (void) setupWorld {
    ship = [[Sprite3D spriteWithModel:
                     [Model modelFromFile:@"ship.pod"]] retain];
    [ship setScale:4.0f];
}

Because we called retain on the ship, we will need to let go of it in our dealloc function:

-(void) dealloc {
    [ship release];
    [super dealloc];
}

Now we can modify our ...

Get iPhone Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.