Struct speedy2d::Graphics2D[][src]

pub struct Graphics2D { /* fields omitted */ }
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

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.

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.

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.

Fills the screen with the specified color.

Draws the provided line 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.

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

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

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.

Draws a triangle with the specified color.

The vertex positions must be provided in clockwise order.

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

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

Draws a quadrilateral with the specified color.

The vertex positions must be provided in clockwise order.

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.

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.

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.

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

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

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

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).

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

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(
        [
                Vector2::new(200.0, 200.0),
                Vector2::new(300.0, 200.0),
                Vector2::new(300.0, 300.0)],
        [Color::MAGENTA; 3],
        [
                Vector2::new(-1.0, -1.0),
                Vector2::new(1.0, -1.0),
                Vector2::new(1.0, 1.0)]);

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.