Device manager
This commit is contained in:
parent
9fc694f1ff
commit
702232d994
31
src/device.rs
Normal file
31
src/device.rs
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
24
src/main.rs
24
src/main.rs
@ -1,3 +1,5 @@
|
|||||||
|
mod device;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate glium;
|
extern crate glium;
|
||||||
extern crate nalgebra_glm as glm;
|
extern crate nalgebra_glm as glm;
|
||||||
@ -32,14 +34,16 @@ fn main() {
|
|||||||
let fragment_shader_src = include_str!("flat.frag");
|
let fragment_shader_src = include_str!("flat.frag");
|
||||||
let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap();
|
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| {
|
events_loop.run(move |ev, _, control_flow| {
|
||||||
|
if device_manager.is_pressed(&glutin::event::VirtualKeyCode::W) {
|
||||||
frames += 1;
|
offset += 0.001;
|
||||||
|
}
|
||||||
let mvp = glm::perspective::<f32>(1.0, 45.0_f32.to_radians(), 0.2, 100.0);
|
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! {
|
let uniforms = uniform! {
|
||||||
MVP: *mvp.as_ref(),
|
MVP: *mvp.as_ref(),
|
||||||
};
|
};
|
||||||
@ -54,10 +58,16 @@ fn main() {
|
|||||||
let next_frame_time = std::time::Instant::now() +
|
let next_frame_time = std::time::Instant::now() +
|
||||||
std::time::Duration::from_nanos(16_666_667);
|
std::time::Duration::from_nanos(16_666_667);
|
||||||
*control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);
|
*control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);
|
||||||
if let glutin::event::Event::WindowEvent { event, .. } = ev {
|
match ev {
|
||||||
|
glutin::event::Event::WindowEvent { event, .. } => {
|
||||||
if event == glutin::event::WindowEvent::CloseRequested {
|
if event == glutin::event::WindowEvent::CloseRequested {
|
||||||
*control_flow = glutin::event_loop::ControlFlow::Exit;
|
*control_flow = glutin::event_loop::ControlFlow::Exit;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
glutin::event::Event::DeviceEvent { event, .. } => {
|
||||||
|
device_manager.handle(event);
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user