您的位置:首页 > 其它

[MetaHook] Quake FMOD player demo

2015-08-06 21:09 369 查看
CFMOD.h

#ifndef CFMOD_H
#define CFMOD_H

#include "qfmod.h"

struct Sound_t
{
char *pszName;
FMOD_SOUND *pSound;
FMOD_CHANNEL *pChannel;
Sound_t *pNext;
};

#ifdef PlaySound
#undef PlaySound
#endif

class CFmod
{
public:
CFmod();
~CFmod();
public:
void Init(void);
void PlaySound(char *pszFileName);
void StopSound(char *pszFileName);
void Shutdown(void);
private:
Sound_t* FindSound(char *pszFileName);
bool CacheSound(char *pszFileName);
private:
FMOD_SYSTEM *m_pSystem;
Sound_t *m_pBaseSound;
};

#endif


CFMOD.cpp

#include <metahook.h>

#include "qfmod.h"

#include "cfmod.h"

extern IFileSystem *g_pFileSystem;

CFmod::CFmod()
{
m_pSystem = NULL;
m_pBaseSound = NULL;
}

CFmod::~CFmod()
{
m_pSystem = NULL;
m_pBaseSound = NULL;
}

void CFmod::Init(void)
{
CFmod();

qFMOD_System_Create(&m_pSystem);
qFMOD_System_Init(m_pSystem, 32, FMOD_INIT_NORMAL, NULL);
}

void CFmod::PlaySound(char *pszFileName)
{
if (!m_pSystem)
return;

Sound_t *pSound = FindSound(pszFileName);

if (!pSound)
{
if (!CacheSound(pszFileName))
return;
}

pSound = FindSound(pszFileName);

if (!pSound)
return;

qFMOD_System_PlaySound(m_pSystem, FMOD_CHANNEL_FREE, pSound->pSound, NULL, &pSound->pChannel);
}

void CFmod::StopSound(char *pszFileName)
{
if (!m_pSystem)
return;

if (pszFileName != NULL)
{
Sound_t *pSound = FindSound(pszFileName);

if (!pSound)
return;

if (pSound->pChannel)
qFMOD_Channel_Stop(pSound->pChannel);
}
else
{
for (Sound_t *p = m_pBaseSound; p; p = p->pNext)
{
if (p->pChannel)
qFMOD_Channel_Stop(p->pChannel);
}
}
}

void CFmod::Shutdown(void)
{
if (!m_pSystem)
return;

Sound_t *p = m_pBaseSound;
Sound_t *t;

while (p)
{
t = p->pNext;

free(p->pszName);
qFMOD_Sound_Release(p->pSound);
delete p;

p = t;
}

qFMOD_System_Close(m_pSystem);
qFMOD_System_Release(m_pSystem);
}

Sound_t* CFmod::FindSound(char *pszFileName)
{
if (!m_pSystem)
return NULL;

for (Sound_t *p = m_pBaseSound; p; p = p->pNext)
{
if (p->pszName && !strcmp(p->pszName, pszFileName))
return p;
}

return NULL;
}

bool CFmod::CacheSound(char *pszFileName)
{
if (!m_pSystem)
return false;

FileHandle_t pFile;
uint32 iFileLen;
BYTE *pBuffer;
FMOD_CREATESOUNDEXINFO ExInfo;
FMOD_SOUND *pSound;
Sound_t *pCache;

pFile = g_pFileSystem->Open(pszFileName, "rb");

if (!pFile)
{
return false;
}

g_pFileSystem->Seek(pFile, 0, FILESYSTEM_SEEK_TAIL);
iFileLen = g_pFileSystem->Tell(pFile);
g_pFileSystem->Seek(pFile, 0, FILESYSTEM_SEEK_HEAD);

pBuffer = (BYTE *)malloc(iFileLen);
g_pFileSystem->Read(pBuffer, iFileLen, pFile);

g_pFileSystem->Close(pFile);

memset(&ExInfo, 0, sizeof(ExInfo));
ExInfo.cbsize = sizeof(ExInfo);
ExInfo.length = iFileLen;

if (qFMOD_System_CreateSound(m_pSystem, (const char *)pBuffer, FMOD_HARDWARE | FMOD_OPENMEMORY, &ExInfo, &pSound) != FMOD_OK)
{
free(pBuffer);
return false;
}

free(pBuffer);

pCache = new Sound_t;
pCache->pszName = (char *)malloc(strlen(pszFileName) * sizeof(char) + 1);
strcpy(pCache->pszName, pszFileName);
pCache->pSound = pSound;
pCache->pChannel = NULL;
pCache->pNext = m_pBaseSound;
m_pBaseSound = pCache;

return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: