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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#![recursion_limit = "256"]
#![allow(clippy::needless_doctest_main)]
//!
//! `geng` (Game ENGine) is an engine for Rust Programming Language.
//!
//! # Quick start
//! More examples are available [here](https://github.com/geng-engine/geng/tree/main/crates/geng/examples).
//!
//! ```no_run
//! use geng::prelude::*;
//!
//! fn main() {
//!     logger::init();
//!     geng::setup_panic_handler();
//!     Geng::run("Application Name", |geng| async move {
//!         let mut events = geng.window().events();
//!         while let Some(event) = events.next().await {
//!             if let geng::Event::Draw = event {
//!                 geng.window().with_framebuffer(|framebuffer| {
//!                     ugli::clear(framebuffer, Some(Rgba::BLACK), None, None);
//!                 });
//!             }
//!         }
//!     });
//! }
//! ```
//!

pub mod prelude {
    pub use crate::{asset::Hot, draw2d, Geng};
    pub use crate::{AbstractCamera2d, AbstractCamera3d, Camera2d};
    pub use ::batbox;
    pub use ::batbox::prelude::*;
    pub use gilrs::{self, Gilrs};
    pub use ugli::{self, Ugli};
}

use crate::prelude::*;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

mod cli_args;
mod context;
mod loading_screen;

pub use geng_asset as asset;
pub use geng_async_state as async_state;
#[cfg(feature = "audio")]
pub use geng_audio::{self as audio, *};
pub use geng_camera::{
    self as camera, AbstractCamera2d, AbstractCamera3d, Camera2d, PixelPerfectCamera,
};
pub use geng_draw2d::{self as draw2d, Draw2d};
pub use geng_font::{self as font, Font, TextAlign};
pub use geng_net as net;
pub use geng_shader as shader;
pub use geng_state::{self as state, State};
pub use geng_texture_atlas::{self as texture_atlas, TextureAtlas};
pub use geng_ui as ui;
pub use geng_window::{self as window, CursorType, Event, Key, MouseButton, Touch, Window};

pub use cli_args::*;
pub use context::*;
pub use loading_screen::*;

#[cfg(not(target_arch = "wasm32"))]
pub fn setup_panic_handler() {
    let default_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |info: &std::panic::PanicInfo| {
        let log_file_path = preferences::base_path().join("error.txt");
        if let Ok(mut file) = std::fs::OpenOptions::new()
            .append(true)
            .create(true)
            .open(log_file_path)
        {
            let _ = writeln!(file, "{info}");
        }
        default_hook(info);
    }));
}

#[cfg(target_arch = "wasm32")]
pub fn setup_panic_handler() {
    #[wasm_bindgen(inline_js = r#"
    export function show_error(text) {
        document.getElementById("geng-progress-screen").style.display = "none";
        document.getElementById("geng-canvas").style.display = "none";
        document.getElementById("error-message").textContent = text;
        document.getElementById("geng-error-screen").style.display = "block";
    }
    "#)]
    extern "C" {
        fn show_error(s: &str);
    }
    fn panic_hook(info: &std::panic::PanicInfo) {
        console_error_panic_hook::hook(info);
        static ALREADY_PANICKED: std::sync::atomic::AtomicBool =
            std::sync::atomic::AtomicBool::new(false);
        if ALREADY_PANICKED.swap(true, std::sync::atomic::Ordering::Relaxed) {
            return;
        }
        let error: String = if let Some(error) = info.payload().downcast_ref::<String>() {
            error.clone()
        } else if let Some(error) = info.payload().downcast_ref::<&str>() {
            error.to_string()
        } else {
            String::from("Something went wrong")
        };
        show_error(&error);
    }
    std::panic::set_hook(Box::new(panic_hook));
}

impl Geng {
    pub(crate) fn set_loading_progress_title(&self, title: &str) {
        log::trace!("Set loading progress title to {title:?}");
        // TODO: native
        #[cfg(target_arch = "wasm32")]
        {
            #[wasm_bindgen(inline_js = r#"
            export function set_progress_title(title) {
                window.gengUpdateProgressTitle(title);
            }
            "#)]
            extern "C" {
                fn set_progress_title(title: &str);
            }
            set_progress_title(title);
        }
    }

    pub(crate) fn set_loading_progress(&self, progress: f64, total: Option<f64>) {
        log::trace!("Loading progress {progress:?}/{total:?}");
        // TODO: native
        #[cfg(target_arch = "wasm32")]
        {
            #[wasm_bindgen(inline_js = r#"
            export function set_progress(progress, total) {
                window.gengUpdateProgress(progress, total);
            }
            "#)]
            extern "C" {
                fn set_progress(progress: f64, total: Option<f64>);
            }
            set_progress(progress, total);
        }
    }

    pub(crate) fn finish_loading(&self) {
        #[cfg(target_arch = "wasm32")]
        {
            #[wasm_bindgen(inline_js = r#"
            export function finish_loading() {
                document.getElementById("geng-progress-screen").style.display = "none";
                document.getElementById("geng-canvas").style.display = "block";
            }
            "#)]
            extern "C" {
                fn finish_loading();
            }
            finish_loading();
        }
    }
}