pub struct Canvas<T: RenderTarget> { /* private fields */ }
Expand description
Manages and owns a target (Surface
or Window
) 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
Texture
s 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 every time
// 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.
Implementations§
source§impl<'s> Canvas<Surface<'s>>
impl<'s> Canvas<Surface<'s>>
Methods for the SurfaceCanvas
.
sourcepub fn from_surface(surface: Surface<'s>) -> Result<Self, String>
pub fn from_surface(surface: Surface<'s>) -> Result<Self, String>
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.
sourcepub fn surface(&self) -> &SurfaceRef
pub fn surface(&self) -> &SurfaceRef
Gets a reference to the associated surface of the Canvas
sourcepub fn surface_mut(&mut self) -> &mut SurfaceRef
pub fn surface_mut(&mut self) -> &mut SurfaceRef
Gets a mutable reference to the associated surface of the Canvas
sourcepub fn into_surface(self) -> Surface<'s>
pub fn into_surface(self) -> Surface<'s>
Gets the associated surface of the Canvas and destroys the Canvas
sourcepub fn texture_creator(&self) -> TextureCreator<SurfaceContext<'s>>
pub fn texture_creator(&self) -> TextureCreator<SurfaceContext<'s>>
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.
source§impl Canvas<Window>
impl Canvas<Window>
Methods for the WindowCanvas
.
sourcepub fn window_mut(&mut self) -> &mut Window
pub fn window_mut(&mut self) -> &mut Window
Gets a mutable reference to the associated window of the Canvas
sourcepub fn into_window(self) -> Window
pub fn into_window(self) -> Window
Gets the associated window of the Canvas and destroys the Canvas
pub fn default_pixel_format(&self) -> PixelFormatEnum
sourcepub fn texture_creator(&self) -> TextureCreator<WindowContext>
pub fn texture_creator(&self) -> TextureCreator<WindowContext>
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.
source§impl<T: RenderTarget> Canvas<T>
impl<T: RenderTarget> Canvas<T>
sourcepub fn render_target_supported(&self) -> bool
pub fn render_target_supported(&self) -> bool
Determine whether a window supports the use of render targets.
sourcepub fn with_texture_canvas<F>(
&mut self,
texture: &mut Texture<'_>,
f: F,
) -> Result<(), TargetRenderError>
pub fn with_texture_canvas<F>( &mut self, texture: &mut Texture<'_>, f: F, ) -> Result<(), TargetRenderError>
Temporarily sets the target of Canvas
to a Texture
. This effectively allows rendering
to a Texture
in any way you want: you can make a Texture
a combination of other
Texture
s, be a complex geometry form with the gfx
module, … You can draw pixel by
pixel in it if you want, so you can do basically anything with that Texture
.
If you want to set the content of multiple Texture
at once the most efficient way
possible, don’t make a loop and call this function every time and use
with_multiple_texture_canvas
instead. Using with_texture_canvas
is actually
inefficient because the target is reset to the source (the Window
or the Surface
)
at the end of this function, but using it in a loop would make this reset useless.
Plus, the check that render_target is actually supported on that Canvas
is also
done every time, leading to useless checks.
§Notes
Note that the Canvas
in the closure is exactly the same as the one you call this
function with, meaning that you can call every function of your original Canvas
.
That means you can also call with_texture_canvas
and with_multiple_texture_canvas
from
the inside of the closure. Even though this is useless and inefficient, this is totally
safe to do and allowed.
Since the render target is now a Texture, some calls of Canvas might return another result
than if the target was to be the original source. For instance output_size
will return
this size of the current Texture
in the closure, but the size of the Window
or
Surface
outside of the closure.
You do not need to call present
after drawing in the Canvas in the closure, the changes
are applied directly to the Texture
instead of a hidden buffer.
§Errors
- 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
.
Using a texture which was not created with the texture access Target
is undefined
behavior.
§Examples
The example below changes a newly created Texture
to be a 150-by-150 black texture with a
50-by-50 red square in the middle.
let texture_creator = canvas.texture_creator();
let mut texture = texture_creator
.create_texture_target(texture_creator.default_pixel_format(), 150, 150)
.unwrap();
let result = canvas.with_texture_canvas(&mut texture, |texture_canvas| {
texture_canvas.set_draw_color(Color::RGBA(0, 0, 0, 255));
texture_canvas.clear();
texture_canvas.set_draw_color(Color::RGBA(255, 0, 0, 255));
texture_canvas.fill_rect(Rect::new(50, 50, 50, 50)).unwrap();
});
sourcepub fn with_multiple_texture_canvas<'t: 'a, 'a: 's, 's, I, F, U: 's>(
&mut self,
textures: I,
f: F,
) -> Result<(), TargetRenderError>
pub fn with_multiple_texture_canvas<'t: 'a, 'a: 's, 's, I, F, U: 's>( &mut self, textures: I, f: F, ) -> Result<(), TargetRenderError>
Same as with_texture_canvas
, but allows to change multiple Texture
s at once with the
least amount of overhead. It means that between every iteration the Target is not reset to
the source, and that the fact that the Canvas supports render target isn’t checked every
iteration either; the check is actually only done once, at the beginning, avoiding useless
checks.
The closure is run once for every Texture
sent as parameter.
The main changes from with_texture_canvas
is that is takes an Iterator
of (&mut Texture, U)
, where U is a type defined by the user. The closure takes a &mut Canvas
, and
&U
as arguments instead of a simple &mut Canvas
. This user-defined type allows you to
keep track of what to do with the Canvas you have received in the closure.
You will usually want to keep track of the number, a property, or anything that will allow
you to uniquely track this Texture
, but it can also be an empty struct or ()
as well!
§Examples
Let’s create two textures, one which will be yellow, and the other will be white
let texture_creator = canvas.texture_creator();
enum TextureColor {
Yellow,
White,
};
let mut square_texture1 : Texture =
texture_creator.create_texture_target(None, 100, 100).unwrap();
let mut square_texture2 : Texture =
texture_creator.create_texture_target(None, 100, 100).unwrap();
let textures : Vec<(&mut Texture, TextureColor)> = vec![
(&mut square_texture1, TextureColor::Yellow),
(&mut square_texture2, TextureColor::White)
];
let result : Result<(), _> =
canvas.with_multiple_texture_canvas(textures.iter(), |texture_canvas, user_context| {
match *user_context {
TextureColor::White => {
texture_canvas.set_draw_color(Color::RGB(255, 255, 255));
},
TextureColor::Yellow => {
texture_canvas.set_draw_color(Color::RGB(255, 255, 0));
}
};
texture_canvas.clear();
});
// square_texture1 is now Yellow and square_texture2 is now White!
source§impl<T: RenderTarget> Canvas<T>
impl<T: RenderTarget> Canvas<T>
Drawing methods
pub fn raw(&self) -> *mut SDL_Renderer
sourcepub fn set_draw_color<C: Into<Color>>(&mut self, color: C)
pub fn set_draw_color<C: Into<Color>>(&mut self, color: C)
Sets the color used for drawing operations (Rect, Line and Clear).
sourcepub fn draw_color(&self) -> Color
pub fn draw_color(&self) -> Color
Gets the color used for drawing operations (Rect, Line and Clear).
sourcepub fn set_blend_mode(&mut self, blend: BlendMode)
pub fn set_blend_mode(&mut self, blend: BlendMode)
Sets the blend mode used for drawing operations (Fill and Line).
sourcepub fn blend_mode(&self) -> BlendMode
pub fn blend_mode(&self) -> BlendMode
Gets the blend mode used for drawing operations.
sourcepub fn present(&mut self)
pub fn present(&mut self)
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.
sourcepub fn output_size(&self) -> Result<(u32, u32), String>
pub fn output_size(&self) -> Result<(u32, u32), String>
Gets the output size of a rendering context.
sourcepub fn set_logical_size(
&mut self,
width: u32,
height: u32,
) -> Result<(), IntegerOrSdlError>
pub fn set_logical_size( &mut self, width: u32, height: u32, ) -> Result<(), IntegerOrSdlError>
Sets a device independent resolution for rendering.
sourcepub fn logical_size(&self) -> (u32, u32)
pub fn logical_size(&self) -> (u32, u32)
Gets device independent resolution for rendering.
sourcepub fn set_viewport<R: Into<Option<Rect>>>(&mut self, rect: R)
pub fn set_viewport<R: Into<Option<Rect>>>(&mut self, rect: R)
Sets the drawing area for rendering on the current target.
sourcepub fn set_clip_rect<R: Into<Option<Rect>>>(&mut self, rect: R)
pub fn set_clip_rect<R: Into<Option<Rect>>>(&mut self, rect: R)
Sets the clip rectangle for rendering on the specified target.
If the rectangle is None
, clipping will be disabled.
sourcepub fn clip_rect(&self) -> Option<Rect>
pub fn clip_rect(&self) -> Option<Rect>
Gets the clip rectangle for the current target.
Returns None
if clipping is disabled.
sourcepub fn set_scale(&mut self, scale_x: f32, scale_y: f32) -> Result<(), String>
pub fn set_scale(&mut self, scale_x: f32, scale_y: f32) -> Result<(), String>
Sets the drawing scale for rendering on the current target.
sourcepub fn draw_point<P: Into<Point>>(&mut self, point: P) -> Result<(), String>
pub fn draw_point<P: Into<Point>>(&mut self, point: P) -> Result<(), String>
Draws a point on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)
sourcepub fn draw_points<'a, P: Into<&'a [Point]>>(
&mut self,
points: P,
) -> Result<(), String>
pub fn draw_points<'a, P: Into<&'a [Point]>>( &mut self, points: P, ) -> Result<(), String>
Draws multiple points on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)
sourcepub fn draw_line<P1: Into<Point>, P2: Into<Point>>(
&mut self,
start: P1,
end: P2,
) -> Result<(), String>
pub fn draw_line<P1: Into<Point>, P2: Into<Point>>( &mut self, start: P1, end: P2, ) -> Result<(), String>
Draws a line on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)
sourcepub fn draw_lines<'a, P: Into<&'a [Point]>>(
&mut self,
points: P,
) -> Result<(), String>
pub fn draw_lines<'a, P: Into<&'a [Point]>>( &mut self, points: P, ) -> Result<(), String>
Draws a series of connected lines on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)
sourcepub fn draw_rect(&mut self, rect: Rect) -> Result<(), String>
pub fn draw_rect(&mut self, rect: Rect) -> Result<(), String>
Draws a rectangle on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)
sourcepub fn draw_rects(&mut self, rects: &[Rect]) -> Result<(), String>
pub fn draw_rects(&mut self, rects: &[Rect]) -> Result<(), String>
Draws some number of rectangles on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)
sourcepub fn fill_rect<R: Into<Option<Rect>>>(
&mut self,
rect: R,
) -> Result<(), String>
pub fn fill_rect<R: Into<Option<Rect>>>( &mut self, rect: R, ) -> Result<(), String>
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)
sourcepub fn fill_rects(&mut self, rects: &[Rect]) -> Result<(), String>
pub fn fill_rects(&mut self, rects: &[Rect]) -> Result<(), String>
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)
sourcepub fn copy<R1, R2>(
&mut self,
texture: &Texture<'_>,
src: R1,
dst: R2,
) -> Result<(), String>
pub fn copy<R1, R2>( &mut self, texture: &Texture<'_>, src: R1, dst: R2, ) -> Result<(), String>
Copies a portion of the texture to the current rendering target.
- If
src
isNone
, the entire texture is copied. - If
dst
isNone
, 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.
sourcepub fn copy_ex<R1, R2, P>(
&mut self,
texture: &Texture<'_>,
src: R1,
dst: R2,
angle: f64,
center: P,
flip_horizontal: bool,
flip_vertical: bool,
) -> Result<(), String>
pub fn copy_ex<R1, R2, P>( &mut self, texture: &Texture<'_>, src: R1, dst: R2, angle: f64, center: P, flip_horizontal: bool, flip_vertical: bool, ) -> Result<(), String>
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
isNone
, the entire texture is copied. - If
dst
isNone
, the texture will be stretched to fill the given rectangle. - If
center
isNone
, rotation will be done around the center point ofdst
, orsrc
ifdst
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.
Methods from Deref<Target = RendererContext<T::Context>>§
sourcepub fn info(&self) -> RendererInfo
pub fn info(&self) -> RendererInfo
Gets information about the rendering context.
sourcepub fn raw(&self) -> *mut SDL_Renderer
pub fn raw(&self) -> *mut SDL_Renderer
Gets the raw pointer to the SDL_Renderer