Online Book Reader

Home Category

iPhone Game Development - Chris Craft [127]

By Root 1612 0


Now we need a context that we can use to render to the device we just opened. Once we get the context, we need to tell OpenAL to use it by calling alcMakeContextCurrent:

newContext = alcCreateContext(newDevice, 0);

if (newContext == NULL)

return false;

alcMakeContextCurrent(newContext);

Now we need a buffer object to host our sounds and we need a source object to reference our sounds:

ALuint buffer;

alGenBuffers(1, &buffer);

if((error = alGetError()) != AL_NO_ERROR)

return false;

alGenSources(1, &source);

if(alGetError() != AL_NO_ERROR)

return false;

With buffer and source objects initialized, we need to load our sounds from the resource bundle into the buffer:

// clear out existing errors

alGetError();

error = AL_NO_ERROR;

ALenum format;

ALsizei size;

ALsizei freq;

void *data;

// reference the main bundle

NSBundle *bundle = [NSBundle mainBundle];

// load the audio from a resource file

CFURLRef fileURL = (CFURLRef)[[NSURL fileURLWithPath:[bundle

pathForResource:@”sound” ofType:@”caf”]] retain];

if (fileURL) {

data = MyGetOpenALAudioData(fileURL, &size, &format, &freq);

CFRelease(fileURL);

if((error = alGetError()) != AL_NO_ERROR)

return false;

// use the static buffer data API

alBufferDataStaticProc(buffer, format, data, size, freq);

if((error = alGetError()) != AL_NO_ERROR)

return false;

}

else {

data = NULL;

return false

}

The file is loaded into the buffer and now you can set engine options, the position for the sound source, and the distance from the listener:

error = AL_NO_ERROR;

alGetError();

// turn on looping

alSourcei(source, AL_LOOPING, AL_TRUE);

// set source location

float sourcePosAL[] = {0.5f, 0.5f, kDefaultDistance};

alSourcefv(source, AL_POSITION, sourcePosAL);

// set distance from listener

alSourcef(source, AL_REFERENCE_DISTANCE, 50.0f);

Finally, you need to bind the source to the buffer. The source is what will be referenced to play the sound when needed:

// attach source to the buffer

alSourcei(source, AL_BUFFER, buffer);

if((error = alGetError()) != AL_NO_ERROR)

return false;

You are done setting up OpenAL; now all you need to do is fire off your sounds when you need them. Thankfully, this only takes one line of code—well, a few more if you count the fact that you should check for errors—but nothing like the number of lines it took to get here.

To play the sound, you need to call alSourcePlay with a source reference:

alSourcePlay(source);

ALenum error;

if((error = alGetError()) != AL_NO_ERROR) {

// handle error

}

To stop a sound from playing, you call alSourceStop:

alSourceStop(source);

ALenum error;

if((error = alGetError()) != AL_NO_ERROR) {

// handle error

}

That's it—you are all set to add OpenAL and great sound to your games!

Looking into Video

As your applications begin to grow and mature, you may need to add video, cut scenes, or display an opening story before a user plays the game for the first time. Fortunately, your iPhone was designed from the beginning to display video, and the code to do this is really straightforward.

LavaFlow is another one of our examples from http://appsamuckcom. Download LavaFlow and you will find an example of playing a short video that turns your iPhone into a virtual lava lamp (Figure 9.12).

FIGURE 9.12

LavaFlow illustrates how to play video files on the iPhone.


In the awakeFromNib message of LavaFlow, you see all of the code that initializes and plays the movie. The actual movie is embedded in the application. The first step is to retrieve the movie from the resource bundle, so you call the NSURL needed to pass to the movie player you will start next:

NSBundle *bundle = [NSBundle mainBundle];

NSString *moviePath = [bundle pathForResource:@”lavaFlow” ofType:@”m4v”];

NSURL *movieURL;

if (moviePath) {

movieURL = [NSURL fileURLWithPath:moviePath];

}

if (movieURL == nil)

return;

If the URL is retrieved successfully, the application

Return Main Page Previous Page Next Page

®Online Book Reader