Coding the SoundEngine class
You might recall from the previous project that all the sound code took up quite a few lines. Now, consider that we will need even more code when we add spatialization in Chapter 20; it’s going to get even longer. To keep our code manageable, we will code a class to manage all our sound effects and music being played.
All this code will be very familiar. Even the new feature of playing some music should seem quite intuitive because of what we did in the other games. Create a new class called SoundEngine
. In the SoundEngine.h
file, add the following code:
#pragma once
#include <SFML/Audio.hpp>
using namespace sf;
class SoundEngine
{
private:
static Music music;
static SoundBuffer m_ClickBuffer;
static Sound m_ClickSound;
static SoundBuffer m_JumpBuffer;
static Sound m_JumpSound;
public:
SoundEngine();
static SoundEngine* m_s_Instance;
static bool mMusicIsPlaying;
static void startMusic();
static void pauseMusic(...