Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
SFML Game Development

You're reading from  SFML Game Development

Product type Book
Published in Jun 2013
Publisher Packt
ISBN-13 9781849696845
Pages 296 pages
Edition 1st Edition
Languages
Authors (5):
 Artur Moreira Artur Moreira
Profile icon Artur Moreira
 Henrik Vogelius Hansson Henrik Vogelius Hansson
Profile icon Henrik Vogelius Hansson
Jan Haller Jan Haller
Profile icon Jan Haller
Henrik Valter Vogelius Henrik Valter Vogelius
Profile icon Henrik Valter Vogelius
View More author details

Table of Contents (18) Chapters

SFML Game Development
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Making a Game Tick 2. Keeping Track of Your Textures – Resource Management 3. Forge of the Gods – Shaping Our World 4. Command and Control – Input Handling 5. Diverting the Game Flow – State Stack 6. Waiting and Maintenance Area – Menus 7. Warfare Unleashed – Implementing Gameplay 8. Every Pixel Counts – Adding Visual Effects 9. Cranking Up the Bass – Music and Sound Effects 10. Company Atop the Clouds – Co-op Multiplayer Index

Chapter 9. Cranking Up the Bass – Music and Sound Effects

Since the beginning of video games, sound has been a central media in games. Many games can be recognized only by listening to them. In the last few years, game industries have chosen to neglect audio in favor of better graphics; yet audio takes a very important part in a wide range of games. Independent studios in particular often put a huge effort in making games unique in their art style, which includes audio, graphics, and story. If used appropriately, music themes and sounds can have a tremendous impact on the atmosphere conveyed by a game.

In this chapter, we are going to cover the technical background of embedding audio into a game, taking the opportunity to have a closer look at SFML's Audio module. We are going to do the following:

  • Play different music themes in the background

  • Play sound effects that correspond to game events such as explosions

  • Position sound effects in the 2D world to convey a feeling of spatial sound

Music themes


First, we want to play background music depending on the state we are currently in. We have prepared two themes: One for the menu, and one for the game itself. We'll define a corresponding enum:

namespace Music
{
    enum ID
    {
        MenuTheme,
        MissionTheme,
    };
}

We'll then create a class that has an interface dedicated to music playing:

class MusicPlayer : private sf::NonCopyable
{
    public:
                            MusicPlayer();

        void                play(Music::ID theme);
        void                stop();

        void                setPaused(bool paused);
        void                setVolume(float volume);

    private:
        sf::Music                           mMusic;
        std::map<Music::ID, std::string>    mFilenames;
        float                               mVolume;
};

The method names should be self-explanatory. We have a single sf::Music instance that represents the currently-played music. The mFilenames variable maps music...

Sound effects


We have many gameplay events that can be represented by sounds: Fired machine guns, launched missiles, explosions, collection of pickups, and so on. Unlike music, sound effects are mostly very short. As a consequence, they can be loaded completely into memory, and we can also use the raw WAV format for these files without wasting too much memory. We are going to use the sf::SoundBuffer resource class to store the audio samples for our sound effects.

The following enumeration of sound effects is used in our game. We'll also create a typedef for the resource holder of sf::SoundBuffer.

namespace SoundEffect
{
    enum ID
    {
        AlliedGunfire,
        EnemyGunfire,
        Explosion1,
        Explosion2,
        LaunchMissile,
        CollectPickup,
        Button,
    };
}

typedef ResourceHolder<sf::SoundBuffer, SoundEffect::ID>    SoundBufferHolder;

We implement a class for the sound effects, one similar to the MusicPlayer class:

class SoundPlayer : private sf::NonCopyable...

Sounds in 3D space


The most interesting part about sound effects is yet to come. An immersive atmosphere only builds up if sounds are properly located within the game world. Like graphical objects, sounds can have a position.

The coordinate system for sounds is three-dimensional. SFML's sound API works with the sf::Vector3f type, a 3D vector with the members x, y, and z. SFML internally uses Open Audio Library (OpenAL), an interface for low-level audio functionality, which is also the origin of the sound spatialization concepts we are going to discuss here. Spatializing sounds means nothing more than to locate them in the 3D space, that is, to give them a spatial representation.

The listener

The audition of spatial sound effects depends on the listener. A useful analogy is to compare the listener with your head. The listener's location and orientation can be described with the following three 3D vectors:

  • Position: This vector describes where the listener is located in 3D space.

  • Up: This vector...

Summary


In this chapter, we have covered the basic topics for dealing with audio in 2D games. We saw how the concepts are implemented using SFML classes and functions. In particular, we played different music themes depending on the application state. We also created sound effects for various in-game events, and positioned them in the world in order to enable a spatial audition.

With this knowledge, you are now able to incorporate audio into your game; a crucial component for creating an immersive gaming experience. As you have seen during this chapter, SFML makes the implementation very simple. The actual challenge is the fine-tuning and the combination of suitable sound effects and music themes in order to create a unique atmosphere.

The audio functionality completes the attempt to create a playable game that uses different sources of media. However, so far, only one player can play it. In the next chapter, we are going to improve the situation by adding multiplayer support.

lock icon The rest of the chapter is locked
You have been reading a chapter from
SFML Game Development
Published in: Jun 2013 Publisher: Packt ISBN-13: 9781849696845
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}