Struct Graphics

Source
pub struct Graphics<'buffer> {
    pub custom_font: HashMap<u8, CustomLetter, BuildHasherDefault<FnvHasher>>,
    /* private fields */
}

Fields§

§custom_font: HashMap<u8, CustomLetter, BuildHasherDefault<FnvHasher>>

Allows you to replace any supported ASCII with a custom glyph To replace ‘a’ with ‘█’ for 4x5 fonts (such as Standard4x5) write

graphics.custom_font.insert(chr_to_code('a'), CustomLetter { _4x5: [true; 20], ..CustomLetter::default });

Characters are replaced on a size basis, so if _4x4 is provided then all _4x4 fonts will draw this custom character

Whitespace isn’t supported and is skipped when drawing

Note: A-Za-z0-9!@$%^&*(),./;'\\[]<>?:\"{}_+~#…¤£¥¢✓|€ are valid for text::chr_to_code

Implementations§

Source§

impl Graphics<'_>

Source

pub fn index(&self, x: usize, y: usize) -> usize

Convert an x,y coord to idx for use with self.pixels

Source

pub fn width(&self) -> usize

Source

pub fn height(&self) -> usize

Source

pub fn is_on_screen(&self, point: Coord) -> bool

Source§

impl Graphics<'_>

Source

pub fn get_translate(&self) -> Coord

Get the canvas offset in pixels

Source

pub fn set_translate(&mut self, new_value: Coord) -> Coord

Set the canvas offset in pixels

All drawing commands will be offset by this value

§Returns

The previous translation value

Source

