Sprite

Struct Sprite 

Source
pub struct Sprite { /* private fields */ }
Expand description

A sprite is a transformable drawable sprite

Display a sprite from a texture

use texture::Texture;
use sprite::Sprite;

let texture = Texture::new("assets/texture.jpg");
let sprite = Sprite::from_texture(Rc::clone(&texutre));
sprite.rotate(45.0);
sprite.set_position(Vector2::new(100.0, 200.0));

A sprite is just attributes for textures to become printable …

Implementations§

Source§

impl Sprite

Source

pub fn new() -> Sprite

Create a empty sprite It’s not very useful but you can assign texture later

Source

pub fn set_color(&mut self, color: &Color)

Set texture color

Examples found in repository?
examples/event.rs (line 83)
48fn event_process(event: Event, window: &mut Window, sprites: &mut HashMap<&'static str, Sprite>) {
49    match event.1 {
50        pressed!(Escape) => window.close(),
51        pressed!(Space) => {
52            sprites.get_mut("dirt_1").unwrap().rotate(45.0);
53        }
54        pressed!(W) => {
55            sprites
56                .get_mut("dirt_2")
57                .unwrap()
58                .translate(Vector::new(0.0, -10.0));
59        }
60        pressed!(A) => {
61            sprites
62                .get_mut("dirt_2")
63                .unwrap()
64                .translate(Vector::new(-10.0, 0.0));
65        }
66        pressed!(S) => {
67            sprites
68                .get_mut("dirt_2")
69                .unwrap()
70                .translate(Vector::new(0.0, 10.0));
71        }
72        pressed!(D) => {
73            sprites
74                .get_mut("dirt_2")
75                .unwrap()
76                .translate(Vector::new(10.0, 0.0));
77        }
78        Events::MouseButton(glfw::MouseButtonLeft, Action::Press, _) => {
79            let mouse_pos = window.mouse_pos();
80
81            if let Some(sprite) = sprites.get_mut("dirt_1") {
82                if sprite.contain(mouse_pos) {
83                    sprite.set_color(&Color::blue());
84                }
85            }
86        }
87        _ => {}
88    }
89}
Source

pub fn get_sizes(&self) -> Vector2<u32>

Get texture sizes

Source

pub fn set_origin_to_center(&mut self) -> Result<(), SpriteError>

Set origin to center of the sprite. Can fail because a sprite sizes are defined by it’s texture, sometimes it can happend that there isn’t one. So it return an SpriteError::NoTexture

Examples found in repository?
examples/multi_window.rs (line 18)
9fn window1() -> Result<(), Box<Error>> {
10    let mut window = Window::new(600, 600, "Hello1");
11    let tex_leave = Rc::new(Texture::from_path("examples/texture/Z.png").unwrap());
12    let tex_dirt = Rc::new(Texture::from_path("examples/texture/Dirt.png").unwrap());
13    let mut sprite = Sprite::from(&tex_dirt);
14    let mut leave = Sprite::from(&tex_leave);
15    leave.set_position(Point::new(500.0, 500.0));
16    sprite.set_position(Point::new(10.0, 10.0));
17    leave.set_scale(Vector::new(0.5, 0.5));
18    leave.set_origin_to_center()?;
19
20    let event_handler = EventHandler::new(&window);
21
22    window.set_clear_color(Color::new(0.45, 0.0, 1.0));
23    window.enable_cursor();
24    window.poll(None);
25
26    while window.is_open() {
27        window.poll_events();
28        leave.rotate(1.0);
29        leave.update();
30        sprite.update();
31
32        for event in event_handler.fetch() {
33            event_process(event, &mut window);
34        }
35
36        window.clear();
37        window.draw(&mut sprite);
38        window.draw(&mut leave);
39        window.display();
40    }
41    Ok(())
42}
More examples
Hide additional examples
examples/basic.rs (line 18)
8fn main() -> Result<(), Box<Error>> {
9    let mut window = Window::new(gust::WIDTH, gust::HEIGHT, "Hello");
10
11    let tex_leave = Rc::new(Texture::from_path("examples/texture/Z.png").unwrap());
12    let tex_dirt = Rc::new(Texture::from_path("examples/texture/Dirt.png").unwrap());
13    let mut sprite = Sprite::from(&tex_dirt);
14    let mut leave = Sprite::from(&tex_leave);
15    leave.set_position(Point::new(500.0, 500.0));
16    sprite.set_position(Point::new(10.0, 10.0));
17    leave.set_scale(Vector::new(0.5, 0.5));
18    leave.set_origin_to_center()?;
19
20    let event_handler = EventHandler::new(&window);
21
22    window.set_clear_color(Color::new(0.45, 0.0, 1.0));
23    window.enable_cursor();
24    window.poll(None);
25
26    while window.is_open() {
27        window.poll_events();
28        leave.rotate(1.0);
29        leave.update();
30        sprite.update();
31
32        for event in event_handler.fetch() {
33            event_process(event, &mut window);
34        }
35
36        window.clear();
37        window.draw(&sprite);
38        window.draw(&leave);
39        window.display();
40    }
41
42    Ok(())
43}
examples/view.rs (line 21)
7fn main() {
8    let mut window = Window::new(gust::WIDTH, gust::HEIGHT, "Hello");
9    let tex_leave = Rc::new(Texture::from_path("examples/texture/Z.png").unwrap());
10    let tex_dirt = Rc::new(Texture::from_path("examples/texture/Dirt.png").unwrap());
11    let event_handler = EventHandler::new(&window);
12    let mut sprite = Sprite::from(&tex_dirt);
13    let mut leave = Sprite::from(&tex_leave);
14
15    leave.set_position(Point::new(300.0, 300.0));
16    window.set_clear_color(Color::new(0.0, 0.0, 1.0));
17    window.enable_cursor();
18    window.poll(None);
19    leave.set_scale(Vector::new(0.5, 0.5));
20    leave
21        .set_origin_to_center()
22        .unwrap_or_else(|e| println!("{}", e));
23    while window.is_open() {
24        window.poll_events();
25        leave.rotate(1.0);
26        leave.update();
27        sprite.update();
28        window.view_mut().update();
29
30        for event in event_handler.fetch() {
31            event_process(event, &mut window);
32        }
33
34        window.clear();
35        window.draw(&mut sprite);
36        window.draw(&mut leave);
37        window.display();
38    }
39}
Source

