draw_state/
draw_state.rs

1extern crate glium_graphics;
2extern crate graphics;
3extern crate piston;
4
5use glium_graphics::{Flip, Glium2d, GliumWindow, OpenGL, Texture, TextureSettings};
6use graphics::draw_state::Blend;
7use piston::event_loop::EventLoop;
8use piston::input::*;
9use piston::window::WindowSettings;
10
11fn main() {
12    println!("Press A to change blending");
13    println!("Press S to change clip inside/out");
14
15    let opengl = OpenGL::V3_2;
16    let (w, h) = (640, 480);
17    let ref mut window: GliumWindow = WindowSettings::new("glium_graphics: image_test", [w, h])
18        .exit_on_esc(true)
19        .graphics_api(opengl)
20        .build()
21        .unwrap();
22
23    let mut blend = Blend::Alpha;
24    let mut clip_inside = true;
25    let rust_logo = Texture::from_path(
26        window,
27        "assets/rust.png",
28        Flip::None,
29        &TextureSettings::new(),
30    )
31    .unwrap();
32
33    let mut g2d = Glium2d::new(opengl, window);
34    window.set_lazy(true);
35    while let Some(e) = window.next() {
36        if let Some(args) = e.render_args() {
37            use graphics::*;
38
39            let mut target = window.draw();
40            g2d.draw(&mut target, args.viewport(), |c, g| {
41                clear([0.8, 0.8, 0.8, 1.0], g);
42                g.clear_stencil(0);
43                Rectangle::new([1.0, 0.0, 0.0, 1.0]).draw(
44                    [0.0, 0.0, 100.0, 100.0],
45                    &c.draw_state,
46                    c.transform,
47                    g,
48                );
49
50                let draw_state = c.draw_state.blend(blend);
51                Rectangle::new([0.5, 1.0, 0.0, 0.3]).draw(
52                    [50.0, 50.0, 100.0, 100.0],
53                    &draw_state,
54                    c.transform,
55                    g,
56                );
57
58                let transform = c.transform.trans(100.0, 100.0);
59                // Compute clip rectangle from upper left corner.
60                let (clip_x, clip_y, clip_w, clip_h) = (100, 100, 100, 100);
61                let (clip_x, clip_y, clip_w, clip_h) = (
62                    clip_x,
63                    c.viewport.unwrap().draw_size[1] - clip_y - clip_h,
64                    clip_w,
65                    clip_h,
66                );
67                let clipped = c.draw_state.scissor([clip_x, clip_y, clip_w, clip_h]);
68                Image::new().draw(&rust_logo, &clipped, transform, g);
69
70                let transform = c.transform.trans(200.0, 200.0);
71                Ellipse::new([1.0, 0.0, 0.0, 1.0]).draw(
72                    [0.0, 0.0, 50.0, 50.0],
73                    &DrawState::new_clip(),
74                    transform,
75                    g,
76                );
77                Image::new().draw(
78                    &rust_logo,
79                    &(if clip_inside {
80                        DrawState::new_inside()
81                    } else {
82                        DrawState::new_outside()
83                    }),
84                    transform,
85                    g,
86                );
87            });
88
89            target.finish().unwrap();
90        }
91
92        if let Some(Button::Keyboard(Key::A)) = e.press_args() {
93            blend = match blend {
94                Blend::Alpha => Blend::Add,
95                Blend::Add => Blend::Multiply,
96                Blend::Multiply => Blend::Invert,
97                Blend::Invert => Blend::Lighter,
98                Blend::Lighter => Blend::Alpha,
99            };
100            println!("Changed blending to {:?}", blend);
101        }
102
103        if let Some(Button::Keyboard(Key::S)) = e.press_args() {
104            clip_inside = !clip_inside;
105            if clip_inside {
106                println!("Changed to clip inside");
107            } else {
108                println!("Changed to clip outside");
109            }
110        }
111    }
112}