dora_ssr/dora/app.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 application_get_frame() -> i32;
11 fn application_get_buffer_size() -> i64;
12 fn application_get_visual_size() -> i64;
13 fn application_get_device_pixel_ratio() -> f32;
14 fn application_get_platform() -> i64;
15 fn application_get_version() -> i64;
16 fn application_get_deps() -> i64;
17 fn application_get_delta_time() -> f64;
18 fn application_get_elapsed_time() -> f64;
19 fn application_get_total_time() -> f64;
20 fn application_get_running_time() -> f64;
21 fn application_get_rand() -> i64;
22 fn application_get_max_fps() -> i32;
23 fn application_is_debugging() -> i32;
24 fn application_set_locale(val: i64);
25 fn application_get_locale() -> i64;
26 fn application_set_theme_color(val: i32);
27 fn application_get_theme_color() -> i32;
28 fn application_set_seed(val: i32);
29 fn application_get_seed() -> i32;
30 fn application_set_target_fps(val: i32);
31 fn application_get_target_fps() -> i32;
32 fn application_set_win_size(val: i64);
33 fn application_get_win_size() -> i64;
34 fn application_set_win_position(val: i64);
35 fn application_get_win_position() -> i64;
36 fn application_set_fps_limited(val: i32);
37 fn application_is_fps_limited() -> i32;
38 fn application_set_idled(val: i32);
39 fn application_is_idled() -> i32;
40 fn application_set_full_screen(val: i32);
41 fn application_is_full_screen() -> i32;
42 fn application_set_always_on_top(val: i32);
43 fn application_is_always_on_top() -> i32;
44 fn application_shutdown();
45}
46/// A struct representing an application.
47pub struct App { }
48impl App {
49 /// Gets the current passed frame number.
50 pub fn get_frame() -> i32 {
51 return unsafe { application_get_frame() };
52 }
53 /// Gets the size of the main frame buffer texture used for rendering.
54 pub fn get_buffer_size() -> crate::dora::Size {
55 return unsafe { crate::dora::Size::from(application_get_buffer_size()) };
56 }
57 /// Gets the logic visual size of the screen.
58 /// The visual size only changes when application window size changes.
59 /// And it won't be affacted by the view buffer scaling factor.
60 pub fn get_visual_size() -> crate::dora::Size {
61 return unsafe { crate::dora::Size::from(application_get_visual_size()) };
62 }
63 /// Gets the ratio of the pixel density displayed by the device
64 /// Can be calculated as the size of the rendering buffer divided by the size of the application window.
65 pub fn get_device_pixel_ratio() -> f32 {
66 return unsafe { application_get_device_pixel_ratio() };
67 }
68 /// Gets the platform the game engine is running on.
69 pub fn get_platform() -> String {
70 return unsafe { crate::dora::to_string(application_get_platform()) };
71 }
72 /// Gets the version string of the game engine.
73 /// Should be in format of "v0.0.0".
74 pub fn get_version() -> String {
75 return unsafe { crate::dora::to_string(application_get_version()) };
76 }
77 /// Gets the dependencies of the game engine.
78 pub fn get_deps() -> String {
79 return unsafe { crate::dora::to_string(application_get_deps()) };
80 }
81 /// Gets the time in seconds since the last frame update.
82 pub fn get_delta_time() -> f64 {
83 return unsafe { application_get_delta_time() };
84 }
85 /// Gets the elapsed time since current frame was started, in seconds.
86 pub fn get_elapsed_time() -> f64 {
87 return unsafe { application_get_elapsed_time() };
88 }
89 /// Gets the total time the game engine has been running until last frame ended, in seconds.
90 /// Should be a contant number when invoked in a same frame for multiple times.
91 pub fn get_total_time() -> f64 {
92 return unsafe { application_get_total_time() };
93 }
94 /// Gets the total time the game engine has been running until this field being accessed, in seconds.
95 /// Should be a increasing number when invoked in a same frame for multiple times.
96 pub fn get_running_time() -> f64 {
97 return unsafe { application_get_running_time() };
98 }
99 /// Gets a random number generated by a random number engine based on Mersenne Twister algorithm.
100 /// So that the random number generated by a same seed should be consistent on every platform.
101 pub fn get_rand() -> i64 {
102 return unsafe { application_get_rand() };
103 }
104 /// Gets the maximum valid frames per second the game engine is allowed to run at.
105 /// The max FPS is being inferred by the device screen max refresh rate.
106 pub fn get_max_fps() -> i32 {
107 return unsafe { application_get_max_fps() };
108 }
109 /// Gets whether the game engine is running in debug mode.
110 pub fn is_debugging() -> bool {
111 return unsafe { application_is_debugging() != 0 };
112 }
113 /// Sets the system locale string, in format like: `zh-Hans`, `en`.
114 pub fn set_locale(val: &str) {
115 unsafe { application_set_locale(crate::dora::from_string(val)) };
116 }
117 /// Gets the system locale string, in format like: `zh-Hans`, `en`.
118 pub fn get_locale() -> String {
119 return unsafe { crate::dora::to_string(application_get_locale()) };
120 }
121 /// Sets the theme color for Dora SSR.
122 pub fn set_theme_color(val: &crate::dora::Color) {
123 unsafe { application_set_theme_color(val.to_argb() as i32) };
124 }
125 /// Gets the theme color for Dora SSR.
126 pub fn get_theme_color() -> crate::dora::Color {
127 return unsafe { crate::dora::Color::from(application_get_theme_color()) };
128 }
129 /// Sets the random number seed.
130 pub fn set_seed(val: i32) {
131 unsafe { application_set_seed(val) };
132 }
133 /// Gets the random number seed.
134 pub fn get_seed() -> i32 {
135 return unsafe { application_get_seed() };
136 }
137 /// Sets the target frames per second the game engine is supposed to run at.
138 /// Only works when `fpsLimited` is set to true.
139 pub fn set_target_fps(val: i32) {
140 unsafe { application_set_target_fps(val) };
141 }
142 /// Gets the target frames per second the game engine is supposed to run at.
143 /// Only works when `fpsLimited` is set to true.
144 pub fn get_target_fps() -> i32 {
145 return unsafe { application_get_target_fps() };
146 }
147 /// Sets the application window size.
148 /// May differ from visual size due to the different DPIs of display devices.
149 /// It is not available to set this property on platform Android and iOS.
150 pub fn set_win_size(val: &crate::dora::Size) {
151 unsafe { application_set_win_size(val.into_i64()) };
152 }
153 /// Gets the application window size.
154 /// May differ from visual size due to the different DPIs of display devices.
155 /// It is not available to set this property on platform Android and iOS.
156 pub fn get_win_size() -> crate::dora::Size {
157 return unsafe { crate::dora::Size::from(application_get_win_size()) };
158 }
159 /// Sets the application window position.
160 /// It is not available to set this property on platform Android and iOS.
161 pub fn set_win_position(val: &crate::dora::Vec2) {
162 unsafe { application_set_win_position(val.into_i64()) };
163 }
164 /// Gets the application window position.
165 /// It is not available to set this property on platform Android and iOS.
166 pub fn get_win_position() -> crate::dora::Vec2 {
167 return unsafe { crate::dora::Vec2::from(application_get_win_position()) };
168 }
169 /// Sets whether the game engine is limiting the frames per second.
170 /// Set `fpsLimited` to true, will make engine run in a busy loop to track the precise frame time to switch to the next frame. And this behavior can lead to 100% CPU usage. This is usually common practice on Windows PCs for better CPU usage occupation. But it also results in extra heat and power consumption.
171 pub fn set_fps_limited(val: bool) {
172 unsafe { application_set_fps_limited(if val { 1 } else { 0 }) };
173 }
174 /// Gets whether the game engine is limiting the frames per second.
175 /// Set `fpsLimited` to true, will make engine run in a busy loop to track the precise frame time to switch to the next frame. And this behavior can lead to 100% CPU usage. This is usually common practice on Windows PCs for better CPU usage occupation. But it also results in extra heat and power consumption.
176 pub fn is_fps_limited() -> bool {
177 return unsafe { application_is_fps_limited() != 0 };
178 }
179 /// Sets whether the game engine is currently idled.
180 /// Set `idled` to true, will make game logic thread use a sleep time and going idled for next frame to come. Due to the imprecision in sleep time. This idled state may cause game engine over slept for a few frames to lost.
181 /// `idled` state can reduce some CPU usage.
182 pub fn set_idled(val: bool) {
183 unsafe { application_set_idled(if val { 1 } else { 0 }) };
184 }
185 /// Gets whether the game engine is currently idled.
186 /// Set `idled` to true, will make game logic thread use a sleep time and going idled for next frame to come. Due to the imprecision in sleep time. This idled state may cause game engine over slept for a few frames to lost.
187 /// `idled` state can reduce some CPU usage.
188 pub fn is_idled() -> bool {
189 return unsafe { application_is_idled() != 0 };
190 }
191 /// Sets whether the game engine is running in full screen mode.
192 /// It is not available to set this property on platform Android and iOS.
193 pub fn set_full_screen(val: bool) {
194 unsafe { application_set_full_screen(if val { 1 } else { 0 }) };
195 }
196 /// Gets whether the game engine is running in full screen mode.
197 /// It is not available to set this property on platform Android and iOS.
198 pub fn is_full_screen() -> bool {
199 return unsafe { application_is_full_screen() != 0 };
200 }
201 /// Sets whether the game engine window is always on top. Default is true.
202 /// It is not available to set this property on platform Android and iOS.
203 pub fn set_always_on_top(val: bool) {
204 unsafe { application_set_always_on_top(if val { 1 } else { 0 }) };
205 }
206 /// Gets whether the game engine window is always on top. Default is true.
207 /// It is not available to set this property on platform Android and iOS.
208 pub fn is_always_on_top() -> bool {
209 return unsafe { application_is_always_on_top() != 0 };
210 }
211 /// Shuts down the game engine.
212 /// It is not working and acts as a dummy function for platform Android and iOS to follow the specification of how mobile platform applications should operate.
213 pub fn shutdown() {
214 unsafe { application_shutdown(); }
215 }
216}