pub fn with_translate<F>(&mut self, set: Coord, method: F)
where F: Fn(&mut Graphics<'_>),

Source

pub fn update_translate(&mut self, delta: Coord)

Adds delta to the current canvas offset

Source

pub fn copy_to_image(&self) -> Image

Copy entire pixels array to an image

Source

pub fn copy_to_indexed_image( &self, simplify_palette: bool, ) -> Result<IndexedImage, GraphicsError>

Copy entire pixels array to an indexed image simplify_palette if true and there’s more than 255 colours, this will simplify/merge the palette until there are under 255 colours

§Errors
  • GraphicsError::TooManyColors Over 255 colors have been used and simplify_palette was false
  • GraphicsError::TooBig Image is bigger than 255x255
  • GraphicsError::ImageError Something went wrong creating the IndexedImage
Source

pub fn get_px_for_char( col: usize, row: usize, font: &PixelFont, ) -> (usize, usize)

Get top left pixel coord for letter col row

Source

pub fn draw_image<P>(&mut self, xy: P, image: &Image)
where P: Into<Coord>,

Draw an image at x, y If the image definitely will draw inside the window you can use [draw_image_unchecked] instead

Source

pub fn draw_indexed_image<P>(&mut self, xy: P, image: &IndexedImage)
where P: Into<Coord>,

Draw an indexed image at x, y

Source

pub fn draw_wrapped_image<P>(&mut self, xy: P, image: &IndexedWrapper)
where P: Into<Coord>,

Source

pub fn draw_animated_image<P>(&mut self, xy: P, image: &AnimatedIndexedImage)
where P: Into<Coord>,

Draw an animated image at x, y

Source

pub fn draw_arc( &mut self, center: Coord, angle_start: isize, angle_end: isize, radius: usize, close: bool, color: Color, )

Source

pub fn draw_line<P1, P2>(&mut self, start: P1, end: P2, color: Color)
where P1: Into<Coord>, P2: Into<Coord>,

Source

pub fn draw_offset<T, P>(&mut self, xy: P, renderable: &dyn Renderable<T>)
where P: Into<Coord>,

Draw renderable offset by [xy]

Source

pub fn draw<T>(&mut self, renderable: &dyn Renderable<T>)

Draw renderable

Source

pub fn get_pixel( &self, x: isize, y: isize, use_translate: bool, ) -> Option<Color>

Get the RGB values for a pixel Alpha will always be 255

If use_translate is true then the x,y will be updated with self.translate

§Returns

Some(Color) if x,y is within bounds

Source

pub fn clear(&mut self, color: Color)

Set every pixel to color, this ignores translate and clip

Source

pub fn clear_aware(&mut self, color: Color)

Set/blend every pixel with color, same as [clear] but this follows translate and clip

Source

pub fn draw_letter( &mut self, pos: (isize, isize), chr: char, font: PixelFont, color: Color, )

Draw chr at pos

§Usage
    graphics.draw_letter((20,20), 'A', PixelFont::Limited3x5, BLUE);
Source

pub fn draw_ascii_letter( &mut self, pos: (isize, isize), code: u8, font: PixelFont, color: Color, )

Shouldn’t be called in normal usage

Source

pub fn draw_ascii<P, F>(&mut self, text: &[Vec<u8>], pos: P, format: F)
where P: Into<TextPos>, F: Into<TextFormat>,

Should only be used by Text::render text param must already be corrected wrapped

Source

pub fn draw_text<P, F>(&mut self, text: &str, pos: P, format: F)
where P: Into<TextPos>, F: Into<TextFormat>,

Draw text on screen at pos using format

§Params
  • format See TextFormat, can be
    • TextFormat
    • Color
    • (Color, PixelFont)
    • (Color, PixelFont, Positioning)
    • (Color, PixelFont, WrappingStrategy, Positioning)
    • (Color, PixelFont, WrappingStrategy)
    • (Color, PixelFont, WrappingStrategy, f32) (f32 = line height)
    • (Color, PixelFont, WrappingStrategy, f32, f32) (1st f32 = line height, 2nd f32 = char width)
    • (Color, PixelFont, WrappingStrategy, f32, f32, Positioning) (1st f32 = line height, 2nd f32 = char width)
§Usage
 //simple example
 graphics.draw_text("Test", TextPos::ColRow(1,1), RED);

//full example
fn draw_message(graphics: &mut Graphics, msg: String) {
    let width_in_columns = PixelFont::Standard6x7.get_max_characters(graphics.width(), graphics.height()).0;
    graphics.draw_text(&msg, TextPos::px(coord!(8,8)), (BLACK, PixelFont::Standard6x7, WrappingStrategy::AtCol(width_in_columns - 1)));
}
Source

pub fn draw_rect<R>(&mut self, rect: R, draw_type: DrawType)
where R: Into<Rect>,

Source

pub fn draw_circle<C>(&mut self, circle: C, draw_type: DrawType)
where C: Into<Circle>,

Source

pub fn draw_polygon<P>(&mut self, polygon: P, draw_type: DrawType)
where P: Into<Polygon>,

Source

pub fn draw_triangle<T>(&mut self, triangle: T, draw_type: DrawType)
where T: Into<Triangle>,

Source

pub fn draw_ellipse<E>(&mut self, ellipse: E, draw_type: DrawType)
where E: Into<Ellipse>,

Source

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

Update a pixel color, replacing or blending depending on whether colors alpha is 255 or not

If the alpha is 0 the call does nothing

Source§

impl Graphics<'_>

Source

pub fn create_buffer_u32(width: usize, height: usize) -> Vec<u32>

Create a buffer of the correct size

Source

pub fn create_buffer_u8(width: usize, height: usize) -> Vec<u8>

Create a buffer of the correct size

Source§

impl<'buffer> Graphics<'_>

Source

pub fn new_u8_rgba( buffer: &'buffer mut [u8], width: usize, height: usize, ) -> Result<Graphics<'buffer>, GraphicsError>

buffer needs to be width * height * 4 long

You can use Graphics::create_buffer_u8 to guarantee the correct size

Source

pub fn new_u32_rgba( buffer: &'buffer mut [u32], width: usize, height: usize, ) -> Result<Graphics<'buffer>, GraphicsError>

buffer needs to be width * height long

You can use Graphics::create_buffer_u32 to guarantee the correct size

Source

pub fn new_u32_argb( buffer: &'buffer mut [u32], width: usize, height: usize, ) -> Result<Graphics<'buffer>, GraphicsError>

buffer needs to be width * height long

You can use Graphics::create_buffer_u32 to guarantee the correct size

Source§

impl Graphics<'_>

Source

pub fn set_clip(&mut self, clip: Clip)

Replace the clip with clip

Source

pub fn clip(&self) -> &Clip

Get ref to [Graphic]’s Clip

Source

pub fn clip_mut(&mut self) -> &mut Clip

Get mut ref to [Graphic]’s Clip

Changes are effective immediately

Auto Trait Implementations§

§

impl<'buffer> Freeze for Graphics<'buffer>

§

impl<'buffer> RefUnwindSafe for Graphics<'buffer>

§

impl<'buffer> Send for Graphics<'buffer>

§

impl<'buffer> Sync for Graphics<'buffer>

§

impl<'buffer> Unpin for Graphics<'buffer>

§

impl<'buffer> !UnwindSafe for Graphics<'buffer>

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> AnyToAny for T
where T: 'static,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSync for T
where T: Sync,