09:23
20 Mar 2010

News

Latest Updates
Recent Comments
Most Popular Downloads

Projects

Blob Wars : Blob And Conquer
Blob Wars : Metal Blob Solid
JGameLaunch
The Legend of Edgar
Project: Starfighter
Random Name Generator
Random Shooter
TANX Squadron
Virus Killer
XMAME GUI

Older Files

List of Older Downloads

Medals

Main & Signup
Recently Awarded
Player List

Games
Blob Wars : Metal Blob Solid
Legend of Edgar
Virus Killer

Online Manuals

The Legend of Edgar
Blob Wars : Blob And Conquer
Blob Wars : Metal Blob Solid
Project: Starfighter
The Legend of Edgar
Virus Killer

LBP Beta Code!

Beta Code Giveaway!

Game Tutorials

Overview and Comments

Basic Series
1.01 - Opening a Window
1.02 - Graphics
1.03 - Sound
1.04 - Input and Movement
1.05 - Simple Shooter Pt. 1
1.06 - Simple Shooter Pt. 2
1.07 - Simple Shooter Pt. 3
1.08 - Sprites and Animation
1.09 - Starfields
1.10 - A Basic Game

Intermediate Series
2.01 - Displaying a Tile Based Map
2.02 - Scrolling a Tile Based Map
2.03 - A Tile Based Map Editor
2.04 - Tile Based Map Collision Detection
2.05 - Advanced Animation

Articles

Making of TANX Squadron
Making of Starfighter
Making of Metal Blob Solid
Making of Blob And Conquer

Blob Wars Review #1
Blob Wars Review #2

Gallery

3D Renders

Help and FAQs

Installation and Licensing Help

About

Contact Information

THE HONOUR OF THE KNIGHTS
The new Creative Commons Licensed
novel from Stephen J Sweeney

Visit the official website,
www.battleforthesolarsystem.com
to start reading online!

Buy Now! from Amazon.co.uk

Buy Now! from Book Depository

Basic Tutorials
Basic Game Tutorial #3 - Images and Sound

An SDL window displaying an image and playing a sound

 Introduction

This third tutorial deals with playing sound in SDL.

Compile and run tutorial03. By pressing Space, you can play a sound clip. Pressing Escape or closing the window will exit.

 An indepth look

Two new files are introduced audio.c and audio.h.

The defs.h file contains a new include, SDL/SDL_mixer.h. SDL is limited to playing WAV files so SDL_mixer allows us to also play music and use other sound types such as OGG. Like SDL_image, it is a very useful addition.

main.h introduces a new structure, Mix_Chunk, which we will use to store the sound that we wish to play.

main.c contains an additional function call, loadSound which is stored in audio.c.

In init.c we initialise SDL's audio system in addition to its video system.

SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)
Once again, if this call fails then we cannot continue, so we would simply exit the program. Once the system is up and running, we attempt open the audio:
Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 4096)
This call can fail for a valid reason, such as another process locking the audio or the system simply not having a sound card. Best practice would be to set a flag telling the program not to play any audio, but since this tutorial is reliant on it, we would exit if we couldn't play any sounds. The Mix_OpenAudio function takes 4 arguments, the frequency of the audio, the format of the audio, the channels (1 for mono, 2 for stereo) and the chunksize of the audio. 22050 is fairly standard for most games, it can be as high as 44100, which is CD quality, but this uses up a lot of CPU time so we leave it at 22050. AUDIO_S16SYS is standard 16 bit sound, there are other formats but some of them are not portable to other systems. 2 simply tells the audio to be played in stereo rather than mono (1). 4096 is a reasonable chunksize, if the chunksize is too big then there will be a delay between when the sound is told to play and when it actually does play. Conversely, if the chunksize is too small then the sound will stutter because of the constant filling and emptying.
Like SDL_Surfacess, Mix_Chunks must be freed when they are no longer needed. The function call
Mix_FreeChunk(dexterBark);
frees the chunk. The audio system must also be closed at the end of the program. So
Mix_CloseAudio();
is called before SDL_Quit.

input.c now calls the function playSound when Space is pressed. The function is stored in audio.c

graphics.c has not changed.

audio.c contains 2 function calls, loadSound and playSound. The loadSound function loads the audio file

Mix_Chunk *sfx = Mix_LoadWAV(name);
As with IMG_Load, there is no need to specify the file type as SDL_mixer will check the extension of the file. Mix_LoadWAV will return the loaded audio chunk or NULL if it fails.
playSound will play the specified audio file by calling
Mix_PlayChannel(-1, sfx, 0);
The first argument is the channel to play the sound on. You may wish to do this if you wanted to have, say, a person speaking and you wanted to have them interupted. Specifying -1 tells SDL to play the sound on the first free channel that it finds. The second argument is the sound chunk to play and the third argument is the additional number of times to play the sound, so 0 means to play it 0 additional times and 1 means to play it 1 additional time etc. Specifying -1 will loop the sound forever.

 Conclusion

Now that we have dealt with opening windows, displaying images and playing sounds, we can start looking at intermediate topics such as input control and collision detection.

 Downloads

Source Code - tutorial03.tar.gz

 Comments

Name
Homepage
Comments
-- Homepage must start with http://
-- Javascript and Cookies must be enabled to use this form
 

1,772,929 pages served

Copyright © 1996 - 2010 Parallel Realities :: About :: License