dora_ssr/dora/
scheduler.rs

1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10	fn scheduler_type() -> i32;
11	fn scheduler_set_time_scale(slf: i64, val: f32);
12	fn scheduler_get_time_scale(slf: i64) -> f32;
13	fn scheduler_set_fixed_fps(slf: i64, val: i32);
14	fn scheduler_get_fixed_fps(slf: i64) -> i32;
15	fn scheduler_update(slf: i64, delta_time: f64) -> i32;
16	fn scheduler_new() -> i64;
17}
18use crate::dora::IObject;
19/// A scheduler that manages the execution of scheduled tasks.
20pub struct Scheduler { raw: i64 }
21crate::dora_object!(Scheduler);
22impl Scheduler {
23	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
24		(unsafe { scheduler_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
25			match raw {
26				0 => None,
27				_ => Some(Box::new(Scheduler { raw: raw }))
28			}
29		})
30	}
31	/// Sets the time scale factor for the scheduler.
32	/// This factor is applied to deltaTime that the scheduled functions will receive.
33	pub fn set_time_scale(&mut self, val: f32) {
34		unsafe { scheduler_set_time_scale(self.raw(), val) };
35	}
36	/// Gets the time scale factor for the scheduler.
37	/// This factor is applied to deltaTime that the scheduled functions will receive.
38	pub fn get_time_scale(&self) -> f32 {
39		return unsafe { scheduler_get_time_scale(self.raw()) };
40	}
41	/// Sets the target frame rate (in frames per second) for a fixed update mode.
42	/// The fixed update will ensure a constant frame rate, and the operation handled in a fixed update can use a constant delta time value.
43	/// It is used for preventing weird behavior of a physics engine or synchronizing some states via network communications.
44	pub fn set_fixed_fps(&mut self, val: i32) {
45		unsafe { scheduler_set_fixed_fps(self.raw(), val) };
46	}
47	/// Gets the target frame rate (in frames per second) for a fixed update mode.
48	/// The fixed update will ensure a constant frame rate, and the operation handled in a fixed update can use a constant delta time value.
49	/// It is used for preventing weird behavior of a physics engine or synchronizing some states via network communications.
50	pub fn get_fixed_fps(&self) -> i32 {
51		return unsafe { scheduler_get_fixed_fps(self.raw()) };
52	}
53	/// Used for manually updating the scheduler if it is created by the user.
54	///
55	/// # Arguments
56	///
57	/// * `deltaTime` - The time in seconds since the last frame update.
58	///
59	/// # Returns
60	///
61	/// * `bool` - `true` if the scheduler was stoped, `false` otherwise.
62	pub fn update(&mut self, delta_time: f64) -> bool {
63		unsafe { return scheduler_update(self.raw(), delta_time) != 0; }
64	}
65	/// Creates a new Scheduler object.
66	pub fn new() -> Scheduler {
67		unsafe { return Scheduler { raw: scheduler_new() }; }
68	}
69}