Struct console_engine::screen::Screen
source · pub struct Screen { /* private fields */ }Expand description
Screen structure
A standalone structure that provides every drawing function that ConsoleEngine provides.
You can get the full content of the screen via the draw method.
Implementations§
source§impl Screen
impl Screen
Basic Usage :
use console_engine::pixel;
use console_engine::screen::Screen;
use console_engine::Color;
fn main() {
// create a screen of 20x11 characters
let mut scr = screen::Screen::new(20,11);
// draw some shapes and prints some text
scr.rect(0,0, 19,10,pixel::pxl('#'));
scr.fill_circle(5,5, 3, pixel::pxl_fg('*', Color::Blue));
scr.print(11,4, "Hello,");
scr.print(11,5, "World!");
// print the screen to the terminal
scr.draw();
}sourcepub fn new(width: u32, height: u32) -> Screen
pub fn new(width: u32, height: u32) -> Screen
Creates a new Screen object with the provided width and height.
sourcepub fn new_fill(width: u32, height: u32, pixel: Pixel) -> Screen
pub fn new_fill(width: u32, height: u32, pixel: Pixel) -> Screen
Creates a new Screen object with the provided width and height filled with a specific Pixel
sourcepub fn from_vec(vec: Vec<Pixel>, width: u32, height: u32) -> Screen
pub fn from_vec(vec: Vec<Pixel>, width: u32, height: u32) -> Screen
Creates a new Screen object with the provided Vec
sourcepub fn from_string(
string: String,
fg: Color,
bg: Color,
width: u32,
height: u32
) -> Screen
pub fn from_string( string: String, fg: Color, bg: Color, width: u32, height: u32 ) -> Screen
Creates a new Screen object with the provided String and colors fitting the width and height parameters. The String length must correspond to width*height
sourcepub fn get_height(&self) -> u32
pub fn get_height(&self) -> u32
Get the screen height
sourcepub fn check_empty(&mut self) -> bool
pub fn check_empty(&mut self) -> bool
checks whenever the screen is full of “zero” characters refresh internal “empty” value
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns a cached result of check_empty
sourcepub fn print(&mut self, x: i32, y: i32, string: &str)
pub fn print(&mut self, x: i32, y: i32, string: &str)
prints a string at the specified coordinates.
The string will be cropped if it reach the right border
usage:
screen.print(0, 0, "Hello, world!");
screen.print(0, 4, format!("Score: {}", score).as_str());sourcepub fn print_fbg(&mut self, x: i32, y: i32, string: &str, fg: Color, bg: Color)
pub fn print_fbg(&mut self, x: i32, y: i32, string: &str, fg: Color, bg: Color)
prints a string at the specified coordinates with the specified foreground and background color
The string will be cropped if it reach the right border
usage:
use console_engine::Color;
// print "Hello, world" in blue on white background
screen.print(0, 0, "Hello, world!", Color::Blue, Color::White);sourcepub fn print_screen(&mut self, x: i32, y: i32, source: &Screen)
pub fn print_screen(&mut self, x: i32, y: i32, source: &Screen)
Prints another screen on specified coordinates. Useful when you want to manage several “subscreen”
usage:
use console_engine::pixel;
use console_engine::screen::Screen;
// create a new Screen struct and draw a square inside it
let mut my_square = Screen::new(8,8);
my_square.rect(0,0,7,7,pixel::pxl('#'));
my_square.print(1,1,"square");
// prints the square in the main window at a specific location
screen.print_screen(5,2, &my_square);sourcepub fn print_screen_alpha(
&mut self,
x: i32,
y: i32,
source: &Screen,
alpha_character: char
)
pub fn print_screen_alpha( &mut self, x: i32, y: i32, source: &Screen, alpha_character: char )
Prints another screen on specified coordinates, ignoring a specific character while printing Ignoring a character will behave like transparency
see print_screen for usage
sourcepub fn h_line(
&mut self,
start_x: i32,
start_y: i32,
end_x: i32,
character: Pixel
)
pub fn h_line( &mut self, start_x: i32, start_y: i32, end_x: i32, character: Pixel )
Optimized horizontal line drawing Automatically called by line if needed
sourcepub fn v_line(
&mut self,
start_x: i32,
start_y: i32,
end_y: i32,
character: Pixel
)
pub fn v_line( &mut self, start_x: i32, start_y: i32, end_y: i32, character: Pixel )
Optimized vertical line drawing Automatically called by line if needed
sourcepub fn line(
&mut self,
start_x: i32,
start_y: i32,
end_x: i32,
end_y: i32,
character: Pixel
)
pub fn line( &mut self, start_x: i32, start_y: i32, end_x: i32, end_y: i32, character: Pixel )
draws a line of the provided character between two sets of coordinates
see: Bresenham’s line algorithm
Note : Your line can start or end out of bounds. These pixels won’t be drawn
usage:
use console_engine::pixel;
// ...
screen.line(0, 0, 9, 9, pixel::pxl('#'));sourcepub fn rect(
&mut self,
start_x: i32,
start_y: i32,
end_x: i32,
end_y: i32,
character: Pixel
)
pub fn rect( &mut self, start_x: i32, start_y: i32, end_x: i32, end_y: i32, character: Pixel )
Draws a rectangle of the provided character between two sets of coordinates
usage:
use console_engine::pixel;
// ...
screen.rect(0, 0, 9, 9, pixel::pxl('#'));sourcepub fn rect_border(
&mut self,
start_x: i32,
start_y: i32,
end_x: i32,
end_y: i32,
rect_style: BorderStyle
)
pub fn rect_border( &mut self, start_x: i32, start_y: i32, end_x: i32, end_y: i32, rect_style: BorderStyle )
Draws a rectangle with custom borders of the provided between two sets of coordinates. Check the BorderStyle struct to learn how to use built-in or custom styles
usage:
use console_engine::rect_style::BorderStyle;
// ...
screen.rect_border(0, 0, 9, 9, BorderStyle::new_simple());sourcepub fn fill_rect(
&mut self,
start_x: i32,
start_y: i32,
end_x: i32,
end_y: i32,
character: Pixel
)
pub fn fill_rect( &mut self, start_x: i32, start_y: i32, end_x: i32, end_y: i32, character: Pixel )
Fill a rectangle of the provided character between two sets of coordinates
usage:
use console_engine::pixel;
// ...
screen.fill_rect(0, 0, 9, 9, pixel::pxl('#'));sourcepub fn circle(&mut self, x: i32, y: i32, radius: u32, character: Pixel)
pub fn circle(&mut self, x: i32, y: i32, radius: u32, character: Pixel)
Draws a circle of the provided character at an x and y position with a radius see: olcPixelGameEngine Repository
usage:
use console_engine::pixel;
// ...
screen.circle(10, 10, 4, pixel::pxl('#'));sourcepub fn fill_circle(&mut self, x: i32, y: i32, radius: u32, character: Pixel)
pub fn fill_circle(&mut self, x: i32, y: i32, radius: u32, character: Pixel)
Fill a circle of the provided character at an x and y position with a radius see: olcPixelGameEngine Repository
usage:
use console_engine::pixel;
// ...
screen.fill_circle(10, 10, 4, pixel::pxl('#'));sourcepub fn triangle(
&mut self,
x1: i32,
y1: i32,
x2: i32,
y2: i32,
x3: i32,
y3: i32,
character: Pixel
)
pub fn triangle( &mut self, x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32, character: Pixel )
Draws a triangle of the provided character using three sets of coordinates
usage:
use console_engine::pixel;
// ...
screen.triangle(8,8, 4,6, 9,2, pixel::pxl('#'));sourcepub fn fill_triangle(
&mut self,
x1: i32,
y1: i32,
x2: i32,
y2: i32,
x3: i32,
y3: i32,
character: Pixel
)
pub fn fill_triangle( &mut self, x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32, character: Pixel )
Fill a triangle of the provided character using three sets of coordinates see: rustyPixelGameEngine Repository
usage:
use console_engine::pixel;
// ...
screen.fill_triangle(8,8, 4,6, 9,2, pixel::pxl('#'));sourcepub fn scroll(&mut self, h_scroll: i32, v_scroll: i32, background: Pixel)
pub fn scroll(&mut self, h_scroll: i32, v_scroll: i32, background: Pixel)
Scrolls the screen for a certain amount of characters vertically or horizontally Scrolling is a destructive process, the outer border will be filled with the background pixel.
Scrolling a positive value will move the screen characters to the left / top, freeing space to the right / bottom
Scrolling a negative value will move the screen characters to the right / bottom, freeing space to the left / top
usage :
use console_engine::pixel;
// fill the screen with characters
screen.fill(pixel::pxl('#'));
// free one space to the bottom
screen.scroll(0,1,pixel::pxl(' '));
// print something at this place
screen.print(0, height-1, "Hello, world!");sourcepub fn set_pxl(&mut self, x: i32, y: i32, character: Pixel)
pub fn set_pxl(&mut self, x: i32, y: i32, character: Pixel)
sets the provided character in the specified coordinates out of bounds pixels will be ignored
usage:
use console_engine::pixel;
// ...
screen.set_pxl(3,8,pixel::pixel('o'));sourcepub fn get_pxl(&self, x: i32, y: i32) -> Result<Pixel, String>
pub fn get_pxl(&self, x: i32, y: i32) -> Result<Pixel, String>
Get the character stored at provided coordinates
usage:
if screen.get_pxl(3,8).unwrap().chr == 'o' {
screen.print(0,0,"Found a 'o'");
}sourcepub fn resize(&mut self, new_width: u32, new_height: u32)
pub fn resize(&mut self, new_width: u32, new_height: u32)
Resizes the screen to match the given width and height truncates the bottom and right side of the screen
usage:
screen.resize()sourcepub fn extract(
&self,
start_x: i32,
start_y: i32,
end_x: i32,
end_y: i32,
default: Pixel
) -> Screen
pub fn extract( &self, start_x: i32, start_y: i32, end_x: i32, end_y: i32, default: Pixel ) -> Screen
Extracts part of the current screen as a separate Screen object
The original screen is not altered
If the coordinates are out of bounds, they’ll be replace by the default pixel
usage:
use console_engine::pixel;
// extract a 3x2 screen from the screen variable and print it
let scr_chunk = screen.extract(10, 4, 12, 5, pixel::pxl(' '));
scr_chunk.draw();