Texture

Struct Texture 

Source
pub struct Texture {
    pub id: u32,
    /* private fields */
}
Expand description

§Texture structure

A texture is an id inside openGL that can contain a array of byte this array can be spreaded to drawable object

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

let window = Window::new(1080, 1920, "Test");
let leave = Rc::new(Texture::new("path/to/test"));
let sprite = Sprite::from(&leave);

Fields§

§id: u32

Implementations§

Source§

impl Texture

Source

pub fn new() -> Texture

Create an empty texture

Source

pub unsafe fn from_data( data: *mut c_void, mode: RgbMode, width: u32, height: u32, ) -> Texture

Create a texture from a raw data pointer needed for Font handling unsafe version of from slice

Examples found in repository?
examples/texture.rs (line 16)
7fn main() {
8    let mut window = Window::new(gust::WIDTH, gust::HEIGHT, "Hello");
9    let mut pixels: Vec<u8> = vec![125; 200 * 200 * 4];
10    let pix: Vec<u8> = vec![255; 100 * 100 * 3];
11    let mut my_tex = Texture::from_path("examples/texture/New.png").unwrap();
12    let blank;
13
14    unsafe {
15        use std::os::raw::c_void;
16        blank = Texture::from_data(pixels.as_mut_ptr() as *mut c_void, RgbMode::RGBA, 200, 200);
17    }
18
19    my_tex
20        .update_block(
21            pix.as_slice(),
22            Vector::new(100, 100),
23            Vector::new(10, 10),
24            None,
25        )
26        .unwrap();
27
28    let blank_rc = Rc::new(blank);
29    let tex_dirt = Rc::new(my_tex);
30    let event_handler = EventHandler::new(&window);
31    let mut sprite = Sprite::from(&tex_dirt);
32    let mut leave = Sprite::from(&blank_rc);
33    let mut sprite2 = Sprite::from(&tex_dirt);
34
35    leave.set_position(Point::new(600.0, 100.0));
36    sprite.set_position(Vector::new(100.0, 100.0));
37    sprite2.set_position(Point::new(1000.0, 100.0));
38    sprite.update();
39    sprite2.update();
40    leave.update();
41
42    window.poll(None);
43    window.enable_cursor();
44    window.set_clear_color(Color::new(0.0, 0.0, 1.0));
45    while window.is_open() {
46        window.poll_events();
47
48        for event in event_handler.fetch() {
49            event_process(event, &mut window, &tex_dirt, &mut sprite);
50        }
51
52        window.clear();
53        window.draw(&sprite);
54        window.draw(&leave);
55        window.draw(&sprite2);
56        window.display();
57    }
58}
Source

pub fn from_slice( data: &mut [u8], mode: RgbMode, width: u32, height: u32, ) -> Texture

Create a texture from a slice

Source

pub fn from_color(color: Color, sizes: Vector<u32>) -> Texture

Source

pub fn from_size(sizes: Vector<u32>) -> Texture

Create an empty texture with a size

Source

pub fn from_image(img: DynamicImage) -> Result<Texture, TextureError>

Create a texture from an image

Source

pub fn from_path<P: AsRef<Path>>( path_to_file: P, ) -> Result<Texture, TextureError>

Create new texture from file path

Examples found in repository?
examples/threads.rs (line 42)
39fn render(shared: &mut SharedWindow, recv: mpsc::Receiver<()>) {
40    shared.active();
41    let texture_rc =
42        Rc::new(Texture::from_path("examples/texture/Dirt.png").expect("Cannot open New.png"));
43    let sprite = Sprite::from(&texture_rc);
44
45    loop {
46        if recv.try_recv() == Ok(()) {
47            break;
48        }
49        shared.clear(Color::red());
50        shared.draw(&sprite);
51        shared.display();
52    }
53}
More examples
Hide additional examples
examples/event.rs (line 17)
15fn main() {
16    let mut window = Window::new(gust::WIDTH, gust::HEIGHT, "Hello");
17    let tex_dirt = Rc::new(Texture::from_path("examples/texture/Dirt.png").unwrap());
18    let event_handler = EventHandler::new(&window);
19    let mut sprites = HashMap::new();
20    sprites.insert("dirt_1", Sprite::from(&tex_dirt));
21    sprites.insert("dirt_2", Sprite::from(&tex_dirt));
22
23    window.set_clear_color(Color::new(0.0, 0.0, 1.0));
24    window.enable_cursor();
25    window.poll(None);
26    while window.is_open() {
27        window.poll_events();
28
29        for event in event_handler.fetch() {
30            event_process(event, &mut window, &mut sprites);
31        }
32
33        draw(&mut window, &mut sprites);
34    }
35}
examples/batch.rs (line 18)
15fn main() -> Result<(), Box<Error>> {
16    let mut window = Window::new(gust::WIDTH, gust::HEIGHT, "Hello");
17
18    let texture = Rc::new(Texture::from_path("examples/texture/Dirt.png").unwrap());
19    let mut batch = SpriteBatch::from(&texture);
20    for i in 0..1_000_000 {
21        let mut data = SpriteData::new(Vector::new(i as f32 * 1.0, i as f32 * 10.0));
22        data.set_texture_raw([Vector::new(0.0, 0.0), Vector::new(1.0, 1.0)]);
23        batch.push_sprite(data);
24    }
25
26    let event_handler = EventHandler::new(&window);
27
28    window.set_clear_color(Color::new(0.45, 0.0, 1.0));
29    window.enable_cursor();
30    window.poll(None);
31    while window.is_open() {
32        window.poll_events();
33        for event in event_handler.fetch() {
34            event_process(event, &mut window, &mut batch);
35        }
36        window.clear();
37        window.draw_mut(&mut batch);
38        window.display();
39    }
40
41    Ok(())
42}
examples/multi_window.rs (line 11)
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}
examples/basic.rs (line 11)
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 9)
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 to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<dyn Error>>

