]> git.lizzy.rs Git - nothing.git/commitdiff
Merge pull request #137 from tsoding/135
authorAlexey Kutepov <reximkut@gmail.com>
Sun, 8 Apr 2018 18:24:01 +0000 (01:24 +0700)
committerGitHub <noreply@github.com>
Sun, 8 Apr 2018 18:24:01 +0000 (01:24 +0700)
(#135) Play sounds on a free channel

src/main.c
src/sound_medium.c

index 85a289b5b5de55d56e684957d9aa1d7754fcbee0..e2e8127a0bcf56f067d6fc5032e47e781420d3c3 100644 (file)
@@ -111,8 +111,6 @@ int main(int argc, char *argv[])
     }
     PUSH_LT(lt, 42, Mix_CloseAudio_lt);
 
-    Mix_AllocateChannels(16);
-
     Mix_Chunk * sound_samples[] = {
         PUSH_LT(lt, Mix_LoadWAV("./sounds/nothing.wav"), Mix_FreeChunk),
         PUSH_LT(lt, Mix_LoadWAV("./sounds/something.wav"), Mix_FreeChunk)
index 1a688ac7c669d6fc87e3d078e890d63b40cfb5ac..18802217159d7aa7f8640d25feb8a9527e85a806 100644 (file)
@@ -16,6 +16,17 @@ struct sound_medium_t
     size_t samples_count;
 };
 
+static int mix_get_free_channel(void)
+{
+    for (int i = 0; i < MIX_CHANNELS; ++i) {
+        if (!Mix_Playing(i)) {
+            return i;
+        }
+    }
+
+    return -1;
+}
+
 sound_medium_t *create_sound_medium(Mix_Chunk **samples, size_t samples_count)
 {
     assert(samples);
@@ -56,7 +67,11 @@ int sound_medium_play_sound(sound_medium_t *sound_medium,
     (void) position;
 
     if (sound_index < sound_medium->samples_count) {
-        return Mix_PlayChannel(0, sound_medium->samples[sound_index], 0);
+        const int free_channel = mix_get_free_channel();
+
+        if (free_channel >= 0) {
+            return Mix_PlayChannel(free_channel, sound_medium->samples[sound_index], 0);
+        }
     }
 
     return 0;