0
0

You will find that filecallbacks example uses fopen to open, read seek and close…

Unfortunately, under windows, fopen occasionally fails to open a file that is already open. I hit this snag under a specific set of circumstances that FMOD’s default file procs handle… But if you hooked the file system and used the example file procs you would get an erroneous file not found error.

This win32 variation of these callbacks will fix the problem.
[code:3019gesm]
FMOD_RESULT F_CALLBACK myopen(const char *name, int unicode, unsigned int *filesize, void **handle, void **userdata)
{
if (name)
{
//FILE *fp = NULL;
HANDLE fp = NULL;
//fp = fopen(name, "rb");
fp = CreateFile(name, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (!fp) //occasional erroneous file not found if using fopen
{
return FMOD_ERR_FILE_NOTFOUND;
}

    //fseek(fp, 0, SEEK_END);
    //*filesize = ftell(fp);
    *filesize = SetFilePointer (fp, 0, NULL, FILE_END);

    //fseek(fp, 0, SEEK_SET);
    SetFilePointer (fp, 0, NULL, FILE_BEGIN);
    *handle = fp;
}

return FMOD_OK;

}

FMOD_RESULT F_CALLBACK myclose(void *handle, void *userdata)
{
if (!handle)
{
return FMOD_ERR_INVALID_PARAM;
}

CloseHandle((HANDLE)handle);
//fclose((FILE *)handle);

return FMOD_OK;

}

FMOD_RESULT F_CALLBACK myread(void *handle, void *buffer, unsigned int sizebytes, unsigned int *bytesread, void *userdata)
{
if (!handle)
{
return FMOD_ERR_INVALID_PARAM;
}

if (bytesread)
{
    if(!ReadFile((HANDLE) handle, buffer, (DWORD) sizebytes, (DWORD*) bytesread, NULL))
    {
        return FMOD_ERR_FILE_EOF;
    }

    if (*bytesread < sizebytes)
    {
        return FMOD_ERR_FILE_EOF;
    }
}
return FMOD_OK;

}

FMOD_RESULT F_CALLBACK myseek(void *handle, unsigned int pos, void *userdata)
{
if (!handle)
{
return FMOD_ERR_INVALID_PARAM;
}

//fseek((FILE *)handle, pos, SEEK_SET);
if(pos != SetFilePointer((HANDLE) handle, pos, NULL, FILE_BEGIN))
    return FMOD_ERR_FILE_COULDNOTSEEK;

return FMOD_OK;

}
[/code:3019gesm]

use at your own risk…

  • You must to post comments
Showing 0 results
Your Answer

Please first to submit.