Texture

Struct Texture 

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

Contains all the information need to render an image/texture to the screen. In order to be used it must be put inside a Material

Implementations§

Source§

impl Texture

Source

pub fn new<P>( engine: &mut Engine, path: P, loading_op: LoadingOp, ) -> ResourceId<Texture>
where P: AsRef<Path>,

Attempts to both read a file at the specified path and turn it into an image.

Examples found in repository?
examples/rectangles.rs (line 15)
12fn main() {
13    let mut engine = EngineBuilder::new().build().unwrap();
14
15    let texture = Texture::new(&mut engine, "examples/bplogo.png", LoadingOp::Blocking);
16
17    let texture_material = MaterialBuilder::new()
18        .add_texture(texture)
19        .build(&mut engine);
20    let regular_material = MaterialBuilder::new().build(&mut engine);
21
22    let pos = Position {
23        pos: vec2! { 0.0 },
24        regular_material,
25        texture_material,
26        state: false,
27    };
28
29    engine.run(pos);
30}
31
32struct Position {
33    pos: Vec2<f32>,
34    regular_material: Material,
35    texture_material: Material,
36    state: bool,
37}
38
39impl Game for Position {
40    fn render<'o>(&'o mut self, mut render: RenderHandle<'o>) {
41        let mut render_handle = render.begin_pass(Colour::BLACK);
42
43        let defualt_size = vec2! { 50.0 };
44        self.regular_material.add_rectangle(
45            vec2! { 0.0 },
46            defualt_size,
47            Colour::RED,
48            &render_handle,
49        );
50        self.regular_material
51            .add_rectangle(self.pos, vec2! { 100.0 }, Colour::RED, &render_handle);
52        self.texture_material.add_rectangle(
53            vec2! { 0.0, 50.0 },
54            defualt_size,
55            Colour::WHITE,
56            &render_handle,
57        );
58        self.texture_material.add_rectangle_with_uv(
59            Vec2 { x: 0.0, y: 100.0 },
60            defualt_size,
61            vec2! { 311.0 },
62            vec2! { 311.0 },
63            Colour::WHITE,
64            &render_handle,
65        );
66        self.regular_material.add_rectangle_with_rotation(
67            Vec2 { x: 0.0, y: 150.0 },
68            defualt_size,
69            Colour::GREEN,
70            45.0,
71            &render_handle,
72        );
73
74        let points = [
75            Vec2 { x: 0.0, y: 300.0 },
76            Vec2 { x: 80.0, y: 290.0 },
77            Vec2 { x: 100.0, y: 400.0 },
78            Vec2 { x: 60.0, y: 400.0 },
79        ];
80        let uvs = [
81            vec2! { 0.0 },
82            Vec2 { x: 1.0, y: 0.0 },
83            vec2! { 1.0 },
84            Vec2 { x: 0.0, y: 1.0 },
85        ];
86
87        self.regular_material
88            .add_custom(points, uvs, 0.0, Colour::RED, &render_handle);
89
90        self.regular_material.add_screenspace_rectangle(
91            vec2!(0.0, 0.0),
92            vec2!(1.0),
93            Colour::YELLOW,
94            &render_handle,
95        );
96
97        if self.state {
98            self.regular_material.add_rectangle(
99                vec2!(300.0),
100                vec2!(100.0),
101                Colour::GREEN,
102                &render_handle,
103            );
104        }
105
106        self.texture_material.draw(&mut render_handle);
107        self.regular_material.draw(&mut render_handle);
108    }
109
110    fn update(&mut self, engine_handle: &mut Engine) {
111        let dt = engine_handle.get_frame_delta_time();
112        self.pos.x += 100.0 * dt;
113        if engine_handle.is_mouse_key_pressed(MouseKey::Left) {
114            let new_texture = Texture::new(
115                engine_handle,
116                "examples/eggshark.png",
117                LoadingOp::Background,
118            );
119            self.texture_material.change_texture(new_texture);
120        }
121
122        self.state = engine_handle.is_key_down(Key::Space);
123    }
More examples
Hide additional examples
examples/camera.rs (line 16)
13fn main() {
14    let mut engine = EngineBuilder::new().build().unwrap();
15
16    let texture = Texture::new(&mut engine, "examples/bplogo.png", LoadingOp::Blocking);
17
18    let material = MaterialBuilder::new()
19        .add_texture(texture)
20        .build(&mut engine);
21
22    let camera = Camera::default();
23
24    let text = TextMaterial::new(
25        "Mouse pos: 0,0 \n Mouse pos: 0, 0",
26        Colour::WHITE,
27        15.0,
28        20.0,
29    );
30
31    let game = CameraExample {
32        material,
33        text,
34        camera,
35    };
36
37    engine.run(game);
38}
examples/texture.rs (line 19)
12fn main() {
13    let mut engine = EngineBuilder::new()
14        .set_window_title("Testing Triangle")
15        .with_resolution((400, 400))
16        .build()
17        .unwrap();
18
19    let texture = Texture::new(&mut engine, "examples/bplogo.png", LoadingOp::Blocking);
20
21    let texture = MaterialBuilder::new()
22        .add_texture(texture)
23        .build(&mut engine);
24    let defualt = MaterialBuilder::new().build(&mut engine);
25
26    let s = TextureExample {
27        current: texture,
28        other: defualt,
29        pos: vec2! { 0.0 },
30    };
31
32    engine.run(s);
33}
Source

pub fn new_with_sampler<P>( engine: &mut Engine, path: P, sampler: SamplerType, loading_op: LoadingOp, ) -> ResourceId<Texture>
where P: AsRef<Path>,

Attempts to both read a file at the specified path and turn it into an image.

Source

pub fn new_with_mag_min_sampler<P>( engine: &mut Engine, path: P, mag_sampler: SamplerType, min_sampler: SamplerType, loading_op: LoadingOp, ) -> ResourceId<Texture>
where P: AsRef<Path>,

Attempts to load the file at the path and then turn it into a texture. This also allows you to select what sampling type to use for both the mag_sampler, when the texture is being drawn larger than the orignal resolution and min_sampler, when the texture is being drawn smaller than the original resolution.

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

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

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

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

§

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
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,