pub fn set_texture(&mut self, texture: &Resource<Texture>)

Set a new texture and set the sprite to update state.

Trait Implementations§

Source§

impl Clone for Sprite

Source§

fn clone(&self) -> Sprite

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Sprite

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Sprite

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drawable for Sprite

Drawing trait for sprite sturct

Source§

fn draw<T: Drawer>(&self, window: &mut T)

Draw the actual sprite on a context

Source§

fn draw_with_context<'a>(&self, context: &'a mut Context<'_>)

Draw the actual sprite with your own context.

Source§

fn update(&mut self)

Update the sprite, this is a heavy operation because it’s an operation that reconstruct the model matrix (that represent transformation of the sprite) from scratch. However this function is computed only when it’s necessary. (self.need_update == true) TODO: Make this computation in shader program.

Source§

fn setup_draw(&self, context: &mut Context<'_>)

Setup the draw for the final system you don’t have to implement it in a normal drawable
Source§

impl DrawableMut for Sprite

Source§

fn draw_mut<T: Drawer>(&mut self, window: &mut T)

Draw the actual sprite on a context

Source§

fn draw_with_context_mut<'a>(&mut self, context: &'a mut Context<'_>)

Mutable draw context function.
Source§

impl<'a> From<&'a Rc<Texture>> for Sprite

Source§

fn from(tex: &'a Resource<Texture>) -> Sprite

You can create sprite from texture (precisly Rc)

use gust::texture::Texture;
use gust::sprite::Sprite;
use std::rc::Rc;

let texture = Rc::new(Texture::new("My great texture"));
let personnage = Sprite::from(&texture);
Source§

impl Movable for Sprite

Source§

fn translate<T>(&mut self, vec: Vector2<T>)
where T: Scalar + Into<f32>,

Move the sprite off the offset
Source§

fn get_position(&self) -> Vector2<f32>

Get current position
Source§

fn set_position<T>(&mut self, vec: Vector2<T>)
where T: Scalar + Into<f32>,

Set position of the sprite
Source§

impl PartialEq for Sprite

Source§

fn eq(&self, other: &Sprite) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Rotable for Sprite

Source§

fn rotate<T>(&mut self, angle: T)
where T: Scalar + Into<f32>,

Rotate from the actual angle with the angle given in argument.
Source§

fn set_rotation<T>(&mut self, angle: T)
where T: Scalar + Into<f32>,

Set the rotation of the Rotable struct.
Source§

fn get_rotation(&self) -> f32

Return the rotation of the struct.
Source§

impl Scalable for Sprite

Source§

fn set_scale<T>(&mut self, vec: Vector2<T>)
where T: Scalar + Into<f32>,

Set the scale of the sprite
Source§

fn get_scale(&self) -> Vector2<f32>

Get the current scale
Source§

fn scale<T>(&mut self, factor: Vector2<T>)
where T: Scalar + Into<f32>,

Scale the sprite from a factor
Source§

impl Transformable for Sprite

Source§

fn contain<T: Scalar + Into<f32>>(&self, _point: Point<T>) -> bool

TODO: Transform the point tested.

Source§

fn set_origin<T: Scalar + Into<f32>>(&mut self, origin: Vector2<T>)

Set the origin of the object.
Source§

fn get_origin(&self) -> Vector2<f32>

Get the origin of the transformable.
Source§

impl StructuralPartialEq for Sprite

Auto Trait Implementations§

§

impl Freeze for Sprite

§

impl RefUnwindSafe for Sprite

§

impl !Send for Sprite

§

impl !Sync for Sprite

§

impl Unpin for Sprite

§

impl UnwindSafe for Sprite

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SetParameter for T

Source§

fn set<T>(&mut self, value: T) -> <T as Parameter<Self>>::Result
where T: Parameter<Self>,

Sets value as a parameter of self.
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

unsafe fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.