zenyx-engine/engine/src/core/render/mod.rs

73 lines
2.3 KiB
Rust
Raw Normal View History

2025-03-22 18:19:01 -04:00
use std::sync::Arc;
2025-02-04 05:10:53 -05:00
use ctx::WgpuCtx;
2025-03-22 18:19:01 -04:00
use log::{debug, trace};
2025-02-04 05:10:53 -05:00
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::ControlFlow;
2025-03-22 18:19:01 -04:00
use winit::event_loop::{ActiveEventLoop, EventLoop};
2025-02-04 05:10:53 -05:00
use winit::window::{Window, WindowId};
pub mod ctx;
#[derive(Default)]
pub struct App<'window> {
window: Option<Arc<Window>>,
ctx: Option<WgpuCtx<'window>>,
}
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");
2025-03-22 18:19:01 -04:00
let window = Arc::new(
event_loop
.create_window(win_attr)
.expect("create window err."),
);
2025-02-04 05:10:53 -05:00
self.window = Some(window.clone());
let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap();
self.ctx = Some(wgpu_ctx)
}
}
fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
_window_id: WindowId,
event: WindowEvent,
) {
match event {
WindowEvent::CloseRequested => {
event_loop.exit();
debug!("Window closed, exiting");
std::process::exit(0)
}
WindowEvent::RedrawRequested => {
if let Some(ctx) = &mut self.ctx {
ctx.draw();
}
if let Some(window) = &self.window {
window.request_redraw();
}
}
WindowEvent::Resized(size) => {
2025-03-22 18:19:01 -04:00
if let (Some(wgpu_ctx), Some(window)) = (&mut self.ctx, &self.window) {
2025-02-04 05:10:53 -05:00
wgpu_ctx.resize(size.into());
window.request_redraw();
let size_str: String = size.height.to_string() + "x" + &size.width.to_string();
2025-03-22 18:19:01 -04:00
//self.window.as_ref().unwrap().set_title(&format!("you reszed the window to
// {size_str}"));
2025-02-04 05:10:53 -05:00
debug!("Window resized to {:?}", size_str);
}
}
_ => trace!("Unhandled window event"),
}
}
}
pub fn init_renderer(event_loop: EventLoop<()>) {
event_loop.set_control_flow(ControlFlow::Poll);
let mut app = App::default();
event_loop.run_app(&mut app).unwrap();
2025-03-22 18:19:01 -04:00
}