Struct speedy2d::Graphics2D

source ·
pub struct Graphics2D { /* private fields */ }
Expand description

A Graphics2D object allows you to draw shapes, images, and text to the screen.

An instance is provided in the window::WindowHandler::on_draw callback.

If you are managing the GL context yourself, you must invoke GLRenderer::draw_frame to obtain an instance.

Implementations§

source§

impl Graphics2D

source

pub fn create_image_from_raw_pixels<S: Into<UVec2>>( &mut self, data_type: ImageDataType, smoothing_mode: ImageSmoothingMode, size: S, data: &[u8] ) -> Result<ImageHandle, BacktraceError<ErrorMessage>>

Creates a new ImageHandle from the specified raw pixel data.

The data provided in the data parameter must be in the format specified by data_type.

The returned ImageHandle is valid only for the current graphics context.

source

pub fn create_image_from_file_path<S: AsRef<Path>>( &mut self, data_type: Option<ImageFileFormat>, smoothing_mode: ImageSmoothingMode, path: S ) -> Result<ImageHandle, BacktraceError<ErrorMessage>>

Loads an image from the specified file path.

If no data_type is provided, an attempt will be made to guess the file format.

For a list of supported image types, see image::ImageFileFormat.

The returned ImageHandle is valid only for the current graphics context.

source

pub fn create_image_from_file_bytes<R: Seek + BufRead>( &mut self, data_type: Option<ImageFileFormat>, smoothing_mode: ImageSmoothingMode, file_bytes: R ) -> Result<ImageHandle, BacktraceError<ErrorMessage>>

Loads an image from the provided encoded image file data.

If no data_type is provided, an attempt will be made to guess the file format.

The data source must implement std::io::BufRead and std::io::Seek. For example, if you have a &[u8], you may wrap it in a std::io::Cursor as follows:

use std::io::Cursor;

let image_bytes : &[u8] = include_bytes!("../assets/screenshots/hello_world.png");

let image_result = graphics.create_image_from_file_bytes(
    None,
    ImageSmoothingMode::Linear,
    Cursor::new(image_bytes));

For a list of supported image types, see image::ImageFileFormat.

The returned ImageHandle is valid only for the current graphics context.

source

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

Fills the screen with the specified color.

source

pub fn draw_text<V: Into<Vec2>>( &mut self, position: V, color: Color, text: &FormattedTextBlock )

Draws the provided block of text at the specified position.

Lines of text can be prepared by loading a font (using crate::font::Font::new), and calling layout_text_line() on that font with your desired text.

To fall back to another font if a glyph isn’t found, see crate::font::FontFamily.

To achieve good performance, it’s possible to layout a line of text once, and then re-use the same crate::font::FormattedTextLine object whenever you need to draw that text to the screen.

Note: Text will be rendered with subpixel precision. If the subpixel position changes between frames, performance may be degraded, as the text will need to be re-rendered and re-uploaded. To avoid this, call round() on the position coordinates, to ensure that the text is always located at an integer pixel position.

source

pub fn draw_text_cropped<V: Into<Vec2>>( &mut self, position: V, crop_window: Rect, color: Color, text: &FormattedTextBlock )

Draws the provided block of text at the specified position, cropped to the specified window. Characters outside this window will not be rendered. Characters partially inside the window will be cropped.

Both position and crop_window are relative to the overall render window.

See the documentation for Graphics2D::draw_text for more details.

source

pub fn draw_polygon<V: Into<Vec2>>( &mut self, polygon: &Polygon, offset: V, color: Color )

Draws a polygon with a single color, with the specified offset in pixels.

source

pub fn draw_triangle_three_color( &mut self, vertex_positions_clockwise: [Vec2; 3], vertex_colors_clockwise: [Color; 3] )

Draws a triangle with the specified colors (one color for each corner).

The vertex positions (and associated colors) must be provided in clockwise order.

source

pub fn draw_triangle_image_tinted_three_color( &mut self, vertex_positions_clockwise: [Vec2; 3], vertex_colors: [Color; 3], image_coords_normalized: [Vec2; 3], image: &ImageHandle )

Draws part of an image, tinted with the provided colors, at the specified location. The sub-image will be scaled to fill the triangle described by the vertices in vertex_positions_clockwise.

The coordinates in image_coords_normalized should be in the range 0.0 to 1.0, and define the portion of the source image which should be drawn.

The tinting is performed by for each pixel by multiplying each color component in the image pixel by the corresponding color component in the color parameter.

The vertex positions (and associated colors and image coordinates) must be provided in clockwise order.

source

pub fn draw_triangle( &mut self, vertex_positions_clockwise: [Vec2; 3], color: Color )

Draws a triangle with the specified color.

The vertex positions must be provided in clockwise order.

source

pub fn draw_quad_four_color( &mut self, vertex_positions_clockwise: [Vec2; 4], vertex_colors: [Color; 4] )

Draws a quadrilateral with the specified colors (one color for each corner).

The vertex positions (and associated colors) must be provided in clockwise order.

source

pub fn draw_quad(&mut self, vertex_positions_clockwise: [Vec2; 4], color: Color)

Draws a quadrilateral with the specified color.

The vertex positions must be provided in clockwise order.

