pub struct Window {
pub width: usize,
pub height: usize,
/* private fields */
}Expand description
Represents a window with a framebuffer for rendering.
Fields§
§width: usizeThe width of the window.
height: usizeThe height of the window.
Implementations§
Source§impl Window
impl Window
Sourcepub fn new(show_fps: bool) -> Result<Self, Box<dyn Error>>
pub fn new(show_fps: bool) -> Result<Self, Box<dyn Error>>
Create a new window with the specified dimensions and an option to show FPS.
show_fps- Whether to display the FPS counter.
Returns a Window instance if successful, or an error if it failed.
Examples found in repository?
examples/hello_world.rs (line 8)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let mut win = Window::new(false)?;
9 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
10 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
11
12 loop {
13 // Check for key presses
14 if let Ok(InputEvent::Key(Key::Char('q'))) = input_rx.try_recv() {
15 break; // Exit the loop if 'q' is pressed
16 }
17
18 // Draw the frame
19 win.draw(|canvas| {
20 canvas.set_named_border(
21 "HELLO WORLD",
22 Align::Right,
23 Attr::NORMAL,
24 Color::White,
25 Color::default(),
26 ); // Set a named border for the canvas
27 canvas.set_str(
28 canvas.width / 2, // Center the text horizontally
29 canvas.height / 2,
30 "Hello, world! (Press 'q' to quit)",
31 Attr::NORMAL, // Set text decoration
32 Color::Green, // Set text color
33 Color::default(), // Set background color
34 Align::Center, // Set text alignment to center
35 );
36 })?;
37
38 thread::sleep(time::Duration::from_millis(100)); // Sleep to prevent high CPU usage
39 }
40 Ok(())
41}More examples
examples/inputs.rs (line 8)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let mut win = Window::new(false)?;
9 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
10 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
11 let mut key_last_pressed = None;
12
13 loop {
14 // Check for key presses
15 if let Ok(event) = input_rx.try_recv() {
16 match event {
17 InputEvent::Key(Key::Char('q')) => break,
18 _ => (),
19 }
20 key_last_pressed = Some(event);
21 }
22
23 // Draw the frame
24 win.draw(|canvas| {
25 canvas.set_named_border(
26 "INPUTS",
27 Align::Right,
28 Attr::NORMAL,
29 Color::White,
30 Color::default(),
31 );
32 let display_text = match key_last_pressed {
33 Some(InputEvent::Key(key)) => format!("Key: {:?}", key),
34 Some(InputEvent::Mouse(mouse)) => match mouse {
35 MouseEvent::Press { button, x, y } => {
36 format!("Mouse Press: {:?} at ({},{})", button, x, y)
37 }
38 MouseEvent::Release { button, x, y } => {
39 format!("Mouse Release: {:?} at ({},{})", button, x, y)
40 }
41 MouseEvent::Move { x, y } => {
42 format!("Mouse Move: at ({},{})", x, y)
43 }
44 },
45 Some(InputEvent::Unknown) => "Unknown input".to_string(),
46 None => "No input yet".to_string(),
47 };
48
49 let full_text = format!("{} (Press 'q' to quit)", display_text);
50
51 canvas.set_str(
52 canvas.width / 2,
53 canvas.height / 2,
54 &full_text,
55 Attr::NORMAL,
56 Color::White,
57 Color::default(),
58 Align::Center,
59 );
60 })?;
61
62 thread::sleep(time::Duration::from_millis(100)); // Sleep to prevent high CPU usage
63 }
64 Ok(())
65}examples/file_reader.rs (line 22)
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12 let args: Vec<String> = std::env::args().collect();
13 let file_path = if args.len() > 1 {
14 &args[1]
15 } else {
16 eprintln!("Usage: file_reader <file_path>");
17 return Ok(());
18 };
19
20 let mut file_reader = FileReader::new(file_path);
21
22 let mut win = Window::new(false)?;
23 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
24 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
25
26 loop {
27 // Check for key presses
28 if let Ok(event) = input_rx.try_recv() {
29 match event {
30 InputEvent::Key(Key::Char('q')) => break,
31 InputEvent::Key(Key::ArrowUp) => {
32 file_reader.scroll_up();
33 }
34 InputEvent::Key(Key::ArrowDown) => {
35 let visible_lines = win.height.saturating_sub(4);
36 file_reader.scroll_down(visible_lines);
37 }
38 InputEvent::Key(Key::PageUp) => {
39 let visible_lines = win.height.saturating_sub(4);
40 file_reader.page_up(visible_lines);
41 }
42 InputEvent::Key(Key::PageDown) => {
43 let visible_lines = win.height.saturating_sub(4);
44 file_reader.page_down(visible_lines);
45 }
46 InputEvent::Key(Key::Home) => {
47 file_reader.current_line = 0;
48 file_reader.scroll_offset = 0;
49 }
50 InputEvent::Key(Key::End) => {
51 let visible_lines = win.height.saturating_sub(4);
52 file_reader.current_line = file_reader.lines.len().saturating_sub(1);
53 file_reader.scroll_offset =
54 file_reader.lines.len().saturating_sub(visible_lines);
55 }
56 _ => {}
57 }
58 }
59
60 // Draw the frame
61 win.draw(|canvas| {
62 file_reader.render(canvas);
63 })?;
64
65 thread::sleep(time::Duration::from_millis(31)); // Sleep to prevent high CPU usage
66 }
67 Ok(())
68}examples/colors.rs (line 8)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let mut win = Window::new(false)?;
9 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
10 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
11
12 loop {
13 // Check for key presses
14 if let Ok(InputEvent::Key(Key::Char('q'))) = input_rx.try_recv() {
15 break; // Exit the loop if 'q' is pressed
16 }
17
18 // Draw the frame
19 win.draw(|canvas| {
20 canvas.set_named_border(
21 "COLORS",
22 Align::Right,
23 Attr::NORMAL,
24 Color::White,
25 Color::default(),
26 );
27
28 // Draw color circle
29 let outer_radius: i32 = 12;
30 let inner_radius: i32 = 6;
31 for hue in 0..360 {
32 for radius in inner_radius..outer_radius {
33 let x = (radius as f32 * (hue as f32).to_radians().cos()) as i32;
34 let y = (radius as f32 * (hue as f32).to_radians().sin()) as i32;
35 let sat = 255 * (radius - inner_radius - outer_radius)
36 / (outer_radius - inner_radius);
37 let color = Color::HSV((hue as f32 / 360.0 * 255.0) as u8, sat as u8, 255);
38 canvas.set_str(
39 ((canvas.width / 2) as i32 + x * 2) as usize,
40 ((canvas.height / 2) as i32 + y) as usize,
41 " ",
42 Attr::NORMAL,
43 Color::default(),
44 color,
45 Align::Left,
46 );
47 }
48 }
49 canvas.set_str(
50 canvas.width / 2 + 1,
51 canvas.height / 2,
52 "Color Circle",
53 Attr::BOLD,
54 Color::White,
55 Color::default(),
56 Align::Center,
57 );
58 canvas.set_str(
59 3,
60 2,
61 "Press 'q' to quit",
62 Attr::BOLD,
63 Color::White,
64 Color::default(),
65 Align::Left,
66 );
67 })?;
68
69 thread::sleep(time::Duration::from_millis(100)); // Sleep to prevent high CPU usage
70 }
71 Ok(())
72}Sourcepub fn initialize(
&mut self,
rendering_rate: Duration,
) -> Result<(), Box<dyn Error>>
pub fn initialize( &mut self, rendering_rate: Duration, ) -> Result<(), Box<dyn Error>>
Initialize the window and start the rendering thread
rendering_rate- The rate at which to render frames.
Returns Ok(()) if the initialization was successful, or an error if it failed.
Examples found in repository?
examples/hello_world.rs (line 9)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let mut win = Window::new(false)?;
9 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
10 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
11
12 loop {
13 // Check for key presses
14 if let Ok(InputEvent::Key(Key::Char('q'))) = input_rx.try_recv() {
15 break; // Exit the loop if 'q' is pressed
16 }
17
18 // Draw the frame
19 win.draw(|canvas| {
20 canvas.set_named_border(
21 "HELLO WORLD",
22 Align::Right,
23 Attr::NORMAL,
24 Color::White,
25 Color::default(),
26 ); // Set a named border for the canvas
27 canvas.set_str(
28 canvas.width / 2, // Center the text horizontally
29 canvas.height / 2,
30 "Hello, world! (Press 'q' to quit)",
31 Attr::NORMAL, // Set text decoration
32 Color::Green, // Set text color
33 Color::default(), // Set background color
34 Align::Center, // Set text alignment to center
35 );
36 })?;
37
38 thread::sleep(time::Duration::from_millis(100)); // Sleep to prevent high CPU usage
39 }
40 Ok(())
41}More examples
examples/inputs.rs (line 9)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let mut win = Window::new(false)?;
9 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
10 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
11 let mut key_last_pressed = None;
12
13 loop {
14 // Check for key presses
15 if let Ok(event) = input_rx.try_recv() {
16 match event {
17 InputEvent::Key(Key::Char('q')) => break,
18 _ => (),
19 }
20 key_last_pressed = Some(event);
21 }
22
23 // Draw the frame
24 win.draw(|canvas| {
25 canvas.set_named_border(
26 "INPUTS",
27 Align::Right,
28 Attr::NORMAL,
29 Color::White,
30 Color::default(),
31 );
32 let display_text = match key_last_pressed {
33 Some(InputEvent::Key(key)) => format!("Key: {:?}", key),
34 Some(InputEvent::Mouse(mouse)) => match mouse {
35 MouseEvent::Press { button, x, y } => {
36 format!("Mouse Press: {:?} at ({},{})", button, x, y)
37 }
38 MouseEvent::Release { button, x, y } => {
39 format!("Mouse Release: {:?} at ({},{})", button, x, y)
40 }
41 MouseEvent::Move { x, y } => {
42 format!("Mouse Move: at ({},{})", x, y)
43 }
44 },
45 Some(InputEvent::Unknown) => "Unknown input".to_string(),
46 None => "No input yet".to_string(),
47 };
48
49 let full_text = format!("{} (Press 'q' to quit)", display_text);
50
51 canvas.set_str(
52 canvas.width / 2,
53 canvas.height / 2,
54 &full_text,
55 Attr::NORMAL,
56 Color::White,
57 Color::default(),
58 Align::Center,
59 );
60 })?;
61
62 thread::sleep(time::Duration::from_millis(100)); // Sleep to prevent high CPU usage
63 }
64 Ok(())
65}examples/file_reader.rs (line 23)
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12 let args: Vec<String> = std::env::args().collect();
13 let file_path = if args.len() > 1 {
14 &args[1]
15 } else {
16 eprintln!("Usage: file_reader <file_path>");
17 return Ok(());
18 };
19
20 let mut file_reader = FileReader::new(file_path);
21
22 let mut win = Window::new(false)?;
23 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
24 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
25
26 loop {
27 // Check for key presses
28 if let Ok(event) = input_rx.try_recv() {
29 match event {
30 InputEvent::Key(Key::Char('q')) => break,
31 InputEvent::Key(Key::ArrowUp) => {
32 file_reader.scroll_up();
33 }
34 InputEvent::Key(Key::ArrowDown) => {
35 let visible_lines = win.height.saturating_sub(4);
36 file_reader.scroll_down(visible_lines);
37 }
38 InputEvent::Key(Key::PageUp) => {
39 let visible_lines = win.height.saturating_sub(4);
40 file_reader.page_up(visible_lines);
41 }
42 InputEvent::Key(Key::PageDown) => {
43 let visible_lines = win.height.saturating_sub(4);
44 file_reader.page_down(visible_lines);
45 }
46 InputEvent::Key(Key::Home) => {
47 file_reader.current_line = 0;
48 file_reader.scroll_offset = 0;
49 }
50 InputEvent::Key(Key::End) => {
51 let visible_lines = win.height.saturating_sub(4);
52 file_reader.current_line = file_reader.lines.len().saturating_sub(1);
53 file_reader.scroll_offset =
54 file_reader.lines.len().saturating_sub(visible_lines);
55 }
56 _ => {}
57 }
58 }
59
60 // Draw the frame
61 win.draw(|canvas| {
62 file_reader.render(canvas);
63 })?;
64
65 thread::sleep(time::Duration::from_millis(31)); // Sleep to prevent high CPU usage
66 }
67 Ok(())
68}examples/colors.rs (line 9)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let mut win = Window::new(false)?;
9 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
10 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
11
12 loop {
13 // Check for key presses
14 if let Ok(InputEvent::Key(Key::Char('q'))) = input_rx.try_recv() {
15 break; // Exit the loop if 'q' is pressed
16 }
17
18 // Draw the frame
19 win.draw(|canvas| {
20 canvas.set_named_border(
21 "COLORS",
22 Align::Right,
23 Attr::NORMAL,
24 Color::White,
25 Color::default(),
26 );
27
28 // Draw color circle
29 let outer_radius: i32 = 12;
30 let inner_radius: i32 = 6;
31 for hue in 0..360 {
32 for radius in inner_radius..outer_radius {
33 let x = (radius as f32 * (hue as f32).to_radians().cos()) as i32;
34 let y = (radius as f32 * (hue as f32).to_radians().sin()) as i32;
35 let sat = 255 * (radius - inner_radius - outer_radius)
36 / (outer_radius - inner_radius);
37 let color = Color::HSV((hue as f32 / 360.0 * 255.0) as u8, sat as u8, 255);
38 canvas.set_str(
39 ((canvas.width / 2) as i32 + x * 2) as usize,
40 ((canvas.height / 2) as i32 + y) as usize,
41 " ",
42 Attr::NORMAL,
43 Color::default(),
44 color,
45 Align::Left,
46 );
47 }
48 }
49 canvas.set_str(
50 canvas.width / 2 + 1,
51 canvas.height / 2,
52 "Color Circle",
53 Attr::BOLD,
54 Color::White,
55 Color::default(),
56 Align::Center,
57 );
58 canvas.set_str(
59 3,
60 2,
61 "Press 'q' to quit",
62 Attr::BOLD,
63 Color::White,
64 Color::default(),
65 Align::Left,
66 );
67 })?;
68
69 thread::sleep(time::Duration::from_millis(100)); // Sleep to prevent high CPU usage
70 }
71 Ok(())
72}Sourcepub fn draw(
&mut self,
f: impl FnOnce(&mut Framebuffer),
) -> Result<(), Box<dyn Error>>
pub fn draw( &mut self, f: impl FnOnce(&mut Framebuffer), ) -> Result<(), Box<dyn Error>>
Draw the contents of the framebuffer
f- A closure that takes a mutable reference to the framebuffer.
Returns Ok(()) if the drawing was successful, or an error if it failed.
Examples found in repository?
examples/hello_world.rs (lines 19-36)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let mut win = Window::new(false)?;
9 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
10 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
11
12 loop {
13 // Check for key presses
14 if let Ok(InputEvent::Key(Key::Char('q'))) = input_rx.try_recv() {
15 break; // Exit the loop if 'q' is pressed
16 }
17
18 // Draw the frame
19 win.draw(|canvas| {
20 canvas.set_named_border(
21 "HELLO WORLD",
22 Align::Right,
23 Attr::NORMAL,
24 Color::White,
25 Color::default(),
26 ); // Set a named border for the canvas
27 canvas.set_str(
28 canvas.width / 2, // Center the text horizontally
29 canvas.height / 2,
30 "Hello, world! (Press 'q' to quit)",
31 Attr::NORMAL, // Set text decoration
32 Color::Green, // Set text color
33 Color::default(), // Set background color
34 Align::Center, // Set text alignment to center
35 );
36 })?;
37
38 thread::sleep(time::Duration::from_millis(100)); // Sleep to prevent high CPU usage
39 }
40 Ok(())
41}More examples
examples/inputs.rs (lines 24-60)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let mut win = Window::new(false)?;
9 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
10 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
11 let mut key_last_pressed = None;
12
13 loop {
14 // Check for key presses
15 if let Ok(event) = input_rx.try_recv() {
16 match event {
17 InputEvent::Key(Key::Char('q')) => break,
18 _ => (),
19 }
20 key_last_pressed = Some(event);
21 }
22
23 // Draw the frame
24 win.draw(|canvas| {
25 canvas.set_named_border(
26 "INPUTS",
27 Align::Right,
28 Attr::NORMAL,
29 Color::White,
30 Color::default(),
31 );
32 let display_text = match key_last_pressed {
33 Some(InputEvent::Key(key)) => format!("Key: {:?}", key),
34 Some(InputEvent::Mouse(mouse)) => match mouse {
35 MouseEvent::Press { button, x, y } => {
36 format!("Mouse Press: {:?} at ({},{})", button, x, y)
37 }
38 MouseEvent::Release { button, x, y } => {
39 format!("Mouse Release: {:?} at ({},{})", button, x, y)
40 }
41 MouseEvent::Move { x, y } => {
42 format!("Mouse Move: at ({},{})", x, y)
43 }
44 },
45 Some(InputEvent::Unknown) => "Unknown input".to_string(),
46 None => "No input yet".to_string(),
47 };
48
49 let full_text = format!("{} (Press 'q' to quit)", display_text);
50
51 canvas.set_str(
52 canvas.width / 2,
53 canvas.height / 2,
54 &full_text,
55 Attr::NORMAL,
56 Color::White,
57 Color::default(),
58 Align::Center,
59 );
60 })?;
61
62 thread::sleep(time::Duration::from_millis(100)); // Sleep to prevent high CPU usage
63 }
64 Ok(())
65}examples/file_reader.rs (lines 61-63)
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12 let args: Vec<String> = std::env::args().collect();
13 let file_path = if args.len() > 1 {
14 &args[1]
15 } else {
16 eprintln!("Usage: file_reader <file_path>");
17 return Ok(());
18 };
19
20 let mut file_reader = FileReader::new(file_path);
21
22 let mut win = Window::new(false)?;
23 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
24 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
25
26 loop {
27 // Check for key presses
28 if let Ok(event) = input_rx.try_recv() {
29 match event {
30 InputEvent::Key(Key::Char('q')) => break,
31 InputEvent::Key(Key::ArrowUp) => {
32 file_reader.scroll_up();
33 }
34 InputEvent::Key(Key::ArrowDown) => {
35 let visible_lines = win.height.saturating_sub(4);
36 file_reader.scroll_down(visible_lines);
37 }
38 InputEvent::Key(Key::PageUp) => {
39 let visible_lines = win.height.saturating_sub(4);
40 file_reader.page_up(visible_lines);
41 }
42 InputEvent::Key(Key::PageDown) => {
43 let visible_lines = win.height.saturating_sub(4);
44 file_reader.page_down(visible_lines);
45 }
46 InputEvent::Key(Key::Home) => {
47 file_reader.current_line = 0;
48 file_reader.scroll_offset = 0;
49 }
50 InputEvent::Key(Key::End) => {
51 let visible_lines = win.height.saturating_sub(4);
52 file_reader.current_line = file_reader.lines.len().saturating_sub(1);
53 file_reader.scroll_offset =
54 file_reader.lines.len().saturating_sub(visible_lines);
55 }
56 _ => {}
57 }
58 }
59
60 // Draw the frame
61 win.draw(|canvas| {
62 file_reader.render(canvas);
63 })?;
64
65 thread::sleep(time::Duration::from_millis(31)); // Sleep to prevent high CPU usage
66 }
67 Ok(())
68}examples/colors.rs (lines 19-67)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let mut win = Window::new(false)?;
9 win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
10 let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
11
12 loop {
13 // Check for key presses
14 if let Ok(InputEvent::Key(Key::Char('q'))) = input_rx.try_recv() {
15 break; // Exit the loop if 'q' is pressed
16 }
17
18 // Draw the frame
19 win.draw(|canvas| {
20 canvas.set_named_border(
21 "COLORS",
22 Align::Right,
23 Attr::NORMAL,
24 Color::White,
25 Color::default(),
26 );
27
28 // Draw color circle
29 let outer_radius: i32 = 12;
30 let inner_radius: i32 = 6;
31 for hue in 0..360 {
32 for radius in inner_radius..outer_radius {
33 let x = (radius as f32 * (hue as f32).to_radians().cos()) as i32;
34 let y = (radius as f32 * (hue as f32).to_radians().sin()) as i32;
35 let sat = 255 * (radius - inner_radius - outer_radius)
36 / (outer_radius - inner_radius);
37 let color = Color::HSV((hue as f32 / 360.0 * 255.0) as u8, sat as u8, 255);
38 canvas.set_str(
39 ((canvas.width / 2) as i32 + x * 2) as usize,
40 ((canvas.height / 2) as i32 + y) as usize,
41 " ",
42 Attr::NORMAL,
43 Color::default(),
44 color,
45 Align::Left,
46 );
47 }
48 }
49 canvas.set_str(
50 canvas.width / 2 + 1,
51 canvas.height / 2,
52 "Color Circle",
53 Attr::BOLD,
54 Color::White,
55 Color::default(),
56 Align::Center,
57 );
58 canvas.set_str(
59 3,
60 2,
61 "Press 'q' to quit",
62 Attr::BOLD,
63 Color::White,
64 Color::default(),
65 Align::Left,
66 );
67 })?;
68
69 thread::sleep(time::Duration::from_millis(100)); // Sleep to prevent high CPU usage
70 }
71 Ok(())
72}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