Skip to main content

RaylibTexture2D

Trait RaylibTexture2D 

Source
pub trait RaylibTexture2D: AsRef<Texture2D> + AsMut<Texture2D> {
    // Provided methods
    fn width(&self) -> i32 { ... }
    fn height(&self) -> i32 { ... }
    fn mipmaps(&self) -> i32 { ... }
    fn format(&self) -> i32 { ... }
    fn update_texture(
        &mut self,
        pixels: &[u8],
    ) -> Result<(), UpdateTextureError> { ... }
    fn update_texture_rec(
        &mut self,
        rec: impl Into<Rectangle>,
        pixels: &[u8],
    ) -> Result<(), UpdateTextureError> { ... }
    fn load_image(&self) -> Result<Image, InvalidImageError> { ... }
    fn gen_texture_mipmaps(&mut self) { ... }
    fn set_texture_filter(&self, _: &RaylibThread, filter_mode: TextureFilter) { ... }
    fn set_texture_wrap(&self, _: &RaylibThread, wrap_mode: TextureWrap) { ... }
    fn is_texture_valid(&self) -> bool { ... }
}
Expand description

Extension methods for types that wrap a raylib Texture2D (both owned Texture2D and WeakTexture2D).

Implemented for Texture2D (RAII-owned, freed on drop) and WeakTexture2D (no-drop alias from Texture2D::make_weak). Provides the field accessors (width, height, mipmaps, format), GPU upload helpers (update_texture, update_texture_rec), readback to a CPU Image (load_image), mipmap generation (gen_texture_mipmaps), and the sampler-state setters (set_texture_filter, set_texture_wrap).

§Examples

use raylib::prelude::*;
use raylib::core::texture::RaylibTexture2D;

let (mut rl, thread) = raylib::init().size(640, 480).title("tex").build();
let tex = rl
    .load_texture(&thread, "assets/sprite.png")
    .expect("texture load");
println!("{}x{}, mipmaps {}", tex.width(), tex.height(), tex.mipmaps());
while !rl.window_should_close() {
    let mut d = rl.begin_drawing(&thread);
    d.clear_background(Color::RAYWHITE);
    d.draw_texture(&tex, 0, 0, Color::WHITE);
}

§See also

Provided Methods§

Source

fn width(&self) -> i32

Texture base width

Source

fn height(&self) -> i32

Texture base height

Source

fn mipmaps(&self) -> i32

Mipmap levels, 1 by default

Source

fn format(&self) -> i32

Data format (PixelFormat type)

Source

fn update_texture(&mut self, pixels: &[u8]) -> Result<(), UpdateTextureError>

Updates GPU texture with new data.

Source

fn update_texture_rec( &mut self, rec: impl Into<Rectangle>, pixels: &[u8], ) -> Result<(), UpdateTextureError>

Update GPU texture rectangle with new data

Source

fn load_image(&self) -> Result<Image, InvalidImageError>

Gets pixel data from GPU texture and returns an Image. Fairly sure this would never fail. If it does wrap in result.

Source

fn gen_texture_mipmaps(&mut self)

Generates GPU mipmaps for a texture.

Source

fn set_texture_filter(&self, _: &RaylibThread, filter_mode: TextureFilter)

Sets global texture scaling filter mode.

Source

fn set_texture_wrap(&self, _: &RaylibThread, wrap_mode: TextureWrap)

Sets global texture wrapping mode.

Source

fn is_texture_valid(&self) -> bool

Check if a texture is valid (loaded in GPU)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§