[−][src]Struct sdl2::render::Canvas
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.
Methods
impl<'s> Canvas<Surface<'s>>
[src]
Methods for the SurfaceCanvas
.
pub fn from_surface(surface: Surface<'s>) -> Result<Self, String>
[src]
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.
pub fn surface(&self) -> &SurfaceRef
[src]
Gets a reference to the associated surface of the Canvas
pub fn surface_mut(&mut self) -> &mut SurfaceRef
[src]
Gets a mutable reference to the associated surface of the Canvas
pub fn into_surface(self) -> Surface<'s>
[src]
Gets the associated surface of the Canvas and destroys the Canvas
pub fn texture_creator(&self) -> TextureCreator<SurfaceContext<'s>>
[src]
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
.
pub fn window(&self) -> &Window
[src]
Gets a reference to the associated window of the Canvas
pub fn window_mut(&mut self) -> &mut Window
[src]
Gets a mutable reference to the associated window of the Canvas
pub fn into_window(self) -> Window
[src]
Gets the associated window of the Canvas and destroys the Canvas
pub fn default_pixel_format(&self) -> PixelFormatEnum
[src]
pub fn texture_creator(&self) -> TextureCreator<WindowContext>
[src]
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]
pub fn render_target_supported(&self) -> bool
[src]
Determine whether a window supports the use of render targets.
pub fn with_texture_canvas<F>(
&mut self,
texture: &mut Texture,
f: F
) -> Result<(), TargetRenderError> where
F: FnOnce(&'r mut Canvas<T>),
[src]
&mut self,
texture: &mut Texture,
f: F
) -> Result<(), TargetRenderError> where
F: FnOnce(&'r mut Canvas<T>),
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(); });
pub fn with_multiple_texture_canvas<'t: 'a, 'a: 's, 's, I, F, U: 's>(
&mut self,
textures: I,
f: F
) -> Result<(), TargetRenderError> where
F: FnMut(&'r mut Canvas<T>, &U),
I: Iterator<Item = &'s (&'a mut Texture<'t>, U)>,
[src]
&mut self,
textures: I,
f: F
) -> Result<(), TargetRenderError> where
F: FnMut(&'r mut Canvas<T>, &U),
I: Iterator<Item = &'s (&'a mut Texture<'t>, U)>,
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!
impl<T: RenderTarget> Canvas<T>
[src]
Drawing methods
pub fn raw(&self) -> *mut SDL_Renderer
[src]
pub fn set_draw_color<C: Into<Color>>(&mut self, color: C)
[src]
Sets the color used for drawing operations (Rect, Line and Clear).
pub fn draw_color(&self) -> Color
[src]
Gets the color used for drawing operations (Rect, Line and Clear).
pub fn set_blend_mode(&mut self, blend: BlendMode)
[src]
Sets the blend mode used for drawing operations (Fill and Line).
pub fn blend_mode(&self) -> BlendMode
[src]
Gets the blend mode used for drawing operations.
pub fn clear(&mut self)
[src]
Clears the current rendering target with the drawing color.
pub fn present(&mut self)
[src]
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.
pub fn output_size(&self) -> Result<(u32, u32), String>
[src]
Gets the output size of a rendering context.
pub fn set_logical_size(
&mut self,
width: u32,
height: u32
) -> Result<(), IntegerOrSdlError>
[src]
&mut self,
width: u32,
height: u32
) -> Result<(), IntegerOrSdlError>
Sets a device independent resolution for rendering.
pub fn logical_size(&self) -> (u32, u32)
[src]
Gets device independent resolution for rendering.
pub fn set_viewport<R: Into<Option<Rect>>>(&mut self, rect: R)
[src]
Sets the drawing area for rendering on the current target.
pub fn viewport(&self) -> Rect
[src]
Gets the drawing area for the current target.
pub fn set_clip_rect<R: Into<Option<Rect>>>(&mut self, rect: R)
[src]
Sets the clip rectangle for rendering on the specified target.
If the rectangle is None
, clipping will be disabled.
pub fn clip_rect(&self) -> Option<Rect>
[src]
Gets the clip rectangle for the current target.
Returns None
if clipping is disabled.
pub fn set_scale(&mut self, scale_x: f32, scale_y: f32) -> Result<(), String>
[src]
Sets the drawing scale for rendering on the current target.
pub fn scale(&self) -> (f32, f32)
[src]
Gets the drawing scale for the current target.
pub fn draw_point<P: Into<Point>>(&mut self, point: P) -> Result<(), String>
[src]
Draws a point on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)
pub fn draw_points<'a, P: Into<&'a [Point]>>(
&mut self,
points: P
) -> Result<(), String>
[src]
&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)
pub fn draw_line<P1: Into<Point>, P2: Into<Point>>(
&mut self,
start: P1,
end: P2
) -> Result<(), String>
[src]
&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)
pub fn draw_lines<'a, P: Into<&'a [Point]>>(
&mut self,
points: P
) -> Result<(), String>
[src]
&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)
pub fn draw_rect(&mut self, rect: Rect) -> Result<(), String>
[src]
Draws a rectangle on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)
pub fn draw_rects(&mut self, rects: &[Rect]) -> Result<(), String>
[src]
Draws some number of rectangles on the current rendering target. Errors if drawing fails for any reason (e.g. driver failure)
pub fn fill_rect<R: Into<Option<Rect>>>(
&mut self,
rect: R
) -> Result<(), String>
[src]
&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)
pub fn fill_rects(&mut self, rects: &[Rect]) -> Result<(), String>
[src]
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)
pub fn copy<R1, R2>(
&mut self,
texture: &Texture,
src: R1,
dst: R2
) -> Result<(), String> where
R1: Into<Option<Rect>>,
R2: Into<Option<Rect>>,
[src]
&mut self,
texture: &Texture,
src: R1,
dst: R2
) -> Result<(), String> where
R1: Into<Option<Rect>>,
R2: Into<Option<Rect>>,
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.
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> where
R1: Into<Option<Rect>>,
R2: Into<Option<Rect>>,
P: Into<Option<Point>>,
[src]
&mut self,
texture: &Texture,
src: R1,
dst: R2,
angle: f64,
center: P,
flip_horizontal: bool,
flip_vertical: bool
) -> Result<(), String> where
R1: Into<Option<Rect>>,
R2: Into<Option<Rect>>,
P: Into<Option<Point>>,
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.
pub fn read_pixels<R: Into<Option<Rect>>>(
&self,
rect: R,
format: PixelFormatEnum
) -> Result<Vec<u8>, String>
[src]
&self,
rect: R,
format: PixelFormatEnum
) -> Result<Vec<u8>, String>
Reads pixels from the current rendering target.
Remarks
WARNING: This is a very slow operation, and should not be used frequently.
Methods from Deref<Target = RendererContext<T::Context>>
pub fn info(&self) -> RendererInfo
[src]
Gets information about the rendering context.
pub fn raw(&self) -> *mut SDL_Renderer
[src]
Gets the raw pointer to the SDL_Renderer
Trait Implementations
impl<T: RenderTarget> Deref for Canvas<T>
[src]
type Target = RendererContext<T::Context>
The resulting type after dereferencing.
fn deref(&self) -> &RendererContext<T::Context>
[src]
impl<T> DrawRenderer for Canvas<T> where
T: RenderTarget,
[src]
T: RenderTarget,
fn pixel<C: ToColor>(&self, x: i16, y: i16, color: C) -> Result<(), String>
[src]
fn hline<C: ToColor>(
&self,
x1: i16,
x2: i16,
y: i16,
color: C
) -> Result<(), String>
[src]
&self,
x1: i16,
x2: i16,
y: i16,
color: C
) -> Result<(), String>
fn vline<C: ToColor>(
&self,
x: i16,
y1: i16,
y2: i16,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y1: i16,
y2: i16,
color: C
) -> Result<(), String>
fn rectangle<C: ToColor>(
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
color: C
) -> Result<(), String>
[src]
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
color: C
) -> Result<(), String>
fn rounded_rectangle<C: ToColor>(
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
rad: i16,
color: C
) -> Result<(), String>
[src]
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
rad: i16,
color: C
) -> Result<(), String>
fn box_<C: ToColor>(
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
color: C
) -> Result<(), String>
[src]
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
color: C
) -> Result<(), String>
fn rounded_box<C: ToColor>(
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
rad: i16,
color: C
) -> Result<(), String>
[src]
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
rad: i16,
color: C
) -> Result<(), String>
fn line<C: ToColor>(
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
color: C
) -> Result<(), String>
[src]
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
color: C
) -> Result<(), String>
fn aa_line<C: ToColor>(
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
color: C
) -> Result<(), String>
[src]
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
color: C
) -> Result<(), String>
fn thick_line<C: ToColor>(
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
width: u8,
color: C
) -> Result<(), String>
[src]
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
width: u8,
color: C
) -> Result<(), String>
fn circle<C: ToColor>(
&self,
x: i16,
y: i16,
rad: i16,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y: i16,
rad: i16,
color: C
) -> Result<(), String>
fn aa_circle<C: ToColor>(
&self,
x: i16,
y: i16,
rad: i16,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y: i16,
rad: i16,
color: C
) -> Result<(), String>
fn filled_circle<C: ToColor>(
&self,
x: i16,
y: i16,
rad: i16,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y: i16,
rad: i16,
color: C
) -> Result<(), String>
fn arc<C: ToColor>(
&self,
x: i16,
y: i16,
rad: i16,
start: i16,
end: i16,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y: i16,
rad: i16,
start: i16,
end: i16,
color: C
) -> Result<(), String>
fn ellipse<C: ToColor>(
&self,
x: i16,
y: i16,
rx: i16,
ry: i16,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y: i16,
rx: i16,
ry: i16,
color: C
) -> Result<(), String>
fn aa_ellipse<C: ToColor>(
&self,
x: i16,
y: i16,
rx: i16,
ry: i16,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y: i16,
rx: i16,
ry: i16,
color: C
) -> Result<(), String>
fn filled_ellipse<C: ToColor>(
&self,
x: i16,
y: i16,
rx: i16,
ry: i16,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y: i16,
rx: i16,
ry: i16,
color: C
) -> Result<(), String>
fn pie<C: ToColor>(
&self,
x: i16,
y: i16,
rad: i16,
start: i16,
end: i16,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y: i16,
rad: i16,
start: i16,
end: i16,
color: C
) -> Result<(), String>
fn filled_pie<C: ToColor>(
&self,
x: i16,
y: i16,
rad: i16,
start: i16,
end: i16,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y: i16,
rad: i16,
start: i16,
end: i16,
color: C
) -> Result<(), String>
fn trigon<C: ToColor>(
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
x3: i16,
y3: i16,
color: C
) -> Result<(), String>
[src]
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
x3: i16,
y3: i16,
color: C
) -> Result<(), String>
fn aa_trigon<C: ToColor>(
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
x3: i16,
y3: i16,
color: C
) -> Result<(), String>
[src]
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
x3: i16,
y3: i16,
color: C
) -> Result<(), String>
fn filled_trigon<C: ToColor>(
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
x3: i16,
y3: i16,
color: C
) -> Result<(), String>
[src]
&self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
x3: i16,
y3: i16,
color: C
) -> Result<(), String>
fn polygon<C: ToColor>(
&self,
vx: &[i16],
vy: &[i16],
color: C
) -> Result<(), String>
[src]
&self,
vx: &[i16],
vy: &[i16],
color: C
) -> Result<(), String>
fn aa_polygon<C: ToColor>(
&self,
vx: &[i16],
vy: &[i16],
color: C
) -> Result<(), String>
[src]
&self,
vx: &[i16],
vy: &[i16],
color: C
) -> Result<(), String>
fn filled_polygon<C: ToColor>(
&self,
vx: &[i16],
vy: &[i16],
color: C
) -> Result<(), String>
[src]
&self,
vx: &[i16],
vy: &[i16],
color: C
) -> Result<(), String>
fn textured_polygon<C: ToColor>(
&self,
vx: &[i16],
vy: &[i16],
texture: &Surface,
texture_dx: i16,
texture_dy: i16,
color: C
) -> Result<(), String>
[src]
&self,
vx: &[i16],
vy: &[i16],
texture: &Surface,
texture_dx: i16,
texture_dy: i16,
color: C
) -> Result<(), String>
fn bezier<C: ToColor>(
&self,
vx: &[i16],
vy: &[i16],
s: i32,
color: C
) -> Result<(), String>
[src]
&self,
vx: &[i16],
vy: &[i16],
s: i32,
color: C
) -> Result<(), String>
fn character<C: ToColor>(
&self,
x: i16,
y: i16,
c: char,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y: i16,
c: char,
color: C
) -> Result<(), String>
fn string<C: ToColor>(
&self,
x: i16,
y: i16,
s: &str,
color: C
) -> Result<(), String>
[src]
&self,
x: i16,
y: i16,
s: &str,
color: C
) -> Result<(), String>
Auto Trait Implementations
impl<T> !RefUnwindSafe for Canvas<T>
impl<T> !Send for Canvas<T>
impl<T> !Sync for Canvas<T>
impl<T> Unpin for Canvas<T> where
T: Unpin,
T: Unpin,
impl<T> !UnwindSafe for Canvas<T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,