CrosstermCanvas

Struct CrosstermCanvas 

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

A canvas implementation that renders to the terminal using crossterm.

This canvas provides terminal-based rendering using Unicode half blocks and ANSI colors. It supports frame rate limiting and efficient updates by only redrawing changed parts of the screen.

§Example

use pixel_loop::canvas::CrosstermCanvas;
use pixel_loop::color::*;
use pixel_loop::canvas::Canvas;
use pixel_loop::canvas::RenderableCanvas;
use std::ops::Range;
use anyhow::Result;

fn main() -> Result<()> {
  let mut canvas = CrosstermCanvas::new(80, 24);
  canvas.filled_rect(5, 5, 10, 10, &Color::from_rgb(255, 0, 0));
  // Should of course be called within the [pixel_loop::run] function.
  canvas.render()?;
 Ok(())
}

Implementations§

Source§

impl CrosstermCanvas

Source

pub fn new() -> Self

Creates a new terminal canvas automatically toking the size of the terminal it is spawned in.

A canvas based on the terminals size is resizable by default.

§Example
use pixel_loop::canvas::CrosstermCanvas;

let canvas = CrosstermCanvas::new();
Source

pub fn new_with_size(width: u32, height: u32) -> Self

Creates a new terminal canvas with the specified dimensions.

A canvas with specified dimensions is not resizable by default.

§Arguments
  • width - The width of the canvas in characters
  • height - The height of the canvas in half characters
§Example
use pixel_loop::canvas::CrosstermCanvas;

let canvas = CrosstermCanvas::new(80, 42);
Source

pub fn with_resizable(self, resizable: bool) -> Self

Sets the canvas to be resizable or not.

Source

pub fn with_refresh_limit(self, limit: usize) -> Self

Sets the frame rate limit.

§Arguments
  • limit - Target frames per second

By default, the canvas is limited to 60 frames per second.

§Example
use pixel_loop::canvas::CrosstermCanvas;

// Limit the frame rate to 30 frames per second
let mut canvas = CrosstermCanvas::new(80, 24).with_refresh_limit(30);

Trait Implementations§

Source§

impl Canvas for CrosstermCanvas

Source§

fn width(&self) -> u32

Get the width of the canvas in pixels
Source§

fn height(&self) -> u32

Get the height of the canvas in pixels
Source§

fn set_range(&mut self, range: Range<usize>, color: &[Color])

Set a range of pixels to a given Color
Source§

fn get_range(&self, range: Range<usize>) -> &[Color]

Get a range of pixels as a slice of Colors
Source§

fn blit<C: Canvas>( &mut self, src_canvas: &C, dst_x: i64, dst_y: i64, tint: Option<&Color>, )

Blit a full input canvas to this canvas instance at a given position, optionally tinting the input canvas with a color. Read more
Source§

fn blit_rect<C: Canvas>( &mut self, src_canvas: &C, src_x: u32, src_y: u32, width: u32, height: u32, dst_x: i64, dst_y: i64, tint: Option<&Color>, )

Blit only a rectangular region of the input canvas to this canvas instance at a given position, optionally tinting the input canvas with a color. If the source rectangle is partially out of view, only the visible part will be blitted. If the destination rectangle is partially out of view, only the visible part will be blitted. Read more
Source§

fn get(&self, x: u32, y: u32) -> &Color

Get the color of a specific pixel at a given position
Source§

fn maybe_get(&self, x: i64, y: i64) -> Option<&Color>

Get the color of a specific pixel at a given position, if it is in bounds of the canvas. Read more
Source§

fn is_empty_or_color(&self, x: i64, y: i64, color: &Color) -> bool

Check if a specific pixel at a given position is out of bounds (empty) or has the specified color. Read more
Source§

fn set(&mut self, x: u32, y: u32, color: &Color)

Set the color of a specific pixel at a given position
Source§

fn clip_rect( &self, x: i64, y: i64, width: u32, height: u32, ) -> Option<(u32, u32, u32, u32)>

Clip a rectangle to the bounds of the canvas. Read more
Source§

fn clear_screen(&mut self, color: &Color)

Clear (fill) the whole canvas with a specific color
Source§

fn filled_rect( &mut self, sx: i64, sy: i64, width: u32, height: u32, color: &Color, )

Draw a filled rectangle at a given position with a given width and height
Source§

impl RenderableCanvas for CrosstermCanvas

Source§

fn run<State: 'static>(pixel_loop: PixelLoop<State, Self>) -> !

Runs the pixel loop.

Source§

type Input = CrosstermInputState

Source§

fn render(&mut self) -> Result<()>

Source§

fn resize_surface(&mut self, width: u32, height: u32, scale_factor: Option<f64>)

Called when the surface of the canvas has been resized. The physical_dimensions of the new rendering surface are supplied. Read more
Source§

fn did_resize(&self) -> Option<(u32, u32)>

Can be asked in an update loop, to check if the canvas resized since the last update loop. Read more
Source§

fn begin(&mut self) -> Result<()>

Called before the main loop starts.
Source§

fn finish(&mut self, _code: i32) -> Result<()>

Called after the main loop finishes.

Auto Trait Implementations§

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

Source§

fn downcast(&self) -> &T

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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V