Struct sdl2::render::Canvas [−][src]
pub struct Canvas<T: RenderTarget> { /* fields omitted */ }
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
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
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.
Methods for the WindowCanvas
.
Gets a mutable reference to the associated window of the Canvas
Gets the associated window of the Canvas and destroys the Canvas
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.
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
for<'r> F: FnOnce(&'r mut Canvas<T>),
pub fn with_texture_canvas<F>(
&mut self,
texture: &mut Texture<'_>,
f: F
) -> Result<(), TargetRenderError> where
for<'r> 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();
});
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!
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.
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.
Sets the drawing area for rendering on 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 whether to force integer scales for resolution-independent rendering.
Gets whether integer scales are forced for resolution-independent rendering.
Sets the drawing scale for rendering on 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
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.
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>
pub fn read_pixels<R: Into<Option<Rect>>>(
&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>>
Gets information about the rendering context.
Gets the raw pointer to the SDL_Renderer
Trait Implementations
type Target = RendererContext<T::Context>
type Target = RendererContext<T::Context>
The resulting type after dereferencing.
Dereferences the value.