pub struct Texture { /* private fields */ }Expand description
An image which lives on the GPU.
Implementations§
Source§impl Texture
impl Texture
Sourcepub fn load(path: impl AsRef<Path>) -> Self
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 }Sourcepub fn load_with(path: impl AsRef<Path>, options: impl Into<Options>) -> Self
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);Sourcepub fn try_load(
path: impl AsRef<Path>,
options: impl Into<Options>,
) -> Result<Self, LoadError>
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.
Sourcepub fn from_image(
img: DynamicImage,
options: impl Into<Options>,
) -> Result<Self, LoadError>
pub fn from_image( img: DynamicImage, options: impl Into<Options>, ) -> Result<Self, LoadError>
Creates a texture from an image in memory.
Sourcepub fn slice(&self, rect: Rect) -> TextureSlice
pub fn slice(&self, rect: Rect) -> TextureSlice
Creates a slice which points to part of this texture. Useful for spritesheets.
Examples found in repository?
More 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 }Sourcepub const fn with_origin(self, origin: Origin) -> Self
pub const fn with_origin(self, origin: Origin) -> Self
Sets the origin on this texture.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Texture
impl RefUnwindSafe for Texture
impl !Send for Texture
impl !Sync for Texture
impl Unpin for Texture
impl UnwindSafe for Texture
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