ddnet/src/game/client/components/sounds.cpp

229 lines
5.1 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2011-08-31 11:56:04 +00:00
#include <base/tl/array.h>
2011-02-27 16:56:03 +00:00
#include <engine/engine.h>
2010-05-29 07:25:38 +00:00
#include <engine/sound.h>
#include <engine/shared/config.h>
2010-05-29 07:25:38 +00:00
#include <game/generated/client_data.h>
#include <game/client/gameclient.h>
#include <game/client/components/camera.h>
2011-02-27 16:56:03 +00:00
#include <game/client/components/menus.h>
2010-05-29 07:25:38 +00:00
#include "sounds.h"
2008-08-29 05:34:18 +00:00
2011-02-27 16:56:03 +00:00
struct CUserData
{
CGameClient *m_pGameClient;
bool m_Render;
} g_UserData;
static int LoadSoundsThread(void *pUser)
{
CUserData *pData = static_cast<CUserData *>(pUser);
2011-02-27 16:56:03 +00:00
for(int s = 0; s < g_pData->m_NumSounds; s++)
{
for(int i = 0; i < g_pData->m_aSounds[s].m_NumSounds; i++)
{
int Id = pData->m_pGameClient->Sound()->LoadWV(g_pData->m_aSounds[s].m_aSounds[i].m_pFilename);
g_pData->m_aSounds[s].m_aSounds[i].m_Id = Id;
}
if(pData->m_Render)
pData->m_pGameClient->m_pMenus->RenderLoading();
}
return 0;
}
2012-01-06 18:38:40 +00:00
int CSounds::GetSampleId(int SetId)
{
if(!g_Config.m_SndEnable || !Sound()->IsSoundEnabled() || m_WaitForSoundJob || SetId < 0 || SetId >= g_pData->m_NumSounds)
return -1;
CDataSoundset *pSet = &g_pData->m_aSounds[SetId];
if(!pSet->m_NumSounds)
return -1;
if(pSet->m_NumSounds == 1)
return pSet->m_aSounds[0].m_Id;
// return random one
int Id;
do
{
Id = rand() % pSet->m_NumSounds;
}
while(Id == pSet->m_Last);
pSet->m_Last = Id;
return pSet->m_aSounds[Id].m_Id;
}
2010-05-29 07:25:38 +00:00
void CSounds::OnInit()
2008-08-29 05:34:18 +00:00
{
// setup sound channels
2010-05-29 07:25:38 +00:00
Sound()->SetChannel(CSounds::CHN_GUI, 1.0f, 0.0f);
Sound()->SetChannel(CSounds::CHN_MUSIC, 1.0f, 0.0f);
Sound()->SetChannel(CSounds::CHN_WORLD, 0.9f, 1.0f);
Sound()->SetChannel(CSounds::CHN_GLOBAL, 1.0f, 0.0f);
2014-10-10 17:10:57 +00:00
Sound()->SetChannel(CSounds::CHN_AMBIENT, 0.7f, 1.0f);
2008-08-30 08:01:29 +00:00
2010-05-29 07:25:38 +00:00
Sound()->SetListenerPos(0.0f, 0.0f);
ClearQueue();
2011-02-27 16:56:03 +00:00
// load sounds
if(g_Config.m_ClThreadsoundloading)
{
g_UserData.m_pGameClient = m_pClient;
g_UserData.m_Render = false;
m_pClient->Engine()->AddJob(&m_SoundJob, LoadSoundsThread, &g_UserData);
m_WaitForSoundJob = true;
}
else
{
g_UserData.m_pGameClient = m_pClient;
g_UserData.m_Render = true;
LoadSoundsThread(&g_UserData);
m_WaitForSoundJob = false;
}
2010-05-29 07:25:38 +00:00
}
void CSounds::OnReset()
{
if(Client()->State() >= IClient::STATE_ONLINE)
{
Sound()->StopAll();
ClearQueue();
}
}
void CSounds::OnStateChange(int NewState, int OldState)
{
if(NewState == IClient::STATE_ONLINE || NewState == IClient::STATE_DEMOPLAYBACK)
OnReset();
2008-08-29 05:34:18 +00:00
}
2010-05-29 07:25:38 +00:00
void CSounds::OnRender()
2008-08-29 05:34:18 +00:00
{
2011-02-27 16:56:03 +00:00
// check for sound initialisation
if(m_WaitForSoundJob)
{
if(m_SoundJob.Status() == CJob::STATE_DONE)
m_WaitForSoundJob = false;
else
return;
}
2011-02-27 16:56:03 +00:00
2008-08-29 05:34:18 +00:00
// set listner pos
2010-05-29 07:25:38 +00:00
Sound()->SetListenerPos(m_pClient->m_pCamera->m_Center.x, m_pClient->m_pCamera->m_Center.y);
// play sound from queue
if(m_QueuePos > 0)
{
int64 Now = time_get();
2010-05-29 07:25:38 +00:00
if(m_QueueWaitTime <= Now)
{
2012-01-06 18:38:40 +00:00
Play(m_aQueue[0].m_Channel, m_aQueue[0].m_SetId, 1.0f);
2010-05-29 07:25:38 +00:00
m_QueueWaitTime = Now+time_freq()*3/10; // wait 300ms before playing the next one
if(--m_QueuePos > 0)
mem_move(m_aQueue, m_aQueue+1, m_QueuePos*sizeof(QueueEntry));
2010-05-29 07:25:38 +00:00
}
}
2008-08-29 05:34:18 +00:00
}
2010-05-29 07:25:38 +00:00
void CSounds::ClearQueue()
{
2010-05-29 07:25:38 +00:00
mem_zero(m_aQueue, sizeof(m_aQueue));
m_QueuePos = 0;
m_QueueWaitTime = time_get();
}
void CSounds::Enqueue(int Channel, int SetId)
2010-05-29 07:25:38 +00:00
{
// add sound to the queue
if(m_QueuePos < QUEUE_SIZE)
{
if(Channel == CHN_MUSIC || !g_Config.m_ClEditor)
{
m_aQueue[m_QueuePos].m_Channel = Channel;
m_aQueue[m_QueuePos++].m_SetId = SetId;
}
}
2010-05-29 07:25:38 +00:00
}
void CSounds::PlayAndRecord(int Chn, int SetId, float Vol, vec2 Pos)
{
CNetMsg_Sv_SoundGlobal Msg;
Msg.m_SoundID = SetId;
2010-05-29 07:25:38 +00:00
Client()->SendPackMsg(&Msg, MSGFLAG_NOSEND|MSGFLAG_RECORD);
2012-01-06 18:38:40 +00:00
Play(Chn, SetId, Vol);
}
2012-01-06 18:38:40 +00:00
void CSounds::Play(int Chn, int SetId, float Vol)
2008-08-29 05:34:18 +00:00
{
2012-01-06 18:38:40 +00:00
if(Chn == CHN_MUSIC && !g_Config.m_SndMusic)
2010-05-29 07:25:38 +00:00
return;
2012-01-06 18:38:40 +00:00
int SampleId = GetSampleId(SetId);
if(SampleId == -1)
2008-08-29 05:34:18 +00:00
return;
2011-08-11 08:59:14 +00:00
int Flags = 0;
if(Chn == CHN_MUSIC)
Flags = ISound::FLAG_LOOP;
2008-08-29 05:34:18 +00:00
2012-01-06 18:38:40 +00:00
Sound()->Play(Chn, SampleId, Flags);
}
void CSounds::PlayAt(int Chn, int SetId, float Vol, vec2 Pos)
{
if(Chn == CHN_MUSIC && !g_Config.m_SndMusic)
return;
int SampleId = GetSampleId(SetId);
if(SampleId == -1)
2008-08-29 05:34:18 +00:00
return;
2012-01-06 18:38:40 +00:00
int Flags = 0;
if(Chn == CHN_MUSIC)
Flags = ISound::FLAG_LOOP;
Sound()->PlayAt(Chn, SampleId, Flags, Pos.x, Pos.y);
2008-08-29 05:34:18 +00:00
}
void CSounds::Stop(int SetId)
{
if(m_WaitForSoundJob || SetId < 0 || SetId >= g_pData->m_NumSounds)
return;
2011-08-11 08:59:14 +00:00
2011-06-01 18:19:12 +00:00
CDataSoundset *pSet = &g_pData->m_aSounds[SetId];
2011-08-11 08:59:14 +00:00
for(int i = 0; i < pSet->m_NumSounds; i++)
Sound()->Stop(pSet->m_aSounds[i].m_Id);
}
2014-10-10 17:10:57 +00:00
2014-10-12 14:12:13 +00:00
ISound::CVoiceHandle CSounds::PlaySample(int Chn, int SampleId, float Vol, int Flags)
2014-10-10 17:10:57 +00:00
{
if((Chn == CHN_MUSIC && !g_Config.m_SndMusic) || SampleId == -1)
2014-10-12 14:12:13 +00:00
return ISound::CVoiceHandle();
2014-10-10 17:10:57 +00:00
if(Chn == CHN_MUSIC)
Flags |= ISound::FLAG_LOOP;
2014-10-12 14:12:13 +00:00
return Sound()->Play(Chn, SampleId, Flags);
2014-10-10 17:10:57 +00:00
}
2014-10-12 14:12:13 +00:00
ISound::CVoiceHandle CSounds::PlaySampleAt(int Chn, int SampleId, float Vol, vec2 Pos, int Flags)
2014-10-10 17:10:57 +00:00
{
if((Chn == CHN_MUSIC && !g_Config.m_SndMusic) || SampleId == -1)
2014-10-12 14:12:13 +00:00
return ISound::CVoiceHandle();
2014-10-10 17:10:57 +00:00
if(Chn == CHN_MUSIC)
Flags |= ISound::FLAG_LOOP;
2014-10-12 14:12:13 +00:00
return Sound()->PlayAt(Chn, SampleId, Flags, Pos.x, Pos.y);
2014-10-10 17:10:57 +00:00
}