Move more event handling to the render context
This commit is contained in:
parent
9a4bd5d1a0
commit
2a6bee18a6
|
@ -84,51 +84,44 @@ impl App {
|
||||||
let mut cumulative_dt = 0.0;
|
let mut cumulative_dt = 0.0;
|
||||||
let mut frames_accumulated = 0.0;
|
let mut frames_accumulated = 0.0;
|
||||||
let mut last_render_time = Instant::now();
|
let mut last_render_time = Instant::now();
|
||||||
self.event_loop
|
self.event_loop.run(move |event, _, control_flow| {
|
||||||
.run(move |event, _, control_flow| match event {
|
if !self.render_ctx.handle_window_event(&event, control_flow) {
|
||||||
Event::WindowEvent {
|
match event {
|
||||||
ref event,
|
Event::WindowEvent {
|
||||||
window_id,
|
ref event,
|
||||||
} if window_id == self.render_ctx.window.id()
|
window_id,
|
||||||
&& !self.render_ctx.handle_window_event(event) =>
|
} if window_id == self.render_ctx.window.id() => {
|
||||||
{
|
camera_controller.process_events(event);
|
||||||
match event {
|
}
|
||||||
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
Event::RedrawRequested(_) => {
|
||||||
_ => {
|
let now = Instant::now();
|
||||||
camera_controller.process_events(event);
|
let dt = now - last_render_time;
|
||||||
|
last_render_time = now;
|
||||||
|
camera_controller.update(dt);
|
||||||
|
camera_controller.update_buffer(&self.render_ctx);
|
||||||
|
renderer.render(&self.render_ctx);
|
||||||
|
renderer.update(&dt, &self.render_ctx);
|
||||||
|
renderer.update_brickmap(&self.render_ctx, &mut world);
|
||||||
|
|
||||||
|
// Simple framerate tracking
|
||||||
|
self.render_ctx.window.set_title(&format!(
|
||||||
|
"{}: {} fps",
|
||||||
|
self.title,
|
||||||
|
(1.0 / dt.as_secs_f32()).floor()
|
||||||
|
));
|
||||||
|
cumulative_dt += dt.as_secs_f32();
|
||||||
|
frames_accumulated += 1.0;
|
||||||
|
if cumulative_dt >= 1.0 {
|
||||||
|
let fps = frames_accumulated * 1.0 / cumulative_dt;
|
||||||
|
let frame_time = cumulative_dt * 1000.0 / frames_accumulated;
|
||||||
|
log::info!("FPS: {}, Frame Time: {}", fps.floor(), frame_time);
|
||||||
|
cumulative_dt = 0.0;
|
||||||
|
frames_accumulated = 0.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
Event::MainEventsCleared => {
|
}
|
||||||
self.render_ctx.window.request_redraw();
|
});
|
||||||
}
|
|
||||||
Event::RedrawRequested(_) => {
|
|
||||||
let now = Instant::now();
|
|
||||||
let dt = now - last_render_time;
|
|
||||||
last_render_time = now;
|
|
||||||
camera_controller.update(dt);
|
|
||||||
camera_controller.update_buffer(&self.render_ctx);
|
|
||||||
renderer.render(&self.render_ctx);
|
|
||||||
renderer.update(&dt, &self.render_ctx);
|
|
||||||
renderer.update_brickmap(&self.render_ctx, &mut world);
|
|
||||||
|
|
||||||
// Simple framerate tracking
|
|
||||||
self.render_ctx.window.set_title(&format!(
|
|
||||||
"{}: {} fps",
|
|
||||||
self.title,
|
|
||||||
(1.0 / dt.as_secs_f32()).floor()
|
|
||||||
));
|
|
||||||
cumulative_dt += dt.as_secs_f32();
|
|
||||||
frames_accumulated += 1.0;
|
|
||||||
if cumulative_dt >= 1.0 {
|
|
||||||
let fps = frames_accumulated * 1.0 / cumulative_dt;
|
|
||||||
let frame_time = cumulative_dt * 1000.0 / frames_accumulated;
|
|
||||||
log::info!("FPS: {}, Frame Time: {}", fps.floor(), frame_time);
|
|
||||||
cumulative_dt = 0.0;
|
|
||||||
frames_accumulated = 0.0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
use winit::{dpi::PhysicalSize, event::WindowEvent, window::Window};
|
use winit::{
|
||||||
|
dpi::PhysicalSize,
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::ControlFlow,
|
||||||
|
window::Window,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
pub window: Window,
|
pub window: Window,
|
||||||
|
@ -78,14 +83,32 @@ impl Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_window_event(&mut self, event: &WindowEvent) -> bool {
|
pub fn handle_window_event(
|
||||||
|
&mut self,
|
||||||
|
event: &Event<()>,
|
||||||
|
control_flow: &mut ControlFlow,
|
||||||
|
) -> bool {
|
||||||
match event {
|
match event {
|
||||||
WindowEvent::Resized(physical_size) => {
|
Event::WindowEvent {
|
||||||
self.resize(*physical_size);
|
ref event,
|
||||||
true
|
window_id,
|
||||||
}
|
} if *window_id == self.window.id() => match event {
|
||||||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
WindowEvent::CloseRequested => {
|
||||||
self.resize(**new_inner_size);
|
*control_flow = ControlFlow::Exit;
|
||||||
|
true
|
||||||
|
}
|
||||||
|
WindowEvent::Resized(physical_size) => {
|
||||||
|
self.resize(*physical_size);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||||
|
self.resize(**new_inner_size);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
},
|
||||||
|
Event::MainEventsCleared => {
|
||||||
|
self.window.request_redraw();
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
|
|
Loading…
Reference in New Issue