Struct Window

Source
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

Source

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?
examples/font.rs (line 9)
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
Hide additional examples
examples/city.rs (line 8)
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}
examples/squares.rs (line 80)
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}
Source

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?
examples/font.rs (line 10)
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
Hide additional examples
examples/city.rs (line 13)
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}
examples/squares.rs (line 86)
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}
Source

pub fn has_event(&self) -> bool

Return true when there is an event waiting in the queue for processing.

Examples found in repository?
examples/squares.rs (line 88)
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}
Source

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?
examples/squares.rs (line 89)
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}
Source

pub fn is_key_down(&self, key: Key) -> bool

Return true if the button is currently pressed. NOTE: This function is probably not performant.

Source

pub fn is_mouse_button_down(&self, button: MouseButton) -> bool

Return true if the specified button is down. NOTE: Unknown mouse buttons are NOT handled and will always return false.

Source

pub fn mouse_position(&self) -> (i32, i32)

Return the current position of the mouse, relative to the top-left corner of the Window.

Source

pub fn set_font(&mut self, font: Font)

Use this Font for future calls to print().

Source

pub fn quit(&mut self)

This does not cause the program to exit immediately. It just means that next_frame will return false on the next call.

Source§

impl Window

§Drawing Methods

Source

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?
examples/squares.rs (line 73)
72    fn draw(&self, app: &mut Window) {
73        app.set_color(self.color.0, self.color.1, self.color.2, self.color.3);
74        app.fill_rect(Rect::new(self.x as i32 - 32, self.y as i32 - 32, 64, 64));
75    }
More examples
Hide additional examples
examples/font.rs (line 13)
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}
examples/city.rs (line 19)
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}
Source

pub fn draw_rect(&mut self, rect: Rect)

Source

pub fn fill_rect(&mut self, rect: Rect)

Examples found in repository?
examples/squares.rs (line 74)
72    fn draw(&self, app: &mut Window) {
73        app.set_color(self.color.0, self.color.1, self.color.2, self.color.3);
74        app.fill_rect(Rect::new(self.x as i32 - 32, self.y as i32 - 32, 64, 64));
75    }
Source

pub fn draw_point(&mut self, point: Point)

Source

pub fn draw_polygon(&mut self, polygon: Polygon)

Source

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?
examples/city.rs (line 20)
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}
Source

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?
examples/font.rs (line 14)
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

pub fn clear(&mut self)

Clear the screen to black. Does not affect the current rendering color.

Examples found in repository?
examples/city.rs (line 14)
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
Hide additional examples
examples/squares.rs (line 102)
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}
Source

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?
examples/font.rs (line 11)
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

Source

pub fn load_image_from_file(&self, filename: &Path) -> Result<Image, String>

Load the image at the path you specify.

Source

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?
examples/city.rs (line 9)
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}
Source

pub fn load_font_from_file( &self, filename: &Path, string: String, ) -> Result<Font, String>

Load a Font from the hard drive. See the documentation on Font for details.

Source

pub fn load_font(&self, data: &[u8], string: String) -> Result<Font, String>

Load a Font from a slice of bytes. See the documentation on Font for details. This function is particularly powerful when used in conjunction with the include_bytes macro that embeds data in the compiled executable.

Auto Trait Implementations§

§

impl Freeze for Window

§

impl RefUnwindSafe for Window

§

impl !Send for Window

§

impl !Sync for Window

§

impl Unpin for Window

§

impl UnwindSafe for Window

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.