use std::sync::Arc; use plutonium::{utils::Position, PlutoniumEngine}; use wgpu::Surface; use winit::{ application::ApplicationHandler, event::{ElementState, KeyEvent, WindowEvent}, event_loop::{ActiveEventLoop, EventLoop}, keyboard::Key, window::{Window, WindowId}, }; struct TextureSvgExample<'a> { window: Option>, engine: Option>, player_position: Position, _surface: Option>, } impl<'a> TextureSvgExample<'a> { pub fn new() -> Self { let player_position = Position { x: 0.0, y: 0.0 }; Self { window: None, _surface: None, engine: None, player_position, } } } impl<'a> ApplicationHandler<()> for TextureSvgExample<'a> { fn resumed(&mut self, event_loop: &ActiveEventLoop) { // Create the window safely with proper error handling let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::default()); let window_attributes = Window::default_attributes().with_title("Moveable Texture SVG Example"); if let Ok(window) = event_loop.create_window(window_attributes) { let window_arc = Arc::new(window); let size = window_arc.as_ref().inner_size(); let surface = instance.create_surface(window_arc.clone()).unwrap(); let mut engine = PlutoniumEngine::new(surface, instance, size); // actual example stuff engine.create_texture_svg( "player", "examples/media/player.svg", self.player_position, 1.0, None, ); window_arc.request_redraw(); self.engine = Some(engine); self.window = Some(window_arc); } } fn window_event( &mut self, event_loop: &ActiveEventLoop, _window_id: WindowId, event: WindowEvent, ) { match event { WindowEvent::CloseRequested => { event_loop.exit(); } WindowEvent::KeyboardInput { event: KeyEvent { logical_key: key, state: ElementState::Pressed, .. }, .. } => { let mut update_position = |dx, dy| { self.player_position.x += dx; self.player_position.y += dy; if let Some(engine) = &mut self.engine { engine.set_texture_position("player", self.player_position); self.window.as_ref().unwrap().request_redraw(); } }; match key.as_ref() { Key::Character("a") => update_position(-10.0, 0.0), Key::Character("d") => update_position(10.0, 0.0), Key::Character("w") => update_position(0.0, -10.0), Key::Character("s") => update_position(0.0, 10.0), _ => (), } } WindowEvent::RedrawRequested => { let _window = self.window.as_ref(); if let Some(engine) = &mut self.engine { // Clear the queue before each frame engine.clear_render_queue(); engine.update(); engine.queue_texture("player"); engine.render().unwrap(); } } _ => (), } } } fn main() -> Result<(), Box> { let event_loop = EventLoop::new().unwrap(); let mut app = TextureSvgExample::new(); match event_loop.run_app(&mut app) { Ok(_) => println!("Application terminated gracefully."), Err(e) => eprintln!("Error running application: {:?}", e), } Ok(()) }