Adding FMOD to an existing Project

Copy the FMOD files (listed below) to your project (put them in the same directory as the other .cpp files). The FMOD files can be found at H:\3DGraphics\Audio\FMOD_Files.

If you are working from home you can download the FMOD engine and copy the files from that.

Changes to code

For this example, we want to modify the "Game" class from the 3DOpenGLStarterProject to play sounds, so the following changes apply to game.h & game.cpp

Add the following lines to the top of game,h

#include "fmod.hpp" //fmod c++ header
#pragma comment( lib, "fmodex_vc.lib" ) // fmod library

Add the bold lines below to the game class declaration in game.h. These create pointers to important FMOD game engine objects required to play sound.

class Game
{
    int height,width;

	FunkyCube cube;


	//FMod Stuff
	FMOD::System     *system; //handle to FMOD engine
    FMOD::Sound      *sound1, *sound2; //sound that will be loaded and played



	//other game class stuff...

};

 

Add the bold lines below to the Game:Init() in order to initialise the FMOD engine and to load in some audio files

bool Game::Init(void)
{
	//init FMOD
	FMOD::System_Create(&system);// create an instance of the game engine
	system->init(32, FMOD_INIT_NORMAL, 0);// initialise the game engine with 32 channels


	//load sounds
	system->createSound("../../media/drumloop.wav", FMOD_HARDWARE, 0, &sound1);
	sound1->setMode(FMOD_LOOP_OFF);    /* drumloop.wav has embedded loop points which automatically makes looping turn on, */
										/* so turn it off here.  We could have also just put FMOD_LOOP_OFF in the above CreateSound call. */
   
	system->createSound("../../media/jaguar.wav", FMOD_HARDWARE, 0, &sound2);
	
    glEnable(GL_DEPTH_TEST); // check for depth


    
    return true;
}

 

The next set of lines are added to Game::Update(). These will play the sounds on a keypress;

bool Game::Update(float gametime){
	///////////////////////////
	//main game logic goes here
	///////////////////////////
	GetKeyboardState(keys);
	cube.Update(keys);


	if(keys['A']>1){
		system->playSound(FMOD_CHANNEL_FREE, sound1, false, 0);
		
	}


	if(keys['S']>1){
		
		system->playSound(FMOD_CHANNEL_FREE, sound2, false, 0);
	}
	system->update(); //update FMOD, need to call this once per frame to keep the sound engine running


	return false; //return false if you want to continue game
}

Finally, we need to add the following lines to Game::Done() to release any resources used

	sound1->release();
    sound2->release();
    system->close();
    system->release();