mirror of
https://github.com/hannobraun/Fornjot
synced 2025-02-11 17:56:00 +00:00
Move main loop to dedicated module
This commit is contained in:
parent
7a2849d42a
commit
e5e58704dd
@ -3,31 +3,19 @@ mod camera;
|
|||||||
mod config;
|
mod config;
|
||||||
mod graphics;
|
mod graphics;
|
||||||
mod input;
|
mod input;
|
||||||
|
mod run;
|
||||||
mod window;
|
mod window;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use fj_export::export;
|
use fj_export::export;
|
||||||
use fj_host::{Model, Parameters};
|
use fj_host::{Model, Parameters};
|
||||||
use fj_operations::shape_processor::ShapeProcessor;
|
use fj_operations::shape_processor::ShapeProcessor;
|
||||||
use futures::executor::block_on;
|
|
||||||
use tracing::{trace, warn};
|
|
||||||
use tracing_subscriber::fmt::format;
|
use tracing_subscriber::fmt::format;
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
use winit::{
|
|
||||||
event::{Event, WindowEvent},
|
|
||||||
event_loop::{ControlFlow, EventLoop},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{args::Args, config::Config, run::run};
|
||||||
args::Args,
|
|
||||||
camera::Camera,
|
|
||||||
config::Config,
|
|
||||||
graphics::{DrawConfig, Renderer},
|
|
||||||
window::Window,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
// Respect `RUST_LOG`. If that's not defined or erroneous, log warnings and
|
// Respect `RUST_LOG`. If that's not defined or erroneous, log warnings and
|
||||||
@ -72,133 +60,7 @@ fn main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let watcher = model.load_and_watch(parameters)?;
|
let watcher = model.load_and_watch(parameters)?;
|
||||||
|
run(watcher, shape_processor)?;
|
||||||
|
|
||||||
let event_loop = EventLoop::new();
|
Ok(())
|
||||||
let window = Window::new(&event_loop);
|
|
||||||
|
|
||||||
let mut previous_time = Instant::now();
|
|
||||||
|
|
||||||
let mut input_handler = input::Handler::new(previous_time);
|
|
||||||
let mut renderer = block_on(Renderer::new(&window))?;
|
|
||||||
|
|
||||||
let mut draw_config = DrawConfig::default();
|
|
||||||
|
|
||||||
let mut shape = None;
|
|
||||||
let mut camera = None;
|
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
|
||||||
trace!("Handling event: {:?}", event);
|
|
||||||
|
|
||||||
let mut actions = input::Actions::new();
|
|
||||||
|
|
||||||
let now = Instant::now();
|
|
||||||
|
|
||||||
if let Some(new_shape) = watcher.receive() {
|
|
||||||
let new_shape = shape_processor.process(&new_shape);
|
|
||||||
renderer.update_geometry(
|
|
||||||
(&new_shape.mesh).into(),
|
|
||||||
(&new_shape.debug_info).into(),
|
|
||||||
new_shape.aabb,
|
|
||||||
);
|
|
||||||
|
|
||||||
if camera.is_none() {
|
|
||||||
camera = Some(Camera::new(&new_shape.aabb));
|
|
||||||
}
|
|
||||||
|
|
||||||
shape = Some(new_shape);
|
|
||||||
}
|
|
||||||
|
|
||||||
match event {
|
|
||||||
Event::WindowEvent {
|
|
||||||
event: WindowEvent::CloseRequested,
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
*control_flow = ControlFlow::Exit;
|
|
||||||
}
|
|
||||||
Event::WindowEvent {
|
|
||||||
event: WindowEvent::Resized(size),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
renderer.handle_resize(size);
|
|
||||||
}
|
|
||||||
Event::WindowEvent {
|
|
||||||
event: WindowEvent::KeyboardInput { input, .. },
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
input_handler.handle_keyboard_input(input, &mut actions);
|
|
||||||
}
|
|
||||||
Event::WindowEvent {
|
|
||||||
event: WindowEvent::CursorMoved { position, .. },
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
if let Some(camera) = &mut camera {
|
|
||||||
input_handler
|
|
||||||
.handle_cursor_moved(position, camera, &window);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Event::WindowEvent {
|
|
||||||
event: WindowEvent::MouseInput { state, button, .. },
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
if let (Some(shape), Some(camera)) = (&shape, &camera) {
|
|
||||||
let focus_point = camera.focus_point(
|
|
||||||
&window,
|
|
||||||
input_handler.cursor(),
|
|
||||||
&shape.mesh,
|
|
||||||
);
|
|
||||||
|
|
||||||
input_handler.handle_mouse_input(
|
|
||||||
button,
|
|
||||||
state,
|
|
||||||
focus_point,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Event::WindowEvent {
|
|
||||||
event: WindowEvent::MouseWheel { delta, .. },
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
input_handler.handle_mouse_wheel(delta, now);
|
|
||||||
}
|
|
||||||
Event::MainEventsCleared => {
|
|
||||||
let delta_t = now.duration_since(previous_time);
|
|
||||||
previous_time = now;
|
|
||||||
|
|
||||||
if let (Some(shape), Some(camera)) = (&shape, &mut camera) {
|
|
||||||
input_handler.update(
|
|
||||||
delta_t.as_secs_f64(),
|
|
||||||
now,
|
|
||||||
camera,
|
|
||||||
&window,
|
|
||||||
&shape.mesh,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
window.inner().request_redraw();
|
|
||||||
}
|
|
||||||
Event::RedrawRequested(_) => {
|
|
||||||
if let (Some(shape), Some(camera)) = (&shape, &mut camera) {
|
|
||||||
camera.update_planes(&shape.aabb);
|
|
||||||
|
|
||||||
if let Err(err) = renderer.draw(camera, &draw_config) {
|
|
||||||
warn!("Draw error: {}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
if actions.exit {
|
|
||||||
*control_flow = ControlFlow::Exit;
|
|
||||||
}
|
|
||||||
if actions.toggle_model {
|
|
||||||
draw_config.draw_model = !draw_config.draw_model;
|
|
||||||
}
|
|
||||||
if actions.toggle_mesh {
|
|
||||||
draw_config.draw_mesh = !draw_config.draw_mesh;
|
|
||||||
}
|
|
||||||
if actions.toggle_debug {
|
|
||||||
draw_config.draw_debug = !draw_config.draw_debug;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
151
fj-app/src/run.rs
Normal file
151
fj-app/src/run.rs
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use fj_host::Watcher;
|
||||||
|
use fj_operations::shape_processor::ShapeProcessor;
|
||||||
|
use futures::executor::block_on;
|
||||||
|
use tracing::{trace, warn};
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::{ControlFlow, EventLoop},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
camera::Camera,
|
||||||
|
graphics::{self, DrawConfig, Renderer},
|
||||||
|
input,
|
||||||
|
window::Window,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn run(
|
||||||
|
watcher: Watcher,
|
||||||
|
shape_processor: ShapeProcessor,
|
||||||
|
) -> Result<(), graphics::InitError> {
|
||||||
|
let event_loop = EventLoop::new();
|
||||||
|
let window = Window::new(&event_loop);
|
||||||
|
|
||||||
|
let mut previous_time = Instant::now();
|
||||||
|
|
||||||
|
let mut input_handler = input::Handler::new(previous_time);
|
||||||
|
let mut renderer = block_on(Renderer::new(&window))?;
|
||||||
|
|
||||||
|
let mut draw_config = DrawConfig::default();
|
||||||
|
|
||||||
|
let mut shape = None;
|
||||||
|
let mut camera = None;
|
||||||
|
|
||||||
|
event_loop.run(move |event, _, control_flow| {
|
||||||
|
trace!("Handling event: {:?}", event);
|
||||||
|
|
||||||
|
let mut actions = input::Actions::new();
|
||||||
|
|
||||||
|
let now = Instant::now();
|
||||||
|
|
||||||
|
if let Some(new_shape) = watcher.receive() {
|
||||||
|
let new_shape = shape_processor.process(&new_shape);
|
||||||
|
renderer.update_geometry(
|
||||||
|
(&new_shape.mesh).into(),
|
||||||
|
(&new_shape.debug_info).into(),
|
||||||
|
new_shape.aabb,
|
||||||
|
);
|
||||||
|
|
||||||
|
if camera.is_none() {
|
||||||
|
camera = Some(Camera::new(&new_shape.aabb));
|
||||||
|
}
|
||||||
|
|
||||||
|
shape = Some(new_shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CloseRequested,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
*control_flow = ControlFlow::Exit;
|
||||||
|
}
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::Resized(size),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
renderer.handle_resize(size);
|
||||||
|
}
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::KeyboardInput { input, .. },
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
input_handler.handle_keyboard_input(input, &mut actions);
|
||||||
|
}
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CursorMoved { position, .. },
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if let Some(camera) = &mut camera {
|
||||||
|
input_handler
|
||||||
|
.handle_cursor_moved(position, camera, &window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::MouseInput { state, button, .. },
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if let (Some(shape), Some(camera)) = (&shape, &camera) {
|
||||||
|
let focus_point = camera.focus_point(
|
||||||
|
&window,
|
||||||
|
input_handler.cursor(),
|
||||||
|
&shape.mesh,
|
||||||
|
);
|
||||||
|
|
||||||
|
input_handler.handle_mouse_input(
|
||||||
|
button,
|
||||||
|
state,
|
||||||
|
focus_point,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::MouseWheel { delta, .. },
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
input_handler.handle_mouse_wheel(delta, now);
|
||||||
|
}
|
||||||
|
Event::MainEventsCleared => {
|
||||||
|
let delta_t = now.duration_since(previous_time);
|
||||||
|
previous_time = now;
|
||||||
|
|
||||||
|
if let (Some(shape), Some(camera)) = (&shape, &mut camera) {
|
||||||
|
input_handler.update(
|
||||||
|
delta_t.as_secs_f64(),
|
||||||
|
now,
|
||||||
|
camera,
|
||||||
|
&window,
|
||||||
|
&shape.mesh,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.inner().request_redraw();
|
||||||
|
}
|
||||||
|
Event::RedrawRequested(_) => {
|
||||||
|
if let (Some(shape), Some(camera)) = (&shape, &mut camera) {
|
||||||
|
camera.update_planes(&shape.aabb);
|
||||||
|
|
||||||
|
if let Err(err) = renderer.draw(camera, &draw_config) {
|
||||||
|
warn!("Draw error: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if actions.exit {
|
||||||
|
*control_flow = ControlFlow::Exit;
|
||||||
|
}
|
||||||
|
if actions.toggle_model {
|
||||||
|
draw_config.draw_model = !draw_config.draw_model;
|
||||||
|
}
|
||||||
|
if actions.toggle_mesh {
|
||||||
|
draw_config.draw_mesh = !draw_config.draw_mesh;
|
||||||
|
}
|
||||||
|
if actions.toggle_debug {
|
||||||
|
draw_config.draw_debug = !draw_config.draw_debug;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user