diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs index 6303919..3c7f20d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,14 +12,14 @@ use winit::{ window::{Window, WindowId}, }; +mod graphics; + +use graphics::{RenderResult, Renderer}; + +#[derive(Default)] struct App { window: Option<Arc<Window>>, -} - -impl App { - fn new() -> Self { - Self { window: None } - } + renderer: Option<Renderer>, } impl ApplicationHandler for App { @@ -29,6 +29,7 @@ impl ApplicationHandler for App { event_loop.set_control_flow(ControlFlow::Poll); let attributes = Window::default_attributes() + .with_title("君は呪われている!") .with_inner_size(LogicalSize::new(1280, 720)) .with_fullscreen(None); @@ -37,7 +38,17 @@ impl ApplicationHandler for App { Err(e) => panic!("error: {e}"), }; + let renderer = pollster::block_on(Renderer::new( + Box::new(event_loop.owned_display_handle()), + window.clone(), + )); + let renderer = match renderer { + Ok(v) => v, + Err(e) => panic!("error: {e}"), + }; + self.window = Some(window); + self.renderer = Some(renderer); } fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) { @@ -73,14 +84,29 @@ impl ApplicationHandler for App { fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { let start = Instant::now(); - let Some(_) = &self.window else { + let (Some(window), Some(renderer)) = (&self.window, &self.renderer) else { // NOTE: `window`が`Some`になる`resumed`メソッドで`ControlFlow::Poll`を指定しているため、 // ここで`ControlFlow::WaitUntil`を指定しても問題ない。 event_loop.set_control_flow(ControlFlow::WaitUntil(start + Duration::from_millis(16))); return; }; - sync_60fps(start); + // NOTE: Presentation modeがFIFOの場合はここでスレッドが停止する。 + let current_texture = match renderer.get_current_texture(window) { + RenderResult::Ok(v) => v, + RenderResult::ShouldSkip => { + event_loop + .set_control_flow(ControlFlow::WaitUntil(start + Duration::from_millis(16))); + return; + } + RenderResult::Lost => panic!("fatal: device or surface lost."), + }; + + renderer.render(window, current_texture); + + if !renderer.is_presentation_mode_fifo() { + sync_60fps(start); + } } } @@ -114,7 +140,7 @@ fn main() { Err(e) => panic!("error: {e}"), }; - match event_loop.run_app(&mut App::new()) { + match event_loop.run_app(&mut App::default()) { Ok(()) => {} Err(e) => panic!("error: {e}"), } |
