Skip to content

Instantly share code, notes, and snippets.

@jdmunro
Created December 2, 2008 14:49
Show Gist options
  • Save jdmunro/31131 to your computer and use it in GitHub Desktop.
Save jdmunro/31131 to your computer and use it in GitHub Desktop.
diff --git a/code/client/snd_local.h b/code/client/snd_local.h
index 86c7d4a..62b998f 100644
--- a/code/client/snd_local.h
+++ b/code/client/snd_local.h
@@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "../qcommon/q_shared.h"
#include "../qcommon/qcommon.h"
#include "snd_public.h"
+#include <SDL_thread.h>
#define PAINTBUFFER_SIZE 4096 // this is in samples
@@ -32,6 +33,31 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#define SND_CHUNK_SIZE_FLOAT (SND_CHUNK_SIZE/2) // floats
#define SND_CHUNK_SIZE_BYTE (SND_CHUNK_SIZE*2) // floats
+// holds the thread objects
+typedef struct {
+ SDL_Thread *thread;
+ SDL_mutex *lock;
+ SDL_cond *update;
+} sndThread_t;
+
+// the data to be used for each thread update
+typedef struct {
+ vec3_t origin;
+ sfxHandle_t sfx;
+ int entnum;
+ int channel;
+ int nullorg;
+ int type;
+ int stream;
+ int samples;
+ int rate;
+ int width;
+ byte *data;
+ float volume;
+ char loop[128];
+ char intro[128];
+} sndThreadRes_t;
+
typedef struct {
int left; // the final values will be clamped to +/- 0x00ffff00 and shifted down
int right;
diff --git a/code/client/snd_main.c b/code/client/snd_main.c
index 2f48117..657da72 100644
--- a/code/client/snd_main.c
+++ b/code/client/snd_main.c
@@ -33,6 +33,8 @@ cvar_t *s_backend;
cvar_t *s_muteWhenMinimized;
static soundInterface_t si;
+static sndThread_t st;
+static sndThreadRes_t sr;
/*
=================
@@ -80,9 +82,23 @@ S_StartSound
*/
void S_StartSound( vec3_t origin, int entnum, int entchannel, sfxHandle_t sfx )
{
- if( si.StartSound ) {
- si.StartSound( origin, entnum, entchannel, sfx );
- }
+ SDL_mutexP( st.lock );
+
+ if( origin == NULL ) {
+ sr.nullorg = 1;
+ } else {
+ sr.nullorg = 0;
+ VectorCopy( origin, sr.origin );
+ }
+
+ sr.type = 0;
+ sr.entnum = entnum;
+ sr.channel = entchannel;
+ memcpy( &sr.sfx, &sfx, sizeof( sfxHandle_t ) );
+
+
+ SDL_mutexV( st.lock );
+ SDL_CondSignal( st.update );
}
/*
@@ -92,9 +108,14 @@ S_StartLocalSound
*/
void S_StartLocalSound( sfxHandle_t sfx, int channelNum )
{
- if( si.StartLocalSound ) {
- si.StartLocalSound( sfx, channelNum );
- }
+ SDL_mutexP( st.lock );
+
+ sr.type = 1;
+ sr.channel = channelNum;
+ memcpy( &sr.sfx, &sfx, sizeof( sfxHandle_t ) );
+
+ SDL_mutexV( st.lock );
+ SDL_CondSignal( st.update );
}
/*
@@ -104,9 +125,16 @@ S_StartBackgroundTrack
*/
void S_StartBackgroundTrack( const char *intro, const char *loop )
{
- if( si.StartBackgroundTrack ) {
- si.StartBackgroundTrack( intro, loop );
- }
+ SDL_mutexP( st.lock );
+
+ sr.type = 2;
+
+ // FIXME this is a bit crude
+ strcpy( sr.intro, intro );
+ strcpy( sr.loop, loop );
+
+ SDL_mutexV( st.lock );
+ SDL_CondSignal( st.update );
}
/*
@@ -436,6 +464,55 @@ void S_Music_f( void ) {
/*
=================
+S_Thread
+=================
+*/
+int S_Thread( void *unused )
+{
+ // FIXME this needs to be while game is active
+ while( 1 ) {
+ SDL_mutexP( st.lock );
+ SDL_CondWait( st.update, st.lock );
+
+ // this is kinda like the syscall functionality
+ switch( sr.type ) {
+ case 0:
+ {
+ if( si.StartSound ) {
+ if( sr.nullorg ) {
+ si.StartSound( NULL, sr.entnum, sr.channel, sr.sfx );
+ } else {
+ si.StartSound( sr.origin, sr.entnum, sr.channel, sr.sfx );
+ }
+ }
+ break;
+ }
+
+ case 1:
+ {
+ if( si.StartLocalSound ) {
+ si.StartLocalSound( sr.sfx, sr.channel );
+ }
+ break;
+ }
+
+ case 2:
+ {
+ if( si.StartBackgroundTrack ) {
+ si.StartBackgroundTrack( sr.intro, sr.loop );
+ }
+ break;
+ }
+ }
+
+ SDL_mutexV( st.lock );
+ }
+
+ return 0;
+}
+
+/*
+=================
S_Init
=================
*/
@@ -487,6 +564,11 @@ void S_Init( void )
} else {
Com_Printf( "Sound initialization failed.\n" );
}
+
+ Com_Printf( "Initializing sound thread\n" );
+ st.lock = SDL_CreateMutex();
+ st.update = SDL_CreateCond();
+ st.thread = SDL_CreateThread(&S_Thread,NULL);
}
Com_Printf( "--------------------------------\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment