pub struct Window { /* private fields */ }
Expand description
A Window can display graphics and handle events.
A Window has a draw color at all times, and that color is applied to every operation. If you set
the color to (255, 0, 0)
, all drawn graphics and images will have a red tint.
Creating multiple Windows is untested and will probably crash!
Implementations§
Source§impl Window
§Top-level Running / Creation Methods
impl Window
§Top-level Running / Creation Methods
Sourcepub fn new(name: &str, width: u16, height: u16) -> Self
pub fn new(name: &str, width: u16, height: u16) -> Self
Intialize a new running window. name
is used as a caption.
Examples found in repository?
8fn main() {
9 let mut app = Window::new("Image Font Demo", 640, 480);
10 while app.next_frame() {
11 app.clear_to_color(32, 64, 32);
12
13 app.set_color(255, 255, 255, 255);
14 app.print("Hello world!", 32, 32);
15 app.print("This example demonstrates ImageFont rendering :)", 32, 64);
16 app.print("You can even write symbols: !@#$%^&*()", 32, 96);
17
18 app.set_color(0, 255, 255, 255);
19 app.print("16777216 possible rendering colors!", 32, 128);
20 }
21}
More examples
7fn main() {
8 let mut window = simple::Window::new("Los Angeles", 640, 480);
9 let mut pic = window.load_image(include_bytes!("city.jpg")).unwrap();
10
11 let mut frame_number: u64 = 0;
12
13 while window.next_frame() {
14 window.clear();
15
16 let sine = (frame_number as f32 / 150.0).sin().abs();
17 let color = (sine * 255f32) as u8;
18
19 window.set_color(100 + color / 3, color, 255 - color, 255);
20 window.draw_image(&mut pic, 0, 0);
21
22 frame_number += 1;
23 }
24}
78fn main() {
79 // Create an application
80 let mut app = Window::new("Squares", SCREEN_WIDTH, SCREEN_HEIGHT);
81
82 // Create some objects to live in the application
83 let mut squares = vec![Square::new(), Square::new(), Square::new()];
84
85 // Run the game loop
86 while app.next_frame() {
87 // event handling
88 while app.has_event() {
89 match app.next_event() {
90 // If the user clicks, we add a new Square at the position of the mouse event.
91 Event::Mouse {
92 is_down: true,
93 mouse_x,
94 mouse_y,
95 ..
96 } => squares.push(Square::new_at_position(mouse_x as f32, mouse_y as f32)),
97
98 _ => (),
99 }
100 }
101
102 app.clear();
103
104 // update and draw
105 for square in squares.iter_mut() {
106 square.update();
107 square.draw(&mut app);
108 }
109 }
110}
Sourcepub fn next_frame(&mut self) -> bool
pub fn next_frame(&mut self) -> bool
Redrawing and update the display, while maintaining a consistent framerate and updating the event queue. You should draw your objects immediately before you call this function.
NOTE: This function returns false if the program should terminate. This allows for nice
constructs like while app.next_frame() { ... }
Examples found in repository?
8fn main() {
9 let mut app = Window::new("Image Font Demo", 640, 480);
10 while app.next_frame() {
11 app.clear_to_color(32, 64, 32);
12
13 app.set_color(255, 255, 255, 255);
14 app.print("Hello world!", 32, 32);
15 app.print("This example demonstrates ImageFont rendering :)", 32, 64);
16 app.print("You can even write symbols: !@#$%^&*()", 32, 96);
17
18 app.set_color(0, 255, 255, 255);
19 app.print("16777216 possible rendering colors!", 32, 128);
20 }
21}
More examples
7fn main() {
8 let mut window = simple::Window::new("Los Angeles", 640, 480);
9 let mut pic = window.load_image(include_bytes!("city.jpg")).unwrap();
10
11 let mut frame_number: u64 = 0;
12
13 while window.next_frame() {
14 window.clear();
15
16 let sine = (frame_number as f32 / 150.0).sin().abs();
17 let color = (sine * 255f32) as u8;
18
19 window.set_color(100 + color / 3, color, 255 - color, 255);
20 window.draw_image(&mut pic, 0, 0);
21
22 frame_number += 1;
23 }
24}
78fn main() {
79 // Create an application
80 let mut app = Window::new("Squares", SCREEN_WIDTH, SCREEN_HEIGHT);
81
82 // Create some objects to live in the application
83 let mut squares = vec![Square::new(), Square::new(), Square::new()];
84
85 // Run the game loop
86 while app.next_frame() {
87 // event handling
88 while app.has_event() {
89 match app.next_event() {
90 // If the user clicks, we add a new Square at the position of the mouse event.
91 Event::Mouse {
92 is_down: true,
93 mouse_x,
94 mouse_y,
95 ..
96 } => squares.push(Square::new_at_position(mouse_x as f32, mouse_y as f32)),
97
98 _ => (),
99 }
100 }
101
102 app.clear();
103
104 // update and draw
105 for square in squares.iter_mut() {
106 square.update();
107 square.draw(&mut app);
108 }
109 }
110}
Sourcepub fn has_event(&self) -> bool
pub fn has_event(&self) -> bool
Return true when there is an event waiting in the queue for processing.
Examples found in repository?
78fn main() {
79 // Create an application
80 let mut app = Window::new("Squares", SCREEN_WIDTH, SCREEN_HEIGHT);
81
82 // Create some objects to live in the application
83 let mut squares = vec![Square::new(), Square::new(), Square::new()];
84
85 // Run the game loop
86 while app.next_frame() {
87 // event handling
88 while app.has_event() {
89 match app.next_event() {
90 // If the user clicks, we add a new Square at the position of the mouse event.
91 Event::Mouse {
92 is_down: true,
93 mouse_x,
94 mouse_y,
95 ..
96 } => squares.push(Square::new_at_position(mouse_x as f32, mouse_y as f32)),
97
98 _ => (),
99 }
100 }
101
102 app.clear();
103
104 // update and draw
105 for square in squares.iter_mut() {
106 square.update();
107 square.draw(&mut app);
108 }
109 }
110}
Sourcepub fn next_event(&mut self) -> Event
pub fn next_event(&mut self) -> Event
Get the next event from the queue. NOTE: If the event queue on the Window is empty, this
function will panic. Call has_event()
to find out if there is an event ready for
processing.
Note that events are handled in a first-in-first-out order. If a user presses three keys 1, 2, 3 during a frame, then the next three calls to next_event will return 1, 2, 3 in the same order.
Examples found in repository?
78fn main() {
79 // Create an application
80 let mut app = Window::new("Squares", SCREEN_WIDTH, SCREEN_HEIGHT);
81
82 // Create some objects to live in the application
83 let mut squares = vec![Square::new(), Square::new(), Square::new()];
84
85 // Run the game loop
86 while app.next_frame() {
87 // event handling
88 while app.has_event() {
89 match app.next_event() {
90 // If the user clicks, we add a new Square at the position of the mouse event.
91 Event::Mouse {
92 is_down: true,
93 mouse_x,
94 mouse_y,
95 ..
96 } => squares.push(Square::new_at_position(mouse_x as f32, mouse_y as f32)),
97
98 _ => (),
99 }
100 }
101
102 app.clear();
103
104 // update and draw
105 for square in squares.iter_mut() {
106 square.update();
107 square.draw(&mut app);
108 }
109 }
110}
Sourcepub fn is_key_down(&self, key: Key) -> bool
pub fn is_key_down(&self, key: Key) -> bool
Return true if the button is currently pressed. NOTE: This function is probably not performant.
Return true if the specified button is down. NOTE: Unknown mouse buttons are NOT handled
and will always return false
.
Sourcepub fn mouse_position(&self) -> (i32, i32)
pub fn mouse_position(&self) -> (i32, i32)
Return the current position of the mouse, relative to the top-left corner of the Window.
Source§impl Window
§Drawing Methods
impl Window
§Drawing Methods
Sourcepub fn set_color(&mut self, red: u8, green: u8, blue: u8, alpha: u8)
pub fn set_color(&mut self, red: u8, green: u8, blue: u8, alpha: u8)
Windows have a color set on them at all times. This color is applied to every draw operation. To “unset” the color, call set_color with (255,255,255,255)
Examples found in repository?
More examples
8fn main() {
9 let mut app = Window::new("Image Font Demo", 640, 480);
10 while app.next_frame() {
11 app.clear_to_color(32, 64, 32);
12
13 app.set_color(255, 255, 255, 255);
14 app.print("Hello world!", 32, 32);
15 app.print("This example demonstrates ImageFont rendering :)", 32, 64);
16 app.print("You can even write symbols: !@#$%^&*()", 32, 96);
17
18 app.set_color(0, 255, 255, 255);
19 app.print("16777216 possible rendering colors!", 32, 128);
20 }
21}
7fn main() {
8 let mut window = simple::Window::new("Los Angeles", 640, 480);
9 let mut pic = window.load_image(include_bytes!("city.jpg")).unwrap();
10
11 let mut frame_number: u64 = 0;
12
13 while window.next_frame() {
14 window.clear();
15
16 let sine = (frame_number as f32 / 150.0).sin().abs();
17 let color = (sine * 255f32) as u8;
18
19 window.set_color(100 + color / 3, color, 255 - color, 255);
20 window.draw_image(&mut pic, 0, 0);
21
22 frame_number += 1;
23 }
24}
pub fn draw_rect(&mut self, rect: Rect)
pub fn draw_point(&mut self, point: Point)
pub fn draw_polygon(&mut self, polygon: Polygon)
Sourcepub fn draw_image(&mut self, image: &mut Image, x: i32, y: i32)
pub fn draw_image(&mut self, image: &mut Image, x: i32, y: i32)
Display the image with its top-left corner at (x, y)
Examples found in repository?
7fn main() {
8 let mut window = simple::Window::new("Los Angeles", 640, 480);
9 let mut pic = window.load_image(include_bytes!("city.jpg")).unwrap();
10
11 let mut frame_number: u64 = 0;
12
13 while window.next_frame() {
14 window.clear();
15
16 let sine = (frame_number as f32 / 150.0).sin().abs();
17 let color = (sine * 255f32) as u8;
18
19 window.set_color(100 + color / 3, color, 255 - color, 255);
20 window.draw_image(&mut pic, 0, 0);
21
22 frame_number += 1;
23 }
24}
Sourcepub fn print(&mut self, text: &str, x: i32, y: i32) -> Rect
pub fn print(&mut self, text: &str, x: i32, y: i32) -> Rect
Write the text to the screen at (x, y) using the currently set font on the Window. Return a Rectangle describing the area of the screen that was modified.
Examples found in repository?
8fn main() {
9 let mut app = Window::new("Image Font Demo", 640, 480);
10 while app.next_frame() {
11 app.clear_to_color(32, 64, 32);
12
13 app.set_color(255, 255, 255, 255);
14 app.print("Hello world!", 32, 32);
15 app.print("This example demonstrates ImageFont rendering :)", 32, 64);
16 app.print("You can even write symbols: !@#$%^&*()", 32, 96);
17
18 app.set_color(0, 255, 255, 255);
19 app.print("16777216 possible rendering colors!", 32, 128);
20 }
21}
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear the screen to black. Does not affect the current rendering color.
Examples found in repository?
7fn main() {
8 let mut window = simple::Window::new("Los Angeles", 640, 480);
9 let mut pic = window.load_image(include_bytes!("city.jpg")).unwrap();
10
11 let mut frame_number: u64 = 0;
12
13 while window.next_frame() {
14 window.clear();
15
16 let sine = (frame_number as f32 / 150.0).sin().abs();
17 let color = (sine * 255f32) as u8;
18
19 window.set_color(100 + color / 3, color, 255 - color, 255);
20 window.draw_image(&mut pic, 0, 0);
21
22 frame_number += 1;
23 }
24}
More examples
78fn main() {
79 // Create an application
80 let mut app = Window::new("Squares", SCREEN_WIDTH, SCREEN_HEIGHT);
81
82 // Create some objects to live in the application
83 let mut squares = vec![Square::new(), Square::new(), Square::new()];
84
85 // Run the game loop
86 while app.next_frame() {
87 // event handling
88 while app.has_event() {
89 match app.next_event() {
90 // If the user clicks, we add a new Square at the position of the mouse event.
91 Event::Mouse {
92 is_down: true,
93 mouse_x,
94 mouse_y,
95 ..
96 } => squares.push(Square::new_at_position(mouse_x as f32, mouse_y as f32)),
97
98 _ => (),
99 }
100 }
101
102 app.clear();
103
104 // update and draw
105 for square in squares.iter_mut() {
106 square.update();
107 square.draw(&mut app);
108 }
109 }
110}
Sourcepub fn clear_to_color(&mut self, r: u8, g: u8, b: u8)
pub fn clear_to_color(&mut self, r: u8, g: u8, b: u8)
Clear the screen to the color you specify.
Examples found in repository?
8fn main() {
9 let mut app = Window::new("Image Font Demo", 640, 480);
10 while app.next_frame() {
11 app.clear_to_color(32, 64, 32);
12
13 app.set_color(255, 255, 255, 255);
14 app.print("Hello world!", 32, 32);
15 app.print("This example demonstrates ImageFont rendering :)", 32, 64);
16 app.print("You can even write symbols: !@#$%^&*()", 32, 96);
17
18 app.set_color(0, 255, 255, 255);
19 app.print("16777216 possible rendering colors!", 32, 128);
20 }
21}
Source§impl Window
§Resource Loading Methods
impl Window
§Resource Loading Methods
Sourcepub fn load_image_from_file(&self, filename: &Path) -> Result<Image, String>
pub fn load_image_from_file(&self, filename: &Path) -> Result<Image, String>
Load the image at the path you specify.
Sourcepub fn load_image(&self, data: &[u8]) -> Result<Image, String>
pub fn load_image(&self, data: &[u8]) -> Result<Image, String>
Load an image from a slice of bytes. This function is particularly powerful when
used in conjunction with the include_bytes
macro that embeds data in the compiled
executable. In this way, you can pack all of your game data into your executable.
Examples found in repository?
7fn main() {
8 let mut window = simple::Window::new("Los Angeles", 640, 480);
9 let mut pic = window.load_image(include_bytes!("city.jpg")).unwrap();
10
11 let mut frame_number: u64 = 0;
12
13 while window.next_frame() {
14 window.clear();
15
16 let sine = (frame_number as f32 / 150.0).sin().abs();
17 let color = (sine * 255f32) as u8;
18
19 window.set_color(100 + color / 3, color, 255 - color, 255);
20 window.draw_image(&mut pic, 0, 0);
21
22 frame_number += 1;
23 }
24}