amethyst_window/
bundle.rs

1use crate::{DisplayConfig, EventsLoopSystem, WindowSystem};
2use amethyst_config::{Config, ConfigError};
3use amethyst_core::{bundle::SystemBundle, ecs::World, shred::DispatcherBuilder};
4use amethyst_error::Error;
5use winit::EventsLoop;
6
7/// Screen width used in predefined display configuration.
8#[cfg(feature = "test-support")]
9pub const SCREEN_WIDTH: u32 = 800;
10/// Screen height used in predefined display configuration.
11#[cfg(feature = "test-support")]
12pub const SCREEN_HEIGHT: u32 = 600;
13
14/// Bundle providing easy initializing of the appopriate `Window`, `WindowSystem` `EventLoop` and
15/// `EventLoopSystem` constructs used for creating the rendering window of amethyst with `winit`
16#[derive(Debug)]
17pub struct WindowBundle {
18    config: DisplayConfig,
19}
20
21impl WindowBundle {
22    /// Builds a new window bundle from a loaded `DisplayConfig`.
23    pub fn from_config(config: DisplayConfig) -> Self {
24        WindowBundle { config }
25    }
26
27    /// Builds a new window bundle by loading the `DisplayConfig` from `path`.
28    ///
29    /// Will fall back to `DisplayConfig::default()` in case of an error.
30    pub fn from_config_path(path: impl AsRef<std::path::Path>) -> Result<Self, ConfigError> {
31        Ok(WindowBundle::from_config(DisplayConfig::load(
32            path.as_ref(),
33        )?))
34    }
35
36    /// Builds a new window bundle with a predefined `DisplayConfig`.
37    ///
38    /// This uses a `DisplayConfig::default()`, but with the following differences:
39    ///
40    /// * `dimensions` is changed to `Some((SCREEN_WIDTH, SCREEN_HEIGHT))`.
41    /// * `visibility` is `false`.
42    #[cfg(feature = "test-support")]
43    pub fn from_test_config() -> Self {
44        let mut display_config = DisplayConfig::default();
45        display_config.dimensions = Some((SCREEN_WIDTH, SCREEN_HEIGHT));
46        display_config.visibility = false;
47
48        WindowBundle::from_config(display_config)
49    }
50}
51
52impl<'a, 'b> SystemBundle<'a, 'b> for WindowBundle {
53    fn build(
54        self,
55        world: &mut World,
56        builder: &mut DispatcherBuilder<'a, 'b>,
57    ) -> Result<(), Error> {
58        let event_loop = EventsLoop::new();
59        builder.add(
60            WindowSystem::from_config(world, &event_loop, self.config),
61            "window",
62            &[],
63        );
64        builder.add_thread_local(EventsLoopSystem::new(event_loop));
65        Ok(())
66    }
67}