fmod/studio/event_description/instance.rs
1// Copyright (c) 2024 Lily Lyons
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use fmod_sys::*;
8use std::ffi::c_int;
9
10use crate::studio::{EventDescription, EventInstance};
11
12impl EventDescription {
13 /// Creates a playable instance.
14 ///
15 /// When an event instance is created, any required non-streaming sample data is loaded asynchronously.
16 ///
17 /// Use [`EventDescription::get_sample_loading_state`] to check the loading status.
18 ///
19 /// Sample data can be loaded ahead of time with [`EventDescription::load_sample_data`] or [`super::Bank::load_sample_data`]. See Sample Data Loading for more information.
20 pub fn create_instance(&self) -> Result<EventInstance> {
21 let mut instance = std::ptr::null_mut();
22 unsafe {
23 FMOD_Studio_EventDescription_CreateInstance(self.inner, &mut instance).to_result()?;
24 Ok(EventInstance::from(instance))
25 }
26 }
27
28 /// Retrieves the number of instances.
29 pub fn instance_count(&self) -> Result<c_int> {
30 let mut count = 0;
31 unsafe {
32 FMOD_Studio_EventDescription_GetInstanceCount(self.inner, &mut count).to_result()?;
33 }
34 Ok(count)
35 }
36
37 pub fn get_instance_list(&self) -> Result<Vec<EventInstance>> {
38 let expected_count = self.instance_count()?;
39 let mut count = 0;
40 let mut list = vec![std::ptr::null_mut(); expected_count as usize];
41
42 unsafe {
43 FMOD_Studio_EventDescription_GetInstanceList(
44 self.inner,
45 // eventinstance is repr transparent and has the same layout as *mut FMOD_STUDIO_EVENTINSTANCE, so this cast is ok
46 list.as_mut_ptr(),
47 list.capacity() as c_int,
48 &mut count,
49 )
50 .to_result()?;
51
52 debug_assert_eq!(count, expected_count);
53
54 // *mut FMOD_STUDIO_EVENTINSTANCE is transmutable to EventInstance
55 Ok(std::mem::transmute::<
56 Vec<*mut fmod_sys::FMOD_STUDIO_EVENTINSTANCE>,
57 Vec<EventInstance>,
58 >(list))
59 }
60 }
61
62 /// Releases all instances.
63 ///
64 /// This function immediately stops and releases all instances of the event.
65 pub fn release_all_instances(&self) -> Result<()> {
66 unsafe { FMOD_Studio_EventDescription_ReleaseAllInstances(self.inner).to_result() }
67 }
68}