Window

Struct Window 

Source
pub struct Window { /* private fields */ }
Expand description

Window representation. Used for drawing and events handling.

Implementations§

Source§

impl Window

Source

pub fn new(height: u16, width: u16) -> Result<Self>

Creates a window.

Examples found in repository?
examples/demo.rs (line 19)
13fn main() -> Result<()> {
14    println!("Use arrows or WASD to move.");
15    println!("[Press any key to continue]");
16    terminal::enable_raw_mode()?;
17    event::read()?;
18
19    let mut window = Window::new(25, 25)?;
20    let background_color = Color::Rgb {
21        r: 100,
22        g: 100,
23        b: 100,
24    };
25    for y in 0..window.height() {
26        for x in 0..window.width() {
27            window.set_pixel(y, x, background_color);
28        }
29    }
30    let mut player = Player { x: 12, y: 12 };
31    loop {
32        window.poll_events()?;
33        if window.get_key(KeyCode::Esc) {
34            break;
35        }
36        window.set_pixel(player.y, player.x, background_color);
37        if player.y > 0 && (window.get_key(KeyCode::Up) || window.get_key(KeyCode::Char('w'))) {
38            player.y -= 1;
39        }
40        if player.y < window.height() - 1
41            && (window.get_key(KeyCode::Down) || window.get_key(KeyCode::Char('s')))
42        {
43            player.y += 1;
44        }
45        if player.x > 0 && (window.get_key(KeyCode::Left) || window.get_key(KeyCode::Char('a'))) {
46            player.x -= 1;
47        }
48        if player.x < window.width() - 1
49            && (window.get_key(KeyCode::Right) || window.get_key(KeyCode::Char('d')))
50        {
51            player.x += 1;
52        }
53        window.set_pixel(player.y, player.x, Color::Red);
54        window.redraw()?;
55    }
56    Ok(())
57}
Source

pub fn width(&self) -> u16

Gets the window width.

Examples found in repository?
examples/demo.rs (line 26)
13fn main() -> Result<()> {
14    println!("Use arrows or WASD to move.");
15    println!("[Press any key to continue]");
16    terminal::enable_raw_mode()?;
17    event::read()?;
18
19    let mut window = Window::new(25, 25)?;
20    let background_color = Color::Rgb {
21        r: 100,
22        g: 100,
23        b: 100,
24    };
25    for y in 0..window.height() {
26        for x in 0..window.width() {
27            window.set_pixel(y, x, background_color);
28        }
29    }
30    let mut player = Player { x: 12, y: 12 };
31    loop {
32        window.poll_events()?;
33        if window.get_key(KeyCode::Esc) {
34            break;
35        }
36        window.set_pixel(player.y, player.x, background_color);
37        if player.y > 0 && (window.get_key(KeyCode::Up) || window.get_key(KeyCode::Char('w'))) {
38            player.y -= 1;
39        }
40        if player.y < window.height() - 1
41            && (window.get_key(KeyCode::Down) || window.get_key(KeyCode::Char('s')))
42        {
43            player.y += 1;
44        }
45        if player.x > 0 && (window.get_key(KeyCode::Left) || window.get_key(KeyCode::Char('a'))) {
46            player.x -= 1;
47        }
48        if player.x < window.width() - 1
49            && (window.get_key(KeyCode::Right) || window.get_key(KeyCode::Char('d')))
50        {
51            player.x += 1;
52        }
53        window.set_pixel(player.y, player.x, Color::Red);
54        window.redraw()?;
55    }
56    Ok(())
57}
Source

pub fn height(&self) -> u16

Gets the window height.

