11#![ allow( clippy:: single_match) ]
22use vangers:: {
3- config:: { settings :: Terrain , Settings } ,
3+ config:: Settings ,
44 render:: { GraphicsContext , ScreenTargets , DEPTH_FORMAT } ,
55} ;
66
77use futures:: executor:: LocalPool ;
88use log:: info;
99use winit:: {
1010 event,
11- event_loop:: { ControlFlow , EventLoop } ,
11+ event_loop:: EventLoop ,
1212 window:: { Window , WindowBuilder } ,
1313} ;
1414
1515pub trait Application {
16- fn on_key ( & mut self , input : event:: KeyboardInput ) -> bool ;
16+ fn on_key ( & mut self , input : event:: KeyEvent , modifiers : event :: Modifiers ) -> bool ;
1717 fn on_mouse_wheel ( & mut self , _delta : event:: MouseScrollDelta ) { }
1818 fn on_cursor_move ( & mut self , _position : ( f64 , f64 ) ) { }
1919 fn on_mouse_button ( & mut self , _state : event:: ElementState , _button : event:: MouseButton ) { }
@@ -24,28 +24,27 @@ pub trait Application {
2424 fn draw ( & mut self , device : & wgpu:: Device , targets : ScreenTargets ) -> wgpu:: CommandBuffer ;
2525}
2626
27- struct WindowContext {
27+ struct WindowContext < ' a > {
2828 window : Window ,
2929 task_pool : LocalPool ,
30- surface : wgpu:: Surface ,
30+ surface : wgpu:: Surface < ' a > ,
3131 present_mode : wgpu:: PresentMode ,
3232 reload_on_focus : bool ,
3333 egui_platform : egui_winit_platform:: Platform ,
3434 depth_target : wgpu:: TextureView ,
3535}
3636
37- pub struct Harness {
37+ pub struct Harness < ' a > {
3838 event_loop : EventLoop < ( ) > ,
39- window_ctx : WindowContext ,
39+ window_ctx : WindowContext < ' a > ,
4040 pub graphics_ctx : GraphicsContext ,
4141}
4242
4343pub struct HarnessOptions {
4444 pub title : & ' static str ,
45- pub uses_level : bool ,
4645}
4746
48- impl Harness {
47+ impl Harness < ' _ > {
4948 pub fn init ( options : HarnessOptions ) -> ( Self , Settings ) {
5049 env_logger:: init ( ) ;
5150 let mut task_pool = LocalPool :: new ( ) ;
@@ -61,17 +60,22 @@ impl Harness {
6160 info ! ( "Initializing the window" ) ;
6261 let instance = wgpu:: Instance :: new ( wgpu:: InstanceDescriptor {
6362 backends : settings. backend . to_wgpu ( ) ,
63+ gles_minor_version : wgpu:: Gles3MinorVersion :: Automatic ,
6464 ..Default :: default ( )
6565 } ) ;
66- let event_loop = EventLoop :: new ( ) ;
66+ let event_loop = EventLoop :: new ( ) . unwrap ( ) ;
6767 let window = WindowBuilder :: new ( )
6868 . with_title ( options. title )
6969 . with_inner_size ( winit:: dpi:: PhysicalSize :: new ( extent. width , extent. height ) )
7070 . with_resizable ( true )
7171 . build ( & event_loop)
7272 . unwrap ( ) ;
73- let surface =
74- unsafe { instance. create_surface ( & window) } . expect ( "Unable to create surface." ) ;
73+
74+ //TODO: use safe `create_surface`. Problematic given our return type.
75+ let surface = unsafe {
76+ instance. create_surface_unsafe ( wgpu:: SurfaceTargetUnsafe :: from_window ( & window) . unwrap ( ) )
77+ }
78+ . expect ( "Unable to create surface." ) ;
7579
7680 info ! ( "Initializing the device" ) ;
7781 let adapter = task_pool
@@ -83,14 +87,16 @@ impl Harness {
8387 . expect ( "Unable to initialize GPU via the selected backend." ) ;
8488
8589 let downlevel_caps = adapter. get_downlevel_capabilities ( ) ;
86- let limits = settings. render . get_device_limits ( & adapter. limits ( ) ) ;
90+ let required_limits = settings
91+ . render
92+ . get_device_limits ( & adapter. limits ( ) , settings. game . geometry . height ) ;
8793
8894 let ( device, queue) = task_pool
8995 . run_until ( adapter. request_device (
9096 & wgpu:: DeviceDescriptor {
9197 label : None ,
92- features : wgpu:: Features :: empty ( ) ,
93- limits ,
98+ required_features : wgpu:: Features :: empty ( ) ,
99+ required_limits ,
94100 } ,
95101 if settings. render . wgpu_trace_path . is_empty ( ) {
96102 None
@@ -123,6 +129,7 @@ impl Harness {
123129 present_mode,
124130 alpha_mode : wgpu:: CompositeAlphaMode :: Auto ,
125131 view_formats : Vec :: new ( ) ,
132+ desired_maximum_frame_latency : 1 ,
126133 } ;
127134 surface. configure ( & device, & config) ;
128135
@@ -177,6 +184,7 @@ impl Harness {
177184 let start_time = time:: Instant :: now ( ) ;
178185 let mut last_time = time:: Instant :: now ( ) ;
179186 let mut needs_reload = false ;
187+ let mut modifiers = event:: Modifiers :: default ( ) ;
180188 let Harness {
181189 event_loop,
182190 window_ctx : mut win,
@@ -185,9 +193,8 @@ impl Harness {
185193
186194 let mut egui_pass = egui_wgpu_backend:: RenderPass :: new ( & gfx. device , gfx. color_format , 1 ) ;
187195
188- event_loop. run ( move |event, _ , control_flow | {
196+ let _ = event_loop. run ( move |event, target_window | {
189197 let _ = win. window ;
190- * control_flow = ControlFlow :: Poll ;
191198 win. task_pool . run_until_stalled ( ) ;
192199
193200 win. egui_platform . handle_event ( & event) ;
@@ -214,6 +221,7 @@ impl Harness {
214221 present_mode : win. present_mode ,
215222 alpha_mode : wgpu:: CompositeAlphaMode :: Auto ,
216223 view_formats : Vec :: new ( ) ,
224+ desired_maximum_frame_latency : 1 ,
217225 } ;
218226 win. surface . configure ( & gfx. device , & config) ;
219227 win. depth_target = gfx
@@ -241,11 +249,14 @@ impl Harness {
241249 needs_reload = false ;
242250 }
243251 event:: WindowEvent :: CloseRequested => {
244- * control_flow = ControlFlow :: Exit ;
252+ target_window. exit ( ) ;
253+ }
254+ event:: WindowEvent :: ModifiersChanged ( mods) => {
255+ modifiers = mods;
245256 }
246- event:: WindowEvent :: KeyboardInput { input , .. } => {
247- if !app. on_key ( input ) {
248- * control_flow = ControlFlow :: Exit ;
257+ event:: WindowEvent :: KeyboardInput { event , .. } => {
258+ if !app. on_key ( event , modifiers ) {
259+ target_window . exit ( ) ;
249260 }
250261 }
251262 event:: WindowEvent :: MouseWheel { delta, .. } => app. on_mouse_wheel ( delta) ,
@@ -257,7 +268,7 @@ impl Harness {
257268 }
258269 _ => { }
259270 } ,
260- event:: Event :: MainEventsCleared => {
271+ event:: Event :: AboutToWait => {
261272 let duration = time:: Instant :: now ( ) - last_time;
262273 last_time += duration;
263274
@@ -274,8 +285,10 @@ impl Harness {
274285 app. draw_ui ( & win. egui_platform . context ( ) ) ;
275286 let egui_output = win. egui_platform . end_frame ( Some ( & win. window ) ) ;
276287
277- let egui_primitives =
278- win. egui_platform . context ( ) . tessellate ( egui_output. shapes ) ;
288+ let egui_primitives = win
289+ . egui_platform
290+ . context ( )
291+ . tessellate ( egui_output. shapes , 1.0 ) ;
279292 let screen_descriptor = egui_wgpu_backend:: ScreenDescriptor {
280293 physical_width : gfx. screen_size . width ,
281294 physical_height : gfx. screen_size . height ,
0 commit comments