Open
Description
One of the more advanced features of FMOD allows pulling information back out of FMOD at runtime.
I've been trying to figure out how to do so with bevy_fmod recently. While I can register callbacks, capturing bevy context to trigger Events for example has been proving... a bit more challenging.
It seems useful enough to me to consider adding an example for such useage (if possible at all).
Has anyone attempted to do this before with bevy? I'll share some code snippets as examples for what I mean.
basic callback example:
use bevy::prelude::*;
use bevy_fmod::fmod_studio::FmodStudio;
use libfmod::ffi::{FMOD_OK, FMOD_RESULT, FMOD_STUDIO_EVENTINSTANCE, FMOD_STUDIO_EVENT_CALLBACK_TYPE};
// Implement a function with the expected FFI signature that calls the closure
extern "C" fn callback_bridge<F>(
param1: u32,
param2: *mut FMOD_STUDIO_EVENTINSTANCE,
param3: *mut std::ffi::c_void,
) -> i32
where
F: FnMut(u32, *mut FMOD_STUDIO_EVENTINSTANCE, *mut std::ffi::c_void) -> i32,
{
unsafe {
// Get the closure from the context data
let callback_wrapper: &mut CallbackWrapper<F> = &mut *(param3 as *mut CallbackWrapper<F>);
// Call the closure with the provided parameters
(callback_wrapper.callback)(param1, param2, param3)
}
}
impl<F> CallbackWrapper<F>
where
F: FnMut(u32, *mut FMOD_STUDIO_EVENTINSTANCE, *mut std::ffi::c_void) -> i32,
{
// Function to create a new CallbackWrapper
fn new(callback: F) -> CallbackWrapper<F> {
CallbackWrapper { callback }
}
// Function to get the function pointer to the bridge function
fn get_callback_bridge(&self) -> libfmod::ffi::FMOD_STUDIO_EVENT_CALLBACK {
Some(callback_bridge::<F>)
}
}
pub fn setup_fmod_callbacks_system(
studio: Res<FmodStudio>
) {
let mut my_closure = move |param1: u32,
param2: *mut FMOD_STUDIO_EVENTINSTANCE,
param3: *mut std::ffi::c_void| {
print!("triggered by FMOD event");
FMOD_OK
};
let callback_wrapper = CallbackWrapper::new(my_closure);
let callback_bridge = callback_wrapper.get_callback_bridge();
let _ = studio
.0
.get_event("event:/my-event")
.unwrap()
.set_callback(callback_bridge, 0xFFFF_FFFFu32);
}
This reliably triggers when the appropriate FMOD event triggers, multiple times due to the catch_all mask FFFF_FFFF
.
But as for relaying this information to bevy... I've tried an approach using crossbeam-channel.