Create a window
Description
TODO update this for winit 0.29.2
This is a graphical application so we'll need a window to draw into. The window is also a source of important events for our application, so we'll end up building our event loop around the window.
The winit crate provides an API for window creation and event management. It wraps several OS-specific APIs.
This change creates a window and an event loop that will run
indefinitely. The function passed to run
will be called on each
event. In this case we're only handling the event to close the window
which is sent when a user clicks on the close button.
We can set the control_flow
variable to determine what happens after
the event is processed. This application doesn't do much for now, so we
default to ControlFlow::Wait
, which means the application doesn't do
anything between events. Later on we'll switch this to
ControlFlow::Poll
which lets us do other processing (e.g. updating the
main game loop, rendering animations). See ControlFlow for more
options.
Screenshot
Commands
git clone git@github.com:atsheehan/iridium
cd iridium
git checkout 4ddd8441b219bf0d7d6f8027e3e3b6991305c2c1
cargo run --release
Code Changes
Modified Cargo.lockGitHub
Hiding contents due to file size.
Modified Cargo.tomlGitHub
@@ -6,3 +6,6 @@66 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77
88 [dependencies]
9+
10+ # Cross-platform window creation and event handling
11+ winit = "0.29.2"
@@ -6,3 +6,6 @@6 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8 [dependencies]
@@ -6,3 +6,6 @@6 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8 [dependencies]
9+
10+ # Cross-platform window creation and event handling
11+ winit = "0.29.2"
Modified src/main.rsGitHub
@@ -1,3 +1,25 @@1+ use winit::{
2+ event::{Event, WindowEvent},
3+ event_loop::EventLoop,
4+ window::WindowBuilder,
5+ };
6+
17 fn main() {
2- println!("Hello, world!");
8+ let event_loop = EventLoop::new().unwrap();
9+ let window = WindowBuilder::new()
10+ .with_title("iridium")
11+ .build(&event_loop)
12+ .unwrap();
13+
14+ event_loop
15+ .run(move |event, window_target| match event {
16+ Event::WindowEvent {
17+ event: WindowEvent::CloseRequested,
18+ window_id,
19+ } if window_id == window.id() => {
20+ window_target.exit();
21+ }
22+ _ => (),
23+ })
24+ .unwrap();
325 }
@@ -1,3 +1,25 @@ 1 fn main() {
2- println!("Hello, world!");
3 }
@@ -1,3 +1,25 @@1+ use winit::{
2+ event::{Event, WindowEvent},
3+ event_loop::EventLoop,
4+ window::WindowBuilder,
5+ };
6+
7 fn main() {
8+ let event_loop = EventLoop::new().unwrap();
9+ let window = WindowBuilder::new()
10+ .with_title("iridium")
11+ .build(&event_loop)
12+ .unwrap();
13+
14+ event_loop
15+ .run(move |event, window_target| match event {
16+ Event::WindowEvent {
17+ event: WindowEvent::CloseRequested,
18+ window_id,
19+ } if window_id == window.id() => {
20+ window_target.exit();
21+ }
22+ _ => (),
23+ })
24+ .unwrap();
25 }