fix selecting wrong window for key events

This commit is contained in:
Chance 2025-03-25 15:13:57 -04:00 committed by BitSyndicate
parent ce4702ec5f
commit ceefdaf46b
5 changed files with 119 additions and 15 deletions

View file

@ -24,6 +24,7 @@ bytemuck = "1.21.0"
futures = "0.3.31"
cgmath = "0.18.0"
tracing = "0.1.41"
tracing-subscriber = "0.3.19"
[profile.dev]

View file

@ -5,7 +5,7 @@ use std::time::Instant;
use cgmath::{Matrix4, Point3, Rad, Vector3, perspective};
use futures::executor::block_on;
use thiserror::Error;
use wgpu::util::DeviceExt;
use wgpu::{Backends, InstanceDescriptor, util::DeviceExt};
use winit::window::Window;
#[derive(Debug, Error)]
@ -240,7 +240,10 @@ pub struct WgpuCtx<'window> {
impl<'window> WgpuCtx<'window> {
pub async fn new(window: Arc<Window>) -> Result<WgpuCtx<'window>, ContextError> {
let instance = wgpu::Instance::default();
let instance = wgpu::Instance::new(&InstanceDescriptor {
backends: Backends::from_comma_list("dx12,metal,opengl,webgpu"),
..Default::default()
});
let surface = instance.create_surface(Arc::clone(&window))?;
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
@ -255,8 +258,7 @@ impl<'window> WgpuCtx<'window> {
&wgpu::DeviceDescriptor {
label: None,
required_features: wgpu::Features::empty(),
required_limits: wgpu::Limits::downlevel_webgl2_defaults()
.using_resolution(adapter.limits()),
required_limits: wgpu::Limits::default().using_resolution(adapter.limits()),
memory_hints: wgpu::MemoryHints::Performance,
},
None,

View file

@ -31,7 +31,13 @@ impl ApplicationHandler for App<'_> {
);
let window_id = window.id();
let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap();
self.windows.insert(window_id, WindowContext { window, ctx: wgpu_ctx });
self.windows.insert(
window_id,
WindowContext {
window,
ctx: wgpu_ctx,
},
);
}
}
@ -45,7 +51,7 @@ impl ApplicationHandler for App<'_> {
WindowEvent::CloseRequested => {
if let Some(window_context) = self.windows.remove(&window_id) {
drop(window_context);
println!("Window: {:?} closed, exiting", window_id);
debug!("Window: {:?} closed, exiting", window_id);
}
if self.windows.is_empty() {
event_loop.exit();
@ -63,10 +69,14 @@ impl ApplicationHandler for App<'_> {
match code {
winit::keyboard::KeyCode::Space => {
debug!("Space key pressed");
if let Some(window_context) = self.windows.values_mut().next() {
if let Some(window_context) = self.windows.get_mut(&window_id){
match window_context.ctx.bg_color() {
wgpu::Color::WHITE => window_context.ctx.change_bg_color(wgpu::Color::BLACK),
wgpu::Color::BLACK => window_context.ctx.change_bg_color(wgpu::Color::WHITE),
wgpu::Color::WHITE => {
window_context.ctx.change_bg_color(wgpu::Color::BLACK)
}
wgpu::Color::BLACK => {
window_context.ctx.change_bg_color(wgpu::Color::WHITE)
}
_ => window_context.ctx.change_bg_color(wgpu::Color::WHITE),
}
}
@ -82,7 +92,13 @@ impl ApplicationHandler for App<'_> {
);
let window_id = new_window.id();
let wgpu_ctx = WgpuCtx::new_blocking(new_window.clone()).unwrap();
self.windows.insert(window_id, WindowContext { window: new_window, ctx: wgpu_ctx });
self.windows.insert(
window_id,
WindowContext {
window: new_window,
ctx: wgpu_ctx,
},
);
}
_ => info!("Unimplemented keycode: {:?}", code),
}
@ -99,8 +115,7 @@ impl ApplicationHandler for App<'_> {
if let Some(window_context) = self.windows.get_mut(&window_id) {
window_context.ctx.resize(size.into());
window_context.window.request_redraw();
let size_str: String =
size.height.to_string() + "x" + &size.width.to_string();
let size_str: String = size.height.to_string() + "x" + &size.width.to_string();
debug!("Window resized to {:?}", size_str);
}
}

View file

@ -4,18 +4,33 @@ use colored::Colorize;
use tokio::runtime;
#[allow(unused_imports)]
use tracing::{debug, error, info, warn};
use tracing::{level_filters::LevelFilter, subscriber::set_global_default};
use tracing_subscriber::{layer::Filter, util::SubscriberInitExt};
use winit::event_loop::EventLoop;
pub mod core;
fn init_logger() {
let subscriber = tracing_subscriber::fmt()
.with_max_level(LevelFilter::DEBUG)
.with_level(true)
.compact()
.pretty()
.log_internal_errors(false)
.without_time()
.with_thread_names(true)
.finish();
set_global_default(subscriber).expect("Failed to set default subscriber");
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
init_logger();
if !cfg!(debug_assertions) {
println!("{}", "Debug mode disabled".bright_blue());
info!("{}", "Debug mode disabled".bright_blue());
set_panic_hook();
}
setup();
splash::print_splash();
info!("Type 'help' for a list of commands.");
let repl_thread = std::thread::spawn(|| {
let rt = runtime::Builder::new_current_thread()
@ -26,6 +41,9 @@ async fn main() -> anyhow::Result<()> {
});
let event_loop = EventLoop::new().unwrap();
splash::print_splash();
info!("Type 'help' for a list of commands.");
core::render::init_renderer(event_loop);
if let Err(_) = repl_thread.join() {