pub struct Graphics {
pub canvas: Canvas<Window>,
pub sdl_context: Sdl,
pub texture_creator: Rc<TextureCreator<WindowContext>>,
pub ttf_context: Sdl3TtfContext,
/* private fields */
}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<F>(
name: String,
dimensions: (u32, u32),
resource_manager: Arc<Mutex<ResourceManager>>,
resource_initialization: F,
config: WindowConfig,
) -> Result<Graphics, Box<dyn Error>>
pub fn new<F>( name: String, dimensions: (u32, u32), resource_manager: Arc<Mutex<ResourceManager>>, resource_initialization: F, config: WindowConfig, ) -> 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), resource_manager, |_| {}, config)?;Sourcepub fn draw_image(&mut self, path: String, position: (f32, f32)) -> Result<()>
pub fn draw_image(&mut self, path: String, position: (f32, f32)) -> 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 (line 23)
18 fn draw(&mut self, window: &mut Window) -> anyhow::Result<()> {
19 window.graphics.canvas.set_draw_color(Color::RGB(0, 0, 0));
20 window.graphics.canvas.clear();
21 window
22 .graphics
23 .draw_image("assets/example.png".to_string(), (0.0, 0.0))
24 .expect("failed to draw an image");
25 window.graphics.draw_text(
26 String::from("hi"),
27 (64.0, 64.0),
28 String::from("Terminus"),
29 Color::RGBA(255, 0, 0, 0),
30 16.0,
31 )?;
32 window.graphics.draw_text(
33 String::from("hi"),
34 (64.0, 64.0),
35 String::from("Terminus"),
36 Color::RGBA(255, 0, 0, 0),
37 64.0,
38 )?;
39 window.graphics.canvas.present();
40 Ok(())
41 }Sourcepub fn draw_text(
&mut self,
text: String,
position: (f32, f32),
font: String,
color: Color,
size: f32,
) -> Result<()>
pub fn draw_text( &mut self, text: String, position: (f32, f32), font: String, color: Color, size: f32, ) -> Result<()>
Create a texture from font + text and render it on the canvas
Examples found in repository?
examples/hello.rs (lines 25-31)
18 fn draw(&mut self, window: &mut Window) -> anyhow::Result<()> {
19 window.graphics.canvas.set_draw_color(Color::RGB(0, 0, 0));
20 window.graphics.canvas.clear();
21 window
22 .graphics
23 .draw_image("assets/example.png".to_string(), (0.0, 0.0))
24 .expect("failed to draw an image");
25 window.graphics.draw_text(
26 String::from("hi"),
27 (64.0, 64.0),
28 String::from("Terminus"),
29 Color::RGBA(255, 0, 0, 0),
30 16.0,
31 )?;
32 window.graphics.draw_text(
33 String::from("hi"),
34 (64.0, 64.0),
35 String::from("Terminus"),
36 Color::RGBA(255, 0, 0, 0),
37 64.0,
38 )?;
39 window.graphics.canvas.present();
40 Ok(())
41 }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