|
1 | 1 | use baseview::{MouseEvent, Size, Window, WindowHandler, WindowOpenOptions};
|
2 |
| -use wgpu::{util::DeviceExt, Buffer, Device, Queue, RenderPipeline, Surface}; |
| 2 | +use wgpu::{util::DeviceExt, Buffer, Device, Queue, RenderPipeline, Surface, SurfaceCapabilities}; |
3 | 3 |
|
4 | 4 | struct WgpuExample {
|
5 | 5 | pipeline: RenderPipeline,
|
6 | 6 | device: Device,
|
7 | 7 | surface: Surface,
|
8 | 8 | queue: Queue,
|
9 | 9 | vertex_buffer: Buffer,
|
| 10 | + surface_caps: SurfaceCapabilities, |
10 | 11 | }
|
11 | 12 |
|
12 | 13 | impl<'a> WgpuExample {
|
@@ -49,9 +50,8 @@ impl<'a> WgpuExample {
|
49 | 50 | let config = wgpu::SurfaceConfiguration {
|
50 | 51 | usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
51 | 52 | format: surface_format,
|
52 |
| - // TODO this needs to be twice the window size for some reason |
53 |
| - width: 1024, |
54 |
| - height: 1024, |
| 53 | + width: 512, |
| 54 | + height: 512, |
55 | 55 | present_mode: surface_caps.present_modes[0],
|
56 | 56 | alpha_mode: surface_caps.alpha_modes[0],
|
57 | 57 | view_formats: vec![],
|
@@ -103,7 +103,7 @@ impl<'a> WgpuExample {
|
103 | 103 |
|
104 | 104 | surface.configure(&device, &config);
|
105 | 105 |
|
106 |
| - Self { pipeline, device, surface, queue, vertex_buffer } |
| 106 | + Self { pipeline, device, surface, queue, vertex_buffer, surface_caps } |
107 | 107 | }
|
108 | 108 | }
|
109 | 109 |
|
@@ -143,27 +143,53 @@ impl WindowHandler for WgpuExample {
|
143 | 143 | fn on_event(
|
144 | 144 | &mut self, _window: &mut baseview::Window, event: baseview::Event,
|
145 | 145 | ) -> baseview::EventStatus {
|
146 |
| - if let baseview::Event::Mouse(MouseEvent::CursorMoved { position, modifiers: _ }) = event { |
147 |
| - let center_x: f32 = (position.x as f32 - 256.0) / 256.0; |
148 |
| - let center_y: f32 = (256.0 - position.y as f32) / 256.0; |
149 |
| - let vertices = &[ |
150 |
| - Vertex { position: [center_x, center_y + 0.25, 0.0], color: [1.0, 0.0, 0.0] }, |
151 |
| - Vertex { |
152 |
| - position: [center_x - 0.25, center_y - 0.25, 0.0], |
153 |
| - color: [0.0, 1.0, 0.0], |
154 |
| - }, |
155 |
| - Vertex { |
156 |
| - position: [center_x + 0.25, center_y - 0.25, 0.0], |
157 |
| - color: [0.0, 0.0, 1.0], |
158 |
| - }, |
159 |
| - ]; |
160 |
| - let vertex_buffer = self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor { |
161 |
| - label: Some("Vertex Buffer"), |
162 |
| - contents: bytemuck::cast_slice(vertices), |
163 |
| - usage: wgpu::BufferUsages::VERTEX, |
164 |
| - }); |
165 |
| - |
166 |
| - self.vertex_buffer = vertex_buffer; |
| 146 | + match event { |
| 147 | + baseview::Event::Mouse(MouseEvent::CursorMoved { position, modifiers: _ }) => { |
| 148 | + let center_x: f32 = (position.x as f32 - 256.0) / 256.0; |
| 149 | + let center_y: f32 = (256.0 - position.y as f32) / 256.0; |
| 150 | + let vertices = &[ |
| 151 | + Vertex { position: [center_x, center_y + 0.25, 0.0], color: [1.0, 0.0, 0.0] }, |
| 152 | + Vertex { |
| 153 | + position: [center_x - 0.25, center_y - 0.25, 0.0], |
| 154 | + color: [0.0, 1.0, 0.0], |
| 155 | + }, |
| 156 | + Vertex { |
| 157 | + position: [center_x + 0.25, center_y - 0.25, 0.0], |
| 158 | + color: [0.0, 0.0, 1.0], |
| 159 | + }, |
| 160 | + ]; |
| 161 | + let vertex_buffer = |
| 162 | + self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor { |
| 163 | + label: Some("Vertex Buffer"), |
| 164 | + contents: bytemuck::cast_slice(vertices), |
| 165 | + usage: wgpu::BufferUsages::VERTEX, |
| 166 | + }); |
| 167 | + |
| 168 | + self.vertex_buffer = vertex_buffer; |
| 169 | + } |
| 170 | + baseview::Event::Window(baseview::WindowEvent::Resized(size)) => { |
| 171 | + let surface_format = self |
| 172 | + .surface_caps |
| 173 | + .formats |
| 174 | + .iter() |
| 175 | + .copied() |
| 176 | + .find(|f| f.is_srgb()) |
| 177 | + .unwrap_or(self.surface_caps.formats[0]); |
| 178 | + |
| 179 | + self.surface.configure( |
| 180 | + &self.device, |
| 181 | + &wgpu::SurfaceConfiguration { |
| 182 | + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, |
| 183 | + format: surface_format, |
| 184 | + width: size.physical_size().width, |
| 185 | + height: size.physical_size().height, |
| 186 | + present_mode: self.surface_caps.present_modes[0], |
| 187 | + alpha_mode: self.surface_caps.alpha_modes[0], |
| 188 | + view_formats: vec![], |
| 189 | + }, |
| 190 | + ) |
| 191 | + } |
| 192 | + _ => {} |
167 | 193 | }
|
168 | 194 | baseview::EventStatus::Captured
|
169 | 195 | }
|
|
0 commit comments