Window init
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
//! Fenster-Frontend: winit-Loop um den wgpu-Renderer.
|
||||
//!
|
||||
//! Stand: Schritt 1+2 des Renderer-Plans (notes/renderer-plan.md) —
|
||||
//! Fenster, internes 320×240-Target mit Testmuster, Nearest-Upscale mit
|
||||
//! 4:3-Letterbox. Szene, PS1-Shader und Flycam folgen als nächste Schritte.
|
||||
//!
|
||||
//! Gleiche Schicht-Regel wie `cli`: Geschwister von `engine`, konsumiert
|
||||
//! dessen Schnittstellen (sobald hier eine Szene läuft) — nie umgekehrt.
|
||||
|
||||
mod gpu;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::{ElementState, KeyEvent, WindowEvent};
|
||||
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
|
||||
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
use winit::window::{Window, WindowId};
|
||||
|
||||
use gpu::Gpu;
|
||||
|
||||
pub fn run() {
|
||||
let event_loop = EventLoop::new().expect("winit: Event-Loop");
|
||||
// Poll statt Wait: wir rendern kontinuierlich (Spiel, kein Editor).
|
||||
event_loop.set_control_flow(ControlFlow::Poll);
|
||||
let mut app = App::default();
|
||||
event_loop.run_app(&mut app).expect("winit: run");
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct App {
|
||||
window: Option<Arc<Window>>,
|
||||
gpu: Option<Gpu>,
|
||||
}
|
||||
|
||||
impl ApplicationHandler for App {
|
||||
/// winit liefert das Fenster erst hier, nicht beim Loop-Start —
|
||||
/// deshalb sind `window`/`gpu` Options statt Konstruktor-Felder.
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
if self.window.is_some() { return; }
|
||||
let attrs = Window::default_attributes()
|
||||
.with_title("wds")
|
||||
// 3× interne Auflösung als Startgröße; frei resizebar.
|
||||
.with_inner_size(winit::dpi::LogicalSize::new(
|
||||
gpu::INTERNAL_W * 3, gpu::INTERNAL_H * 3));
|
||||
let window = Arc::new(event_loop.create_window(attrs).expect("winit: Fenster"));
|
||||
self.gpu = Some(Gpu::new(window.clone(), event_loop.owned_display_handle()));
|
||||
self.window = Some(window);
|
||||
}
|
||||
|
||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
||||
match event {
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
WindowEvent::KeyboardInput {
|
||||
event: KeyEvent {
|
||||
physical_key: PhysicalKey::Code(KeyCode::Escape),
|
||||
state: ElementState::Pressed, ..
|
||||
}, ..
|
||||
} => event_loop.exit(),
|
||||
WindowEvent::Resized(size) => {
|
||||
if let Some(gpu) = &mut self.gpu {
|
||||
gpu.resize(size.width, size.height);
|
||||
}
|
||||
}
|
||||
WindowEvent::RedrawRequested => {
|
||||
if let Some(gpu) = &mut self.gpu { gpu.frame(); }
|
||||
// Sofort den nächsten Frame anfordern (Vsync drosselt).
|
||||
if let Some(w) = &self.window { w.request_redraw(); }
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user