Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Mix_ChannelFinished work in C++ #161

Open
SDLBugzilla opened this issue Feb 11, 2021 · 0 comments
Open

make Mix_ChannelFinished work in C++ #161

SDLBugzilla opened this issue Feb 11, 2021 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@SDLBugzilla
Copy link
Collaborator

SDLBugzilla commented Feb 11, 2021

This bug report was migrated from our old Bugzilla tracker.

Reported in version: unspecified
Reported for operating system, platform: All, All

Comments on the original bug report:

On 2014-03-23 13:38:43 +0000, Daniel Sobe wrote:

Hi,

I suggest to change the Mix_ChannelFinished callback (or add a new appropriate one if API compatibility is a must). The patch below changes the callback to also carry a void* argument, so that C++ objects can be supplied, and thus C++ can utilize the callback.

--- SDL_mixer/SDL_mixer.h	2014-03-23 14:04:43.089323652 +0100
+++ SDL_mixer_new/SDL_mixer.h	2014-03-23 11:04:40.000000000 +0100
@@ -223,7 +223,7 @@
  *  inside the audio callback, or SDL_mixer will explicitly lock the audio
  *  before calling your callback.
  */
-extern DECLSPEC void SDLCALL Mix_ChannelFinished(void (*channel_finished)(int channel));
+extern DECLSPEC void SDLCALL Mix_ChannelFinished(void *userdata, void (*channel_finished)(void *userdata, int channel));
 
 
 /* Special Effects API by ryan c. gordon. (icculus@icculus.org) */

--- SDL_mixer/mixer.c	2014-03-23 14:04:46.421417431 +0100
+++ SDL_mixer_new/mixer.c	2014-03-23 11:17:49.000000000 +0100
@@ -93,7 +94,8 @@
 static void *mix_postmix_data = NULL;
 
 /* rcg07062001 callback to alert when channels are done playing. */
-static void (*channel_done_callback)(int channel) = NULL;
+static void (*channel_done_callback)(void *userdata, int channel) = NULL;
+static void *channel_done_callback_userdata;
 
 /* Music function declarations */
 extern int open_music(SDL_AudioSpec *mixer);
@@ -261,7 +317,7 @@
 static void _Mix_channel_done_playing(int channel)
 {
     if (channel_done_callback) {
-        channel_done_callback(channel);
+        channel_done_callback(channel_done_callback_userdata, channel);
     }
 
     /*
@@ -842,10 +903,11 @@
     return(music_data);
 }
 
-void Mix_ChannelFinished(void (*channel_finished)(int channel))
+void Mix_ChannelFinished(void *userdata, void (*channel_finished)(void *userdata, int channel))
 {
     SDL_LockAudio();
     channel_done_callback = channel_finished;
+    channel_done_callback_userdata = userdata;
     SDL_UnlockAudio();
 }

On 2017-10-21 20:07:05 +0000, Sam Lantinga wrote:

This is a good idea, but needs to wait for a breaking ABI rev

On 2017-10-28 20:36:55 +0000, Ivan Epifanov wrote:

Having userdata in callback would be great not only from c++ POV.
But, you can use this in c++ even now by utilizing std::bind and some more magic. See https://github.com/falltergeist/falltergeist/blob/develop/src/Audio/Mixer.cpp#L154 for example.

@sezero sezero added enhancement New feature or request and removed bug labels Feb 28, 2021
@slouken slouken added this to the 3.0 milestone Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants