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

130 lines
5 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
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-24 19:42:32 -04:00
use tracing::{debug, error, info, trace, warn};
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> {
windows: HashMap<WindowId, Arc<Window>>,
2025-02-04 05:10:53 -05:00
ctx: Option<WgpuCtx<'window>>,
}
impl ApplicationHandler for App<'_> {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
if self.windows.is_empty() {
2025-02-04 05:10:53 -05:00
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."),
);
let window_id = window.id();
self.windows.insert(window_id, window.clone());
2025-02-04 05:10:53 -05:00
let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap();
self.ctx = Some(wgpu_ctx)
}
}
2025-03-24 23:21:05 -04:00
2025-02-04 05:10:53 -05:00
fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
2025-03-24 19:42:32 -04:00
window_id: WindowId,
2025-02-04 05:10:53 -05:00
event: WindowEvent,
) {
match event {
WindowEvent::CloseRequested => {
if let Some(window) = self.windows.remove(&window_id) {
2025-03-25 00:32:49 -04:00
let mut window = Arc::into_inner(window);
window = None;
// _ = window;
2025-03-24 23:21:05 -04:00
drop(window);
2025-03-24 19:42:32 -04:00
event_loop.exit();
2025-03-25 00:32:49 -04:00
println!("Window: {:?} closed, exiting", window_id);
2025-03-24 19:42:32 -04:00
}
2025-03-25 00:32:49 -04:00
// if self.windows.is_empty() {
// event_loop.exit();
// }
2025-02-04 05:10:53 -05:00
}
2025-03-24 19:42:32 -04:00
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");
2025-03-24 18:08:00 -04:00
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),
2025-03-24 19:42:32 -04:00
}
2025-03-24 18:08:00 -04:00
}
}
2025-03-24 19:42:32 -04:00
winit::keyboard::KeyCode::Escape => {
debug!("Escape key pressed, spawning new window");
let win_attr =
2025-03-25 00:32:49 -04:00
Window::default_attributes().with_title(format!("Zenyx - New Window {}",self.windows.len()));
2025-03-24 19:42:32 -04:00
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());
2025-03-24 19:42:32 -04:00
let wgpu_ctx = WgpuCtx::new_blocking(new_window.clone()).unwrap();
self.ctx = Some(wgpu_ctx);
}
_ => info!("Unimplemented keycode: {:?}", code),
2025-03-24 18:08:00 -04:00
}
}
_ => {}
2025-03-24 19:42:32 -04:00
},
2025-02-04 05:10:53 -05:00
WindowEvent::RedrawRequested => {
2025-03-25 00:32:49 -04:00
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();
2025-03-24 19:42:32 -04:00
}
2025-03-25 00:32:49 -04:00
None => ()
2025-02-04 05:10:53 -05:00
}
2025-03-25 00:32:49 -04:00
2025-02-04 05:10:53 -05:00
}
WindowEvent::Resized(size) => {
if let Some(window) = self.windows.get(&window_id) {
2025-03-24 19:42:32 -04:00
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);
}
2025-02-04 05:10:53 -05:00
}
}
_ => 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
}