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
#[cfg(target_arch="wasm32")]
use geom::Vector;
use graphics::{Window, WindowBuilder};
use input::Event;

/// The structure responsible for managing the game loop state
pub trait State {
    /// Create the state given the window and canvas
    fn new() -> Self where Self: Sized;
    /// Tick the State forward one frame
    ///
    /// Will happen at a fixed rate of 60 ticks per second under ideal conditions. Under non-ideal conditions,
    /// the game loop will do its best to still call the update at about 60 TPS. 
    ///
    /// By default it does nothing
    fn update(&mut self, &mut Window) {}
    /// Process an incoming event
    ///
    /// By default it does nothing
    fn event(&mut self, &Event, &mut Window) {}
    /// Draw the state to the screen
    ///
    /// Will happen as often as possible, only limited by vysnc
    ///
    /// By default it draws a black screen
    fn draw(&mut self, window: &mut Window) {
        use graphics::Color;
        window.clear(Color::black());
        window.present();
    }
}

/// Run the application's game loop
///
/// On desktop platforms, this yields control to a simple game loop controlled by a Timer. On wasm,
/// this yields control to the browser functions setInterval and requestAnimationFrame
pub fn run<T: 'static + State>(window: WindowBuilder) {
    run_impl::<T>(window)
}

#[doc(hidden)]
pub struct Application {
    state: Box<State>, 
    window: Window,
    event_buffer: Vec<Event>
}

impl Application {
    fn update(&mut self) {
        self.state.update(&mut self.window);
    }

    fn draw(&mut self) {
        self.state.draw(&mut self.window);
    }

    #[cfg(target_arch="wasm32")]
    fn event(&mut self, event: &Event) {
        self.window.process_event(event);
        self.state.event(event, &mut self.window);
    }

    fn process_events(&mut self) {
        self.window.update_gamepads(&mut self.event_buffer);
        for i in 0..self.event_buffer.len() {
            self.window.process_event(&self.event_buffer[i]);
            self.state.event(&self.event_buffer[i], &mut self.window);
        }
        self.event_buffer.clear();
    }
}

#[cfg(not(target_arch="wasm32"))]
fn run_impl<T: 'static + State>(window: WindowBuilder) {
    use input::EventProvider;
    let (window, events_loop) = window.build();
    let mut events = EventProvider::new(events_loop);
    let event_buffer = Vec::new();
    let state = Box::new(T::new());
    let mut app = Application { window, state, event_buffer };
    use std::time::Duration;
    #[cfg(feature="sounds")] {
        use sound::Sound;
        Sound::initialize();
    }
    let mut timer = ::Timer::new();
    let mut running = true;
    while running {
        running = events.generate_events(&mut app.window, &mut app.event_buffer);
        app.process_events();
        timer.tick(||  { 
            app.update(); 
            Duration::from_millis(16) 
        });
        app.draw();
        app.window.clear_temporary_states();
    }
}

#[cfg(target_arch="wasm32")]
fn run_impl<T: 'static + State>(window: WindowBuilder) {
    use ffi::wasm;
    use std::os::raw::c_void;
    let window = window.build();
    let app = Box::new(Application { 
        window,
        state: Box::new(T::new()),
        event_buffer: Vec::new()
    });
    unsafe { wasm::set_app(Box::into_raw(app) as *mut c_void) };
}

#[doc(hidden)]
#[no_mangle]
#[cfg(target_arch="wasm32")]
pub extern "C" fn update(app: *mut Application) {
    let mut app = unsafe { Box::from_raw(app) };
    app.process_events();
    app.update();
    app.window.clear_temporary_states();
    Box::into_raw(app);
}

#[doc(hidden)]
#[no_mangle]
#[cfg(target_arch="wasm32")]
pub extern "C" fn draw(app: *mut Application) {
    let mut app = unsafe { Box::from_raw(app) };
    app.draw();
    Box::into_raw(app);
}

#[doc(hidden)]
#[no_mangle]
#[cfg(target_arch="wasm32")]
pub unsafe extern "C" fn event(app: *mut Application, event_tag: u32) {
    use ffi::wasm;
    let mut app = Box::from_raw(app);
    use input::{BUTTON_STATE_LIST, GAMEPAD_AXIS_LIST, GAMEPAD_BUTTON_LIST, KEY_LIST, MOUSE_BUTTON_LIST};
    let event = match event_tag {
        0 => Event::Closed,
        1 => Event::Focused,
        2 => Event::Unfocused,
        3 => Event::Key(KEY_LIST[wasm::event_data_button() as usize], BUTTON_STATE_LIST[wasm::event_data_state() as usize]),
        4 => Event::MouseMoved(Vector::new(wasm::event_data_f1(), wasm::event_data_f2())),
        5 => Event::MouseEntered,
        6 => Event::MouseExited,
        7 => Event::MouseWheel(Vector::new(wasm::event_data_f1(), wasm::event_data_f2())),
        8 => Event::MouseButton(MOUSE_BUTTON_LIST[wasm::event_data_button() as usize], BUTTON_STATE_LIST[wasm::event_data_state() as usize]),
        9 => Event::GamepadAxis(wasm::event_data_id(), GAMEPAD_AXIS_LIST[wasm::event_data_button() as usize], wasm::event_data_f1()),
        10 => Event::GamepadButton(wasm::event_data_id(), GAMEPAD_BUTTON_LIST[wasm::event_data_button() as usize], BUTTON_STATE_LIST[wasm::event_data_state() as usize]),
        11 => Event::GamepadConnected(wasm::event_data_id()),
        12 => Event::GamepadDisconnected(wasm::event_data_id()),
        _ => {
            Box::into_raw(app);
            return;
        }
    };
    app.event(&event);
    Box::into_raw(app);
}