如何以稳定的帧速率运行 winit 应用程序?

问题描述 投票:0回答:1

我正在尝试使用 winit Rust 箱每隔一段时间更新一次应用程序。我认为我正确设置了控制流,但我似乎没有以 60 FPS 接收到任何事件。

我在 Windows 11 上使用 winit 0.30.5 运行 Cargo 1.79.0。

这是我的代码,基于 docs.rs 上找到的 winit 示例代码:

use std::time::{Duration, Instant};

use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::window::{Window, WindowId};

const NANOS_PER_SEC: u64 = 1_000_000_000;

#[derive(Default)]
struct App {
    window: Option<Window>,
    t: Option<std::time::Instant>,
}

impl ApplicationHandler for App {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        self.window = Some(event_loop.create_window(Window::default_attributes()).unwrap());
    }

    fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
        match event {
            WindowEvent::CloseRequested => {
                println!("The close button was pressed; stopping");
                event_loop.exit();
            },
            WindowEvent::RedrawRequested => {
                // Redraw the application.
                //
                // It's preferable for applications that do not render continuously to render in
                // this event rather than in AboutToWait, since rendering in here allows
                // the program to gracefully handle redraws requested by the OS.

                // Draw.

                if let Some(t) = self.t {
                    println!("FPS: {:>6.2}", 1000.0 / t.elapsed().as_millis() as f64);
                }
                self.t = Some(std::time::Instant::now());

                // Queue a RedrawRequested event.
                //
                // You only need to call this if you've determined that you need to redraw in
                // applications which do not always need to. Applications that redraw continuously
                // can render here instead.
                self.window.as_ref().unwrap().request_redraw();
            }
            _ => ()
        }
    }
}

fn main() {
    let event_loop = EventLoop::new().unwrap();

    event_loop.set_control_flow(ControlFlow::WaitUntil(Instant::now() + Duration::from_nanos(NANOS_PER_SEC / 60)));

    let mut app = App::default();
    event_loop.run_app(&mut app).unwrap();
}

基本上,我需要向 ApplicationHandler 实现添加什么才能每秒运行某些代码 60 次?

rust window winit
1个回答
0
投票

找不到任何延迟请求重画功能,实现方法应该是不断排队重画请求并检查上次重画时间,看看是否到了重画时间

use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::window::{Window, WindowId};

const DT_FPS_60_NANO: u128 = 1_000_000_000 / 60;

struct App {
    window: Option<Window>,
    last_redraw: std::time::Instant,
}

impl ApplicationHandler for App {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        self.window = Some(
            event_loop
                .create_window(Window::default_attributes())
                .unwrap(),
        );
    }

    fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
        match event {
            WindowEvent::CloseRequested => {
                println!("The close button was pressed; stopping");
                event_loop.exit();
            }
            WindowEvent::RedrawRequested => {
                // Queue another RedrawRequest
                self.window.as_ref().unwrap().request_redraw();

                // Check if should redraw
                if self.last_redraw.elapsed().as_nanos() > DT_FPS_60_NANO {
                    // actually redraw
                    println!("FPS: {:>6.2}", 1000.0 / self.last_redraw.elapsed().as_millis() as f64);
                    // record the last redraw time
                    self.last_redraw = std::time::Instant::now();
                }
            }
            _ => {}
        }
    }
}

fn main() {
    let event_loop = EventLoop::new().unwrap();
    let mut app = App {
        window: None,
        last_redraw: std::time::Instant::now(),
    };
    event_loop.set_control_flow(ControlFlow::Poll);
    event_loop.run_app(&mut app).unwrap();
}
© www.soinside.com 2019 - 2024. All rights reserved.