sotrh / learn-wgpu

Guide for using gfx-rs's wgpu library.
https://sotrh.github.io/learn-wgpu/
MIT License
1.53k stars 267 forks source link

Tutorial 2 event_loop.run() code in render() section does not seem to be correct #574

Closed myth0genesis closed 1 month ago

myth0genesis commented 2 months ago

The following code block:

event_loop.run(move |event, _, control_flow| {
    match event {
        // ... with the other WindowEvents
        WindowEvent::RedrawRequested(window_id) if window_id == state.window().id() => {
            state.update();
            match state.render() {
                Ok(_) => {}
                // Reconfigure the surface if lost
                Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
                // The system is out of memory, we should probably quit
                Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
                // All other errors (Outdated, Timeout) should be resolved by the next frame
                Err(e) => eprintln!("{:?}", e),
            }
        }

        // ... at the end of the WindowEvent block
        Event::AboutToWait => {
            // RedrawRequested will only trigger once unless we manually
            // request it.
            state.window().request_redraw();
        }
        // ...
    }
});

does not seem to match the source linked at the end of the chapter. Furthermore, when attempting to use the above code, I get an error stating the closure in run() is expected to take 2 arguments, not the 3 shown in the example above, WindowEvent::RedrawRequested() throws an error stating "this pattern has 1 field, but the corresponding tuple struct has 0 fields", and Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit, gives the error "failed to resolve: use of undeclared type 'ControlFlow' use of undeclared type 'ControlFlow'". Could further clarification be provided as to how this block of code is supposed to be correctly integrated into the renderer? Or was this just an error resulting from you using a different version of winit than what the source uses?

Croug commented 1 month ago

Yeah this needs to be corrected, they probably just haven't gotten around to updating the site yet. the relevant changes are listed below.

the closure passed into event_loop.run only takes two arguments, eliminate the _, and you're good to go. The entire match arm for RedrawRequested can be simplified to WindowEvent::RedrawRequested => { In the state.render() match block, replace *control_flow = ControlFlow::Exit with control_flow.exit()

Hope this helps!