Texture

Struct Texture 

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

An image which lives on the GPU.

Implementations§

Source§

impl Texture

Source

pub fn empty() -> Self

Creates an empty texture. This is a placeholder value.

Source

pub fn load(path: impl AsRef<Path>) -> Self

Loads a texture at a given path.

Examples found in repository?
examples/soko.rs (line 40)
39    fn new() -> Self {
40        let tiles = Texture::load("examples/tiles.png");
41
42        let object_slice = tiles.slice(Rect::new(0, 9, 8, 8));
43        let target_slice = tiles.slice(Rect::new(9, 9, 8, 8));
44        let player_slice = tiles.slice(Rect::new(27, 9, 8, 8));
45        let mut wall_slices = Vec::new();
46        for x in 0..4 {
47            wall_slices.push(tiles.slice(Rect::new(x * 9, 0, 8, 8)));
48        }
49
50        let mut player = Entity::new(player_slice, vec2(0., 0.));
51        let mut objects = Vec::new();
52        let mut targets = Vec::new();
53        let mut walls = Vec::new();
54        let map = Vec::from_iter(MAP.split('\n').map(|line| Vec::from_iter(line.chars())));
55
56        for (y, line) in map.iter().enumerate() {
57            for (x, tile) in line.iter().enumerate() {
58                let position = vec2(x as f32, y as f32);
59                match tile {
60                    'O' => targets.push(Entity::new(target_slice.clone(), position)),
61                    '*' => objects.push(Entity::new(object_slice.clone(), position)),
62                    '@' => {
63                        targets.push(Entity::new(target_slice.clone(), position));
64                        objects.push(Entity::new(object_slice.clone(), position));
65                    }
66                    'P' => player.position = position,
67                    '#' => {
68                        let left = map[y].get(x.wrapping_sub(1)) == Some(&'#');
69                        let right = map[y].get(x + 1) == Some(&'#');
70                        let i = left as usize * 2 + right as usize;
71
72                        walls.push(Entity::new(wall_slices[i].clone(), position));
73                    }
74                    _ => continue,
75                }
76            }
77        }
78
79        Self {
80            player,
81            objects,
82            targets,
83            walls,
84            won: false,
85        }
86    }
Source

pub fn load_with(path: impl AsRef<Path>, options: impl Into<Options>) -> Self

Loads a texture at a given path, with custom options.

You may specify a ScaleMode or an Origin for the texture, or both using TextureOptions.

// Create a texture which is positioned around its center.
let my_texture = Texture::load_with("resources/image.png", Origin::CENTER);
// Create a texture with linear scaling
let my_texture = Texture::load_with("resources/image.png", ScaleMode::Linear);
Examples found in repository?
examples/transform.rs (line 20)
15    fn default() -> Self {
16        Self {
17            position: vec2(800., 600.) / 2., // window center
18            scale: Vec2::splat(8.),
19            angle: 0.,
20            creature: Texture::load_with("examples/tiles.png", Origin::CENTER)
21                .slice(Rect::new(27, 9, 8, 8)),
22        }
23    }
Source

pub fn try_load( path: impl AsRef<Path>, options: impl Into<Options>, ) -> Result<Self, LoadError>

Like load, but returns an error instead of outputting a warning.

Source

pub fn from_image( img: DynamicImage, options: impl Into<Options>, ) -> Result<Self, LoadError>

Creates a texture from an image in memory.

Source

pub fn slice(&self, rect: Rect) -> TextureSlice

Creates a slice which points to part of this texture. Useful for spritesheets.

Examples found in repository?
examples/transform.rs (line 21)
15    fn default() -> Self {
16        Self {
17            position: vec2(800., 600.) / 2., // window center
18            scale: Vec2::splat(8.),
19            angle: 0.,
20            creature: Texture::load_with("examples/tiles.png", Origin::CENTER)
21                .slice(Rect::new(27, 9, 8, 8)),
22        }
23    }
More examples
Hide additional examples
examples/soko.rs (line 42)
39    fn new() -> Self {
40        let tiles = Texture::load("examples/tiles.png");
41
42        let object_slice = tiles.slice(Rect::new(0, 9, 8, 8));
43        let target_slice = tiles.slice(Rect::new(9, 9, 8, 8));
44        let player_slice = tiles.slice(Rect::new(27, 9, 8, 8));
45        let mut wall_slices = Vec::new();
46        for x in 0..4 {
47            wall_slices.push(tiles.slice(Rect::new(x * 9, 0, 8, 8)));
48        }
49
50        let mut player = Entity::new(player_slice, vec2(0., 0.));
51        let mut objects = Vec::new();
52        let mut targets = Vec::new();
53        let mut walls = Vec::new();
54        let map = Vec::from_iter(MAP.split('\n').map(|line| Vec::from_iter(line.chars())));
55
56        for (y, line) in map.iter().enumerate() {
57            for (x, tile) in line.iter().enumerate() {
58                let position = vec2(x as f32, y as f32);
59                match tile {
60                    'O' => targets.push(Entity::new(target_slice.clone(), position)),
61                    '*' => objects.push(Entity::new(object_slice.clone(), position)),
62                    '@' => {
63                        targets.push(Entity::new(target_slice.clone(), position));
64                        objects.push(Entity::new(object_slice.clone(), position));
65                    }
66                    'P' => player.position = position,
67                    '#' => {
68                        let left = map[y].get(x.wrapping_sub(1)) == Some(&'#');
69                        let right = map[y].get(x + 1) == Some(&'#');
70                        let i = left as usize * 2 + right as usize;
71
72                        walls.push(Entity::new(wall_slices[i].clone(), position));
73                    }
74                    _ => continue,
75                }
76            }
77        }
78
79        Self {
80            player,
81            objects,
82            targets,
83            walls,
84            won: false,
85        }
86    }
Source

pub const fn with_origin(self, origin: Origin) -> Self

Sets the origin on this texture.

Source

pub fn width(&self) -> u32

The width of this texture.

Source

pub fn height(&self) -> u32

The height of this texture.

Trait Implementations§

Source§

impl Clone for Texture

Source§

fn clone(&self) -> Texture

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 Drawable for Texture

Source§

fn draw(&self, canvas: &mut Canvas, transform: Transform)

Draws this object, applying a transform to it. Read more

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> 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> 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.