FMOD IMPLEMENTATION FOR BACKGROUND SOUND

 
The parts of the code in red show the modification to the original Gtest source code.

Tutorial by Bob Lawrence:
This is just to get you started, many things can be done from here. Add more error checking and hardware detection support for instance (many more improvements/usage can be found in the doc’s). Also I am using the streaming as you can see, it loads faster. If you use pak files you will need to modify the calls fairly easily. This is setup to add a new Entity, but the code in FMOD.C can be pulled out and used internally to play the .ogg (or other supported files) very easily. I just thought I would make this an Entity to help people out understanding them also.

Good Luck! (Bob http://www.ourfun.com)
 

Get the fmodapi333.zip (http://www.fmod.org/)

Includes (add to your project path)

#include "fmod.h"
#include "fmod_errors.h" // optional

Global’s used in FMOD.h (just for your reference)

int FMODchannel = -1, FMODlength;
FSOUND_STREAM *FMODSample;

Libs (Add fmodvc.lib to your link settings, add fmod.dll to your program directory)

fmodvc.lib
fmod.dll


In GameMgr.c in GameMgr_FreeAllObjects add this:
FSOUND_Close(); //safety

In GameMgr_SetWorld add this around after // Create the coronas call:
//FMOD Background Sound
 if (!PlayFMOD_Spawn(GameMgr_GetWorld(GMgr)))
 GenVS_Error("GameMgr_SetWorld: FMODsound_Spawn failed.\n");


In Client.c add this at the end of the other includes:
#include "FMOD.h"

Then in CheckClientPlayerChanges add this near the top after assert(World);:
 
  if (!FSOUND_IsPlaying(FMODchannel))
 {
 
  FSOUND_Stream_Close(FMODSample);
  FSOUND_Close();

 }
 


Here is the FMOD.h file:

//**************
//FMOD Simple
//**************
 

#ifndef FMODsound_H
#define FMODsound_H

#ifdef __cplusplus
extern "C" {
#endif

#include "Genesis.h"
#include "FMOD/api/inc/fmod.h"
#include "FMOD/api/inc/fmod_errors.h" // optional

#pragma warning( disable : 4068 )

#pragma GE_Type("FMOD.bmp")

typedef struct FMODsound
{
    #pragma GE_Published

    char *TheFile;
    geVec3d Origin; // Where the entity is located

    #pragma GE_DefaultValue(TheFile, "spookywind.ogg")
 
    #pragma GE_Origin(Origin)

} FMODsound;

geBoolean PlayFMOD_Spawn(geWorld *World);

int FMODchannel, FMODlength;
FSOUND_STREAM *FMODSample;

#ifdef __cplusplus
}
#endif

#endif


And here is the FMOD.c file:

//*********
//FMOD
//*********
#include <windows.h>
#include <math.h>
#include <assert.h>

#include "genesis.h"
#include "ErrorLog.h"
#include "FMOD.h"

geBoolean PlayFMOD_Spawn(geWorld *World)
{
    geEntity_EntitySet * Set;
    geEntity * Entity;
    FMODsound * Fsound;
    float pos[3] = { -10.0f, -0.0f, 0.0f };
    float vel[3] = { 0,0,0 };
    //FMOD Stuff

    //FSOUND_SAMPLE *FMODSample = NULL;
    if (!FSOUND_Init(44100, 32, 0))
    {
        return GE_FALSE;
    }

    Set = geWorld_GetEntitySet(World, "FMODsound"); // Find the FMODSound entity in the World
    if (Set == NULL)
        return GE_TRUE;

    for (Entity= geEntity_EntitySetGetNextEntity(Set, NULL);
            Entity;
            Entity= geEntity_EntitySetGetNextEntity(Set, Entity))
    {
        Fsound = geEntity_GetUserData(Entity); // Get data from the

        // INITIALIZE

        //Open Sound File
        FMODSample = FSOUND_Stream_OpenFile(Fsound->TheFile,FSOUND_HW3D,0);
        //Get FMODlength (this can go, or you can use it for instance in a “Sleep(FMODlength);” statement).

        FMODlength = FSOUND_Stream_GetLengthMs(FMODSample);
        //Play the Sound File
        FMODchannel = FSOUND_Stream_Play3DAttrib(FSOUND_FREE, FMODSample,-1, -1, -1, pos, vel);
        //Set Paused to false
        FSOUND_Stream_SetPaused(FMODSample, FALSE);
        FSOUND_Reverb_SetMix(FMODchannel, SOUND_REVERBMIX_USEDISTANCE);

        return GE_TRUE;
    }

    return GE_TRUE;
}