Revving up your engine

Now that you have a better understanding of how audio works in your computer, it's time to write some code to bring audio into your game. We generally don't work with audio directly. Instead, there are audio engines that do all of the hard work for us, and one of the most popular ones is FMOD.

FMOD is a C and C++ API that allows us to load, manage, and play audio sources. FMOD is free to use for student and independent projects, so it is the perfect audio engine for our game. To use FMOD, you will have to go to the FMOD website, download the appropriate version of the API, and install it on your system:

  1. To download FMOD, go to http://www.FMOD.org/download/.
  2. There several downloads to choose from. Scroll down to the FMOD Ex Programmer's API, and click the Download button for Windows.
  3. You will have to locate the exe file that you just downloaded and install it. Make a note of the folder that FMOD is installed in.
  4. Once you have downloaded FMOD, you will have to incorporate it into the game project. Start by opening the RoboRacer2D project.

Tip

I'm sure that you would like to see the full documentation for the FMOD API. If you installed FMOD in the default location, you will find the documentation at C:Program Files (x86)FMOD SoundSystemFMOD Programmers API Windowsdocumentation. The main documentation is found in the file fmodex.chm.

Now, it's time to set up our game to use FMOD. Similar to most third-party libraries, there are three steps to hooking things up:

  1. Accessing the .dll file.
  2. Linking to the library.
  3. Point to the include files.

Let' walk through this process.

Accessing the FMOD .dll file

There are several .dll files that are included with FMOD, and it is important to use the correct file. The following table summarizes the dll files that come with FMOD and their associated library file:

Dll

Description

Library

fmodex.dll

32 bit FMOD API

fmodex_vc.lib

fmodexL.dll

32 bit FMOD API with debug logging

fmodexL_vc.lib

fmodex64.dll

64 bit FMOD API

fmodex64_vc.lib

fmodexL64.dll

64 bit FMOD API with debug logging

fmodexL64_vc.lib

It's up to you to decide whether or not to use the 32-bit or 64-bit versions of the library. The debug versions of the library write logging information out to a file. You can find more information in the documentation.

We are going to use the 32-bit file in our game. There are several places where we can place the file, but the simplest method is to simply copy the .dll file into our project:

  1. Navigate to C:Program Files (x86)FMOD SoundSystemFMOD Programmers API Windowsapi.

    Tip

    The preceding path assumes that you used the default install location. You may have to modify the path if you chose another location.

  2. Copy fmodex.dll to the project folder that contains the RoboRacer2D source code.

Linking to the library

The next step is to tell Visual Studio that we want to access the FMOD library. This is done by adding the library to the project properties:

  1. Right-click on the project and choose Properties.
  2. Open the Linker branch under Configuration Properties and click on Input.
    Linking to the library
  3. Click in the Addition Dependencies entry, then click the drop-down arrow and choose <Edit…>.
  4. Add fmodex_vc.lib to the list of dependencies.
    Linking to the library
  5. Click OK to close the Additional Dependencies window.
  6. Click OK to close the Property Pages window.

Now, we have to tell Visual Studio where to find the library:

  1. Right-click on the project and choose Properties.
    Linking to the library
  2. Open the Linker branch under Configuration Properties and click on General.
  3. Click in the Additional Library Directories entry, then click the drop-down arrow and choose <Edit…>:
    Linking to the library
  4. Click on the New Line icon, and then click the ellipses () that appear.
  5. Navigate to C:Program Files (x86)FMOD SoundSystemFMOD Programmers API Windowsapilib and click Select Folder.
  6. Click on OK to close the Additional Library Directories window.
  7. Click on OK to close the Property Pages window.

Point to the include files

Whenever you use third-party code, you generally have to include C++ header files in your code. Sometimes, we just copy the relevant header files into the project folder (for example, this is what we did with SOIL.h).

With larger code bases, such as FMOD, we point Visual Studio to the location where the header files are installed:

  1. Right-click on the project and choose Properties.
    Point to the include files
  2. Open the C/C++ branch under Configuration Properties and click on General.
  3. Click on the Additional Include Directories entry, then click the drop-down arrow, and choose <Edit…>.
    Point to the include files
  4. Click the New Line icon, and then click the ellipses () that appear.
  5. Navigate to C:Program Files (x86)FMOD SoundSystemFMOD Programmers API Windowsapiinc and click Select Folder.
  6. Click OK to close the Additional Include Directories window.
  7. Click OK to close the Property Pages window.

The final step is to include the header files into our program. Open RoboRacer2D.cpp and add the following line to include the header file:

#include "fmod.hpp"

You are finally ready to use our audio engine!

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

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