Struct sdl2::render::Canvas [] [src]

pub struct Canvas<T: RenderTarget> { /* fields omitted */ }

Manages and owns a target (Surface, Window, or Texture) and allows drawing in it.

If the Window manipulates the shell of the Window, Canvas<Window> allows you to manipulate both the shell and the inside of the window; you can manipulate pixel by pixel (not recommended), lines, colored rectangles, or paste Textures to this Canvas.

Drawing to the Canvas does not take effect immediately, it draws to a buffer until you call present(), where all the operations you did until the last present() are updated to your target

Its context may be shared with the TextureCreator.

The context will not be dropped until all references of it are out of scope.

Examples

let window = video_subsystem.window("Example", 800, 600).build().unwrap();

// Let's create a Canvas which we will use to draw in our Window
let mut canvas : Canvas<Window> = window.into_canvas()
    .present_vsync() //< this means the screen cannot
    // render faster than your display rate (usually 60Hz or 144Hz)
    .build().unwrap();

canvas.set_draw_color(Color::RGB(0, 0, 0));
// fills the canvas with the color we set in `set_draw_color`.
canvas.clear();

// change the color of our drawing with a gold-color ...
canvas.set_draw_color(Color::RGB(255, 210, 0));
// A draw a rectangle which almost fills our window with it !
canvas.fill_rect(Rect::new(10, 10, 780, 580));

// However the canvas has not been updated to the window yet,
// everything has been processed to an internal buffer,
// but if we want our buffer to be displayed on the window,
// we need to call `present`. We need to call this everytime
// we want to render a new frame on the window.
canvas.present();
// present does not "clear" the buffer, that means that
// you have to clear it yourself before rendering again,
// otherwise leftovers of what you've renderer before might
// show up on the window !
//
// A good rule of thumb is to `clear()`, draw every texture
// needed, and then `present()`; repeat this every new frame.

Methods

impl<'s> Canvas<Surface<'s>>
[src]

Methods for the SurfaceCanvas.

Creates a 2D software rendering context for a surface.

This method should only fail if SDL2 is not built with rendering support, or there's an out-of-memory error.

Gets a reference to the associated surface of the Canvas

Gets a mutable reference to the associated surface of the Canvas

Gets the associated surface of the Canvas and destroys the Canvas

Sets the render target to the provided texture Returns a handle for rendering methods to the target Returns TargetRenderError::NotSupported if the renderer does not support the use of render targets, Returns TargetRenderError::SdlError if SDL2 returned with an error code. The texture must be created with the texture access: sdl2::render::TextureAccess::Target.

Returns a TextureCreator that can create Textures to be drawn on this Canvas

This TextureCreator will share a reference to the renderer and target context.

The target (i.e., Window) will not be destroyed and the SDL_Renderer will not be destroyed if the TextureCreator is still in scope.

impl Canvas<Window>
[src]

Methods for the WindowCanvas.

Gets a reference to the associated window of the Canvas

Gets a mutable reference to the associated window of the Canvas

Gets the associated window of the Canvas and destroys the Canvas

Sets the render target to the provided texture Returns a handle for rendering methods to the target Returns TargetRenderError::NotSupported if the renderer does not support the use of render targets, Returns TargetRenderError::SdlError if SDL2 returned with an error code. The texture must be created with the texture access: sdl2::render::TextureAccess::Target.

Returns a TextureCreator that can create Textures to be drawn on this Canvas

This TextureCreator will share a reference to the renderer and target context.

The target (i.e., Window) will not be destroyed and the SDL_Renderer will not be destroyed if the TextureCreator is still in scope.

impl<T: RenderTarget> Canvas<T>
[src]

Determine whether a window supports the use of render targets.

impl<T: RenderTarget> Canvas<T>
[src]

Drawing methods

Sets the color used for drawing operations (Rect, Line and Clear).

Gets the color used for drawing operations (Rect, Line and Clear).

Sets the blend mode used for drawing operations (Fill and Line).

Gets the blend mode used for drawing operations.

Clears the current rendering target with the drawing color.

Updates the screen with any rendering performed since the previous call.

SDL's rendering functions operate on a backbuffer; that is, calling a rendering function such as draw_line() does not directly put a line on the screen, but rather updates the backbuffer. As such, you compose your entire scene and present the composed backbuffer to the screen as a complete picture.

Gets the output size of a rendering context.

Sets a device independent resolution for rendering.

Gets device independent resolution for rendering.

Sets the drawing area for rendering on the current target.

Gets the drawing area for the current target.

Sets the clip rectangle for rendering on the specified target.

If the rectangle is None, clipping will be disabled.

Gets the clip rectangle for the current target.

Returns None if clipping is disabled.

Sets the drawing scale for rendering on the current target.

Gets the drawing scale for the current target.

Draws a point on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)

Draws multiple points on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)

Draws a line on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)

Draws a series of connected lines on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)

Draws a rectangle on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)

Draws some number of rectangles on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)

Fills a rectangle on the current rendering target with the drawing color. Passing None will fill the entire rendering target. Errors if drawing fails for any reason (e.g. driver failure)

Fills some number of rectangles on the current rendering target with the drawing color. Errors if drawing fails for any reason (e.g. driver failure)

Copies a portion of the texture to the current rendering target.

  • If src is None, the entire texture is copied.
  • If dst is None, the texture will be stretched to fill the given rectangle.

Errors if drawing fails for any reason (e.g. driver failure), or if the provided texture does not belong to the renderer.

Copies a portion of the texture to the current rendering target, optionally rotating it by angle around the given center and also flipping it top-bottom and/or left-right.

  • If src is None, the entire texture is copied.
  • If dst is None, the texture will be stretched to fill the given rectangle.
  • If center is None, rotation will be done around the center point of dst, or src if dst is None.

Errors if drawing fails for any reason (e.g. driver failure), if the provided texture does not belong to the renderer, or if the driver does not support RenderCopyEx.

Reads pixels from the current rendering target.

Remarks

WARNING: This is a very slow operation, and should not be used frequently.

impl<'r, 't, TC> Canvas<TextureTarget<'r, 't, TC>>
[src]

Replace the target of the TextureCanvas with a different Texture

Returns the new TextureCanvas and releases the &mut borrow on the old Texture

Methods from Deref<Target = RendererContext<T::Context>>

Gets information about the rendering context.

Gets the raw pointer to the SDL_Renderer

Trait Implementations

impl<T: RenderTarget> Deref for Canvas<T>
[src]

The resulting type after dereferencing

The method called to dereference a value