Skip to content

Commit cc4980f

Browse files
committed
Add a Rectangle abstraction in the demo app
1 parent eb5de06 commit cc4980f

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

direct-composition/src/main.rs

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ extern crate gleam;
66
extern crate webrender;
77
extern crate winit;
88

9-
use direct_composition::{DirectComposition, AngleVisual};
9+
use direct_composition::DirectComposition;
10+
use webrender::api::{ColorF, DeviceUintSize};
1011
use winit::os::windows::WindowExt;
1112

1213
fn main() {
@@ -25,23 +26,22 @@ fn main() {
2526
webrender::RendererOptions::default(),
2627
).unwrap();
2728

28-
let visual1 = composition.create_angle_visual(300, 200);
29-
visual1.set_offset_x(100.);
30-
visual1.set_offset_y(50.);
31-
32-
let visual2 = composition.create_angle_visual(400, 300);
29+
let mut clicks: usize = 0;
3330
let mut offset_y = 100.;
34-
visual2.set_offset_x(200.);
35-
visual2.set_offset_y(offset_y);
31+
let mut rects = [
32+
Rectangle::new(&composition, DeviceUintSize::new(300, 200), 0., 0.2, 0.4, 1.),
33+
Rectangle::new(&composition, DeviceUintSize::new(400, 300), 0., 0.5, 0., 0.5),
34+
];
35+
rects[0].render();
36+
rects[1].render();
3637

37-
composition.commit();
38+
rects[0].visual.set_offset_x(100.);
39+
rects[0].visual.set_offset_y(50.);
3840

39-
let mut rgba1 = (0., 0.2, 0.4, 1.);
40-
let mut rgba2 = (0., 0.5, 0., 0.5);
41-
render_plain_rgba_frame(&visual1, &rgba1);
42-
render_plain_rgba_frame(&visual2, &rgba2);
41+
rects[1].visual.set_offset_x(200.);
42+
rects[1].visual.set_offset_y(offset_y);
4343

44-
let mut clicks: u32 = 0;
44+
composition.commit();
4545

4646
events_loop.run_forever(|event| {
4747
if let winit::Event::WindowEvent { event, .. } = event {
@@ -56,7 +56,7 @@ fn main() {
5656
};
5757
offset_y = (offset_y - 10. * dy).max(0.).min(468.);
5858

59-
visual2.set_offset_y(offset_y);
59+
rects[1].visual.set_offset_y(offset_y);
6060
composition.commit();
6161
}
6262
winit::WindowEvent::MouseInput {
@@ -65,14 +65,10 @@ fn main() {
6565
..
6666
} => {
6767
clicks += 1;
68-
let (rgba, visual) = if clicks % 2 == 0 {
69-
(&mut rgba1, &visual1)
70-
} else {
71-
(&mut rgba2, &visual2)
72-
};
73-
rgba.1 += 0.1;
74-
rgba.1 %= 1.;
75-
render_plain_rgba_frame(visual, rgba)
68+
let rect = &mut rects[clicks % 2];
69+
rect.color.g += 0.1;
70+
rect.color.g %= 1.;
71+
rect.render()
7672
}
7773
_ => {}
7874
}
@@ -89,14 +85,31 @@ fn direct_composition_from_window(window: &winit::Window) -> DirectComposition {
8985
}
9086
}
9187

92-
fn render_plain_rgba_frame(visual: &AngleVisual, &(r, g, b, a): &(f32, f32, f32, f32)) {
93-
visual.make_current();
94-
visual.gleam.clear_color(r, g, b, a);
95-
visual.gleam.clear(gleam::gl::COLOR_BUFFER_BIT);
96-
assert_eq!(visual.gleam.get_error(), 0);
97-
visual.present();
88+
struct Rectangle {
89+
visual: direct_composition::AngleVisual,
90+
color: ColorF,
9891
}
9992

93+
impl Rectangle {
94+
fn new(composition: &DirectComposition, size: DeviceUintSize,
95+
r: f32, g: f32, b: f32, a: f32)
96+
-> Self {
97+
Rectangle {
98+
visual: composition.create_angle_visual(size.width, size.height),
99+
color: ColorF { r, g, b, a },
100+
}
101+
}
102+
103+
fn render(&self) {
104+
self.visual.make_current();
105+
self.visual.gleam.clear_color(self.color.r, self.color.g, self.color.b, self.color.a);
106+
self.visual.gleam.clear(gleam::gl::COLOR_BUFFER_BIT);
107+
assert_eq!(self.visual.gleam.get_error(), 0);
108+
self.visual.present();
109+
}
110+
}
111+
112+
100113
#[derive(Clone)]
101114
struct Notifier {
102115
events_proxy: winit::EventsLoopProxy,

0 commit comments

Comments
 (0)