Examples found in repository?
examples/demo.rs (line 25)
13fn main() -> Result<()> {
14    println!("Use arrows or WASD to move.");
15    println!("[Press any key to continue]");
16    terminal::enable_raw_mode()?;
17    event::read()?;
18
19    let mut window = Window::new(25, 25)?;
20    let background_color = Color::Rgb {
21        r: 100,
22        g: 100,
23        b: 100,
24    };
25    for y in 0..window.height() {
26        for x in 0..window.width() {
27            window.set_pixel(y, x, background_color);
28        }
29    }
30    let mut player = Player { x: 12, y: 12 };
31    loop {
32        window.poll_events()?;
33        if window.get_key(KeyCode::Esc) {
34            break;
35        }
36        window.set_pixel(player.y, player.x, background_color);
37        if player.y > 0 && (window.get_key(KeyCode::Up) || window.get_key(KeyCode::Char('w'))) {
38            player.y -= 1;
39        }
40        if player.y < window.height() - 1
41            && (window.get_key(KeyCode::Down) || window.get_key(KeyCode::Char('s')))
42        {
43            player.y += 1;
44        }
45        if player.x > 0 && (window.get_key(KeyCode::Left) || window.get_key(KeyCode::Char('a'))) {
46            player.x -= 1;
47        }
48        if player.x < window.width() - 1
49            && (window.get_key(KeyCode::Right) || window.get_key(KeyCode::Char('d')))
50        {
51            player.x += 1;
52        }
53        window.set_pixel(player.y, player.x, Color::Red);
54        window.redraw()?;
55    }
56    Ok(())
57}
Source

pub fn set_pixel(&mut self, y: u16, x: u16, color: Color)

Sets a pixel color.

Examples found in repository?
examples/demo.rs (line 27)
13fn main() -> Result<()> {
14    println!("Use arrows or WASD to move.");
15    println!("[Press any key to continue]");
16    terminal::enable_raw_mode()?;
17    event::read()?;
18
19    let mut window = Window::new(25, 25)?;
20    let background_color = Color::Rgb {
21        r: 100,
22        g: 100,
23        b: 100,
24    };
25    for y in 0..window.height() {
26        for x in 0..window.width() {
27            window.set_pixel(y, x, background_color);
28        }
29    }
30    let mut player = Player { x: 12, y: 12 };
31    loop {
32        window.poll_events()?;
33        if window.get_key(KeyCode::Esc) {
34            break;
35        }
36        window.set_pixel(player.y, player.x, background_color);
37        if player.y > 0 && (window.get_key(KeyCode::Up) || window.get_key(KeyCode::Char('w'))) {
38            player.y -= 1;
39        }
40        if player.y < window.height() - 1
41            && (window.get_key(KeyCode::Down) || window.get_key(KeyCode::Char('s')))
42        {
43            player.y += 1;
44        }
45        if player.x > 0 && (window.get_key(KeyCode::Left) || window.get_key(KeyCode::Char('a'))) {
46            player.x -= 1;
47        }
48        if player.x < window.width() - 1
49            && (window.get_key(KeyCode::Right) || window.get_key(KeyCode::Char('d')))
50        {
51            player.x += 1;
52        }
53        window.set_pixel(player.y, player.x, Color::Red);
54        window.redraw()?;
55    }
56    Ok(())
57}
Source

pub fn redraw(&self) -> Result<()>

Redraws the window to the terminal.

Examples found in repository?
examples/demo.rs (line 54)
13fn main() -> Result<()> {
14    println!("Use arrows or WASD to move.");
15    println!("[Press any key to continue]");
16    terminal::enable_raw_mode()?;
17    event::read()?;
18
19    let mut window = Window::new(25, 25)?;
20    let background_color = Color::Rgb {
21        r: 100,
22        g: 100,
23        b: 100,
24    };
25    for y in 0..window.height() {
26        for x in 0..window.width() {
27            window.set_pixel(y, x, background_color);
28        }
29    }
30    let mut player = Player { x: 12, y: 12 };
31    loop {
32        window.poll_events()?;
33        if window.get_key(KeyCode::Esc) {
34            break;
35        }
36        window.set_pixel(player.y, player.x, background_color);
37        if player.y > 0 && (window.get_key(KeyCode::Up) || window.get_key(KeyCode::Char('w'))) {
38            player.y -= 1;
39        }
40        if player.y < window.height() - 1
41            && (window.get_key(KeyCode::Down) || window.get_key(KeyCode::Char('s')))
42        {
43            player.y += 1;
44        }
45        if player.x > 0 && (window.get_key(KeyCode::Left) || window.get_key(KeyCode::Char('a'))) {
46            player.x -= 1;
47        }
48        if player.x < window.width() - 1
49            && (window.get_key(KeyCode::Right) || window.get_key(KeyCode::Char('d')))
50        {
51            player.x += 1;
52        }
53        window.set_pixel(player.y, player.x, Color::Red);
54        window.redraw()?;
55    }
56    Ok(())
57}
Source

