]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_dma.c
Removed a potential buffer overflow and factorized some code
[xonotic/darkplaces.git] / snd_dma.c
index 68f33488da269bf2dacce3352fff6d5b016bc094..de8b482b0c3f82cee5ad5153cf7139422188cd2f 100644 (file)
--- a/snd_dma.c
+++ b/snd_dma.c
@@ -42,16 +42,16 @@ 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;
 volatile dma_t sn;
 
-vec3_t listener_origin;
-vec3_t listener_forward;
-vec3_t listener_right;
-vec3_t listener_up;
+vec3_t listener_vieworigin;
+vec3_t listener_viewforward;
+vec3_t listener_viewleft;
+vec3_t listener_viewup;
 vec_t sound_nominal_clip_dist=1000.0;
 mempool_t *snd_mempool;
 
@@ -144,7 +144,7 @@ void S_LoadSounds(void)
 
 void S_Startup(void)
 {
-       if (!snd_initialized)
+       if (!snd_initialized.integer)
                return;
 
        shm = &sn;
@@ -236,6 +236,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 +245,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;
@@ -263,6 +264,32 @@ void S_Init(void)
 // Load a sound
 // =======================================================================
 
+/*
+=========
+S_IsCached
+
+=========
+*/
+sfx_t *S_GetCached (const char *name)
+{
+       int i;
+
+       if (!snd_initialized.integer)
+               return NULL;
+
+       if (!name)
+               Host_Error("S_IsCached: NULL\n");
+
+       if (strlen(name) >= MAX_QPATH)
+               Host_Error("Sound name too long: %s", name);
+
+       for(i = 0;i < num_sfx;i++)
+               if(!strcmp(known_sfx[i].name, name))
+                       return &known_sfx[i];
+
+       return NULL;
+}
+
 /*
 ==================
 S_FindName
@@ -274,7 +301,7 @@ sfx_t *S_FindName (char *name)
        int i;
        sfx_t *sfx;
 
-       if (!snd_initialized)
+       if (!snd_initialized.integer)
                return NULL;
 
        if (!name)
@@ -319,7 +346,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);
@@ -406,15 +433,15 @@ void SND_Spatialize(channel_t *ch, int isstatic)
                }
 
                // calculate stereo seperation and distance attenuation
-               VectorSubtract(ch->origin, listener_origin, source_vec);
+               VectorSubtract(ch->origin, listener_vieworigin, source_vec);
                dist = VectorNormalizeLength(source_vec);
                // distance
                scale = ch->master_vol * (1.0 - (dist * ch->dist_mult));
                // panning
-               pan = scale * DotProduct(listener_right, source_vec);
+               pan = scale * DotProduct(listener_viewleft, source_vec);
                // calculate the volumes
-               ch->leftvol = (int) (scale - pan);
-               ch->rightvol = (int) (scale + pan);
+               ch->leftvol = (int) (scale + pan);
+               ch->rightvol = (int) (scale - pan);
        }
 
        // LordHavoc: allow adjusting volume of static sounds
@@ -461,8 +488,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);
@@ -650,7 +678,7 @@ void S_UpdateAmbientSounds (void)
        if (!snd_ambient || ambient_level.value <= 0 || !cl.worldmodel || !cl.worldmodel->brush.AmbientSoundLevelsForPoint)
                return;
 
-       cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_origin, ambientlevels, sizeof(ambientlevels));
+       cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_vieworigin, ambientlevels, sizeof(ambientlevels));
 
 // calc ambient sound levels
        for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
@@ -691,20 +719,20 @@ S_Update
 Called once each time through the main loop
 ============
 */
-void S_Update(vec3_t origin, vec3_t forward, vec3_t right, vec3_t up)
+void S_Update(vec3_t origin, vec3_t forward, vec3_t left, vec3_t up)
 {
        int                     i, j;
        int                     total;
        channel_t       *ch;
        channel_t       *combine;
 
-       if (!snd_initialized || (snd_blocked > 0))
+       if (!snd_initialized.integer || (snd_blocked > 0))
                return;
 
-       VectorCopy(origin, listener_origin);
-       VectorCopy(forward, listener_forward);
-       VectorCopy(right, listener_right);
-       VectorCopy(up, listener_up);
+       VectorCopy(origin, listener_vieworigin);
+       VectorCopy(forward, listener_viewforward);
+       VectorCopy(left, listener_viewleft);
+       VectorCopy(up, listener_viewup);
 
 // update general area ambient sound sources
        S_UpdateAmbientSounds ();
@@ -874,7 +902,7 @@ console functions
 ===============================================================================
 */
 
-void S_Play(void)
+static void S_Play_Common(float fvol, float attenuation)
 {
        int     i;
        char name[256];
@@ -884,62 +912,37 @@ void S_Play(void)
        while (i<Cmd_Argc())
        {
                if (!strrchr(Cmd_Argv(i), '.'))
+                       snprintf(name, sizeof(name), "%s.wav", Cmd_Argv(i));
+               else
+                       strlcpy(name, Cmd_Argv(i), sizeof(name));
+               sfx = S_PrecacheSound(name, true);
+
+               // If we need to get the volume from the command line
+               if (fvol == -1.0f)
                {
-                       strcpy(name, Cmd_Argv(i));
-                       strcat(name, ".wav");
+                       fvol = atof(Cmd_Argv(i+1));
+                       i += 2;
                }
                else
-                       strcpy(name, Cmd_Argv(i));
-               sfx = S_PrecacheSound(name, true);
-               S_StartSound(-1, 0, sfx, listener_origin, 1.0, 1.0);
-               i++;
+                       i++;
+
+               S_StartSound(-1, 0, sfx, listener_vieworigin, fvol, attenuation);
        }
 }
 
-void S_Play2(void)
+void S_Play(void)
 {
-       int     i;
-       char name[256];
-       sfx_t   *sfx;
+       S_Play_Common (1.0f, 1.0f);
+}
 
-       i = 1;
-       while (i<Cmd_Argc())
-       {
-               if (!strrchr(Cmd_Argv(i), '.'))
-               {
-                       strcpy(name, Cmd_Argv(i));
-                       strcat(name, ".wav");
-               }
-               else
-                       strcpy(name, Cmd_Argv(i));
-               sfx = S_PrecacheSound(name, true);
-               S_StartSound(-1, 0, sfx, listener_origin, 1.0, 0.0);
-               i++;
-       }
+void S_Play2(void)
+{
+       S_Play_Common (1.0f, 0.0f);
 }
 
 void S_PlayVol(void)
 {
-       int i;
-       float vol;
-       char name[256];
-       sfx_t   *sfx;
-
-       i = 1;
-       while (i<Cmd_Argc())
-       {
-               if (!strrchr(Cmd_Argv(i), '.'))
-               {
-                       strcpy(name, Cmd_Argv(i));
-                       strcat(name, ".wav");
-               }
-               else
-                       strcpy(name, Cmd_Argv(i));
-               sfx = S_PrecacheSound(name, true);
-               vol = atof(Cmd_Argv(i+1));
-               S_StartSound(-1, 0, sfx, listener_origin, vol, 1.0);
-               i+=2;
-       }
+       S_Play_Common (-1.0f, 0.0f);
 }
 
 void S_SoundList(void)
@@ -968,7 +971,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);