(#135) Play sounds on a free channel
}
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)
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);
(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;