mod consts; mod game; mod inputstatus; mod objects; mod physics; mod shipbehavior; use std::path::PathBuf; use anyhow::Result; use winit::{ event::{Event, KeyboardInput, WindowEvent}, event_loop::{ControlFlow, EventLoop}, window::WindowBuilder, }; fn main() -> Result<()> { // TODO: error if missing let content = galactica_content::Content::load_dir( PathBuf::from(consts::CONTENT_ROOT), PathBuf::from(consts::TEXTURE_ROOT), consts::STARFIELD_TEXTURE_NAME.to_owned(), )?; let event_loop = EventLoop::new(); let window = WindowBuilder::new().build(&event_loop).unwrap(); let mut gpu = pollster::block_on(galactica_render::GPUState::new(window, &content))?; let mut game = game::Game::new(content); gpu.update_starfield_buffer(&game.system.starfield); event_loop.run(move |event, _, control_flow| { match event { Event::RedrawRequested(window_id) if window_id == gpu.window().id() => { match gpu.render( game.camera.pos, game.camera.zoom, &game.get_object_sprites(), &game.get_ui_sprites(), ) { Ok(_) => {} Err(wgpu::SurfaceError::Lost) => { gpu.resize(&game.system.starfield, gpu.window_size) } Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit, // All other errors (Outdated, Timeout) should be resolved by the next frame Err(e) => eprintln!("{:?}", e), } } Event::MainEventsCleared => { game.update(); gpu.window().request_redraw(); } Event::WindowEvent { ref event, window_id, } if window_id == gpu.window.id() => match event { WindowEvent::Focused(state) => { game.set_paused(!state); } WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::KeyboardInput { input: KeyboardInput { state, virtual_keycode: Some(key), .. }, .. } => game.process_key(state, key), WindowEvent::MouseInput { state, button, .. } => { game.process_click(state, button); } WindowEvent::MouseWheel { delta, phase, .. } => { game.process_scroll(delta, phase); } WindowEvent::Resized(physical_size) => { gpu.resize(&game.system.starfield, *physical_size); } WindowEvent::ScaleFactorChanged { new_inner_size, .. } => { gpu.resize(&game.system.starfield, **new_inner_size); } _ => {} }, _ => {} } }); }