Fixed looping behavior and Mix_Playing() result for looping channels
authorSam Lantinga <slouken@libsdl.org>
Tue, 09 Jul 2013 06:59:10 -0700
changeset 648bd8389c6dd20
parent 647 b3176bed9dcf
child 649 8e1e3c53786e
Fixed looping behavior and Mix_Playing() result for looping channels

Lee Salzman

If you passed in a negative loop count, it is supposed to behave as if it loops forever. However, it blindly decrements the loop counter regardless of whether or not the loop count is actually positive. So first fix is just to put a > 0 guard before the loop counter decrement.

The second fix is that Mix_Playing somehow checked only for looping > 0, so it ignored negative loop counts entirely, which just seems like a bug too. This has prevented me from using Mix_Playing internally in my engine if only because it never actually worked with looping. Mix_PlayingMusic oddly does it correctly and checks for non-zero.
mixer.c
music.c
native_midi/native_midi_haiku.cpp
native_midi/native_midi_win32.c
     1.1 --- a/mixer.c	Fri Jun 28 09:54:16 2013 -0700
     1.2 +++ b/mixer.c	Tue Jul 09 06:59:10 2013 -0700
     1.3 @@ -385,13 +385,17 @@
     1.4                      if (mix_input != mix_channel[i].chunk->abuf)
     1.5                          SDL_free(mix_input);
     1.6  
     1.7 -                    --mix_channel[i].looping;
     1.8 +                    if (mix_channel[i].looping > 0) {
     1.9 +                        --mix_channel[i].looping;
    1.10 +                    }
    1.11                      mix_channel[i].samples = mix_channel[i].chunk->abuf + remaining;
    1.12                      mix_channel[i].playing = mix_channel[i].chunk->alen - remaining;
    1.13                      index += remaining;
    1.14                  }
    1.15                  if ( ! mix_channel[i].playing && mix_channel[i].looping ) {
    1.16 -                    --mix_channel[i].looping;
    1.17 +                    if (mix_channel[i].looping > 0) {
    1.18 +                        --mix_channel[i].looping;
    1.19 +                    }
    1.20                      mix_channel[i].samples = mix_channel[i].chunk->abuf;
    1.21                      mix_channel[i].playing = mix_channel[i].chunk->alen;
    1.22                  }
    1.23 @@ -1111,14 +1115,14 @@
    1.24  
    1.25          for ( i=0; i<num_channels; ++i ) {
    1.26              if ((mix_channel[i].playing > 0) ||
    1.27 -                (mix_channel[i].looping > 0))
    1.28 +                mix_channel[i].looping)
    1.29              {
    1.30                  ++status;
    1.31              }
    1.32          }
    1.33      } else if ( which < num_channels ) {
    1.34          if ( (mix_channel[which].playing > 0) ||
    1.35 -             (mix_channel[which].looping > 0) )
    1.36 +             mix_channel[which].looping )
    1.37          {
    1.38              ++status;
    1.39          }
     2.1 --- a/music.c	Fri Jun 28 09:54:16 2013 -0700
     2.2 +++ b/music.c	Tue Jul 09 06:59:10 2013 -0700
     2.3 @@ -210,7 +210,9 @@
     2.4          if (music_loops)
     2.5          {
     2.6              Mix_Fading current_fade;
     2.7 -            --music_loops;
     2.8 +            if (music_loops > 0) {
     2.9 +                --music_loops;
    2.10 +            }
    2.11              current_fade = music_playing->fading;
    2.12              music_internal_play(music_playing, 0.0);
    2.13              music_playing->fading = current_fade;
     3.1 --- a/native_midi/native_midi_haiku.cpp	Fri Jun 28 09:54:16 2013 -0700
     3.2 +++ b/native_midi/native_midi_haiku.cpp	Tue Jul 09 06:59:10 2013 -0700
     3.3 @@ -71,7 +71,7 @@
     3.4      {
     3.5        if (!ev) {
     3.6          if (fLoops && fEvs) {
     3.7 -          --fLoops;
     3.8 +          if (fLoops > 0) --fLoops;
     3.9            fPos = 0;
    3.10            ev = fEvs;
    3.11          } else
     4.1 --- a/native_midi/native_midi_win32.c	Fri Jun 28 09:54:16 2013 -0700
     4.2 +++ b/native_midi/native_midi_win32.c	Tue Jul 09 06:59:10 2013 -0700
     4.3 @@ -175,7 +175,8 @@
     4.4      case MOM_POSITIONCB:
     4.5        if ((currentsong->MusicLoaded) && (dwParam1 == (DWORD_PTR)&currentsong->MidiStreamHdr[currentsong->CurrentHdr])) {
     4.6          if (currentsong->Loops) {
     4.7 -          --currentsong->Loops;
     4.8 +          if (currentsong->Loops > 0)
     4.9 +            --currentsong->Loops;
    4.10            currentsong->NewPos=0;
    4.11            BlockOut(currentsong);
    4.12          } else {