Skip to content

Commit

Permalink
SFX|Fixed: Sound priorities on the first tic
Browse files Browse the repository at this point in the history
The listener mobj was not updated until the end of the first frame.
This meant sound priority calculations were not working correctly
if the sounds were started right at the start of the map.

The listener is now updated immediately after the map has been
set up.
  • Loading branch information
skyjake committed Mar 2, 2012
1 parent 1f629f7 commit 94dd92c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
6 changes: 6 additions & 0 deletions doomsday/engine/portable/include/s_main.h
Expand Up @@ -48,6 +48,12 @@ void S_Register(void);
boolean S_Init(void);
void S_Shutdown(void);
void S_MapChange(void);

/**
* Must be called after the map has been changed.
*/
void S_SetupForChangedMap(void);

void S_Reset(void);
void S_StartFrame(void);
void S_EndFrame(void);
Expand Down
1 change: 1 addition & 0 deletions doomsday/engine/portable/include/s_sfx.h
Expand Up @@ -66,6 +66,7 @@ void Sfx_Shutdown(void);
void Sfx_Reset(void);
void Sfx_AllowRefresh(boolean allow);
void Sfx_MapChange(void);
void Sfx_SetListener(mobj_t* mobj);
void Sfx_StartFrame(void);
void Sfx_EndFrame(void);
void Sfx_PurgeCache(void);
Expand Down
2 changes: 2 additions & 0 deletions doomsday/engine/portable/src/r_world.c
Expand Up @@ -1409,6 +1409,8 @@ void R_SetupMap(int mode, int flags)
Materials_ProcessCacheQueue();
VERBOSE( Con_Message("Precaching took %.2f seconds.\n", Sys_GetSeconds() - startTime) )

S_SetupForChangedMap();

// Map setup has been completed.

// Run any commands specified in Map Info.
Expand Down
6 changes: 6 additions & 0 deletions doomsday/engine/portable/src/s_main.c
Expand Up @@ -158,6 +158,12 @@ void S_MapChange(void)
Sfx_MapChange();
}

void S_SetupForChangedMap(void)
{
// Update who is listening now.
Sfx_SetListener(S_GetListenerMobj());
}

/**
* Stop all channels and music, delete the entire sample cache.
*/
Expand Down
38 changes: 21 additions & 17 deletions doomsday/engine/portable/src/s_sfx.c
Expand Up @@ -318,16 +318,15 @@ int Sfx_CountPlaying(int id)
/**
* The priority of a sound is affected by distance, volume and age.
*/
float Sfx_Priority(mobj_t* emitter, float* fixPos, float volume,
int startTic)
float Sfx_Priority(mobj_t* emitter, float* fixPos, float volume, int startTic)
{
// In five seconds all priority of a sound is gone.
float timeoff =
1000 * (Sys_GetTime() - startTic) / (5.0f * TICSPERSEC);
float orig[3];
float timeoff = 1000 * (Sys_GetTime() - startTic) / (5.0f * TICSPERSEC);
float orig[3];

if(!listener || (!emitter && !fixPos))
{ // The sound does not have an origin.
{
// The sound does not have an origin.
return 1000 * volume - timeoff;
}

Expand All @@ -342,8 +341,7 @@ float Sfx_Priority(mobj_t* emitter, float* fixPos, float volume,
memcpy(orig, fixPos, sizeof(orig));
}

return 1000 * volume - P_MobjPointDistancef(listener, 0, orig) / 2 -
timeoff;
return 1000 * volume - P_MobjPointDistancef(listener, 0, orig) / 2 - timeoff;
}

/**
Expand Down Expand Up @@ -511,6 +509,11 @@ void Sfx_ChannelUpdate(sfxchannel_t* ch)
}
}

void Sfx_SetListener(mobj_t* mobj)
{
listener = mobj;
}

void Sfx_ListenerUpdate(void)
{
int i;
Expand All @@ -521,7 +524,7 @@ void Sfx_ListenerUpdate(void)
return;

// Update the listener mobj.
listener = S_GetListenerMobj();
Sfx_SetListener(S_GetListenerMobj());

if(listener)
{
Expand Down Expand Up @@ -680,13 +683,13 @@ int Sfx_StartSound(sfxsample_t* sample, float volume, float freq,
for(selCh = NULL, i = 0, ch = channels; i < numChannels;
++i, ch++)
{
if(ch->buffer && (ch->buffer->flags & SFXBF_PLAYING) &&
ch->buffer->sample->id == sample->id &&
myPrio >= channelPrios[i] &&
(!selCh || channelPrios[i] <= lowPrio))
if(ch->buffer && (ch->buffer->flags & SFXBF_PLAYING) && ch->buffer->sample->id == sample->id)
{
selCh = ch;
lowPrio = channelPrios[i];
if(myPrio >= channelPrios[i] && (!selCh || channelPrios[i] <= lowPrio))
{
selCh = ch;
lowPrio = channelPrios[i];
}
}
}

Expand Down Expand Up @@ -1265,11 +1268,12 @@ void Sfx_DebugInfo(void)
FR_SetColor(1, 1, 0);
}

sprintf(buf, "%02i: %c%c%c v=%3.1f f=%3.3f st=%i et=%u", i,
sprintf(buf, "%02i: %c%c%c v=%3.1f f=%3.3f st=%i et=%u mobj=%i", i,
!(ch->flags & SFXCF_NO_ORIGIN) ? 'O' : '.',
!(ch->flags & SFXCF_NO_ATTENUATION) ? 'A' : '.',
ch->emitter ? 'E' : '.', ch->volume, ch->frequency,
ch->startTime, ch->buffer ? ch->buffer->endTime : 0);
ch->startTime, ch->buffer ? ch->buffer->endTime : 0,
ch->emitter? ch->emitter->thinker.id : 0);
FR_DrawTextXY(buf, 5, lh * (1 + i * 2));

if(!ch->buffer) continue;
Expand Down

0 comments on commit 94dd92c

Please sign in to comment.