pub fn poll_events(&mut self) -> Result<()>

Clears events and polls for newer events.

Examples found in repository?
examples/demo.rs (line 32)
13fn main() -> Result<()> {
14    println!("Use arrows or WASD to move.");
15    println!("[Press any key to continue]");
16    terminal::enable_raw_mode()?;
17    event::read()?;
18
19    let mut window = Window::new(25, 25)?;
20    let background_color = Color::Rgb {
21        r: 100,
22        g: 100,
23        b: 100,
24    };
25    for y in 0..window.height() {
26        for x in 0..window.width() {
27            window.set_pixel(y, x, background_color);
28        }
29    }
30    let mut player = Player { x: 12, y: 12 };
31    loop {
32        window.poll_events()?;
33        if window.get_key(KeyCode::Esc) {
34            break;
35        }
36        window.set_pixel(player.y, player.x, background_color);
37        if player.y > 0 && (window.get_key(KeyCode::Up) || window.get_key(KeyCode::Char('w'))) {
38            player.y -= 1;
39        }
40        if player.y < window.height() - 1
41            && (window.get_key(KeyCode::Down) || window.get_key(KeyCode::Char('s')))
42        {
43            player.y += 1;
44        }
45        if player.x > 0 && (window.get_key(KeyCode::Left) || window.get_key(KeyCode::Char('a'))) {
46            player.x -= 1;
47        }
48        if player.x < window.width() - 1
49            && (window.get_key(KeyCode::Right) || window.get_key(KeyCode::Char('d')))
50        {
51            player.x += 1;
52        }
53        window.set_pixel(player.y, player.x, Color::Red);
54        window.redraw()?;
55    }
56    Ok(())
57}
Source

pub fn get_key(&mut self, key: KeyCode) -> bool

Returns true if key was read during the last call to Window::poll_events.

Examples found in repository?
examples/demo.rs (line 33)
13fn main() -> Result<()> {
14    println!("Use arrows or WASD to move.");
15    println!("[Press any key to continue]");
16    terminal::enable_raw_mode()?;
17    event::read()?;
18
19    let mut window = Window::new(25, 25)?;
20    let background_color = Color::Rgb {
21        r: 100,
22        g: 100,
23        b: 100,
24    };
25    for y in 0..window.height() {
26        for x in 0..window.width() {
27            window.set_pixel(y, x, background_color);
28        }
29    }
30    let mut player = Player { x: 12, y: 12 };
31    loop {
32        window.poll_events()?;
33        if window.get_key(KeyCode::Esc) {
34            break;
35        }
36        window.set_pixel(player.y, player.x, background_color);
37        if player.y > 0 && (window.get_key(KeyCode::Up) || window.get_key(KeyCode::Char('w'))) {
38            player.y -= 1;
39        }
40        if player.y < window.height() - 1
41            && (window.get_key(KeyCode::Down) || window.get_key(KeyCode::Char('s')))
42        {
43            player.y += 1;
44        }
45        if player.x > 0 && (window.get_key(KeyCode::Left) || window.get_key(KeyCode::Char('a'))) {
46            player.x -= 1;
47        }
48        if player.x < window.width() - 1
49            && (window.get_key(KeyCode::Right) || window.get_key(KeyCode::Char('d')))
50        {
51            player.x += 1;
52        }
53        window.set_pixel(player.y, player.x, Color::Red);
54        window.redraw()?;
55    }
56    Ok(())
57}
Source

pub fn get_modifiers(&mut self, modifiers: KeyModifiers) -> bool

Returns true if modifiers was read during the last call to Window::poll_events.

Trait Implementations§

Source§

impl Debug for Window

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Window

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.