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
Texture2D— owning texture handle.Image— CPU-side counterpart; upload withRaylibHandle::load_texture_from_image.RaylibRenderTexture2D— companion trait for offscreen render targets.
Provided Methods§
Sourcefn update_texture(&mut self, pixels: &[u8]) -> Result<(), UpdateTextureError>
fn update_texture(&mut self, pixels: &[u8]) -> Result<(), UpdateTextureError>
Updates GPU texture with new data.
Sourcefn update_texture_rec(
&mut self,
rec: impl Into<Rectangle>,
pixels: &[u8],
) -> Result<(), UpdateTextureError>
fn update_texture_rec( &mut self, rec: impl Into<Rectangle>, pixels: &[u8], ) -> Result<(), UpdateTextureError>
Update GPU texture rectangle with new data
Sourcefn load_image(&self) -> Result<Image, InvalidImageError>
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.
Sourcefn gen_texture_mipmaps(&mut self)
fn gen_texture_mipmaps(&mut self)
Generates GPU mipmaps for a texture.
Sourcefn set_texture_filter(&self, _: &RaylibThread, filter_mode: TextureFilter)
fn set_texture_filter(&self, _: &RaylibThread, filter_mode: TextureFilter)
Sets global texture scaling filter mode.
Sourcefn set_texture_wrap(&self, _: &RaylibThread, wrap_mode: TextureWrap)
fn set_texture_wrap(&self, _: &RaylibThread, wrap_mode: TextureWrap)
Sets global texture wrapping mode.
Sourcefn is_texture_valid(&self) -> bool
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".