From ddd231037232d5a8f3a34d391e638a7a080d8622 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 9 Jul 2013 06:59:10 -0700 Subject: [PATCH] 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 | 12 ++++++++---- music.c | 4 +++- native_midi/native_midi_haiku.cpp | 2 +- native_midi/native_midi_win32.c | 3 ++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/mixer.c b/mixer.c index d8ccbbf3..651c9497 100644 --- a/mixer.c +++ b/mixer.c @@ -385,13 +385,17 @@ static void mix_channels(void *udata, Uint8 *stream, int len) if (mix_input != mix_channel[i].chunk->abuf) SDL_free(mix_input); - --mix_channel[i].looping; + if (mix_channel[i].looping > 0) { + --mix_channel[i].looping; + } mix_channel[i].samples = mix_channel[i].chunk->abuf + remaining; mix_channel[i].playing = mix_channel[i].chunk->alen - remaining; index += remaining; } if ( ! mix_channel[i].playing && mix_channel[i].looping ) { - --mix_channel[i].looping; + if (mix_channel[i].looping > 0) { + --mix_channel[i].looping; + } mix_channel[i].samples = mix_channel[i].chunk->abuf; mix_channel[i].playing = mix_channel[i].chunk->alen; } @@ -1111,14 +1115,14 @@ int Mix_Playing(int which) for ( i=0; i 0) || - (mix_channel[i].looping > 0)) + mix_channel[i].looping) { ++status; } } } else if ( which < num_channels ) { if ( (mix_channel[which].playing > 0) || - (mix_channel[which].looping > 0) ) + mix_channel[which].looping ) { ++status; } diff --git a/music.c b/music.c index 4f14d5b6..76d967d8 100644 --- a/music.c +++ b/music.c @@ -210,7 +210,9 @@ static int music_halt_or_loop (void) if (music_loops) { Mix_Fading current_fade; - --music_loops; + if (music_loops > 0) { + --music_loops; + } current_fade = music_playing->fading; music_internal_play(music_playing, 0.0); music_playing->fading = current_fade; diff --git a/native_midi/native_midi_haiku.cpp b/native_midi/native_midi_haiku.cpp index 594b0ce4..3d524918 100644 --- a/native_midi/native_midi_haiku.cpp +++ b/native_midi/native_midi_haiku.cpp @@ -71,7 +71,7 @@ class MidiEventsStore : public BMidi { if (!ev) { if (fLoops && fEvs) { - --fLoops; + if (fLoops > 0) --fLoops; fPos = 0; ev = fEvs; } else diff --git a/native_midi/native_midi_win32.c b/native_midi/native_midi_win32.c index 410407b4..e277b61e 100644 --- a/native_midi/native_midi_win32.c +++ b/native_midi/native_midi_win32.c @@ -175,7 +175,8 @@ void CALLBACK MidiProc( HMIDIIN hMidi, UINT uMsg, DWORD_PTR dwInstance, case MOM_POSITIONCB: if ((currentsong->MusicLoaded) && (dwParam1 == (DWORD_PTR)¤tsong->MidiStreamHdr[currentsong->CurrentHdr])) { if (currentsong->Loops) { - --currentsong->Loops; + if (currentsong->Loops > 0) + --currentsong->Loops; currentsong->NewPos=0; BlockOut(currentsong); } else {