Adding FMOD to the Game Engine

The Audio class is very easy to use, so we don’t need to abstract it any further inside the game engine; it will suffice to just add a public Audio object to the engine so it’s available via the global g_engine. The object will be called audio, and you will be able to access it via g_engine->audio. Let’s take a look at the minor changes made to the engine to accommodate the new audio system.

In the Advanced2D.h header, an Audio class instance is defined as public:

//simplified public Audio object
Audio *audio;

Over in the Advanced2D.cpp engine implementation file, scroll down to the Engine::Init method and you will find the audio initialization:

//create audio system
audio = new Audio();
if (!audio->Init()) return 0;

Scrolling down a ways in the file, locate the Engine::Update method and the following lines of code:

//update audio system
audio->Update();

The FMOD system is not multi-threaded, so it does not automatically update the audio stream in the background—we must call a function to give FMOD an opportunity to update audio playback.

The last change made is to the destructor, where we need to wipe the audio object from memory:

Engine::~Engine()
{

        audio->StopAll();
        delete audio;
        delete p_input;
        if (this->p_device) this->p_device->Release();
        if (this->p_d3d) this->p_d3d->Release();}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset