]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_dma.c
cleaned up R_RenderView setup code a bit, and shuffled SCR_CalcRefdef stuff to SCR_Up...
[xonotic/darkplaces.git] / snd_dma.c
index 32de7663e2c19e73baa7b35b07cc8b19f8153301..e3b4a09b439aab65cbba8b6072660f897ffb73db 100644 (file)
--- a/snd_dma.c
+++ b/snd_dma.c
@@ -22,9 +22,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "quakedef.h"
 
 #ifdef _WIN32
-#include "winquake.h"
+#include <windows.h>
+#include <dsound.h>
+extern DWORD gSndBufSize;
+extern LPDIRECTSOUND pDS;
+extern LPDIRECTSOUNDBUFFER pDSBuf;
 #endif
 
+#include "ogg.h"
+
+
 void S_Play(void);
 void S_PlayVol(void);
 void S_Play2(void);
@@ -42,7 +49,7 @@ int total_channels;
 
 int snd_blocked = 0;
 static qboolean snd_ambient = 1;
-qboolean snd_initialized = false;
+cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0"};
 
 // pointer should go away
 volatile dma_t *shm = 0;
@@ -144,7 +151,7 @@ void S_LoadSounds(void)
 
 void S_Startup(void)
 {
-       if (!snd_initialized)
+       if (!snd_initialized.integer)
                return;
 
        shm = &sn;
@@ -236,6 +243,7 @@ void S_Init(void)
 
        Cvar_RegisterVariable(&nosound);
        Cvar_RegisterVariable(&snd_precache);
+       Cvar_RegisterVariable(&snd_initialized);
        Cvar_RegisterVariable(&bgmbuffer);
        Cvar_RegisterVariable(&ambient_level);
        Cvar_RegisterVariable(&ambient_fade);
@@ -244,7 +252,7 @@ void S_Init(void)
        Cvar_RegisterVariable(&_snd_mixahead);
        Cvar_RegisterVariable(&snd_swapstereo); // LordHavoc: for people with backwards sound wiring
 
-       snd_initialized = true;
+       Cvar_SetValueQuick(&snd_initialized, true);
 
        known_sfx = Mem_Alloc(snd_mempool, MAX_SFX*sizeof(sfx_t));
        num_sfx = 0;
@@ -256,6 +264,8 @@ void S_Init(void)
 
        total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;   // no statics
        memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
+
+       OGG_OpenLibrary ();
 }
 
 
@@ -265,7 +275,7 @@ void S_Init(void)
 
 /*
 =========
-S_IsCached
+S_GetCached
 
 =========
 */
@@ -273,7 +283,7 @@ sfx_t *S_GetCached (const char *name)
 {
        int i;
 
-       if (!snd_initialized)
+       if (!snd_initialized.integer)
                return NULL;
 
        if (!name)
@@ -300,7 +310,7 @@ sfx_t *S_FindName (char *name)
        int i;
        sfx_t *sfx;
 
-       if (!snd_initialized)
+       if (!snd_initialized.integer)
                return NULL;
 
        if (!name)
@@ -332,9 +342,53 @@ S_TouchSound
 */
 void S_TouchSound (char *name)
 {
-       S_FindName(name);
+       sfx_t *sfx;
+
+       sfx = S_FindName (name);
+
+       // Set the "used" flag for this sound
+       if (sfx != NULL)
+               sfx->flags |= SFXFLAG_USED;
+}
+
+
+/*
+==================
+S_ClearUsed
+
+Reset the "used" flag of all precached sounds
+==================
+*/
+void S_ClearUsed (void)
+{
+       int i;
+
+       for (i = 0; i < num_sfx; i++)
+               known_sfx[i].flags &= ~SFXFLAG_USED;
+}
+
+
+/*
+==================
+S_PurgeUnused
+
+Free all precached sounds without the "used" flag
+==================
+*/
+void S_PurgeUnused (void)
+{
+       int i;
+       sfx_t *sfx;
+
+       for (i = 0; i < num_sfx; i++)
+       {
+               sfx = &known_sfx[i];
+               if (! (sfx->flags & SFXFLAG_USED))
+                       S_UnloadSound (sfx);
+       }
 }
 
+
 /*
 ==================
 S_PrecacheSound
@@ -345,7 +399,7 @@ sfx_t *S_PrecacheSound (char *name, int complain)
 {
        sfx_t *sfx;
 
-       if (!snd_initialized)
+       if (!snd_initialized.integer)
                return NULL;
 
        sfx = S_FindName(name);
@@ -468,7 +522,7 @@ void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f
        int             ch_idx;
        int             skip;
 
-       if (!sound_started || !sfx || nosound.integer)
+       if (!sound_started || !sfx || !sfx->sfxcache || nosound.integer)
                return;
 
        vol = fvol*255;
@@ -487,8 +541,9 @@ void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f
        target_chan->entchannel = entchannel;
        SND_Spatialize(target_chan, false);
 
-       if (!target_chan->leftvol && !target_chan->rightvol)
-               return;         // not audible at all
+       // LordHavoc: spawn the sound anyway because the player might teleport to it
+       //if (!target_chan->leftvol && !target_chan->rightvol)
+       //      return;         // not audible at all
 
 // new channel
        sc = S_LoadSound (sfx, true);
@@ -576,7 +631,7 @@ void S_ClearBuffer(void)
 
                reps = 0;
 
-               while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pData, &dwSize, NULL, NULL, 0)) != DS_OK)
+               while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, (LPVOID*)&pData, &dwSize, NULL, NULL, 0)) != DS_OK)
                {
                        if (hresult != DSERR_BUFFERLOST)
                        {
@@ -681,7 +736,8 @@ void S_UpdateAmbientSounds (void)
 // calc ambient sound levels
        for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
        {
-               if (ambient_sfx[ambient_channel] && ambient_sfx[ambient_channel]->silentlymissing)
+               if (ambient_sfx[ambient_channel] &&
+                       (ambient_sfx[ambient_channel]->flags & SFXFLAG_SILENTLYMISSING))
                        continue;
                chan = &channels[ambient_channel];
                chan->forceloop = true;
@@ -724,7 +780,7 @@ void S_Update(vec3_t origin, vec3_t forward, vec3_t left, vec3_t up)
        channel_t       *ch;
        channel_t       *combine;
 
-       if (!snd_initialized || (snd_blocked > 0))
+       if (!snd_initialized.integer || (snd_blocked > 0))
                return;
 
        VectorCopy(origin, listener_vieworigin);
@@ -810,12 +866,8 @@ void GetSoundtime(void)
 
 // it is possible to miscount buffers if it has wrapped twice between
 // calls to S_Update.  Oh well.
-#ifdef __sun__
-       soundtime = SNDDMA_GetSamples();
-#else
        samplepos = SNDDMA_GetDMAPos();
 
-
        if (samplepos < oldsamplepos)
        {
                buffers++;                                      // buffer wrapped
@@ -830,7 +882,6 @@ void GetSoundtime(void)
        oldsamplepos = samplepos;
 
        soundtime = buffers*fullsamples + samplepos/shm->channels;
-#endif
 }
 
 void IN_Accumulate (void);
@@ -875,7 +926,7 @@ void S_Update_(void)
 
                if (pDSBuf)
                {
-                       if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DD_OK)
+                       if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DS_OK)
                                Con_Printf ("Couldn't get sound buffer status\n");
 
                        if (dwStatus & DSBSTATUS_BUFFERLOST)
@@ -969,7 +1020,7 @@ void S_LocalSound (char *sound)
 {
        sfx_t   *sfx;
 
-       if (!snd_initialized || nosound.integer)
+       if (!snd_initialized.integer || nosound.integer)
                return;
 
        sfx = S_PrecacheSound (sound, true);
@@ -982,21 +1033,6 @@ void S_LocalSound (char *sound)
 }
 
 
-void S_ClearPrecache (void)
-{
-}
-
-
-void S_BeginPrecaching (void)
-{
-}
-
-
-void S_EndPrecaching (void)
-{
-}
-
-
 #define RAWSAMPLESBUFFER 32768
 short s_rawsamplesbuffer[RAWSAMPLESBUFFER * 2];
 int s_rawsamplesbuffer_start;