about summary refs log tree commit diff
path: root/src/game/client/components/sounds.cpp
diff options
context:
space:
mode:
authoroy <Tom_Adams@web.de>2011-02-27 17:56:03 +0100
committeroy <Tom_Adams@web.de>2011-02-27 17:56:03 +0100
commit0bc13c91ebcc6404fc67e079e5a9acad10176c98 (patch)
treec3b1e1cd155fc62fba0555a8d3549cccc6986db4 /src/game/client/components/sounds.cpp
parentd9ce7203871cea0046144bce175df5bf1d7fcc19 (diff)
downloadzcatch-0bc13c91ebcc6404fc67e079e5a9acad10176c98.tar.gz
zcatch-0bc13c91ebcc6404fc67e079e5a9acad10176c98.zip
made cl_threadsoundloading work again
Diffstat (limited to 'src/game/client/components/sounds.cpp')
-rw-r--r--src/game/client/components/sounds.cpp63
1 files changed, 56 insertions, 7 deletions
diff --git a/src/game/client/components/sounds.cpp b/src/game/client/components/sounds.cpp
index f162e5e6..f481b494 100644
--- a/src/game/client/components/sounds.cpp
+++ b/src/game/client/components/sounds.cpp
@@ -1,12 +1,39 @@
 /* (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.                */
+#include <engine/engine.h>
 #include <engine/sound.h>
 #include <engine/shared/config.h>
 #include <game/generated/client_data.h>
 #include <game/client/gameclient.h>
 #include <game/client/components/camera.h>
+#include <game/client/components/menus.h>
 #include "sounds.h"
 
+
+struct CUserData
+{
+	CGameClient *m_pGameClient;
+	bool m_Render;
+} g_UserData;
+
+static int LoadSoundsThread(void *pUser)
+{
+	CUserData *pData = static_cast<CUserData *>(pUser);
+	
+	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;
+}
 void CSounds::OnInit()
 {
 	// setup sound channels
@@ -18,6 +45,22 @@ void CSounds::OnInit()
 	Sound()->SetListenerPos(0.0f, 0.0f);
 
 	ClearQueue();
+
+	// 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;
+	}
 }
 
 void CSounds::OnReset()
@@ -28,6 +71,10 @@ void CSounds::OnReset()
 
 void CSounds::OnRender()
 {
+	// check for sound initialisation
+	if(m_WaitForSoundJob && m_SoundJob.Status() == CJob::STATE_DONE)
+		m_WaitForSoundJob = false;
+
 	// set listner pos
 	Sound()->SetListenerPos(m_pClient->m_pCamera->m_Center.x, m_pClient->m_pCamera->m_Center.y);
 
@@ -70,7 +117,7 @@ void CSounds::PlayAndRecord(int Chn, int SetId, float Vol, vec2 Pos)
 
 void CSounds::Play(int Chn, int SetId, float Vol, vec2 Pos)
 {
-	if(SetId < 0 || SetId >= g_pData->m_NumSounds)
+	if(m_WaitForSoundJob || SetId < 0 || SetId >= g_pData->m_NumSounds)
 		return;
 
 	SOUNDSET *pSet = &g_pData->m_aSounds[SetId];
@@ -85,10 +132,12 @@ void CSounds::Play(int Chn, int SetId, float Vol, vec2 Pos)
 	}
 
 	// play a random one
-	int id;
-	do {
-		id = rand() % pSet->m_NumSounds;
-	} while(id == pSet->m_Last);
-	Sound()->PlayAt(Chn, pSet->m_aSounds[id].m_Id, 0, Pos.x, Pos.y);
-	pSet->m_Last = id;
+	int Id;
+	do
+	{
+		Id = rand() % pSet->m_NumSounds;
+	}
+	while(Id == pSet->m_Last);
+	Sound()->PlayAt(Chn, pSet->m_aSounds[Id].m_Id, 0, Pos.x, Pos.y);
+	pSet->m_Last = Id;
 }