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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#![no_std]
#![allow(dead_code, non_camel_case_types, non_snake_case)]

pub extern crate log;

extern crate alloc;
// TODO: Resolve when glow is fixed. They use standard HashSet >:(
// #[cfg(any(test, not(target_arch = "wasm32")))]
extern crate std;

/// Asset utilities.
pub mod asset;
/// Audio primitives. Creating and controlling sounds are included in here.
pub mod audio;
/// Color primitives. These are used in the graphics and image modules for managing images and
/// textures.
pub mod color;
/// Event utilities.
pub mod event;
/// Graphics primitives.
pub mod graphics;
/// Image utilities. Images are used for creating textures.
pub mod image;
/// Math utilities.
pub mod math;
/// Synchronization utilities.
pub mod sync;
/// Time utilities.
pub mod time;

mod engine;

pub use cgmath;
pub use engine::{start, Context};
pub use fontdue;

/// Type that holds all of your application state and handles events.
pub trait App: 'static + Sized {
    /// Function to create the app from a context.
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    fn new(_ctx: &mut Context<Self>) -> Self;

    /// This event is useful as a place to put your code that should be run after all state-changing
    /// events have been handled and you want to do stuff (updating state, performing calculations,
    /// etc) that happens as the "main body" of your event loop.
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    /// * `delta` - The time passed since the last update in seconds.
    fn on_update(&mut self, _ctx: &mut Context<Self>, _delta: f32) {}

    /// The window has requested it close.
    fn on_close_requested(&mut self, _ctx: &mut Context<Self>) {}

    /// Received a character. This includes control characters.
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    /// * `character` - The character typed.
    fn on_received_character(&mut self, _ctx: &mut Context<Self>, _character: char) {}

    /// Keyboard press event. Includes a flag for if this is a repeat event.
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    /// * `key` - The button pressed.
    /// * `is_repeat` - Flag for if this key was already pressed. Some environments may fire repeat
    /// key pressed events when the key is held.
    fn on_key_pressed(&mut self, _ctx: &mut Context<Self>, _key: event::KeyboardButton, _is_repeat: bool) {}

    /// Keyboard release event.
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    /// * `key` - The button released.
    fn on_key_released(&mut self, _ctx: &mut Context<Self>, _key: event::KeyboardButton) {}

    /// Cursor press event. Contains the button pressed and the position it was pressed at.
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    /// * `button` - The button pressed.
    /// * `physical_pos` - Cursor position at time of press. This is based on the physical size of
    /// the window, with (0,0) being the bottom left.
    /// * `normalized_pos` - Cursor position at time of press. This is normalized where the x and y
    /// values are between -1 and 1, with the bottom left of the screen being (-1, -1), and the top
    /// right being (1, 1). This may be useful for converting screen space coordinates into world
    /// space.
    fn on_cursor_pressed(
        &mut self,
        _ctx: &mut Context<Self>,
        _button: event::CursorButton,
        _physical_pos: cgmath::Vector2<f32>,
        _normalized_pos: cgmath::Vector2<f32>,
    ) {
    }

    /// Cursor press event. Contains the button pressed and the position it was pressed at.
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    /// * `button` - The button released.
    /// * `physical_pos` - Cursor position at time of release. This is base with (0,0) being the
    /// bottom left.
    /// * `normalized_pos` - Cursor position at time of release. This is normalized where the x and
    /// y values are between -1 and 1, with the bottom left of the screen being (-1, -1), and the
    /// top right being (1, 1). This may be useful for converting screen space coordinates into
    /// world space.
    fn on_cursor_released(
        &mut self,
        _ctx: &mut Context<Self>,
        _button: event::CursorButton,
        _physical_pos: cgmath::Vector2<f32>,
        _normalized_pos: cgmath::Vector2<f32>,
    ) {
    }

    /// Cursor wheel scroll event..
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    /// * `direction` - The direction scrolled.
    fn on_cursor_scroll(&mut self, _ctx: &mut Context<Self>, _direction: event::ScrollDirection) {}

    /// Cursor moved event. Use this for interacting with UI. Contains the new position of the
    /// cursor.
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    /// * `physical_pos` - Current cursor position. This is based on the physical size of the
    /// window, with (0,0) being the bottom left.
    /// * `normalized_pos` - Current cursor position. This is normalized where the x and y values
    /// are between -1 and 1, with the bottom left of the screen being (-1, -1), and the top right
    /// being (1, 1). This may be useful for converting screen space coordinates into world space.
    fn on_cursor_moved(
        &mut self,
        _ctx: &mut Context<Self>,
        _physical_pos: cgmath::Vector2<f32>,
        _normalized_pos: cgmath::Vector2<f32>,
    ) {
    }

    /// Cursor delta event. Use this to control a 3D camera. Contains the represents raw, unfiltered
    /// physical motion. Represents the change in physical position of the pointing device.
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    /// * `delta` - Change from last position. The units are arbitrary and up to the device.
    /// * `focused` - Flag for if the window is focused. This event may return deltas even when the
    /// window is not focused.
    fn on_cursor_delta(&mut self, _ctx: &mut Context<Self>, _delta: cgmath::Vector2<f32>, _focused: bool) {}

    /// Cursor left the bounds of the window event.
    fn on_cursor_left(&mut self, _ctx: &mut Context<Self>) {}

    /// Cursor entered the bounds of the window event.
    fn on_cursor_entered(&mut self, _ctx: &mut Context<Self>) {}

    /// Window resized event. Contains the new dimensions of the window.
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    /// * `physical_size` - The size of the viewport.
    /// * `logical_size` - The logical size of the viewport. This is derived from the physical_size
    /// divided by the scale_factor.
    /// * `scale_factor` - The window's scale factor. This is a multiplier between the physical size
    /// and logical size of the window.
    fn on_window_resized(
        &mut self,
        _ctx: &mut Context<Self>,
        _physical_size: cgmath::Vector2<f32>,
        _logical_size: cgmath::Vector2<f32>,
        _scale_factor: f32,
    ) {
    }

    /// The window gained or lost focus.
    /// # Arguments
    ///
    /// * `ctx` - The engine context. This can be used to call various API functions.
    /// * `focused` - The parameter is true if the window has gained focus, and false if it has lost focus.
    fn on_window_focused(&mut self, _ctx: &mut Context<Self>, _focused: bool) {}
}