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
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;
}
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 );
Xterm-In'Hate.