Examples found in repository?
examples/texture.rs (line 67)
60fn event_process(event: Event, window: &mut Window, texture: &Texture, sprite: &mut Sprite) {
61    match event.1 {
62        Events::Key(Key::Escape, _, _, _) => {
63            window.close();
64        }
65        Events::MouseButton(_, _, _) => {
66            println!("Dumping texture to test.png");
67            texture.to_file("test.png").unwrap();
68        }
69        Events::CursorPos(x, y) => {
70            sprite.set_position(Vector::new(x as f32, y as f32));
71        }
72        _ => {}
73    }
74}
Source

pub fn update_block<T, U>( &mut self, data: &[u8], sizes: Vector<u32>, pos: T, rgb_mode: U, ) -> Result<(), TextureError>
where T: Into<Option<Vector<u32>>>, U: Into<Option<RgbMode>>,

Update a block of a texture with an offset and a size return TextureError if the sizes are not correct.

Examples found in repository?
examples/texture.rs (lines 20-25)
7fn main() {
8    let mut window = Window::new(gust::WIDTH, gust::HEIGHT, "Hello");
9    let mut pixels: Vec<u8> = vec![125; 200 * 200 * 4];
10    let pix: Vec<u8> = vec![255; 100 * 100 * 3];
11    let mut my_tex = Texture::from_path("examples/texture/New.png").unwrap();
12    let blank;
13
14    unsafe {
15        use std::os::raw::c_void;
16        blank = Texture::from_data(pixels.as_mut_ptr() as *mut c_void, RgbMode::RGBA, 200, 200);
17    }
18
19    my_tex
20        .update_block(
21            pix.as_slice(),
22            Vector::new(100, 100),
23            Vector::new(10, 10),
24            None,
25        )
26        .unwrap();
27
28    let blank_rc = Rc::new(blank);
29    let tex_dirt = Rc::new(my_tex);
30    let event_handler = EventHandler::new(&window);
31    let mut sprite = Sprite::from(&tex_dirt);
32    let mut leave = Sprite::from(&blank_rc);
33    let mut sprite2 = Sprite::from(&tex_dirt);
34
35    leave.set_position(Point::new(600.0, 100.0));
36    sprite.set_position(Vector::new(100.0, 100.0));
37    sprite2.set_position(Point::new(1000.0, 100.0));
38    sprite.update();
39    sprite2.update();
40    leave.update();
41
42    window.poll(None);
43    window.enable_cursor();
44    window.set_clear_color(Color::new(0.0, 0.0, 1.0));
45    while window.is_open() {
46        window.poll_events();
47
48        for event in event_handler.fetch() {
49            event_process(event, &mut window, &tex_dirt, &mut sprite);
50        }
51
52        window.clear();
53        window.draw(&sprite);
54        window.draw(&leave);
55        window.draw(&sprite2);
56        window.display();
57    }
58}
Source

pub fn get_rawsize(&self) -> usize

Source

pub fn get_data(&self) -> Vec<u8>

Get a Vec representing pixels of the texture

Source

pub fn update_from_texture( &mut self, texture: &Texture, pos: Vector<u32>, ) -> Result<(), TextureError>

Nicely working efficiently legacy fb deleted

Source

pub fn update<T>(&mut self, data: &[u8], mode: T) -> Result<(), TextureError>
where T: Into<Option<RgbMode>>,

Update the data of the texture

Source

pub fn repeat_mode(&self)

Repeat mode texture wrap

Source

pub fn linear_mode(&self)

Linear mode for filter

Source

pub fn unbind(&self)

Unbind the texture

Source

pub fn active(&self, num: i32)

Active texture num

Source

pub fn rgb_mode(&self) -> &RgbMode

Getter for color mode

Source

pub fn width(&self) -> u32

Simple getter for width

Source

pub fn height(&self) -> u32

Simple getter for height

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

Source§

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

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

impl Default for Texture

Source§

fn default() -> Texture

Create a 1 white pixel texture

Source§

impl Drop for Texture

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl PartialEq for Texture

Source§

fn eq(&self, other: &Texture) -> 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 Eq for Texture

Source§

impl StructuralPartialEq for Texture

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