Presentation is loading. Please wait.

Presentation is loading. Please wait.

Games at Bolton Sound in SDL Andrew Williams adw1.

Similar presentations


Presentation on theme: "Games at Bolton Sound in SDL Andrew Williams adw1."— Presentation transcript:

1 Games at Bolton Sound in SDL Andrew Williams A.Williams@bolton.ac.uk http://www.bolton.ac.uk/staff/ adw1

2 Games at Bolton 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

3 Games at Bolton 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

4 Games at Bolton SDL_mixer  A separate download http://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

5 Games at Bolton Downloading SDL_mixer http://www.libsdl.org/projects/SDL_mixer/

6 Games at Bolton Installation (SDL_mixer.h)

7 Setting directories – This dialogue is from Tools | Options

8 It’s the same deal for the libs

9 Games at Bolton Configuring your project Get this dialogue from the Project menu

10 Games at Bolton Don’t forget the DLLs  Put them in the same folder as your source

11 Games at Bolton 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

12 Games at Bolton 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 mono int 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); }

13 Games at Bolton 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); }

14 Games at Bolton 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);

15 Games at Bolton Stop the Music  Very simple:  Directed study: –Look at: Mix_HookMusicFinished(...); /* Stop the music from playing */ Mix_HaltMusic();

16 Games at Bolton Playing Sound-Effects 1.Open the audio device –See previous slides; you only do it once per program 2.Load in the.wav file 3.Play the sound

17 Games at Bolton 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");

18 Games at Bolton 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); }


Download ppt "Games at Bolton Sound in SDL Andrew Williams adw1."

Similar presentations


Ads by Google