press esc to change bg color
This commit is contained in:
parent
a6fa240e5d
commit
49178e89c9
2 changed files with 39 additions and 8 deletions
|
@ -78,7 +78,7 @@ impl Vertex {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const CUBE_VERTICES: &[Vertex] = &[
|
static CUBE_VERTICES: &[Vertex] = &[
|
||||||
Vertex {
|
Vertex {
|
||||||
position: [-0.5, -0.5, 0.5],
|
position: [-0.5, -0.5, 0.5],
|
||||||
normal: [0.0, 0.0, 1.0],
|
normal: [0.0, 0.0, 1.0],
|
||||||
|
@ -235,6 +235,7 @@ pub struct WgpuCtx<'window> {
|
||||||
uniform_buffer: wgpu::Buffer,
|
uniform_buffer: wgpu::Buffer,
|
||||||
vertex_buffer: wgpu::Buffer,
|
vertex_buffer: wgpu::Buffer,
|
||||||
start_time: Instant,
|
start_time: Instant,
|
||||||
|
bg_color: wgpu::Color,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'window> WgpuCtx<'window> {
|
impl<'window> WgpuCtx<'window> {
|
||||||
|
@ -346,6 +347,12 @@ impl<'window> WgpuCtx<'window> {
|
||||||
render_pipeline,
|
render_pipeline,
|
||||||
uniform_buffer,
|
uniform_buffer,
|
||||||
vertex_buffer,
|
vertex_buffer,
|
||||||
|
bg_color: wgpu::Color {
|
||||||
|
r: 0.1,
|
||||||
|
g: 0.1,
|
||||||
|
b: 0.1,
|
||||||
|
a: 1.0,
|
||||||
|
},
|
||||||
start_time: Instant::now(),
|
start_time: Instant::now(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -362,7 +369,7 @@ impl<'window> WgpuCtx<'window> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&mut self) {
|
pub fn draw(&mut self) {
|
||||||
let elapsed = self.start_time.elapsed().as_secs_f32();
|
let elapsed = self.start_time.elapsed().as_secs_f32() * 0.80f32;
|
||||||
let model = Matrix4::from_angle_x(Rad(elapsed)) * Matrix4::from_angle_y(Rad(elapsed));
|
let model = Matrix4::from_angle_x(Rad(elapsed)) * Matrix4::from_angle_y(Rad(elapsed));
|
||||||
let view = Matrix4::look_at_rh(
|
let view = Matrix4::look_at_rh(
|
||||||
Point3::new(0.0, 0.0, 3.0),
|
Point3::new(0.0, 0.0, 3.0),
|
||||||
|
@ -399,12 +406,7 @@ impl<'window> WgpuCtx<'window> {
|
||||||
view: &view_texture,
|
view: &view_texture,
|
||||||
resolve_target: None,
|
resolve_target: None,
|
||||||
ops: wgpu::Operations {
|
ops: wgpu::Operations {
|
||||||
load: wgpu::LoadOp::Clear(wgpu::Color {
|
load: wgpu::LoadOp::Clear(self.bg_color),
|
||||||
r: 0.1,
|
|
||||||
g: 0.1,
|
|
||||||
b: 0.1,
|
|
||||||
a: 1.0,
|
|
||||||
}),
|
|
||||||
store: wgpu::StoreOp::Store,
|
store: wgpu::StoreOp::Store,
|
||||||
},
|
},
|
||||||
})],
|
})],
|
||||||
|
@ -428,4 +430,11 @@ impl<'window> WgpuCtx<'window> {
|
||||||
self.queue.submit(Some(encoder.finish()));
|
self.queue.submit(Some(encoder.finish()));
|
||||||
surface_texture.present();
|
surface_texture.present();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn change_bg_color(&mut self, color: wgpu::Color) {
|
||||||
|
self.bg_color = color;
|
||||||
|
}
|
||||||
|
pub fn bg_color(&self) -> wgpu::Color {
|
||||||
|
self.bg_color.clone()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,28 @@ impl ApplicationHandler for App<'_> {
|
||||||
debug!("Window closed, exiting");
|
debug!("Window closed, exiting");
|
||||||
std::process::exit(0)
|
std::process::exit(0)
|
||||||
}
|
}
|
||||||
|
WindowEvent::KeyboardInput { device_id, event, is_synthetic } => {
|
||||||
|
match event.physical_key {
|
||||||
|
winit::keyboard::PhysicalKey::Code(code) => {
|
||||||
|
if event.state.is_pressed() == false {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if code == winit::keyboard::KeyCode::Escape {
|
||||||
|
// event_loop.exit();
|
||||||
|
debug!("Window closed, exiting");
|
||||||
|
if let Some(ctx) = &mut self.ctx {
|
||||||
|
match ctx.bg_color() {
|
||||||
|
wgpu::Color::WHITE => ctx.change_bg_color(wgpu::Color::BLACK),
|
||||||
|
wgpu::Color::BLACK => ctx.change_bg_color(wgpu::Color::WHITE),
|
||||||
|
_ => ctx.change_bg_color(wgpu::Color::WHITE),
|
||||||
|
}
|
||||||
|
// std::process::exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
WindowEvent::RedrawRequested => {
|
WindowEvent::RedrawRequested => {
|
||||||
if let Some(ctx) = &mut self.ctx {
|
if let Some(ctx) = &mut self.ctx {
|
||||||
ctx.draw();
|
ctx.draw();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue