Browse Source

main: Simplify event handling

and only call glViewport once per frame when resizing.
Johannes Hofmann 7 years ago
parent
commit
a215b897bb
1 changed files with 54 additions and 46 deletions
  1. 54
    46
      src/main.rs

+ 54
- 46
src/main.rs View File

42
 enum Action {
42
 enum Action {
43
     Nothing,
43
     Nothing,
44
     Redraw,
44
     Redraw,
45
+    Resize(u32, u32),
45
     Close,
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
 #[derive(Copy, Clone, Debug, PartialEq)]
61
 #[derive(Copy, Clone, Debug, PartialEq)]
49
 struct InputState {
62
 struct InputState {
50
     mouse_position: (f64, f64),
63
     mouse_position: (f64, f64),
166
                 Action::Redraw
179
                 Action::Redraw
167
             },
180
             },
168
             WindowEvent::Resized(w, h) => {
181
             WindowEvent::Resized(w, h) => {
169
-                map.set_viewport_size(w, h);
170
-                Action::Redraw
182
+                Action::Resize(w, h)
171
             },
183
             },
172
             _ => Action::Nothing,
184
             _ => Action::Nothing,
173
         },
185
         },
219
     // estimated draw duration
231
     // estimated draw duration
220
     let mut est_draw_dur = duration_per_frame;
232
     let mut est_draw_dur = duration_per_frame;
221
     let mut last_draw = Instant::now();
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
     loop {
236
     loop {
225
         let start_source_id = sources.current().id();
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
         events_loop.run_forever(|event| {
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
             ControlFlow::Break
243
             ControlFlow::Break
236
         });
244
         });
237
 
245
 
238
-        if close {
246
+        if action == Action::Close {
239
             break;
247
             break;
240
         }
248
         }
241
 
249
 
242
         events_loop.poll_events(|event| {
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
             break;
259
             break;
257
         }
260
         }
258
 
261
 
263
                     std::thread::sleep(dur);
266
                     std::thread::sleep(dur);
264
 
267
 
265
                     events_loop.poll_events(|event| {
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
                         break;
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
         if redraw {
294
         if redraw {
286
             let draw_start = Instant::now();
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
             if !map.viewport_in_map() {
297
             if !map.viewport_in_map() {
294
                 cx.clear_color((0.2, 0.2, 0.2, 1.0));
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
             //TODO increase atlas size earlier to avoid excessive copying to the GPU
307
             //TODO increase atlas size earlier to avoid excessive copying to the GPU
298
             //TODO increase max tile cache size?
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
             last_draw = Instant::now();
319
             last_draw = Instant::now();
312
 
320