Module pix_engine::texture

source ·
Expand description

Texture methods.

Provides texture creation and rendering methods on PixState.

Provided methods:

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.