Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2002 Bolton Institute Sound in SDL Andrew Williams

Similar presentations


Presentation on theme: "Copyright © 2002 Bolton Institute Sound in SDL Andrew Williams"— Presentation transcript:

1 Copyright © 2002 Bolton Institute Sound in SDL Andrew Williams A.Williams@bolton.ac.uk http://www.bolton.ac.uk/staff/adw1

2 Copyright © 2002 Bolton Institute 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 Copyright © 2002 Bolton Institute 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 Copyright © 2002 Bolton Institute 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 Copyright © 2002 Bolton Institute Installation ( SDL_mixer.h )

6 Copyright © 2002 Bolton Institute Installation ( SDL_mixer.lib )

7 Copyright © 2002 Bolton Institute Project Settings

8 Copyright © 2002 Bolton Institute Project Settings Don’t forget the.dll file; Put it in your project directory, or in C:\WINDOWS\System32

9 Copyright © 2002 Bolton Institute 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

10 Copyright © 2002 Bolton Institute 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); }

11 Copyright © 2002 Bolton Institute 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); }

12 Copyright © 2002 Bolton Institute 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);

13 Copyright © 2002 Bolton Institute Stop the Music Very simple: Directed study: –Look at: Mix_HookMusicFinished(...); /* Stop the music from playing */ Mix_HaltMusic();

14 Copyright © 2002 Bolton Institute 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

15 Copyright © 2002 Bolton Institute 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");

16 Copyright © 2002 Bolton Institute 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 "Copyright © 2002 Bolton Institute Sound in SDL Andrew Williams"

Similar presentations


Ads by Google