75 lines
1.7 KiB
Rust
75 lines
1.7 KiB
Rust
pub mod egui_renderer;
|
|
pub mod state;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use winit::{
|
|
application::ApplicationHandler,
|
|
event::WindowEvent,
|
|
event_loop::{self, EventLoop},
|
|
window::Window,
|
|
};
|
|
|
|
use crate::state::State;
|
|
|
|
pub fn run() -> anyhow::Result<()> {
|
|
env_logger::init();
|
|
|
|
let event_loop = EventLoop::with_user_event().build()?;
|
|
let mut app = App::default();
|
|
event_loop.run_app(&mut app)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
// App struct
|
|
#[derive(Default)]
|
|
pub struct App {
|
|
state: Option<State>,
|
|
}
|
|
|
|
impl ApplicationHandler for App {
|
|
fn resumed(&mut self, event_loop: &event_loop::ActiveEventLoop) {
|
|
// Create window
|
|
let window = Arc::new(
|
|
event_loop
|
|
.create_window(
|
|
Window::default_attributes()
|
|
.with_title("Wgpu Template")
|
|
.with_resizable(true),
|
|
)
|
|
.unwrap(),
|
|
);
|
|
|
|
let state = pollster::block_on(State::new(window.clone()));
|
|
self.state = Some(state);
|
|
|
|
window.request_redraw();
|
|
}
|
|
|
|
fn window_event(
|
|
&mut self,
|
|
event_loop: &event_loop::ActiveEventLoop,
|
|
_window_id: winit::window::WindowId,
|
|
event: winit::event::WindowEvent,
|
|
) {
|
|
let state = self.state.as_mut().unwrap();
|
|
state.handle_event(&event);
|
|
|
|
match event {
|
|
WindowEvent::CloseRequested => {
|
|
event_loop.exit();
|
|
}
|
|
WindowEvent::RedrawRequested => {
|
|
state.render();
|
|
state.get_window().request_redraw();
|
|
}
|
|
|
|
WindowEvent::Resized(size) => {
|
|
state.resize(size);
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|