game_toolkit_core/
context.rs1use std::sync::Arc;
2
3use anyhow::Result;
4use winit::event_loop::ActiveEventLoop;
5use winit::window::Window;
6
7use crate::Rng;
8use crate::time::Time;
9
10pub enum GameEvent {
11 Resized { width: u32, height: u32 },
12 FocusChanged(bool),
13 CloseRequested,
14}
15
16pub struct Context {
17 pub gfx: game_toolkit_gfx::Graphics,
18 pub audio: Option<game_toolkit_audio::Audio>,
19 pub input: game_toolkit_input::Input,
20 pub assets: game_toolkit_assets::Assets,
21 pub time: Time,
22 pub rng: Rng,
23 pub window: Arc<Window>,
24 pub(crate) quit_requested: bool,
25}
26
27impl Context {
28 #[allow(clippy::too_many_arguments)]
29 pub(crate) fn new(
30 event_loop: &ActiveEventLoop,
31 title: &str,
32 width: u32,
33 height: u32,
34 vsync: bool,
35 asset_root: std::path::PathBuf,
36 random_seed: u64,
37 depth_format: Option<wgpu::TextureFormat>,
38 msaa_samples: u32,
39 ) -> Result<Self> {
40 let attrs = Window::default_attributes()
41 .with_title(title)
42 .with_inner_size(winit::dpi::LogicalSize::new(width, height));
43 let window = Arc::new(event_loop.create_window(attrs)?);
44 let gfx = pollster::block_on(game_toolkit_gfx::Graphics::new(
45 window.clone(),
46 vsync,
47 depth_format,
48 msaa_samples,
49 ))?;
50 let audio = match game_toolkit_audio::Audio::new() {
51 Ok(a) => Some(a),
52 Err(e) => {
53 log::warn!("audio init failed, continuing muted: {e}");
54 None
55 }
56 };
57 let input = game_toolkit_input::Input::new();
58 let assets = game_toolkit_assets::Assets::new(&asset_root).unwrap_or_else(|e| {
59 log::warn!(
60 "asset root {} unusable ({e}); falling back to cwd",
61 asset_root.display()
62 );
63 game_toolkit_assets::Assets::new(".").expect("cwd should resolve")
64 });
65 Ok(Self {
66 gfx,
67 audio,
68 input,
69 assets,
70 time: Time::new(),
71 rng: Rng::new(random_seed),
72 window,
73 quit_requested: false,
74 })
75 }
76
77 pub fn quit(&mut self) {
78 self.quit_requested = true;
79 }
80}