Skip to content

Commit 9d45399

Browse files
committed
Improve tester
* Add LoopResult::ContinueOnWindowEvent * Added a client_size argument to run_window_app
1 parent f051d81 commit 9d45399

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

unity-native-plugin-sample/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ extern "system" fn FillTexture(unity_texture: *mut IUnknown, x: f32, y: f32, z:
5757
fn test() {
5858
let instant = std::time::Instant::now();
5959
unity_native_plugin_tester::d3d11::test_plugin_d3d11(
60+
(256, 256),
6061
|window, context| {},
6162
|window, context| {
6263
let n = (instant.elapsed().as_millis() % 1000) as f32 / 1000.0;

unity-native-plugin-tester/src/d3d11.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ pub fn test_plugin_d3d11<
164164
FnMain: FnMut(&Window, &TesterContextGraphicsD3D11) -> crate::window::LoopResult,
165165
FnFinalize: FnOnce(&Window, &TesterContextGraphicsD3D11),
166166
>(
167+
client_size: (u32, u32),
167168
fn_init: FnInit,
168169
mut fn_main: FnMain,
169170
fn_finalize: FnFinalize,
@@ -178,6 +179,7 @@ pub fn test_plugin_d3d11<
178179
crate::graphics::initialize_interface(unity_native_plugin::graphics::GfxRenderer::D3D11);
179180

180181
crate::window::run_window_app(
182+
client_size,
181183
|window| {
182184
let ret = TesterContextGraphicsD3D11::new(window).unwrap();
183185
fn_init(window, &ret);

unity-native-plugin-tester/src/window.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@ use winit::platform::desktop::EventLoopExtDesktop;
77
#[cfg(target_os = "windows")]
88
use winit::platform::windows::EventLoopExtWindows;
99

10+
#[derive(PartialEq, Eq)]
1011
pub enum LoopResult {
12+
/// Continue the loop. The next process is executed immediately.
1113
Continue,
14+
15+
/// Continue the loop. The next process is executed when a window event occurs.
16+
/// (e.g., moving the mouse over a window)
17+
/// Use when the process to be implemented is high load.
18+
ContinueOnWindowEvent,
19+
20+
/// End the loop.
1221
Exit,
1322
}
1423

@@ -18,14 +27,20 @@ pub fn run_window_app<
1827
FnMain: FnMut(&Window, &Context) -> LoopResult,
1928
FnFinalize: FnOnce(&Window, &Context),
2029
>(
30+
client_size: (u32, u32),
2131
fn_initialize: FnInit,
2232
mut fn_main: FnMain,
2333
fn_finalize: FnFinalize,
2434
fn_unity_plugin_load: fn(interfaces: &unity_native_plugin::interface::UnityInterfaces),
2535
fn_unity_plugin_unload: fn(),
2636
) {
2737
let mut event_loop = EventLoop::<u32>::new_any_thread();
28-
let window = WindowBuilder::new().build(&event_loop).unwrap();
38+
let window = WindowBuilder::new()
39+
.with_inner_size(winit::dpi::Size::from(
40+
winit::dpi::PhysicalSize::<u32>::from(client_size),
41+
))
42+
.build(&event_loop)
43+
.unwrap();
2944

3045
let context = std::rc::Rc::new(fn_initialize(&window));
3146
unsafe {
@@ -35,23 +50,31 @@ pub fn run_window_app<
3550

3651
fn_unity_plugin_load(unity_native_plugin::interface::UnityInterfaces::get());
3752

53+
let mut last_result = LoopResult::Continue;
3854
event_loop.run_return(|event, _, control_flow| {
55+
let instant = std::time::Instant::now();
3956
match event {
40-
Event::WindowEvent { window_id, event} => {
57+
Event::WindowEvent { window_id, event } => {
4158
if window_id == window.id() {
4259
match event {
43-
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
60+
WindowEvent::CloseRequested => last_result = LoopResult::Exit,
4461
_ => {
45-
*control_flow = match fn_main(&window, context.deref()) {
46-
LoopResult::Continue => ControlFlow::Wait,
47-
_ => ControlFlow::Exit,
48-
};
62+
last_result = fn_main(&window, context.deref());
4963
}
5064
}
5165
}
5266
}
53-
_ => (),
67+
_ => {
68+
if last_result == LoopResult::Continue {
69+
last_result = fn_main(&window, context.deref());
70+
}
71+
},
5472
}
73+
*control_flow = match last_result {
74+
LoopResult::Continue => ControlFlow::WaitUntil(instant + std::time::Duration::from_millis(50)),
75+
LoopResult::ContinueOnWindowEvent => ControlFlow::Wait,
76+
_ => ControlFlow::Exit,
77+
};
5578
});
5679

5780
fn_unity_plugin_unload();

0 commit comments

Comments
 (0)