This commit is contained in:
2019-07-25 16:38:42 +02:00
parent 007e158410
commit 767efab3d4
4 changed files with 111 additions and 145 deletions

View File

@@ -2,28 +2,36 @@ use crate::vulkan::{Vertex, GameData};
use winit::{Event, WindowEvent, ElementState};
use std::time::SystemTime;
use std::iter::FromIterator;
use cgmath::{Matrix4, SquareMatrix, Rad, Point3, Vector3};
mod vulkan;
const PRINT_KEYBOARD_INPUT: bool = false;
impl GameData<'_> {
/// Returns true if event should be ignored by the vulkan handler
fn on_window_event(self: &mut Self, event: &Event) -> bool {
match event {
Event::WindowEvent { event: WindowEvent::KeyboardInput { device_id, input }, .. } => {
let mods = String::from_iter(
vec!["shift", "ctrl", "alt", "logo"].iter()
.zip(vec![input.modifiers.shift, input.modifiers.ctrl, input.modifiers.alt, input.modifiers.logo])
.filter(|(&_name, state)| *state)
.map(|(&name, _state)| name));
if mods.len() > 0 {
println!("Keyboard {:?} input {:?} {:?} + {:?}", device_id, input.state, &mods, input.scancode)
} else {
println!("Keyboard {:?} input {:?} {:?}", device_id, input.state, input.scancode)
if PRINT_KEYBOARD_INPUT {
let mods = String::from_iter(
vec!["shift", "ctrl", "alt", "logo"].iter()
.zip(vec![input.modifiers.shift, input.modifiers.ctrl, input.modifiers.alt, input.modifiers.logo])
.filter(|(&_name, state)| *state)
.map(|(&name, _state)| name));
if mods.len() > 0 {
println!("Keyboard {:?} input {:?} {:?} + {:?}", device_id, input.state, &mods, input.scancode)
} else {
println!("Keyboard {:?} input {:?} {:?}", device_id, input.state, input.scancode)
}
}
if input.state == ElementState::Released && input.modifiers.ctrl && input.scancode == 19 {
self.recreate_pipeline = true;
}
if input.state == ElementState::Released && input.scancode == 1 {
self.shutdown = true;
}
}
_ => {}
}
@@ -32,17 +40,29 @@ impl GameData<'_> {
fn update_push_constants(self: &mut Self) {
self.push_constants.time = self.start_time.elapsed().unwrap().as_millis() as f32 / 1000.0;
self.push_constants.model = Matrix4::identity();
self.push_constants.view = Matrix4::look_at(Point3::new(f32::sin(self.push_constants.time), 0.0, f32::cos(self.push_constants.time)), Point3::new(0.0, 0.0, 0.0), Vector3::new(0.0, 1.0, 0.0));
self.push_constants.projection = cgmath::perspective(Rad(std::f32::consts::FRAC_PI_2), self.aspect_ratio, 0.01, 100.0);
}
}
#[derive(Debug, Clone, Copy)]
pub struct PushConstants {
pub time: f32
pub time: f32,
pub model: Matrix4<f32>,
pub view: Matrix4<f32>,
pub projection: Matrix4<f32>,
}
fn main() {
let mut whatever = String::new();
std::io::stdin().read_line(&mut whatever).unwrap();
let mut pc = PushConstants {
time: 0.0
time: 0.0,
model: Matrix4::identity(),
view: Matrix4::identity(),
projection: Matrix4::identity()
};
let data = GameData {
@@ -57,7 +77,9 @@ fn main() {
],
push_constants: &mut pc,
start_time: SystemTime::now(),
recreate_pipeline: false
recreate_pipeline: false,
aspect_ratio: 1.0,
shutdown: false,
};
vulkan::init(data);