pub struct Graphics { /* private fields */ }
Expand description
The struct that handles sending draw calls to the GPU
The basic flow of using Graphics
is a loop of Graphics::clear
, draw, and Graphics::present
.
When drawing, keep in mind the projection and transformation. The projection is set by
[Graphics::set_projection
], and usually Transform::orthographic
. It is a transformation
applied to every single vertex, and it’s advised to keep it constant as much as possible. The
transformation is used to rotate, scale, or translate a handful of draw calls, and is set by
Graphics::set_transform
.
For best performance, try to reduce unnecessary state changes. Sources of state changes include changing the image you’re drawing, changing the projection, or changing the type of geomety you’re drawing.
Implementations§
Source§impl Graphics
impl Graphics
Sourcepub fn into_raw_context(self) -> Context
pub fn into_raw_context(self) -> Context
Turn this high-level graphics object into a low-level graphics context
It is by design that this is a one-way operation. In order for the graphics API to be safe, Quicksilver takes full control of the context and all shaders. If you want to use custom shaders or rendering setups, you can no longer use the high-level graphics API.
The context returned is the context from the golem rendering library, which is the library
Quicksilver’s graphics stack is built on. The main advantage you gain is custom shaders, as
well as being able to manage multiple different GPU buffers. See the
golem
crate for more details.
Sourcepub fn set_view(&mut self, transform: Transform)
pub fn set_view(&mut self, transform: Transform)
Set the view matrix, which is applied to all vertices on the GPU
Most of the time, you won’t need this at all. However, if you want to apply a change to a great many objects (screen shake, rotations, etc.) setting the view matrix is a good way to do that.
Sourcepub fn set_transform(&mut self, transform: Transform)
pub fn set_transform(&mut self, transform: Transform)
Set the transformation matrix, which is applied to all vertices on the CPU
Use this to rotate, scale, or translate individual draws or small groups of draws.
Sourcepub fn screen_to_camera(&self, window: &Window, position: Vector) -> Vector
pub fn screen_to_camera(&self, window: &Window, position: Vector) -> Vector
Project a point from the screen to the world
Use this when checking the mouse position against rendered objects, like a game or UI. The
given point is scaled from the window to the size of the virtual camera (see
set_camera_size
) and the inverse of the view (see set_view
).
Sourcepub fn set_camera_size(&mut self, size: Vector)
pub fn set_camera_size(&mut self, size: Vector)
Set the size of the virtual camera
Regardless of the size of the actual window, the draw functions all work on a virtual camera size. By default, this is the initial size in your Settings. If you start at 400x300, a 400x300 Rectangle will fill the drawable area. If the Window is doubled in size, a 400x300 Rectangle will still fill the drawable area. This function changes the size of the ‘virtual camera.’
If you want to position a camera at an arbitrary point within world space, or apply
rotations or scaling, use set_view
.
Sourcepub fn set_resize_handler(&mut self, resize: ResizeHandler)
pub fn set_resize_handler(&mut self, resize: ResizeHandler)
Change how to respond to the window resizing
The default method of handling resizes is ResizeHandler::Fit
, which maximizes the area
drawn on screen while maintaining aspect ratio. There are a variety of other
ResizeHandler
options to choose from.
Sourcepub fn set_blend_mode(&mut self, blend_mode: Option<BlendMode>)
pub fn set_blend_mode(&mut self, blend_mode: Option<BlendMode>)
Set the blend mode, which determines how pixels mix when drawn over each other
Pass None
to disable blending entirely
Sourcepub fn draw_elements(
&mut self,
vertices: impl Iterator<Item = Vertex>,
elements: impl Iterator<Item = Element>,
image: Option<&Image>,
)
pub fn draw_elements( &mut self, vertices: impl Iterator<Item = Vertex>, elements: impl Iterator<Item = Element>, image: Option<&Image>, )
Draw a collection of vertices
Elements determines how to interpret the vertices. While it is convenient to mix-and-match within a single call, be aware that this can incur a performance penalty.
If any of the provided vertices reference an image, they will use the provided image.
Sourcepub fn draw_point(&mut self, pos: Vector, color: Color)
pub fn draw_point(&mut self, pos: Vector, color: Color)
Draw a single, pixel-sized point
Sourcepub fn draw_mesh(&mut self, mesh: &Mesh)
pub fn draw_mesh(&mut self, mesh: &Mesh)
Draw a mesh, which is shorthand for passing the Mesh
’s data to
Graphics::draw_elements
Sourcepub fn fill_polygon(&mut self, points: &[Vector], color: Color)
pub fn fill_polygon(&mut self, points: &[Vector], color: Color)
Draw a filled-in polygon of a given color
The provided points must form a clockwise or counter-clockwise set of points in a convex polygon
Sourcepub fn stroke_path(&mut self, points: &[Vector], color: Color)
pub fn stroke_path(&mut self, points: &[Vector], color: Color)
Draw a series of lines that connect the given points, in order
Sourcepub fn stroke_polygon(&mut self, points: &[Vector], color: Color)
pub fn stroke_polygon(&mut self, points: &[Vector], color: Color)
Draw an outline of a polygon of a given color
The provided points must form a clockwise or counter-clockwise set of points in a convex polygon
Sourcepub fn fill_rect(&mut self, rect: &Rectangle, color: Color)
pub fn fill_rect(&mut self, rect: &Rectangle, color: Color)
Draw a filled-in rectangle of a given color
Sourcepub fn stroke_rect(&mut self, rect: &Rectangle, color: Color)
pub fn stroke_rect(&mut self, rect: &Rectangle, color: Color)
Outline a rectangle with a given color
Sourcepub fn fill_circle(&mut self, circle: &Circle, color: Color)
pub fn fill_circle(&mut self, circle: &Circle, color: Color)
Draw a filled-in circle of a given color
Sourcepub fn stroke_circle(&mut self, circle: &Circle, color: Color)
pub fn stroke_circle(&mut self, circle: &Circle, color: Color)
Outline a circle with a given color
Sourcepub fn draw_image(&mut self, image: &Image, location: Rectangle)
pub fn draw_image(&mut self, image: &Image, location: Rectangle)
Drawn an image to the given area, stretching if necessary
Sourcepub fn draw_image_tinted(
&mut self,
image: &Image,
location: Rectangle,
tint: Color,
)
pub fn draw_image_tinted( &mut self, image: &Image, location: Rectangle, tint: Color, )
Drawn a tinted image to the given area, stretching if necessary
The tint is applied by multiplying the color components at each pixel. If the Color has (r, g, b, a) of (1.0, 0.5, 0.0, 1.0), all the pixels will have their normal red value, half their green value, and no blue value.
Sourcepub fn draw_subimage(
&mut self,
image: &Image,
region: Rectangle,
location: Rectangle,
)
pub fn draw_subimage( &mut self, image: &Image, region: Rectangle, location: Rectangle, )
Draw a given part of an image to the screen, see Graphics::draw_image
Sourcepub fn draw_subimage_tinted(
&mut self,
image: &Image,
region: Rectangle,
location: Rectangle,
tint: Color,
)
pub fn draw_subimage_tinted( &mut self, image: &Image, region: Rectangle, location: Rectangle, tint: Color, )
Draw a given part of a tinted image to the screen, see Graphics::draw_image_tinted
Sourcepub fn flush_surface(
&mut self,
surface: &Surface,
) -> Result<(), QuicksilverError>
pub fn flush_surface( &mut self, surface: &Surface, ) -> Result<(), QuicksilverError>
Draw to a Surface
Sourcepub fn flush_window(&mut self, window: &Window) -> Result<(), QuicksilverError>
pub fn flush_window(&mut self, window: &Window) -> Result<(), QuicksilverError>
Draw to the Window, without writing those changes to the screen