Module pix_engine::texture
source · Expand description
Texture
methods.
Provides texture creation and rendering methods on PixState
.
Provided methods:
PixState::texture
: Render a portion of a texture to the current canvas.PixState::texture_transformed
: Render a transformed portion of a texture to the current canvas.PixState::create_texture
: Creates a new texture to render to.PixState::delete_texture
: Delete a texture.PixState::update_texture
: Update texture with u8 slice of pixel data.PixState::set_texture_target
: Target a texture for rendering.PixState::clear_texture_target
: Clear texture target back to primary canvas for rendering.
Example
fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
let texture_id1 = s.create_texture(500, 600, PixelFormat::Rgb)?;
// Does not actually render to the current canvas
s.set_texture_target(texture_id1)?;
s.background(Color::random());
s.text("Rendered texture!")?;
s.clear_texture_target();
// `None` uses PixelFormat::default() which defaults to PixelFormat::Rgba
let texture_id2 = s.create_texture(500, 600, None)?;
// `None` updates the entire texture, pass a Rect<i32> to update a sub-rectangle area
let image = Image::from_file("./some_image.png")?;
let pitch = image.width() as usize;
s.update_texture(texture_id2, None, image.as_bytes(), pitch)?;
// Draw both textures to the current canvas
s.texture(texture_id1, None, rect![0, 0, 500, 600])?;
s.texture(texture_id2, None, rect![500, 0, 500, 600])?;
// These could be stored in `self` to avoid re-creating every frame
s.delete_texture(texture_id1)?;
s.delete_texture(texture_id2)?;
Ok(())
}
Structs
Texture
identifier used to reference and target an internally managed texture.