Files
rust-engine/src/main.rs
2019-07-28 03:26:50 +02:00

126 lines
4.3 KiB
Rust

use winit::{Event, WindowEvent};
use cgmath::{Matrix4, Rad, Point3, Vector3, Deg};
mod vulkan;
use crate::vulkan::{GameData, Game, LinePoint};
mod input;
use crate::input::{InputState, mods_to_string};
mod config;
use crate::config::LogConfig;
struct TestGame {
input: InputState,
cam_pos: Point3<f32>,
log_config: LogConfig,
}
impl Game for TestGame {
fn validation_layers_enabled(self: &Self) -> bool {
self.log_config.vulkan_validation_layers
}
fn update(self: &mut Self, game_data: &mut GameData) {
// User interaction
if self.input.button_just_released("QUIT") {
game_data.shutdown = true;
}
if self.input.button_just_pressed("RELOAD_SHADERS") {
game_data.recreate_pipeline = true;
}
self.cam_pos.x += self.input.get_axis("FORWARD_AXIS") * 0.01;
// Move game objects
game_data.push_constants.time = game_data.start_time.elapsed().unwrap().as_millis() as f32 / 1000.0;
let model = Matrix4::from_angle_z(Rad::from(Deg(game_data.push_constants.time * 100.0)));
let view = Matrix4::look_at(
self.cam_pos,
Point3::new(0.0, 0.0, 0.0),
Vector3::new(0.0, 0.0, 1.0)
);
let mut proj = cgmath::perspective(
Rad::from(Deg(45.0)),
game_data.dimensions[0] as f32 / game_data.dimensions[1] as f32,
0.1,
100.0
);
proj.y.y *= -1.0;
game_data.push_constants.model = model.into();
game_data.push_constants.view = view.into();
game_data.push_constants.projection = proj.into();
game_data.line_push_constants.view = view.into();
game_data.line_push_constants.projection = proj.into();
self.input.frame_end();
}
fn on_window_event(self: &mut Self, event: &Event) {
match event {
Event::WindowEvent { event: WindowEvent::KeyboardInput { device_id, input }, .. } => {
if self.log_config.input {
let mods = mods_to_string(&input.modifiers);
if mods.len() > 0 {
println!("Keyboard {:?} {:?} {:?} + {:?}", device_id, input.state, &mods, input.scancode)
} else {
println!("Keyboard {:?} {:?} {:?}", device_id, input.state, input.scancode)
}
}
self.input.on_keyboard_event(input.state, input.scancode, input.modifiers);
},
Event::WindowEvent { event: WindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => {
if self.log_config.input {
let mods = mods_to_string(modifiers);
if mods.len() > 0 {
println!("Mouse {:?} {:?} {:?} + {:?}", device_id, state, &mods, button)
} else {
println!("Mouse {:?} {:?} {:?}", device_id, state, button)
}
}
self.input.on_mouse_event(state, button, modifiers);
},
Event::WindowEvent { event: WindowEvent::MouseWheel { device_id, delta, phase, modifiers }, .. } => {
if self.log_config.input {
let mods = mods_to_string(modifiers);
if mods.len() > 0 {
println!("Scroll {:?} {:?} {:?} + {:?}", device_id, phase, &mods, delta)
} else {
println!("Scroll {:?} {:?} {:?}", device_id, phase, delta)
}
}
self.input.on_mouse_wheel_event(&delta, &modifiers);
}
_ => {}
}
}
}
fn main() {
let mut game = TestGame {
input: InputState::new("config/input.toml"),
cam_pos: Point3::new(2.0, 2.0, 2.0),
log_config: LogConfig::from_file("config/log.toml"),
};
vulkan::init(
"models/iski51.obj",
(-10..10)
.flat_map(|it| vec![
LinePoint { position: [it as f32, -10., 0.] },
LinePoint { position: [it as f32, 10., 0.] },
LinePoint { position: [-10., it as f32, 0.] },
LinePoint { position: [10., it as f32, 0.] },
]).collect(),
&mut game
);
}