Graphics

Struct Graphics 

Source
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 window
  • sdl_context: the SDL context

Fields§

§canvas: Canvas<Window>

The SDL3 canvas, required to draw

§sdl_context: Sdl

SDL3 contaxt

§texture_creator: Rc<TextureCreator<WindowContext>>

SDL3 texture creator

§ttf_context: Sdl3TtfContext

SDL3 TTF context required for text rendering

Implementations§

Source§

impl Graphics

Source

pub fn new( name: String, dimensions: (u32, u32), resource_manager: Arc<Mutex<ResourceManager>>, ) -> 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))?;
Examples found in repository?
examples/hello.rs (lines 63-67)
61async fn main() {
62    let resource_manager = Arc::new(Mutex::new(ResourceManager::new()));
63    let graphics = graphics::Graphics::new(
64        String::from("my cool window"),
65        (500, 500),
66        resource_manager.clone(),
67    );
68    let mut window = Window::new(graphics.unwrap(), resource_manager);
69    events::run(&mut window, State).await;
70}
Source

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 image
  • position: 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 25)
20    fn draw(&self, window: &mut Window) -> anyhow::Result<()> {
21        window.graphics.canvas.set_draw_color(Color::RGB(0, 0, 0));
22        window.graphics.canvas.clear();
23        window
24            .graphics
25            .draw_image("assets/example.png".to_string(), (0.0, 0.0))
26            .expect("failed to draw an image");
27        window.graphics.draw_text(
28            String::from("hi"),
29            (64.0, 64.0),
30            String::from("assets/terminus.ttf"),
31            Color::RGBA(255, 0, 0, 0),
32            16.0,
33        )?;
34        window.graphics.draw_text(
35            String::from("hi"),
36            (64.0, 150.0),
37            String::from("assets/terminus.ttf"),
38            Color::RGBA(255, 0, 0, 0),
39            128.0,
40        )?;
41        window.graphics.canvas.present();
42        Ok(())
43    }
Source

pub fn draw_text( &mut self, text: String, position: (f32, f32), font_path: 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 27-33)
20    fn draw(&self, window: &mut Window) -> anyhow::Result<()> {
21        window.graphics.canvas.set_draw_color(Color::RGB(0, 0, 0));
22        window.graphics.canvas.clear();
23        window
24            .graphics
25            .draw_image("assets/example.png".to_string(), (0.0, 0.0))
26            .expect("failed to draw an image");
27        window.graphics.draw_text(
28            String::from("hi"),
29            (64.0, 64.0),
30            String::from("assets/terminus.ttf"),
31            Color::RGBA(255, 0, 0, 0),
32            16.0,
33        )?;
34        window.graphics.draw_text(
35            String::from("hi"),
36            (64.0, 150.0),
37            String::from("assets/terminus.ttf"),
38            Color::RGBA(255, 0, 0, 0),
39            128.0,
40        )?;
41        window.graphics.canvas.present();
42        Ok(())
43    }

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,