fmod/studio/command_replay/
playback.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_float, c_int};
9
10use crate::studio::{CommandReplay, PlaybackState};
11
12impl CommandReplay {
13    /// Begins playback.
14    ///
15    /// If the replay is already running then calling this function will restart replay from the beginning.
16    pub fn start(&self) -> Result<()> {
17        unsafe { FMOD_Studio_CommandReplay_Start(self.inner).to_result() }
18    }
19
20    /// Stops playback.
21    ///
22    /// If the [`CommandReplayFlags::SKIP_CLEANUP`] flag has been used then the system state is left as it was at the end of the playback,
23    /// otherwise all resources that were created as part of the replay will be cleaned up.
24    pub fn stop(&self) -> Result<()> {
25        unsafe { FMOD_Studio_CommandReplay_Stop(self.inner).to_result() }
26    }
27
28    /// Retrieves the progress through the command replay.
29    ///
30    /// If this function is called before [`CommandReplay::start`] then both tuple fields will be 0.
31    /// If this function is called after [`CommandReplay::stop`] then the index and time of the last command which was replayed will be returned.
32    pub fn get_current_command(&self) -> Result<(c_int, c_float)> {
33        let mut command_index = 0;
34        let mut current_time = 0.0;
35        unsafe {
36            FMOD_Studio_CommandReplay_GetCurrentCommand(
37                self.inner,
38                &mut command_index,
39                &mut current_time,
40            )
41            .to_result()?;
42        }
43        Ok((command_index, current_time))
44    }
45
46    /// Retrieves the playback state.
47    pub fn get_playback_state(&self) -> Result<PlaybackState> {
48        let mut state = 0;
49        unsafe {
50            FMOD_Studio_CommandReplay_GetPlaybackState(self.inner, &mut state).to_result()?;
51            let state = state.try_into()?;
52            Ok(state)
53        }
54    }
55
56    /// Sets the paused state.
57    pub fn set_paused(&self, paused: bool) -> Result<()> {
58        unsafe { FMOD_Studio_CommandReplay_SetPaused(self.inner, paused.into()).to_result() }
59    }
60
61    /// Retrieves the paused state.
62    pub fn get_paused(&self) -> Result<bool> {
63        let mut paused = FMOD_BOOL::FALSE;
64        unsafe {
65            FMOD_Studio_CommandReplay_GetPaused(self.inner, &mut paused).to_result()?;
66        }
67        Ok(paused.into())
68    }
69
70    /// Seeks the playback position to a command.
71    pub fn seek_to_command(&self, index: c_int) -> Result<()> {
72        unsafe { FMOD_Studio_CommandReplay_SeekToCommand(self.inner, index).to_result() }
73    }
74
75    /// Seeks the playback position to a time.
76    ///
77    /// This function moves the playback position to the the first command at or after `time`.
78    /// If no command exists at or after `time` then [`FMOD_RESULT::FMOD_ERR_EVENT_NOTFOUND`] is returned.
79    pub fn seek_to_time(&self, time: c_float) -> Result<()> {
80        unsafe { FMOD_Studio_CommandReplay_SeekToTime(self.inner, time).to_result() }
81    }
82}