18
Sound in SDL Andrew Williams [email protected] .uk http://

Games at Bolton Sound in SDL Andrew Williams [email protected] adw1

  • View
    214

  • Download
    1

Embed Size (px)

Citation preview

Sound in SDL

Andrew Williams

[email protected]

http://www.bolton.ac.uk/staff/adw1

Sound in SDL

There are two ways of doing sound:1. SDL has some basic sound features

built into it• They are a bit complex to use• They don’t support multiple channels• There is a better alternative

Sound in SDL

There are two ways of doing sound:2. SDL_mixer

• Far more sophisticated• Very easy to use for basic sound features• Music • Sound effects

You should use SDL_mixer for your assignments

SDL_mixer

A separate downloadhttp://www.libsdl.org/projects/SDL_mixer/

Must be installed independently– Already available on the PCs in the lab

and on the PS2s– Must also set up the project to use the

SDL_mixer library

Downloading SDL_mixer

http://www.libsdl.org/projects/SDL_mixer/

Installation (SDL_mixer.h)

Setting directories – This dialogue is from Tools | Options

It’s the same deal for the libs

Configuring your project

Get this dialogue from the Project menu

Don’t forget the DLLs

Put them in the same folder as your source

Playing Music

1. Open the audio device– We have to specify some parameters

2. Load in the music– We just need to supply the file name

3. Play the music– How many times do we want to hear

it?

4. Stop the music– Only if necessary

1. Open the Audio Device

You only do this once per program/* We're going to be requesting certain things from our audio device, so we set them up beforehand */int rate = MIX_DEFAULT_FREQUENCY;Uint16 format = AUDIO_S16; /* 16-bit stereo */int channels = 2; // 2 for stereo, 1 for monoint buffers = 4096;/* Mix_OpenAudio takes as its parameters the audio format we'd _like_ to have. */if(Mix_OpenAudio(rate, format, channels, buffers)) { printf("Unable to open audio!\n"); exit(1);}

2. Load in the Music

Note that you want .ogg format– Find some music here

(http://clickass.org/music/), or convert your own with Audacity

Mix_Music *music = NULL;music = Mix_LoadMUS(“hyporia.ogg");if(!music) {

printf("Error loading music: %s\n", Mix_GetError());exit(1);

}

Play the Music

We have to decide how many times we want to hear the music

/* This begins playing the music - the first argument is a pointer to Mix_Music structure, and the second is how many times you want it to loop (use -1 for infinite, and 0 to have it just play once) */Mix_PlayMusic(music, -1);

Stop the Music

Very simple:

Directed study:– Look at: Mix_HookMusicFinished(...);

/* Stop the music from playing */ Mix_HaltMusic();

Playing Sound-Effects

1. Open the audio device – See previous slides; you only do it once

per program

2. Load in the .wav file3. Play the sound

Load in the .wav File

Very simple of course:

/* We're going to pre-load the sound effects that we need here*/Mix_Chunk *phaser = NULL;phaser = Mix_LoadWAV("phaser.wav");

Play the Sound Effect

This shows it integrated into the program:

if(firing) {blast = laserblasts->allocate_a_bullet();if(blast != NULL) {

Mix_PlayChannel(-1, phaser, 0);blast->set_world_position(blahX, blahY);blast->set_velocities(0.0f, -5.0f);blast->set_accelerations(0.0f, -0.1f);blast->set_auto_accelerate(20);blast->set_auto_move(20);

}}