Explorar el Código

main: Simplify event handling

and only call glViewport once per frame when resizing.
Johannes Hofmann hace 7 años
padre
commit
a215b897bb
Se han modificado 1 ficheros con 54 adiciones y 46 borrados
  1. 54
    46
      src/main.rs

+ 54
- 46
src/main.rs Ver fichero

@@ -42,9 +42,22 @@ use tile_source::TileSource;
42 42
 enum Action {
43 43
     Nothing,
44 44
     Redraw,
45
+    Resize(u32, u32),
45 46
     Close,
46 47
 }
47 48
 
49
+impl Action {
50
+    fn combine_with(&mut self, newer_action: Self) {
51
+        *self = match (*self, newer_action) {
52
+            (Action::Close, _) | (_, Action::Close) => Action::Close,
53
+            (Action::Resize(..), Action::Resize(w, h)) => Action::Resize(w, h),
54
+            (Action::Resize(w, h), _) | (_, Action::Resize(w, h)) => Action::Resize(w, h),
55
+            (Action::Redraw, _) | (_, Action::Redraw) => Action::Redraw,
56
+            (Action::Nothing, Action::Nothing) => Action::Nothing,
57
+        };
58
+    }
59
+}
60
+
48 61
 #[derive(Copy, Clone, Debug, PartialEq)]
49 62
 struct InputState {
50 63
     mouse_position: (f64, f64),
@@ -166,8 +179,7 @@ fn handle_event(event: &Event, map: &mut MapViewGl, input_state: &mut InputState
166 179
                 Action::Redraw
167 180
             },
168 181
             WindowEvent::Resized(w, h) => {
169
-                map.set_viewport_size(w, h);
170
-                Action::Redraw
182
+                Action::Resize(w, h)
171 183
             },
172 184
             _ => Action::Nothing,
173 185
         },
@@ -219,40 +231,31 @@ fn run() -> Result<(), Box<Error>> {
219 231
     // estimated draw duration
220 232
     let mut est_draw_dur = duration_per_frame;
221 233
     let mut last_draw = Instant::now();
222
-    let mut increase_atlas_size = true;
234
+    let mut increase_atlas_size_possible = true;
223 235
 
224 236
     loop {
225 237
         let start_source_id = sources.current().id();
226
-        let mut redraw = false;
227
-        let mut close = false;
238
+        let mut action = Action::Nothing;
228 239
 
229 240
         events_loop.run_forever(|event| {
230
-            match handle_event(&event, &mut map, &mut input_state, &mut sources) {
231
-                Action::Close => close = true,
232
-                Action::Redraw => redraw = true,
233
-                Action::Nothing => {},
234
-            }
241
+            let a = handle_event(&event, &mut map, &mut input_state, &mut sources);
242
+            action.combine_with(a);
235 243
             ControlFlow::Break
236 244
         });
237 245
 
238
-        if close {
246
+        if action == Action::Close {
239 247
             break;
240 248
         }
241 249
 
242 250
         events_loop.poll_events(|event| {
243
-            match handle_event(&event, &mut map, &mut input_state, &mut sources) {
244
-                Action::Close => {
245
-                    close = true;
246
-                    return;
247
-                },
248
-                Action::Redraw => {
249
-                    redraw = true;
250
-                },
251
-                Action::Nothing => {},
251
+            let a = handle_event(&event, &mut map, &mut input_state, &mut sources);
252
+            action.combine_with(a);
253
+            if action == Action::Close {
254
+                return;
252 255
             }
253 256
         });
254 257
 
255
-        if close {
258
+        if action == Action::Close {
256 259
             break;
257 260
         }
258 261
 
@@ -263,50 +266,55 @@ fn run() -> Result<(), Box<Error>> {
263 266
                     std::thread::sleep(dur);
264 267
 
265 268
                     events_loop.poll_events(|event| {
266
-                        match handle_event(&event, &mut map, &mut input_state, &mut sources) {
267
-                            Action::Close => {
268
-                                close = true;
269
-                                return;
270
-                            },
271
-                            Action::Redraw => {
272
-                                redraw = true;
273
-                            },
274
-                            Action::Nothing => {},
269
+                        let a = handle_event(&event, &mut map, &mut input_state, &mut sources);
270
+                        action.combine_with(a);
271
+                        if action == Action::Close {
272
+                            return;
275 273
                         }
276 274
                     });
277 275
 
278
-                    if close {
276
+                    if action == Action::Close {
279 277
                         break;
280 278
                     }
281 279
                 }
282 280
             }
283 281
         }
284 282
 
283
+        if let Action::Resize(w, h) = action {
284
+            gl_window.resize(w, h);
285
+            map.set_viewport_size(w, h);
286
+        }
287
+
288
+        let redraw = match action {
289
+            Action::Redraw => true,
290
+            Action::Resize(..) => true,
291
+            _ => false,
292
+        };
293
+
285 294
         if redraw {
286 295
             let draw_start = Instant::now();
287
-            let draw_result = map.draw(sources.current());
288
-            let draw_dur = draw_start.elapsed();
289
-
290
-            let _ = gl_window.swap_buffers();
291 296
 
292
-            // Move glClear call out of the critical path.
293 297
             if !map.viewport_in_map() {
294 298
                 cx.clear_color((0.2, 0.2, 0.2, 1.0));
295 299
             }
300
+            let draw_result = map.draw(sources.current());
301
+
302
+            let draw_dur = draw_start.elapsed();
303
+
304
+
305
+            let _ = gl_window.swap_buffers();
296 306
 
297 307
             //TODO increase atlas size earlier to avoid excessive copying to the GPU
298 308
             //TODO increase max tile cache size?
299
-            increase_atlas_size = {
300
-                match (draw_result, increase_atlas_size) {
301
-                    (Err(draws), true) if draws > 1 => {
302
-                        map.increase_atlas_size().is_ok()
303
-                    },
304
-                    (Ok(draws), true) if draws > 1 => {
305
-                        map.increase_atlas_size().is_ok()
306
-                    },
307
-                    _ => increase_atlas_size,
309
+            if increase_atlas_size_possible {
310
+                let draws = match draw_result {
311
+                    Ok(x) => x,
312
+                    Err(x) => x,
313
+                };
314
+                if draws > 1 {
315
+                    increase_atlas_size_possible = map.increase_atlas_size().is_ok();
308 316
                 }
309
-            };
317
+            }
310 318
 
311 319
             last_draw = Instant::now();
312 320