(C++) Integrating FMODEx With Irrlicht Zip File System

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
xterminhate
Posts: 206
Joined: Thu Sep 01, 2005 9:26 pm
Location: France

(C++) Integrating FMODEx With Irrlicht Zip File System

Post by xterminhate »

I got frustrated to place a separate folder for sound files, appart from the big Zip file that contains all my project data (textures, meshes, xml files, ...). So, I developped this adapter for FMODEx / Irrlicht.

Note : Place your sound file into the Irrlicht Zip archive.

header (filesystem.hpp) :

Code: Select all

#ifndef _SOUND_FILESYSTEM_
#define _SOUND_FILESYSTEM_

// win32
#define WIN32
#include "windows.h"

// engines
#include <irrlicht.h>
#include <fmod.hpp>

struct t_sound_filesystem
{
    static irr::IrrlichtDevice * device;
};

FMOD_RESULT F_CALLBACK f_sound_filesystem_open(
  const char *  name,
  int  unicode,
  unsigned int *  filesize,
  void **  handle,
  void **  userdata
);

FMOD_RESULT F_CALLBACK f_sound_filesystem_close(
  void *  handle,
  void *  userdata
);

FMOD_RESULT F_CALLBACK f_sound_filesystem_read(
  void *  handle,
  void *  buffer,
  unsigned int  sizebytes,
  unsigned int *  bytesread,
  void *  userdata
);

FMOD_RESULT F_CALLBACK f_sound_filesystem_seek(
  void *  handle,
  unsigned int  pos,
  void *  userdata
);

#endif
source (filesystem.cpp) :

Code: Select all

#include "filesystem.hpp"

irr::IrrlichtDevice * t_sound_filesystem::device = 0;

FMOD_RESULT F_CALLBACK f_sound_filesystem_open(
  const char *  name,
  int  unicode,
  unsigned int *  filesize,
  void **  handle,
  void **  userdata
)
{
    if( t_sound_filesystem::device == 0 )
        return FMOD_ERR_FILE_BAD;

    // open file
    irr::io::IReadFile * rf = t_sound_filesystem::device->getFileSystem()->createAndOpenFile( name );
    *handle = rf;

    if( *handle == 0 )
        return FMOD_ERR_FILE_NOTFOUND;

    {
        irr::core::stringc str = "Loaded sound: ";
        str.append( name );
        t_sound_filesystem::device->getLogger()->log( str.c_str() );
    }

    *filesize = rf->getSize();

    return FMOD_OK;
}

FMOD_RESULT F_CALLBACK f_sound_filesystem_close(
  void *  handle,
  void *  userdata
)
{
    if( t_sound_filesystem::device == 0 )
        return FMOD_ERR_FILE_BAD;

    // close file
    reinterpret_cast<irr::io::IReadFile *>( handle )->drop();

    return FMOD_OK;
}

FMOD_RESULT F_CALLBACK f_sound_filesystem_read(
  void *  handle,
  void *  buffer,
  unsigned int  sizebytes,
  unsigned int *  bytesread,
  void *  userdata
)
{
    if( t_sound_filesystem::device == 0 )
        return FMOD_ERR_FILE_BAD;

    // read file
    *bytesread = reinterpret_cast<irr::io::IReadFile *>( handle )->read( buffer, sizebytes );

    if( *bytesread == 0 )
        return FMOD_ERR_FILE_EOF;

    return FMOD_OK;
}

FMOD_RESULT F_CALLBACK f_sound_filesystem_seek(
  void *  handle,
  unsigned int  pos,
  void *  userdata
)
{
    if( t_sound_filesystem::device == 0 )
        return FMOD_ERR_FILE_BAD;

    // seek into file
    if( ! reinterpret_cast<irr::io::IReadFile *>( handle )->seek( pos ) )
        return FMOD_ERR_FILE_COULDNOTSEEK;

    return FMOD_OK;
}
Initialisation.... in your code :

In the following code (out of my own project), "_device" is the Irrlicht device pointer and "_fmod_system" is the FMOD::System pointer.

Code: Select all

    t_sound_filesystem::device = _device;
    _fmod_system->setFileSystem( f_sound_filesystem_open,
                                 f_sound_filesystem_close,
                                 f_sound_filesystem_read,
                                 f_sound_filesystem_seek,
                                 2048 );
Good luck !
Xterm-In'Hate. 8)
Return to Irrlicht after years... I'm lovin it.
It's hard to be a Man !
Si vis pacem para belum
the burning realm
Posts: 5
Joined: Sun Jun 15, 2014 9:07 pm

Re: (C++) Integrating FMODEx With Irrlicht Zip File System

Post by the burning realm »

Very grateful for this, thank you :)
Post Reply