zenyx-engine/engine/src/core/render/mod.rs
2025-04-22 21:51:34 +02:00

129 lines
5 KiB
Rust

use std::collections::HashMap;
use std::sync::Arc;
use ctx::WgpuCtx;
use tracing::{debug, error, info, trace, warn};
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::ControlFlow;
use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::window::{Window, WindowId};
pub mod ctx;
#[derive(Default)]
pub struct App<'window> {
windows: HashMap<WindowId, Arc<Window>>,
ctx: Option<WgpuCtx<'window>>,
}
impl ApplicationHandler for App<'_> {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
if self.windows.is_empty() {
let win_attr = Window::default_attributes().with_title("Zenyx");
let window = Arc::new(
event_loop
.create_window(win_attr)
.expect("create window err."),
);
let window_id = window.id();
self.windows.insert(window_id, 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 => {
if let Some(window) = self.windows.remove(&window_id) {
let mut window = Arc::into_inner(window);
window = None;
// _ = window;
drop(window);
event_loop.exit();
println!("Window: {:?} closed, exiting", window_id);
}
// if self.windows.is_empty() {
// event_loop.exit();
// }
}
WindowEvent::KeyboardInput {
device_id,
event,
is_synthetic,
} => match event.physical_key {
winit::keyboard::PhysicalKey::Code(code) => {
if event.state.is_pressed() == false {
return;
}
match code {
winit::keyboard::KeyCode::Space => {
debug!("Space key pressed");
if let Some(ctx) = &mut self.ctx {
match ctx.bg_color() {
wgpu::Color::WHITE => ctx.change_bg_color(wgpu::Color::BLACK),
wgpu::Color::BLACK => ctx.change_bg_color(wgpu::Color::WHITE),
_ => ctx.change_bg_color(wgpu::Color::WHITE),
}
}
}
winit::keyboard::KeyCode::Escape => {
debug!("Escape key pressed, spawning new window");
let win_attr =
Window::default_attributes().with_title(format!("Zenyx - New Window {}",self.windows.len()));
let new_window = Arc::new(
event_loop
.create_window(win_attr)
.expect("create window err."),
);
let window_id = new_window.id();
self.windows.insert(window_id, new_window.clone());
let wgpu_ctx = WgpuCtx::new_blocking(new_window.clone()).unwrap();
self.ctx = Some(wgpu_ctx);
}
_ => info!("Unimplemented keycode: {:?}", code),
}
}
_ => {}
},
WindowEvent::RedrawRequested => {
if let Some(ctx) = &mut self.ctx {
ctx.draw();
}
match self.windows.get(&window_id) {
Some(window) => {
if let Some(ctx) = &mut self.ctx {
ctx.draw();
}
window.request_redraw();
}
None => ()
}
}
WindowEvent::Resized(size) => {
if let Some(window) = self.windows.get(&window_id) {
if let Some(wgpu_ctx) = &mut self.ctx {
wgpu_ctx.resize(size.into());
window.request_redraw();
let size_str: String =
size.height.to_string() + "x" + &size.width.to_string();
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();
}