[英]Set frame redraw rate in glium?
我正在嘗試在 rust 中使用 glium 制作游戲循環。 我的目標是讓屏幕每秒重繪 60 次。 使用我擁有的當前事件循環代碼,只有在 window 大小發生變化時才會重繪框架。 我在 glutin docs 中讀到,我需要在某個地方調用 request_redraw,但我不確定如何/在哪里。 到目前為止,這是我的代碼:
event_loop.run(move |event, _target, control_flow| match event {
Event::LoopDestroyed => return,
Event::WindowEvent {
window_id: _window_id,
event: winevent,
} => match winevent {
WindowEvent::Resized(physical_size) => display.gl_window().resize(physical_size),
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
_ => {}
},
Event::RedrawRequested(_window_id) => {
let mut target = display.draw();
target.clear_color_srgb(rng.gen(), rng.gen(), rng.gen(), 1.0);
target.finish().unwrap();
}
_ => {}
});
我以前沒有使用glium
(我已經有一段時間直接從Vulkano
制作了一些圖形應用程序)。 但是,仔細閱讀 API,您似乎可以通過一系列 api 從winit
獲取 Window 句柄。 我在下面的代碼中概述了它們。 像下面這樣的東西應該適合你。 關鍵是從winit
訪問Window
句柄。 滾動瀏覽Window
API 您應該看到: request_redraw 。 然后,您可以在事件處理程序周圍插入游戲循環邏輯,如下所示:
use std::time::Instant;
use glium::Display;
use winit::event_loop::{EventLoop, ControlFlow};
use winit::event::{Event, WindowEvent};
use winit::window::Window;
const TARGET_FPS: u64 = 60;
/* ... some function for main loop ... */
let display: Display = ... /* glium Display instance */
event_loop.run(move |event, _target, control_flow| {
let start_time = Instant::now();
match event {
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
*control_flow = ControlFlow::Exit;
},
...
/*
* Process events here
*/
}
match *control_flow {
ControlFlow::Exit => (),
_ => {
/*
* Grab window handle from the display (untested - based on API)
*/
display.gl_window().window().request_redraw();
/*
* Below logic to attempt hitting TARGET_FPS.
* Basically, sleep for the rest of our milliseconds
*/
let elapsed_time = Instant::now().duration_since(start_time).as_millis() as u64;
let wait_millis = match 1000 / TARGET_FPS >= elapsed_time {
true => 1000 / TARGET_FPS - elapsed_time,
false => 0
};
let new_inst = start_time + std::time::Duration::from_millis(wait_millis);
*control_flow = ControlFlow::WaitUntil(new_inst);
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.