@@ -81,47 +81,55 @@ fn main() {
81
81
let resource_loader = EmbeddedResourceLoader :: new ( ) ;
82
82
let mut renderer = Renderer :: new ( pathfinder_device, & resource_loader, mode, options) ;
83
83
84
- // Make a canvas. We're going to draw a house.
85
84
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 ;
116
86
// Wait for a keypress.
117
87
event_loop. run_forever ( |event| {
88
+ let mut should_render = is_first_render;
118
89
match event {
119
90
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 ;
122
94
}
123
- _ => ControlFlow :: Continue ,
95
+ _ => { } ,
124
96
}
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
125
133
} ) ;
126
134
127
135
// Clean up.
0 commit comments