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
impl Graphics2D
sourcepub 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>>
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.
sourcepub fn create_image_from_file_path<S: AsRef<Path>>(
&mut self,
data_type: Option<ImageFileFormat>,
smoothing_mode: ImageSmoothingMode,
path: S
) -> Result<ImageHandle, BacktraceError<ErrorMessage>>
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.
sourcepub 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>>
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.
sourcepub fn clear_screen(&mut self, color: Color)
pub fn clear_screen(&mut self, color: Color)
Fills the screen with the specified color.
sourcepub fn draw_text<V: Into<Vec2>>(
&mut self,
position: V,
color: Color,
text: &FormattedTextBlock
)
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.
sourcepub fn draw_text_cropped<V: Into<Vec2>>(
&mut self,
position: V,
crop_window: Rect,
color: Color,
text: &FormattedTextBlock
)
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.
sourcepub fn draw_polygon<V: Into<Vec2>>(
&mut self,
polygon: &Polygon,
offset: V,
color: Color
)
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.
sourcepub fn draw_triangle_three_color(
&mut self,
vertex_positions_clockwise: [Vec2; 3],
vertex_colors_clockwise: [Color; 3]
)
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.
sourcepub 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
)
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.
sourcepub fn draw_triangle(
&mut self,
vertex_positions_clockwise: [Vec2; 3],
color: Color
)
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.
sourcepub fn draw_quad_four_color(
&mut self,
vertex_positions_clockwise: [Vec2; 4],
vertex_colors: [Color; 4]
)
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.
sourcepub fn draw_quad(&mut self, vertex_positions_clockwise: [Vec2; 4], color: Color)
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.
sourcepub 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
)
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.
sourcepub fn draw_rectangle_image_subset_tinted(
&mut self,
rect: impl AsRef<Rectangle>,
color: Color,
image_coords_normalized: impl AsRef<Rectangle>,
image: &ImageHandle
)
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.
sourcepub fn draw_rectangle_image_tinted(
&mut self,
rect: impl AsRef<Rectangle>,
color: Color,
image: &ImageHandle
)
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.
sourcepub fn draw_rectangle_image(
&mut self,
rect: impl AsRef<Rectangle>,
image: &ImageHandle
)
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.
sourcepub fn draw_image<P: Into<Vec2>>(&mut self, position: P, image: &ImageHandle)
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.
sourcepub fn draw_rectangle(&mut self, rect: impl AsRef<Rectangle>, color: Color)
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.
sourcepub fn draw_rounded_rectangle(
&mut self,
round_rect: impl AsRef<RoundedRectangle>,
color: Color
)
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.
sourcepub fn draw_line<VStart: Into<Vec2>, VEnd: Into<Vec2>>(
&mut self,
start_position: VStart,
end_position: VEnd,
thickness: f32,
color: Color
)
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)
.
sourcepub fn draw_circle<V: Into<Vec2>>(
&mut self,
center_position: V,
radius: f32,
color: Color
)
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.
sourcepub fn draw_circle_section_triangular_three_color(
&mut self,
vertex_positions_clockwise: [Vec2; 3],
vertex_colors: [Color; 3],
vertex_circle_coords_normalized: [Vec2; 3]
)
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)]);
sourcepub fn set_clip(&mut self, rect: Option<Rectangle<i32>>)
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.
sourcepub fn capture(&mut self, format: ImageDataType) -> RawBitmapData
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.