Skip to main content

draw_texture_ex

Function draw_texture_ex 

Source
pub fn draw_texture_ex(
    texture: &Texture2D,
    x: f32,
    y: f32,
    color: Color,
    params: DrawTextureParams,
)
Examples found in repository?
examples/screen_texture.rs (lines 21-30)
4async fn main() {
5    let texture: Texture2D = load_texture("examples/chess.png").await.unwrap();
6
7    let lens_material = load_material(
8        ShaderSource::Glsl {
9            vertex: LENS_VERTEX_SHADER,
10            fragment: LENS_FRAGMENT_SHADER,
11        },
12        MaterialParams {
13            uniforms: vec![UniformDesc::new("Center", UniformType::Float2)],
14            ..Default::default()
15        },
16    )
17    .unwrap();
18
19    loop {
20        clear_background(WHITE);
21        draw_texture_ex(
22            &texture,
23            0.0,
24            0.0,
25            WHITE,
26            DrawTextureParams {
27                dest_size: Some(vec2(screen_width(), screen_height())),
28                ..Default::default()
29            },
30        );
31
32        let lens_center = mouse_position();
33
34        lens_material.set_uniform("Center", lens_center);
35
36        gl_use_material(&lens_material);
37        draw_circle(lens_center.0, lens_center.1, 250.0, RED);
38        gl_use_default_material();
39
40        next_frame().await
41    }
42}
More examples
Hide additional examples
examples/post_processing.rs (lines 39-48)
4async fn main() {
5    let render_target = render_target(320, 150);
6    render_target.texture.set_filter(FilterMode::Nearest);
7
8    let material = load_material(
9        ShaderSource::Glsl {
10            vertex: CRT_VERTEX_SHADER,
11            fragment: CRT_FRAGMENT_SHADER,
12        },
13        Default::default(),
14    )
15    .unwrap();
16
17    loop {
18        // drawing to the texture
19
20        // 0..100, 0..100 camera
21        set_camera(&Camera2D {
22            zoom: vec2(0.01, 0.01),
23            target: vec2(0.0, 0.0),
24            render_target: Some(render_target.clone()),
25            ..Default::default()
26        });
27
28        clear_background(LIGHTGRAY);
29        draw_line(-30.0, 45.0, 30.0, 45.0, 3.0, BLUE);
30        draw_circle(-45.0, -35.0, 20.0, YELLOW);
31        draw_circle(45.0, -35.0, 20.0, GREEN);
32
33        // drawing to the screen
34
35        set_default_camera();
36
37        clear_background(WHITE);
38        gl_use_material(&material);
39        draw_texture_ex(
40            &render_target.texture,
41            0.,
42            0.,
43            WHITE,
44            DrawTextureParams {
45                dest_size: Some(vec2(screen_width(), screen_height())),
46                ..Default::default()
47            },
48        );
49        gl_use_default_material();
50
51        next_frame().await;
52    }
53}
examples/letterbox.rs (lines 57-67)
7async fn main() {
8    // Setup 'render_target', used to hold the rendering result so we can resize it
9    let render_target = render_target(VIRTUAL_WIDTH as u32, VIRTUAL_HEIGHT as u32);
10    render_target.texture.set_filter(FilterMode::Linear);
11
12    // Setup camera for the virtual screen, that will render to 'render_target'
13    let mut render_target_cam =
14        Camera2D::from_display_rect(Rect::new(0., 0., VIRTUAL_WIDTH, VIRTUAL_HEIGHT));
15    render_target_cam.render_target = Some(render_target.clone());
16
17    loop {
18        // Get required scaling value
19        let scale: f32 = f32::min(
20            screen_width() / VIRTUAL_WIDTH,
21            screen_height() / VIRTUAL_HEIGHT,
22        );
23
24        // Mouse position in the virtual screen
25        let virtual_mouse_pos = Vec2 {
26            x: (mouse_position().0 - (screen_width() - (VIRTUAL_WIDTH * scale)) * 0.5) / scale,
27            y: (mouse_position().1 - (screen_height() - (VIRTUAL_HEIGHT * scale)) * 0.5) / scale,
28        };
29
30        // ------------------------------------------------------------------------
31        // Begin drawing the virtual screen to 'render_target'
32        // ------------------------------------------------------------------------
33        set_camera(&render_target_cam);
34
35        clear_background(LIGHTGRAY);
36
37        draw_text("Hello Letterbox", 20.0, 20.0, 30.0, DARKGRAY);
38        draw_circle(VIRTUAL_WIDTH / 2.0 - 65.0, VIRTUAL_HEIGHT / 2.0, 35.0, RED);
39        draw_circle(VIRTUAL_WIDTH / 2.0 + 65.0, VIRTUAL_HEIGHT / 2.0, 35.0, BLUE);
40        draw_circle(
41            VIRTUAL_WIDTH / 2.0,
42            VIRTUAL_HEIGHT / 2.0 - 65.0,
43            35.0,
44            YELLOW,
45        );
46
47        draw_circle(virtual_mouse_pos.x, virtual_mouse_pos.y, 15.0, BLACK);
48
49        // ------------------------------------------------------------------------
50        // Begin drawing the window screen
51        // ------------------------------------------------------------------------
52        set_default_camera();
53
54        clear_background(BLACK); // Will be the letterbox color
55
56        // Draw 'render_target' to window screen, porperly scaled and letterboxed
57        draw_texture_ex(
58            &render_target.texture,
59            (screen_width() - (VIRTUAL_WIDTH * scale)) * 0.5,
60            (screen_height() - (VIRTUAL_HEIGHT * scale)) * 0.5,
61            WHITE,
62            DrawTextureParams {
63                dest_size: Some(vec2(VIRTUAL_WIDTH * scale, VIRTUAL_HEIGHT * scale)),
64                flip_y: true, // Must flip y otherwise 'render_target' will be upside down
65                ..Default::default()
66            },
67        );
68
69        next_frame().await;
70    }
71}