Guest User

Untitled

a guest
Feb 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. diff --git a/code/client/snd_local.h b/code/client/snd_local.h
  2. index 86c7d4a..62b998f 100644
  3. --- a/code/client/snd_local.h
  4. +++ b/code/client/snd_local.h
  5. @@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  6. #include "../qcommon/q_shared.h"
  7. #include "../qcommon/qcommon.h"
  8. #include "snd_public.h"
  9. +#include <SDL_thread.h>
  10.  
  11. #define PAINTBUFFER_SIZE 4096 // this is in samples
  12.  
  13. @@ -32,6 +33,31 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. #define SND_CHUNK_SIZE_FLOAT (SND_CHUNK_SIZE/2) // floats
  15. #define SND_CHUNK_SIZE_BYTE (SND_CHUNK_SIZE*2) // floats
  16.  
  17. +// holds the thread objects
  18. +typedef struct {
  19. + SDL_Thread *thread;
  20. + SDL_mutex *lock;
  21. + SDL_cond *update;
  22. +} sndThread_t;
  23. +
  24. +// the data to be used for each thread update
  25. +typedef struct {
  26. + vec3_t origin;
  27. + sfxHandle_t sfx;
  28. + int entnum;
  29. + int channel;
  30. + int nullorg;
  31. + int type;
  32. + int stream;
  33. + int samples;
  34. + int rate;
  35. + int width;
  36. + byte *data;
  37. + float volume;
  38. + char loop[128];
  39. + char intro[128];
  40. +} sndThreadRes_t;
  41. +
  42. typedef struct {
  43. int left; // the final values will be clamped to +/- 0x00ffff00 and shifted down
  44. int right;
  45. diff --git a/code/client/snd_main.c b/code/client/snd_main.c
  46. index 2f48117..657da72 100644
  47. --- a/code/client/snd_main.c
  48. +++ b/code/client/snd_main.c
  49. @@ -33,6 +33,8 @@ cvar_t *s_backend;
  50. cvar_t *s_muteWhenMinimized;
  51.  
  52. static soundInterface_t si;
  53. +static sndThread_t st;
  54. +static sndThreadRes_t sr;
  55.  
  56. /*
  57. =================
  58. @@ -80,9 +82,23 @@ S_StartSound
  59. */
  60. void S_StartSound( vec3_t origin, int entnum, int entchannel, sfxHandle_t sfx )
  61. {
  62. - if( si.StartSound ) {
  63. - si.StartSound( origin, entnum, entchannel, sfx );
  64. - }
  65. + SDL_mutexP( st.lock );
  66. +
  67. + if( origin == NULL ) {
  68. + sr.nullorg = 1;
  69. + } else {
  70. + sr.nullorg = 0;
  71. + VectorCopy( origin, sr.origin );
  72. + }
  73. +
  74. + sr.type = 0;
  75. + sr.entnum = entnum;
  76. + sr.channel = entchannel;
  77. + memcpy( &sr.sfx, &sfx, sizeof( sfxHandle_t ) );
  78. +
  79. +
  80. + SDL_mutexV( st.lock );
  81. + SDL_CondSignal( st.update );
  82. }
  83.  
  84. /*
  85. @@ -92,9 +108,14 @@ S_StartLocalSound
  86. */
  87. void S_StartLocalSound( sfxHandle_t sfx, int channelNum )
  88. {
  89. - if( si.StartLocalSound ) {
  90. - si.StartLocalSound( sfx, channelNum );
  91. - }
  92. + SDL_mutexP( st.lock );
  93. +
  94. + sr.type = 1;
  95. + sr.channel = channelNum;
  96. + memcpy( &sr.sfx, &sfx, sizeof( sfxHandle_t ) );
  97. +
  98. + SDL_mutexV( st.lock );
  99. + SDL_CondSignal( st.update );
  100. }
  101.  
  102. /*
  103. @@ -104,9 +125,16 @@ S_StartBackgroundTrack
  104. */
  105. void S_StartBackgroundTrack( const char *intro, const char *loop )
  106. {
  107. - if( si.StartBackgroundTrack ) {
  108. - si.StartBackgroundTrack( intro, loop );
  109. - }
  110. + SDL_mutexP( st.lock );
  111. +
  112. + sr.type = 2;
  113. +
  114. + // FIXME this is a bit crude
  115. + strcpy( sr.intro, intro );
  116. + strcpy( sr.loop, loop );
  117. +
  118. + SDL_mutexV( st.lock );
  119. + SDL_CondSignal( st.update );
  120. }
  121.  
  122. /*
  123. @@ -436,6 +464,55 @@ void S_Music_f( void ) {
  124.  
  125. /*
  126. =================
  127. +S_Thread
  128. +=================
  129. +*/
  130. +int S_Thread( void *unused )
  131. +{
  132. + // FIXME this needs to be while game is active
  133. + while( 1 ) {
  134. + SDL_mutexP( st.lock );
  135. + SDL_CondWait( st.update, st.lock );
  136. +
  137. + // this is kinda like the syscall functionality
  138. + switch( sr.type ) {
  139. + case 0:
  140. + {
  141. + if( si.StartSound ) {
  142. + if( sr.nullorg ) {
  143. + si.StartSound( NULL, sr.entnum, sr.channel, sr.sfx );
  144. + } else {
  145. + si.StartSound( sr.origin, sr.entnum, sr.channel, sr.sfx );
  146. + }
  147. + }
  148. + break;
  149. + }
  150. +
  151. + case 1:
  152. + {
  153. + if( si.StartLocalSound ) {
  154. + si.StartLocalSound( sr.sfx, sr.channel );
  155. + }
  156. + break;
  157. + }
  158. +
  159. + case 2:
  160. + {
  161. + if( si.StartBackgroundTrack ) {
  162. + si.StartBackgroundTrack( sr.intro, sr.loop );
  163. + }
  164. + break;
  165. + }
  166. + }
  167. +
  168. + SDL_mutexV( st.lock );
  169. + }
  170. +
  171. + return 0;
  172. +}
  173. +
  174. +/*
  175. +=================
  176. S_Init
  177. =================
  178. */
  179. @@ -487,6 +564,11 @@ void S_Init( void )
  180. } else {
  181. Com_Printf( "Sound initialization failed.\n" );
  182. }
  183. +
  184. + Com_Printf( "Initializing sound thread\n" );
  185. + st.lock = SDL_CreateMutex();
  186. + st.update = SDL_CreateCond();
  187. + st.thread = SDL_CreateThread(&S_Thread,NULL);
  188. }
  189.  
  190. Com_Printf( "--------------------------------\n");
Add Comment
Please, Sign In to add comment