Skip to content

Commit 2174d23

Browse files
andrew-r-thomasprokopyl
authored andcommitted
fixed window size issue
1 parent 03b60d5 commit 2174d23

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

examples/render_wgpu/src/main.rs

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
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};
33

44
struct WgpuExample {
55
pipeline: RenderPipeline,
66
device: Device,
77
surface: Surface,
88
queue: Queue,
99
vertex_buffer: Buffer,
10+
surface_caps: SurfaceCapabilities,
1011
}
1112

1213
impl<'a> WgpuExample {
@@ -49,9 +50,8 @@ impl<'a> WgpuExample {
4950
let config = wgpu::SurfaceConfiguration {
5051
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
5152
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,
5555
present_mode: surface_caps.present_modes[0],
5656
alpha_mode: surface_caps.alpha_modes[0],
5757
view_formats: vec![],
@@ -103,7 +103,7 @@ impl<'a> WgpuExample {
103103

104104
surface.configure(&device, &config);
105105

106-
Self { pipeline, device, surface, queue, vertex_buffer }
106+
Self { pipeline, device, surface, queue, vertex_buffer, surface_caps }
107107
}
108108
}
109109

@@ -143,27 +143,53 @@ impl WindowHandler for WgpuExample {
143143
fn on_event(
144144
&mut self, _window: &mut baseview::Window, event: baseview::Event,
145145
) -> 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+
_ => {}
167193
}
168194
baseview::EventStatus::Captured
169195
}

examples/render_wgpu/src/shader.wgsl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// Vertex shader
2-
31
struct VertexInput {
42
@location(0) position: vec3<f32>,
53
@location(1) color: vec3<f32>,
@@ -20,8 +18,6 @@ fn vs_main(
2018
return out;
2119
}
2220

23-
// Fragment shader
24-
2521
@fragment
2622
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
2723
return vec4<f32>(in.color, 1.0);

0 commit comments

Comments
 (0)