pub struct Window { /* private fields */ }Expand description
Window representation. Used for drawing and events handling.
Implementations§
Source§impl Window
impl Window
Sourcepub fn new(height: u16, width: u16) -> Result<Self>
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}Sourcepub fn width(&self) -> u16
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}Sourcepub fn height(&self) -> u16
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}Sourcepub fn set_pixel(&mut self, y: u16, x: u16, color: Color)
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}Sourcepub fn redraw(&self) -> Result<()>
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}Sourcepub fn poll_events(&mut self) -> Result<()>
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}Sourcepub fn get_key(&mut self, key: KeyCode) -> bool
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}Sourcepub fn get_modifiers(&mut self, modifiers: KeyModifiers) -> bool
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.