use raw_window_handle::{HasRawDisplayHandle, RawDisplayHandle};
use vltk::vk::{ApiVersion, CreateInstance, InstanceInformation, Vk, VkContext};
use vltk::{VkApplication, WindowExt};
use winit::dpi::PhysicalSize;
use winit::event::{Event, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{Window, WindowBuilder};
fn main() {
println!("Hello VlTk");
let mut app = MyApplication {
event_loop: None,
window: None,
surface: None,
};
<MyApplication as VkApplication<RawDisplayHandle>>::run(&mut app);
}
pub struct MyApplication {
pub event_loop: Option<EventLoop<()>>,
pub window: Option<Window>,
pub surface: Option<vltk::vk::Surface>,
}
impl<T> VkApplication<T> for MyApplication {
fn init_vulkan(&mut self, vk: Vk) -> VkContext {
let event_loop = EventLoop::new();
let (instance, entry) = match vk.create_instance(InstanceInformation {
api_version: ApiVersion(0, 1, 0, 0),
app_name: "Sample App",
version: 0,
additional: event_loop.raw_display_handle(),
}) {
Ok(i) => i,
Err(err) => panic!("{}", err),
};
let context = vk.create_context(instance, entry);
let window = WindowBuilder::new()
.with_title("A fantastic window!")
.with_inner_size(PhysicalSize::<u32>::from((800, 600)))
.build(&event_loop)
.unwrap();
let surface = match window.use_vk(&context) {
Ok(surface) => surface,
Err(err) => {
println!("Error :(");
std::process::exit(1);
}
};
self.event_loop = Some(event_loop);
self.window = Some(window);
self.surface = Some(surface);
context
}
fn main_loop(&mut self, _vk: Vk) {
let event_loop = self.event_loop.take().unwrap();
let window = self.window.take().unwrap();
let surface = self.surface.take().unwrap();
println!("Surface:{:?}", surface);
event_loop.run(move |event, _, control_flow| match event {
winit::event::Event::WindowEvent {
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
input:
winit::event::KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Escape),
..
},
..
},
window_id: _,
} => {
*control_flow = ControlFlow::Exit;
}
Event::RedrawEventsCleared => {}
Event::LoopDestroyed => {
surface.destroy_surface();
}
_ => {}
})
}
fn clean_up(&mut self, _context: Vk) {}
}