Device manager

This commit is contained in:
Dane Johnson 2022-04-07 12:19:55 -05:00
parent 9fc694f1ff
commit 702232d994
2 changed files with 54 additions and 13 deletions

31
src/device.rs Normal file
View File

@ -0,0 +1,31 @@
use std::collections::HashMap;
use glium::glutin;
use glutin::event::*;
pub struct DeviceManager {
key_map: HashMap<VirtualKeyCode, ElementState>
}
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);
}
}

View File

@ -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::<f32>(1.0, 45.0_f32.to_radians(), 0.2, 100.0);
let mvp = glm::translate::<f32>(&mvp, &glm::vec3(0.0, 0.0, -10.0 + frames as f32 * 0.01));
let mvp = glm::translate::<f32>(&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);
},
_ => (),
};
});
}