restructure
This commit is contained in:
107
src/main.rs
107
src/main.rs
@@ -1,17 +1,42 @@
|
||||
use crate::vulkan::{Vertex, GameData};
|
||||
use crate::vulkan::{Vertex, GameData, Game};
|
||||
use winit::{Event, WindowEvent, ElementState};
|
||||
use std::time::SystemTime;
|
||||
use std::iter::FromIterator;
|
||||
use cgmath::{Matrix4, SquareMatrix, Rad, Point3, Vector3, Deg};
|
||||
use cgmath::{Matrix4, Rad, Point3, Vector3, Deg};
|
||||
|
||||
mod vulkan;
|
||||
use vulkan::vs::ty::PushConstants;
|
||||
|
||||
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 {
|
||||
struct TestGame {}
|
||||
|
||||
impl Game for TestGame {
|
||||
fn update(self: &mut Self, game_data: &mut GameData) {
|
||||
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(
|
||||
Point3::new(2.0, 2.0, 2.0),
|
||||
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.aspect_ratio,
|
||||
0.1,
|
||||
10.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();
|
||||
}
|
||||
|
||||
fn on_window_event(self: &mut Self, game_data: &mut GameData, event: &Event) -> bool {
|
||||
match event {
|
||||
Event::WindowEvent { event: WindowEvent::KeyboardInput { device_id, input }, .. } => {
|
||||
if PRINT_KEYBOARD_INPUT {
|
||||
@@ -28,71 +53,29 @@ impl GameData<'_> {
|
||||
}
|
||||
|
||||
if input.state == ElementState::Released && input.modifiers.ctrl && input.scancode == 19 {
|
||||
self.recreate_pipeline = true;
|
||||
game_data.recreate_pipeline = true;
|
||||
}
|
||||
if input.state == ElementState::Released && input.scancode == 1 {
|
||||
self.shutdown = true;
|
||||
game_data.shutdown = true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
fn update_push_constants(self: &mut Self) {
|
||||
self.push_constants.time = self.start_time.elapsed().unwrap().as_millis() as f32 / 1000.0;
|
||||
|
||||
let model = Matrix4::from_angle_z(Rad::from(Deg(self.push_constants.time * 100.0)));
|
||||
|
||||
let view = Matrix4::look_at(
|
||||
Point3::new(2.0, 2.0, 2.0),
|
||||
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)),
|
||||
self.aspect_ratio,
|
||||
0.1,
|
||||
10.0
|
||||
);
|
||||
|
||||
proj.y.y *= -1.0;
|
||||
|
||||
self.push_constants.model = model.into();
|
||||
self.push_constants.view = view.into();
|
||||
self.push_constants.projection = proj.into();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// let mut whatever = String::new();
|
||||
// std::io::stdin().read_line(&mut whatever).unwrap();
|
||||
|
||||
let mut pc = PushConstants {
|
||||
time: 0.0,
|
||||
_dummy0: [0; 12],
|
||||
model: Matrix4::identity().into(),
|
||||
view: Matrix4::identity().into(),
|
||||
projection: Matrix4::identity().into(),
|
||||
};
|
||||
|
||||
let data = GameData {
|
||||
mesh_vertices: vec![
|
||||
Vertex { position: [0.1, 0.2, 0.2] },
|
||||
Vertex { position: [0.2, 0.4, 0.2] },
|
||||
Vertex { position: [0.2, 0.2, 0.3] }
|
||||
],
|
||||
line_vertices: vec![
|
||||
Vertex { position: [-0.9, 1., 0.] },
|
||||
Vertex { position: [0.9, 0., 0.] },
|
||||
],
|
||||
push_constants: &mut pc,
|
||||
start_time: SystemTime::now(),
|
||||
recreate_pipeline: false,
|
||||
aspect_ratio: 1.0,
|
||||
shutdown: false,
|
||||
};
|
||||
|
||||
vulkan::init(data);
|
||||
let mut game = TestGame {};
|
||||
vulkan::init(vec![
|
||||
Vertex { position: [0.1, 0.2, 0.2] },
|
||||
Vertex { position: [0.2, 0.4, 0.2] },
|
||||
Vertex { position: [0.2, 0.2, 0.3] }
|
||||
],
|
||||
vec![
|
||||
Vertex { position: [-0.9, 1., 0.] },
|
||||
Vertex { position: [0.9, 0., 0.] },
|
||||
],
|
||||
&mut game
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user