diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/graphics.rs | 22 | ||||
| -rw-r--r-- | src/main.rs | 21 |
2 files changed, 31 insertions, 12 deletions
diff --git a/src/graphics.rs b/src/graphics.rs index d933c8b..6fa13b3 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -2,7 +2,7 @@ use std::{borrow::Cow, sync::Arc}; use wgpu::*; use winit::{dpi::PhysicalSize, event_loop::OwnedDisplayHandle, window::Window}; -pub enum RenderResult { +pub enum AcquireSurfaceTextureResult { Ok(SurfaceTexture), ShouldSkip, Lost, @@ -42,6 +42,9 @@ impl Renderer { .map_err(|e| e.to_string())?; let surface_cap = surface.get_capabilities(&adapter); + if surface_cap.formats.is_empty() { + return Err("no surface format found".to_string()); + } let (surface_format, view_format) = decide_surface_and_view_format(&adapter, &surface_cap); @@ -110,24 +113,25 @@ impl Renderer { Ok(v) } - pub fn get_current_texture(&self, window: &Window) -> RenderResult { + pub fn get_current_texture(&self, window: &Window) -> AcquireSurfaceTextureResult { match self.surface.get_current_texture() { - CurrentSurfaceTexture::Success(v) => RenderResult::Ok(v), + CurrentSurfaceTexture::Success(v) => AcquireSurfaceTextureResult::Ok(v), CurrentSurfaceTexture::Suboptimal(v) => { // NOTE: 取得したテクスチャが生存した状態でサーフェスを再設定すると // `PreviousOutputExists`エラーになるので、 // 破棄した上で再設定し、描画をスキップする。 drop(v); self.configure_surface(window.inner_size()); - RenderResult::ShouldSkip + AcquireSurfaceTextureResult::ShouldSkip } - CurrentSurfaceTexture::Occluded => RenderResult::ShouldSkip, - CurrentSurfaceTexture::Timeout => RenderResult::ShouldSkip, + CurrentSurfaceTexture::Occluded => AcquireSurfaceTextureResult::ShouldSkip, + CurrentSurfaceTexture::Timeout => AcquireSurfaceTextureResult::ShouldSkip, CurrentSurfaceTexture::Outdated => { self.configure_surface(window.inner_size()); - RenderResult::ShouldSkip + AcquireSurfaceTextureResult::ShouldSkip } - CurrentSurfaceTexture::Lost => RenderResult::Lost, + CurrentSurfaceTexture::Lost => AcquireSurfaceTextureResult::Lost, + // TODO: 一応対応する。 CurrentSurfaceTexture::Validation => unreachable!(), } } @@ -158,6 +162,8 @@ impl Renderer { render_pass.set_pipeline(&self.pipeline); + // TODO: 描画 + drop(render_pass); self.queue.submit([encoder.finish()]); diff --git a/src/main.rs b/src/main.rs index 3c7f20d..d78aee7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use winit::{ mod graphics; -use graphics::{RenderResult, Renderer}; +use graphics::{AcquireSurfaceTextureResult, Renderer}; #[derive(Default)] struct App { @@ -30,6 +30,7 @@ impl ApplicationHandler for App { let attributes = Window::default_attributes() .with_title("君は呪われている!") + .with_resizable(false) .with_inner_size(LogicalSize::new(1280, 720)) .with_fullscreen(None); @@ -74,6 +75,17 @@ impl ApplicationHandler for App { WindowEvent::CursorLeft { .. } => { println!("left"); } + WindowEvent::Resized(size) => { + if let Some(renderer) = self.renderer.as_ref() { + renderer.configure_surface(size); + } + } + WindowEvent::ScaleFactorChanged { .. } => { + let (Some(window), Some(renderer)) = (&self.window, &self.renderer) else { + return; + }; + renderer.configure_surface(window.inner_size()); + } WindowEvent::Focused(false) | WindowEvent::Occluded(true) => { println!("focused or occluded"); } @@ -93,13 +105,14 @@ impl ApplicationHandler for App { // NOTE: Presentation modeがFIFOの場合はここでスレッドが停止する。 let current_texture = match renderer.get_current_texture(window) { - RenderResult::Ok(v) => v, - RenderResult::ShouldSkip => { + AcquireSurfaceTextureResult::Ok(v) => v, + AcquireSurfaceTextureResult::ShouldSkip => { event_loop .set_control_flow(ControlFlow::WaitUntil(start + Duration::from_millis(16))); return; } - RenderResult::Lost => panic!("fatal: device or surface lost."), + // TODO: 一応対応する。 + AcquireSurfaceTextureResult::Lost => panic!("fatal: device or surface lost."), }; renderer.render(window, current_texture); |
