draw with wgpu

This commit is contained in:
Chance 2024-12-01 22:58:52 -05:00 committed by BitSyndicate
parent ae85be1699
commit 71ad70917e
Signed by: bitsyndicate
GPG key ID: 443E4198D6BBA6DE
4 changed files with 129 additions and 16 deletions

View file

@ -1,22 +1,28 @@
use log2::{debug, error};
use std::sync::Arc;
use ctx::WgpuCtx;
use log2::{debug, error, trace};
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::ActiveEventLoop;
use winit::window::{Window, WindowId};
use winit::window::{self, Window, WindowId};
pub mod ctx;
#[derive(Default)]
pub struct App {
window: Option<Window>,
pub struct App<'window> {
window: Option<Arc<Window>>,
ctx: Option<WgpuCtx<'window>>,
}
impl ApplicationHandler for App {
impl ApplicationHandler for App<'_> {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
if self.window.is_none() {
let win_attr = Window::default_attributes().with_title("Zenyx");
let window = event_loop
let window = Arc::new(event_loop
.create_window(win_attr)
.expect("create window err.");
self.window = Some(window);
.expect("create window err."));
self.window = Some(window.clone());
let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap();
self.ctx = Some(wgpu_ctx)
}
}
@ -32,12 +38,21 @@ impl ApplicationHandler for App {
debug!("Window closed, exiting");
std::process::exit(0)
}
WindowEvent::RedrawRequested => {
if let Some(ctx) = &mut self.ctx {
ctx.draw();
}
}
WindowEvent::Resized(size) => {
if let (Some(wgpu_ctx),Some(window)) = (&mut self.ctx, &self.window) {
wgpu_ctx.resize(size.into());
window.request_redraw();
let size_str: String = size.height.to_string() + "x" + &size.width.to_string();
//self.window.as_ref().unwrap().set_title(&format!("you reszed the window to {size_str}"));
debug!("Window resized to {:?}", size_str);
}
_ => error!("Unhandled window event"),
}
_ => trace!("Unhandled window event"),
}
}
}