1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Manage events, input, and the window via the [`blinds`] library
//!
//! The [`run`] function is the entry point for all applications
use crate::geom::{Rectangle, Transform};
use crate::graphics::Graphics;
use crate::Result;
use std::future::Future;

pub use blinds::{
    CursorIcon, ElementState, Event, EventStream, GamepadAxis, GamepadButton, GamepadEvent,
    GamepadId, Key, Modifiers, MouseButton, MouseScrollDelta, Pointer, Settings, Window,
};

/// The entry point of a Quicksilver application
///
/// It provides your application (represented by an async closure or function) with a [`Window`],
/// [`Graphics`] context, and [`EventStream`].
pub fn run<F, T>(settings: Settings, app: F) -> !
where
    T: 'static + Future<Output = Result<()>>,
    F: 'static + FnOnce(Window, Graphics, EventStream) -> T,
{
    #[cfg(feature = "easy_log")]
    set_logger();

    let size = settings.size;
    let screen_region = Rectangle::new_sized(size);

    blinds::run_gl(settings, move |window, ctx, events| {
        #[cfg(not(target_arch = "wasm32"))]
        {
            if std::env::set_current_dir("static").is_err() {
                log::warn!("Warning: no asset directory found. Please place all your assets inside a directory called 'static' so they can be loaded");
                log::warn!("Execution continuing, but any asset-not-found errors are likely due to the lack of a 'static' directory.")
            }
        }

        let ctx = golem::Context::from_glow(ctx).unwrap();
        let mut graphics = Graphics::new(ctx).unwrap();
        graphics.set_projection(Transform::orthographic(screen_region));

        async {
            match app(window, graphics, events).await {
                Ok(()) => log::info!("Exited successfully"),
                Err(err) => {
                    log::error!("Error: {:?}", err);
                    panic!("{:?}", err);
                }
            }
        }
    });
}

#[cfg(feature = "easy_log")]
fn set_logger() {
    #[cfg(target_arch = "wasm32")]
    web_logger::custom_init(web_logger::Config {
        level: log::Level::Debug,
    });
    #[cfg(not(target_arch = "wasm32"))]
    simple_logger::init_with_level(log::Level::Debug).expect("A logger was already initialized");
}