Skip to content

Commit 953a1da

Browse files
authored
Auto merge of #404 - leroycep:minimal-example-rerendering, r=pcwalton
Make `canvas_minimal` example rerender on refresh I'm not sure if this actually works, because the `canvas_minimal` example doesn't run on my computer.
2 parents cd129e8 + 263dc0a commit 953a1da

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

examples/canvas_minimal/src/main.rs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,47 +81,55 @@ fn main() {
8181
let resource_loader = EmbeddedResourceLoader::new();
8282
let mut renderer = Renderer::new(pathfinder_device, &resource_loader, mode, options);
8383

84-
// Make a canvas. We're going to draw a house.
8584
let font_context = CanvasFontContext::from_system_source();
86-
let mut canvas = Canvas::new(framebuffer_size.to_f32()).get_context_2d(font_context);
87-
88-
// Set line width.
89-
canvas.set_line_width(10.0);
90-
91-
// Draw walls.
92-
canvas.stroke_rect(RectF::new(vec2f(75.0, 140.0), vec2f(150.0, 110.0)));
93-
94-
// Draw door.
95-
canvas.fill_rect(RectF::new(vec2f(130.0, 190.0), vec2f(40.0, 60.0)));
96-
97-
// Draw roof.
98-
let mut path = Path2D::new();
99-
path.move_to(vec2f(50.0, 140.0));
100-
path.line_to(vec2f(150.0, 60.0));
101-
path.line_to(vec2f(250.0, 140.0));
102-
path.close_path();
103-
canvas.stroke_path(path);
104-
105-
// Render the canvas to screen.
106-
let mut scene = SceneProxy::from_scene(canvas.into_canvas().into_scene(),
107-
renderer.mode().level,
108-
RayonExecutor);
109-
scene.build_and_render(&mut renderer, BuildOptions::default());
110-
111-
// Present the surface.
112-
let mut surface = device.unbind_surface_from_context(&mut context).unwrap().unwrap();
113-
device.present_surface(&mut context, &mut surface).unwrap();
114-
device.bind_surface_to_context(&mut context, surface).unwrap();
115-
85+
let mut is_first_render = true;
11686
// Wait for a keypress.
11787
event_loop.run_forever(|event| {
88+
let mut should_render = is_first_render;
11889
match event {
11990
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } |
120-
Event::WindowEvent { event: WindowEvent::KeyboardInput { .. }, .. } => {
121-
ControlFlow::Break
91+
Event::WindowEvent { event: WindowEvent::KeyboardInput { .. }, .. } => return ControlFlow::Break,
92+
Event::WindowEvent { event: WindowEvent::Refresh, .. } => {
93+
should_render = true;
12294
}
123-
_ => ControlFlow::Continue,
95+
_ => {},
12496
}
97+
98+
if should_render {
99+
// Make a canvas. We're going to draw a house.
100+
let mut canvas = Canvas::new(framebuffer_size.to_f32()).get_context_2d(font_context.clone());
101+
102+
// Set line width.
103+
canvas.set_line_width(10.0);
104+
105+
// Draw walls.
106+
canvas.stroke_rect(RectF::new(vec2f(75.0, 140.0), vec2f(150.0, 110.0)));
107+
108+
// Draw door.
109+
canvas.fill_rect(RectF::new(vec2f(130.0, 190.0), vec2f(40.0, 60.0)));
110+
111+
// Draw roof.
112+
let mut path = Path2D::new();
113+
path.move_to(vec2f(50.0, 140.0));
114+
path.line_to(vec2f(150.0, 60.0));
115+
path.line_to(vec2f(250.0, 140.0));
116+
path.close_path();
117+
canvas.stroke_path(path);
118+
119+
// Render the canvas to screen.
120+
let mut scene = SceneProxy::from_scene(canvas.into_canvas().into_scene(),
121+
renderer.mode().level,
122+
RayonExecutor);
123+
scene.build_and_render(&mut renderer, BuildOptions::default());
124+
125+
// Present the surface.
126+
let mut surface = device.unbind_surface_from_context(&mut context).unwrap().unwrap();
127+
device.present_surface(&mut context, &mut surface).unwrap();
128+
device.bind_surface_to_context(&mut context, surface).unwrap();
129+
}
130+
131+
is_first_render = false;
132+
ControlFlow::Continue
125133
});
126134

127135
// Clean up.

0 commit comments

Comments
 (0)