pub enum ControlFlow {
Poll,
Wait,
WaitUntil(Instant),
ExitWithCode(i32),
}Expand description
Set by the user callback given to the EventLoop::run method.
Indicates the desired behavior of the event loop after Event::RedrawEventsCleared is emitted.
Defaults to Poll.
§Persistency
Almost every change is persistent between multiple calls to the event loop closure within a
given run loop. The only exception to this is ExitWithCode which, once set, cannot be unset.
Changes are not persistent between multiple calls to run_return - issuing a new call will
reset the control flow to Poll.
Variants§
Poll
When the current loop iteration finishes, immediately begin a new iteration regardless of whether or not new events are available to process.
§Platform-specific
- Web: Events are queued and usually sent when
requestAnimationFramefires but sometimes the events in the queue may be sent before the nextrequestAnimationFramecallback, for example when the scaling of the page has changed. This should be treated as an implementation detail which should not be relied on.
Wait
When the current loop iteration finishes, suspend the thread until another event arrives.
WaitUntil(Instant)
When the current loop iteration finishes, suspend the thread until either another event arrives or the given time is reached.
Useful for implementing efficient timers. Applications which want to render at the display’s
native refresh rate should instead use Poll and the VSync functionality of a graphics API
to reduce odds of missed frames.
ExitWithCode(i32)
Send a LoopDestroyed event and stop the event loop. This variant is sticky - once set,
control_flow cannot be changed from ExitWithCode, and any future attempts to do so will
result in the control_flow parameter being reset to ExitWithCode.
The contained number will be used as exit code. The Exit constant is a shortcut for this
with exit code 0.
§Platform-specific
- Android / iOS / WASM: The supplied exit code is unused.
- Unix: On most Unix-like platforms, only the 8 least significant bits will be used,
which can cause surprises with negative exit values (
-42would end up as214). Seestd::process::exit.
Implementations§
Source§impl ControlFlow
impl ControlFlow
Sourcepub const Exit: ControlFlow
pub const Exit: ControlFlow
Alias for ExitWithCode(0).
Sourcepub fn set_poll(&mut self)
pub fn set_poll(&mut self)
Sets this to Poll.
Examples found in repository?
19fn example() {
20 let el = EventLoop::new();
21 let (vk, window) = Vk::new(&el);
22 window.set_cursor_grab(winit::window::CursorGrabMode::Confined).unwrap();
23 window.set_cursor_visible(false);
24 window.set_title("CHAOS-VK");
25 window.set_window_icon({
26 let w = 8;
27 let h = 8;
28
29 Some(Icon::from_rgba(
30 (0..w*h*4).map(|_| rand_betw(0, 255)).collect(),
31 w,
32 h
33 ).unwrap())
34 });
35 window.set_theme(Some(Theme::Dark));
36 window.set_inner_size(PhysicalSize::new(1200, 900));
37 let size = window.inner_size();
38
39
40 let mut presenter = Presenter::new(vk.clone(), window.clone());
41 let mut renderer = Renderer::new();
42 let mut imgui = ImGui::new(vk.clone(), &presenter);
43
44 renderer.camera.proj = Mat4::perspective_lh(
45 80.0f32.to_radians(),
46 size.width as f32 / size.height as f32, 0.1, 1000.0
47 );
48
49 let vs = shaders::vs::load(vk.device.clone()).unwrap();
50 let fs = shaders::fs::load(vk.device.clone()).unwrap();
51
52 let rp = render_pass_with_depth(vk.clone(), Some(presenter.swapchain.clone()));
53
54 let pipeline = instancing_pipeline(vk.clone(), vs.clone(), fs.clone(), rp.clone(), Viewport {
55 offset: [0.0, 0.0],
56 extent: size.into(),
57 depth_range: 0.0..=1.0,
58 });
59
60 presenter.window_resized = true;
61 presenter.recreate(vk.clone(), rp.clone(), window.clone());
62
63 let sphere = sphere(5, 1.0);
64 renderer.meshes.push(Mesh::new(vk.clone(), &sphere.vertices, &sphere.indices));
65
66 let mut cursor_x = 0.0;
67 let mut cursor_y = 0.0;
68
69 let data = (0..250).map(|_| { PosInstanceData {
70 ofs: [rand_betw(-100.0, 100.0), rand_betw(-10.0, 10.0), rand_betw(-100.0, 100.0)]
71 }
72 }).collect::<Vec<PosInstanceData>>();
73 renderer.meshes[0].instances = data.clone();
74 let instance_buffer = VkIterBuffer::vertex(vk.allocators.clone(), data);
75 renderer.meshes[0].ibo = instance_buffer;
76
77 let mut dt = 0.0;
78
79 el.run(move |event, _target, control_flow| {
80 control_flow.set_poll();
81
82 match event {
83 Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
84 control_flow.set_exit();
85 },
86 Event::WindowEvent { event, .. } => {
87 renderer.camera.input(&event);
88
89 match event {
90 WindowEvent::KeyboardInput { input, .. } => {
91 if input.virtual_keycode == Some(VirtualKeyCode::Escape) {
92 control_flow.set_exit();
93 }
94
95 if input.modifiers.ctrl() && input.virtual_keycode == Some(VirtualKeyCode::S) {
96 Scene::write("assets/scene.cf", &renderer).expect("Failed to write scene");
97 }
98
99 if input.modifiers.ctrl() && input.virtual_keycode == Some(VirtualKeyCode::L) {
100 Scene::read("assets/scene.cf", &mut renderer, vk.clone()).expect("Failed to load scene");
101 }
102
103 window.set_cursor_visible(input.modifiers.alt());
104 },
105
106 WindowEvent::CursorMoved { position, .. } => {
107 imgui.on_mouse_move(position.x as f32, position.y as f32);
108 }
109
110 WindowEvent::MouseInput { button, state, .. } => {
111 imgui.on_mouse_click(button, state);
112 }
113
114 WindowEvent::MouseWheel { delta: MouseScrollDelta::PixelDelta(pos), .. } => {
115 imgui.on_mouse_scroll(pos.x as f32, pos.y as f32);
116 }
117
118 _ => ()
119 }
120 }
121
122 Event::DeviceEvent {
123 event: DeviceEvent::Text { codepoint }, ..
124 } => {
125 dbg!(codepoint);
126 }
127
128 Event::DeviceEvent { event: DeviceEvent::MouseMotion { delta },.. } => {
129 cursor_x += delta.0 as f32;
130 cursor_y += delta.1 as f32;
131
132 renderer.camera.mouse_callback(cursor_x, cursor_y);
133 }
134
135 Event::MainEventsCleared => {
136 let now = std::time::Instant::now();
137
138 let frame = imgui.frame(&window);
139 frame.text("hello, world!");
140 frame.text(format!("dt:{:.1}", dt*1000.0));
141
142 presenter.recreate(vk.clone(), rp.clone(), window.clone());
143
144 renderer.update(dt);
145 presenter.cmd_bufs = get_cmd_bufs(
146 vk.clone(),
147 &renderer,
148 &mut imgui,
149 &presenter,
150 pipeline.clone()
151 );
152
153 presenter.present(vk.clone());
154
155 sleep(Duration::from_millis(16).saturating_sub(Duration::from_millis((dt * 1000.0) as u64)));
156 dt = now.elapsed().as_secs_f32();
157 }
158
159 Event::LoopDestroyed => {println!("EXIT");}
160 _ => {}
161 }
162 });
163}Sourcepub fn set_wait_until(&mut self, instant: Instant)
pub fn set_wait_until(&mut self, instant: Instant)
Sets this to WaitUntil(instant).
Sourcepub fn set_wait_timeout(&mut self, timeout: Duration)
pub fn set_wait_timeout(&mut self, timeout: Duration)
Sourcepub fn set_exit_with_code(&mut self, code: i32)
pub fn set_exit_with_code(&mut self, code: i32)
Sets this to ExitWithCode(code).
Sourcepub fn set_exit(&mut self)
pub fn set_exit(&mut self)
Sets this to Exit.
Examples found in repository?
19fn example() {
20 let el = EventLoop::new();
21 let (vk, window) = Vk::new(&el);
22 window.set_cursor_grab(winit::window::CursorGrabMode::Confined).unwrap();
23 window.set_cursor_visible(false);
24 window.set_title("CHAOS-VK");
25 window.set_window_icon({
26 let w = 8;
27 let h = 8;
28
29 Some(Icon::from_rgba(
30 (0..w*h*4).map(|_| rand_betw(0, 255)).collect(),
31 w,
32 h
33 ).unwrap())
34 });
35 window.set_theme(Some(Theme::Dark));
36 window.set_inner_size(PhysicalSize::new(1200, 900));
37 let size = window.inner_size();
38
39
40 let mut presenter = Presenter::new(vk.clone(), window.clone());
41 let mut renderer = Renderer::new();
42 let mut imgui = ImGui::new(vk.clone(), &presenter);
43
44 renderer.camera.proj = Mat4::perspective_lh(
45 80.0f32.to_radians(),
46 size.width as f32 / size.height as f32, 0.1, 1000.0
47 );
48
49 let vs = shaders::vs::load(vk.device.clone()).unwrap();
50 let fs = shaders::fs::load(vk.device.clone()).unwrap();
51
52 let rp = render_pass_with_depth(vk.clone(), Some(presenter.swapchain.clone()));
53
54 let pipeline = instancing_pipeline(vk.clone(), vs.clone(), fs.clone(), rp.clone(), Viewport {
55 offset: [0.0, 0.0],
56 extent: size.into(),
57 depth_range: 0.0..=1.0,
58 });
59
60 presenter.window_resized = true;
61 presenter.recreate(vk.clone(), rp.clone(), window.clone());
62
63 let sphere = sphere(5, 1.0);
64 renderer.meshes.push(Mesh::new(vk.clone(), &sphere.vertices, &sphere.indices));
65
66 let mut cursor_x = 0.0;
67 let mut cursor_y = 0.0;
68
69 let data = (0..250).map(|_| { PosInstanceData {
70 ofs: [rand_betw(-100.0, 100.0), rand_betw(-10.0, 10.0), rand_betw(-100.0, 100.0)]
71 }
72 }).collect::<Vec<PosInstanceData>>();
73 renderer.meshes[0].instances = data.clone();
74 let instance_buffer = VkIterBuffer::vertex(vk.allocators.clone(), data);
75 renderer.meshes[0].ibo = instance_buffer;
76
77 let mut dt = 0.0;
78
79 el.run(move |event, _target, control_flow| {
80 control_flow.set_poll();
81
82 match event {
83 Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
84 control_flow.set_exit();
85 },
86 Event::WindowEvent { event, .. } => {
87 renderer.camera.input(&event);
88
89 match event {
90 WindowEvent::KeyboardInput { input, .. } => {
91 if input.virtual_keycode == Some(VirtualKeyCode::Escape) {
92 control_flow.set_exit();
93 }
94
95 if input.modifiers.ctrl() && input.virtual_keycode == Some(VirtualKeyCode::S) {
96 Scene::write("assets/scene.cf", &renderer).expect("Failed to write scene");
97 }
98
99 if input.modifiers.ctrl() && input.virtual_keycode == Some(VirtualKeyCode::L) {
100 Scene::read("assets/scene.cf", &mut renderer, vk.clone()).expect("Failed to load scene");
101 }
102
103 window.set_cursor_visible(input.modifiers.alt());
104 },
105
106 WindowEvent::CursorMoved { position, .. } => {
107 imgui.on_mouse_move(position.x as f32, position.y as f32);
108 }
109
110 WindowEvent::MouseInput { button, state, .. } => {
111 imgui.on_mouse_click(button, state);
112 }
113
114 WindowEvent::MouseWheel { delta: MouseScrollDelta::PixelDelta(pos), .. } => {
115 imgui.on_mouse_scroll(pos.x as f32, pos.y as f32);
116 }
117
118 _ => ()
119 }
120 }
121
122 Event::DeviceEvent {
123 event: DeviceEvent::Text { codepoint }, ..
124 } => {
125 dbg!(codepoint);
126 }
127
128 Event::DeviceEvent { event: DeviceEvent::MouseMotion { delta },.. } => {
129 cursor_x += delta.0 as f32;
130 cursor_y += delta.1 as f32;
131
132 renderer.camera.mouse_callback(cursor_x, cursor_y);
133 }
134
135 Event::MainEventsCleared => {
136 let now = std::time::Instant::now();
137
138 let frame = imgui.frame(&window);
139 frame.text("hello, world!");
140 frame.text(format!("dt:{:.1}", dt*1000.0));
141
142 presenter.recreate(vk.clone(), rp.clone(), window.clone());
143
144 renderer.update(dt);
145 presenter.cmd_bufs = get_cmd_bufs(
146 vk.clone(),
147 &renderer,
148 &mut imgui,
149 &presenter,
150 pipeline.clone()
151 );
152
153 presenter.present(vk.clone());
154
155 sleep(Duration::from_millis(16).saturating_sub(Duration::from_millis((dt * 1000.0) as u64)));
156 dt = now.elapsed().as_secs_f32();
157 }
158
159 Event::LoopDestroyed => {println!("EXIT");}
160 _ => {}
161 }
162 });
163}Trait Implementations§
Source§impl Clone for ControlFlow
impl Clone for ControlFlow
Source§fn clone(&self) -> ControlFlow
fn clone(&self) -> ControlFlow
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more