email sol follow sol rss feed of the blog wishlist Sol::Tutorials

DirectX8, Fmod and CFL

FMOD

Open source as .cpp as .html

In order to build the source of this chapter you must link it with fmodvc.lib, which can be found in the fmod sdk, available from www.fmod.org

As a media file I chose to use Soundwave/Virtual Vision's 'Core For Dancer' S3M, first released in his Drastic Scream musicdisk. I've tried to contact him (Lasse Makkonen), but nobody seems to know where he is, all of his email accounts I've tried have bounced, and I've also managed to talk to other people with the same name (oops!). I hope it's ok for me to use the tune here (and I hope he's still making music). update: he mailed me and said it's ok. Yay!

The example uses one feature that appears in FMod 3.34, a version that is still under construction as I write this. If you download the 'bin' or 'final' packages (from 'main' page), that package contains a non-standard version of the fmod dll (which is somewhere between 3.33 and 3.34). Please do not distribute this version.

Into the source. First of all, we include the fmod.h, and create some new variables:


FMUSIC_MODULE           *mod;       // FMOD module pointer

int                     trigger1=0; // Global trigger for effect 1
int                     trigger2=0; // Global trigger for effect 2
int                     trigger3=0; // Global trigger for effect 3

We're just playing one music module here, so we only need one FMUSIC_MODULE object. The trigger1-3 variables are set in the instrument_callback function, which comes next.

In the setup function we have the fmod initialization:


    // Fmod setup (we just die if something goes wrong)
    if (FSOUND_GetVersion() < FMOD_VERSION) exit(1);
    if (!FSOUND_Init(44100, 32, FSOUND_INIT_GLOBALFOCUS)) exit(1);
    mod = FMUSIC_LoadSong("cfd.s3m");
    if (!mod)
    {
        exit(1);
    }

As usual, we just die if something goes wrong. The above is all you need to set up fmod for module playback. MP3 playback is similarly simple.


    // Just start playing..
    FMUSIC_PlaySong(mod);

    // Set up instrument callbacks.
    FMUSIC_SetInstCallback(mod,instrument_callback,2);
    FMUSIC_SetInstCallback(mod,instrument_callback,7);
    FMUSIC_SetInstCallback(mod,instrument_callback,10);
    FMUSIC_SetInstCallback(mod,instrument_callback,11);
    FMUSIC_SetInstCallback(mod,instrument_callback,5);

Here we set up several instrument callbacks. This is the new feature I'm using from 3.34; in earlier versions only the last callback is used.

The rendering function is changed so that it calculates three 'sizeinc' variables based on how long ago one of the 'trigger' variables were set. Then, these three are used to distort the flowers - presto, instant audio sync! (Note that we use time, not frames, to calculate the effect. Your framerates may vary).

Finally, the clean-up function takes care of stopping fmod audio and cleaning up.

Now the application is somewhat complete, so what do we need CFL for? Currently our application needs four files - CFD.S3M, flare64.jpg, fmod.dll and the executable itself. In a typical application you'd have tens if not hundreds of textures, several sound files, other data files, etc - usually so many that you'd opt for creating a directory hierarchy to take care of them. CFL solves this problem by storing them all in one file, somewhat like ZIP does, and provides handy ways of using the files. Let's look at it in more detail.

Next we'll plug in CFL..

Site design & Copyright © 2021 Jari Komppa
Possibly modified around: June 01 2010