pub struct Graphics {
pub canvas: Canvas<Window>,
pub sdl_context: Sdl,
pub texture_creator: Rc<TextureCreator<WindowContext>>,
pub ttf_context: Sdl3TtfContext,
}Expand description
Owned SDL variables used for rendering
canvas: the drawing surface for the windowsdl_context: the SDL context
Fields§
§canvas: Canvas<Window>The SDL3 canvas, required to draw
sdl_context: SdlSDL3 contaxt
texture_creator: Rc<TextureCreator<WindowContext>>SDL3 texture creator
ttf_context: Sdl3TtfContextSDL3 TTF context required for text rendering
Implementations§
Source§impl Graphics
impl Graphics
Sourcepub fn new(
name: String,
dimensions: (u32, u32),
) -> Result<Graphics, Box<dyn Error>>
pub fn new( name: String, dimensions: (u32, u32), ) -> Result<Graphics, Box<dyn Error>>
Initialize SDL3, create a centered, resizable window and return a Graphics
container with the canvas and SDL context.
§Parameters
name: Window title.dimensions: (width, height) in pixels (u32).
§Returns
Ok(Graphics)on success.Err(Box<dyn std::error::Error>)on failure (window/canvas build error).
§Example
ⓘ
let graphics = graphics::new(String::from("my cool game"), (500, 500))?;Sourcepub fn draw_image(
&mut self,
path: String,
position: (f32, f32),
manager: &mut ResourceManager,
) -> Result<()>
pub fn draw_image( &mut self, path: String, position: (f32, f32), manager: &mut ResourceManager, ) -> Result<()>
Cache an image if it isn’t cached and draw it on the canvas
§Parameters
path: Path to the imageposition: Where to draw the image in the window (x,y) in pixels (f32).
§Returns
Ok(())on success.Err(Box<dyn std::error::Error>)on failure
§Example
ⓘ
graphics.draw_image(String::from("examples/example.png"), (0.0, 0.0)).await;Examples found in repository?
examples/hello.rs (lines 19-23)
15 async fn draw(&self, game: &mut Game) -> anyhow::Result<()> {
16 game.graphics.canvas.set_draw_color(Color::RGB(0, 0, 0));
17 game.graphics.canvas.clear();
18 game.graphics
19 .draw_image(
20 "examples/example.png".to_string(),
21 (0.0, 0.0),
22 &mut game.resource_manager,
23 )
24 .expect("failed to draw an image");
25 game.graphics.draw_text("hi".to_string(), (64.0, 64.0), "Terminus (TTF) 32".to_string(), Color::RGBA(255, 0, 0, 0), &mut game.resource_manager)?;
26 game.graphics.canvas.present();
27 Ok(())
28 }Sourcepub fn draw_text(
&mut self,
text: String,
position: (f32, f32),
font_family: String,
color: Color,
manager: &mut ResourceManager,
) -> Result<()>
pub fn draw_text( &mut self, text: String, position: (f32, f32), font_family: String, color: Color, manager: &mut ResourceManager, ) -> Result<()>
Create a texture from font + text and render it on the canvas
This does not cache the font, so you have the load all the fonts you’ll be using in your
initiaization function (like main). This does cache the texture created from supplied
font and text.
Examples found in repository?
examples/hello.rs (line 25)
15 async fn draw(&self, game: &mut Game) -> anyhow::Result<()> {
16 game.graphics.canvas.set_draw_color(Color::RGB(0, 0, 0));
17 game.graphics.canvas.clear();
18 game.graphics
19 .draw_image(
20 "examples/example.png".to_string(),
21 (0.0, 0.0),
22 &mut game.resource_manager,
23 )
24 .expect("failed to draw an image");
25 game.graphics.draw_text("hi".to_string(), (64.0, 64.0), "Terminus (TTF) 32".to_string(), Color::RGBA(255, 0, 0, 0), &mut game.resource_manager)?;
26 game.graphics.canvas.present();
27 Ok(())
28 }Auto Trait Implementations§
impl Freeze for Graphics
impl RefUnwindSafe for Graphics
impl !Send for Graphics
impl !Sync for Graphics
impl Unpin for Graphics
impl UnwindSafe for Graphics
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more