source

pub fn draw_quad_image_tinted_four_color( &mut self, vertex_positions_clockwise: [Vec2; 4], vertex_colors: [Color; 4], image_coords_normalized: [Vec2; 4], image: &ImageHandle )

Draws part of an image, tinted with the provided colors, at the specified location. The sub-image will be scaled to fill the quadrilateral described by the vertices in vertex_positions_clockwise.

The coordinates in image_coords_normalized should be in the range 0.0 to 1.0, and define the portion of the source image which should be drawn.

The tinting is performed by for each pixel by multiplying each color component in the image pixel by the corresponding color component in the color parameter.

The vertex positions (and associated colors and image coordinates) must be provided in clockwise order.

source

pub fn draw_rectangle_image_subset_tinted( &mut self, rect: impl AsRef<Rectangle>, color: Color, image_coords_normalized: impl AsRef<Rectangle>, image: &ImageHandle )

Draws part of an image, tinted with the provided color, at the specified location. The sub-image will be scaled to fill the pixel coordinates in the provided rectangle.

The coordinates in image_coords_normalized should be in the range 0.0 to 1.0, and define the portion of the source image which should be drawn.

The tinting is performed by for each pixel by multiplying each color component in the image pixel by the corresponding color component in the color parameter.

source

pub fn draw_rectangle_image_tinted( &mut self, rect: impl AsRef<Rectangle>, color: Color, image: &ImageHandle )

Draws an image, tinted with the provided color, at the specified location. The image will be scaled to fill the pixel coordinates in the provided rectangle.

The tinting is performed by for each pixel by multiplying each color component in the image pixel by the corresponding color component in the color parameter.

source

pub fn draw_rectangle_image( &mut self, rect: impl AsRef<Rectangle>, image: &ImageHandle )

Draws an image at the specified location. The image will be scaled to fill the pixel coordinates in the provided rectangle.

source

pub fn draw_image<P: Into<Vec2>>(&mut self, position: P, image: &ImageHandle)

Draws an image at the specified pixel location. The image will be drawn at its original size with no scaling.

source

pub fn draw_rectangle(&mut self, rect: impl AsRef<Rectangle>, color: Color)

Draws a single-color rectangle at the specified location. The coordinates of the rectangle are specified in pixels.

source

pub fn draw_rounded_rectangle( &mut self, round_rect: impl AsRef<RoundedRectangle>, color: Color )

Draws a single-color rounded rectangle at the specified location. The coordinates of the rounded rectangle are specified in pixels.

source

pub fn draw_line<VStart: Into<Vec2>, VEnd: Into<Vec2>>( &mut self, start_position: VStart, end_position: VEnd, thickness: f32, color: Color )

Draws a single-color line between the given points, specified in pixels.

§Pixel alignment

On a display with square pixels, an integer-valued coordinate is located at the boundary between two pixels, rather than the center of the pixel. For example:

  • (0.0, 0.0) = Top left of pixel
  • (0.5, 0.5) = Center of pixel
  • (1.0, 1.0) = Bottom right of pixel

If drawing a line of odd-numbered thickness, it is advisable to locate the start and end of the line at the centers of pixels, rather than the edges.

For example, a one-pixel-thick line between (0.0, 10.0) and (100.0, 10.0) will be drawn as a rectangle with corners (0.0, 9.5) and (100.0, 10.5), meaning that the line’s thickness will actually span two half-pixels. Drawing the same line between (0.0, 10.5) and (100.0, 10.5) will result in a pixel-aligned rectangle between (0.0, 10.0) and (100.0, 11.0).

source

pub fn draw_circle<V: Into<Vec2>>( &mut self, center_position: V, radius: f32, color: Color )

Draws a circle, filled with a single color, at the specified pixel location.

source

pub fn draw_circle_section_triangular_three_color( &mut self, vertex_positions_clockwise: [Vec2; 3], vertex_colors: [Color; 3], vertex_circle_coords_normalized: [Vec2; 3] )

Draws a triangular subset of a circle.

Put simply, this function will draw a triangle on the screen, textured with a region of a circle.

The circle region is specified using vertex_circle_coords_normalized, which denotes UV coordinates relative to an infinitely-detailed circle of radius 1.0, and center (0.0, 0.0).

For example, to draw the top-right half of a circle with radius 100px:

graphics.draw_circle_section_triangular_three_color(
        [
                Vec2::new(200.0, 200.0),
                Vec2::new(300.0, 200.0),
                Vec2::new(300.0, 300.0)],
        [Color::MAGENTA; 3],
        [
                Vec2::new(-1.0, -1.0),
                Vec2::new(1.0, -1.0),
                Vec2::new(1.0, 1.0)]);
source

pub fn set_clip(&mut self, rect: Option<Rectangle<i32>>)

Sets the current clip to the rectangle specified by the given coordinates. Rendering operations have no effect outside of the clipping area.

source

pub fn capture(&mut self, format: ImageDataType) -> RawBitmapData

Captures a screenshot of the render window. The returned data contains the color of each pixel. Pixels are represented using a u8 for each component (red, green, blue, and alpha). Use the format parameter to specify the byte layout (and size) of each pixel.

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

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.