From 702232d9941d73972c348bd43af36ecf887e20d8 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Thu, 7 Apr 2022 12:19:55 -0500 Subject: [PATCH] Device manager --- src/device.rs | 31 +++++++++++++++++++++++++++++++ src/main.rs | 36 +++++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 src/device.rs diff --git a/src/device.rs b/src/device.rs new file mode 100644 index 0000000..71e55ae --- /dev/null +++ b/src/device.rs @@ -0,0 +1,31 @@ +use std::collections::HashMap; + +use glium::glutin; +use glutin::event::*; + +pub struct DeviceManager { + key_map: HashMap +} + +impl DeviceManager { + pub fn handle(&mut self, event: DeviceEvent) { + match event { + DeviceEvent::Key( + KeyboardInput{ virtual_keycode: Some(keycode), state, .. } + ) => self.update(keycode, state), + _ => (), + }; + } + pub fn new() -> Self { + DeviceManager { + key_map: HashMap::new(), + } + } + pub fn is_pressed(&self, keycode: &VirtualKeyCode) -> bool { + matches!(self.key_map.get(keycode), Some(ElementState::Pressed)) + } + fn update(&mut self, keycode: VirtualKeyCode, state: ElementState) { + self.key_map.insert(keycode, state); + } + +} diff --git a/src/main.rs b/src/main.rs index e32a687..abd980a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +mod device; + #[macro_use] extern crate glium; extern crate nalgebra_glm as glm; @@ -20,9 +22,9 @@ fn main() { let display = glium::Display::new(wb, cb, &events_loop).unwrap(); - let vertex1 = Vertex { position: [-0.5, -0.5, 0.0], color: [1.0, 0.0, 0.0] }; - let vertex2 = Vertex { position: [ 0.0, 0.5, 0.0], color: [0.0, 1.0, 0.0] }; - let vertex3 = Vertex { position: [ 0.5, -0.25, 0.0], color: [0.0, 0.0, 1.0]}; + let vertex1 = Vertex { position: [-0.5, -0.5 , 0.0], color: [1.0, 0.0, 0.0] }; + let vertex2 = Vertex { position: [ 0.0, 0.5 , 0.0], color: [0.0, 1.0, 0.0] }; + let vertex3 = Vertex { position: [ 0.5, -0.25, 0.0], color: [0.0, 0.0, 1.0] }; let shape = vec![vertex1, vertex2, vertex3]; let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap(); @@ -32,14 +34,16 @@ fn main() { let fragment_shader_src = include_str!("flat.frag"); let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap(); - let mut frames = 0; + let mut device_manager = crate::device::DeviceManager::new(); + + let mut offset = 0.0; events_loop.run(move |ev, _, control_flow| { - - frames += 1; - + if device_manager.is_pressed(&glutin::event::VirtualKeyCode::W) { + offset += 0.001; + } let mvp = glm::perspective::(1.0, 45.0_f32.to_radians(), 0.2, 100.0); - let mvp = glm::translate::(&mvp, &glm::vec3(0.0, 0.0, -10.0 + frames as f32 * 0.01)); + let mvp = glm::translate::(&mvp, &glm::vec3(0.0, 0.0, -10.0 + offset)); let uniforms = uniform! { MVP: *mvp.as_ref(), }; @@ -54,10 +58,16 @@ fn main() { let next_frame_time = std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667); *control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time); - if let glutin::event::Event::WindowEvent { event, .. } = ev { - if event == glutin::event::WindowEvent::CloseRequested { - *control_flow = glutin::event_loop::ControlFlow::Exit; - } - } + match ev { + glutin::event::Event::WindowEvent { event, .. } => { + if event == glutin::event::WindowEvent::CloseRequested { + *control_flow = glutin::event_loop::ControlFlow::Exit; + } + }, + glutin::event::Event::DeviceEvent { event, .. } => { + device_manager.handle(event); + }, + _ => (), + }; }); }