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
use rin_gl as gl;
use rin_window::{Window, WindowExt, EventsPoll};
use rin_events::StreamExt;

#[cfg(feature = "stdweb_window")]
use rin_events::TryIter;
#[cfg(feature = "stdweb_window")]
use rin_window::Event;

use rin_util::Result;
use super::ApplicationCallbacks;

use std::cell::RefCell;

#[cfg(feature = "stdweb_window")]
use stdweb::web;

pub struct Application<App>{
    app: RefCell<App>,
    window: Window,
    events: EventsPoll,
    renderer: gl::Renderer<'static>,
}

#[cfg(any(feature = "emscripten_window", feature="stdweb_window"))]
pub struct LoopData<App>{
    app: RefCell<App>,
    events: TryIter<'static, Event>,
}

impl<App: ApplicationCallbacks> Application<App>{
    /// Creates an object that controls the workflow of a rin application
    pub fn new(app: App, events: EventsPoll, window: Window, renderer: gl::Renderer<'static>) -> Result<Application<App>>{
        Ok(Application{
            app: RefCell::new(app),
            events,
            window,
            renderer
        })
    }

    /// Will call update / draw in a loop + any event callbacks as events happen
    ///
    /// This call only returns when the application is done so calls after it won't
    /// be executed untl that happens, usually until the window is closed
    #[cfg(not(any(feature = "emscripten_window", feature="stdweb_window")))]
    pub fn run(mut self){
        let mut events = self.window.event_stream().try_iter();
        while !self.window.should_close(){
            let frame_duration_s = self.window.curr_frame_duration_s();

			self.window.make_current();

			self.app.borrow_mut().update(frame_duration_s, &self.renderer, &mut self.window);
			self.app.borrow_mut().draw(&self.renderer, &self.window);

            self.window.swap_buffers();

            self.events.poll_events();
            self.window.update();

			for event in events.by_ref(){
	            self.app.borrow_mut().handle_event(&self.renderer, &self.window, &event);
			}

        }
    }


    #[cfg(feature = "emscripten_window")]
    fn main_loop<'a>(window: &mut Window, gl: &mut gl::Renderer, data: &mut LoopData<App>){
        let frame_time_s = window.curr_frame_time_s();

        window.make_current();

        data.app.borrow_mut().update(frame_time_s, gl, window);
        data.app.borrow_mut().draw(gl, window);

        window.swap_buffers();
        window.update();

        for event in data.events.by_ref(){
            data.app.borrow_mut().handle_event(gl, window, &event);
        }
    }

    /// Will call update / draw in a loop + any event callbacks as events happen
    ///
    /// This call usually never returns under emscripten
    #[cfg(feature = "emscripten_window")]
    pub fn run(mut self){
        let events = self.window.event_stream().try_iter();
        let loop_data = LoopData{
            app: self.app,
            events,
        };
        self.window.start_main_loop(Application::main_loop, self.renderer, loop_data);
    }


    #[cfg(feature = "stdweb_window")]
    fn main_loop<'a>(elapsed: f64, mut window: Window, gl: gl::Renderer<'static>, mut data:LoopData<App>) where App: 'static{
        window.set_time_millis(elapsed);
        let frame_time_s = window.curr_frame_duration().as_secs_f64();

        window.make_current();

        data.app.borrow_mut().update(frame_time_s, &gl, &mut window);
        data.app.borrow_mut().draw(&gl, &window);

        window.swap_buffers();
        window.update();

        for event in data.events.by_ref(){
            data.app.borrow_mut().handle_event(&gl, &window, &event);
        }

        web::window().request_animation_frame(move |elapsed|
            Application::main_loop(elapsed, window, gl, data));
    }

    /// Will call update / draw in a loop + any event callbacks as events happen
    ///
    /// This call usually never returns under stdweb_window since the virtual
    /// window can never be closed
    #[cfg(feature = "stdweb_window")]
    pub fn run(mut self) where App: 'static{
        let events = self.window.event_stream().try_iter();
        let loop_data = LoopData{
            app: self.app,
            events,
        };
        let window = self.window;
        let renderer = self.renderer;
        web::window().request_animation_frame(move |elapsed|
            Application::main_loop(elapsed, window, renderer, loop_data));
    }

    #[cfg(target_os="android")]
    fn handle_event(&mut self, event: &(u64,WindowEvent)){
        let &(_time,event) = event;
        match event{
            KeyPressedEvent{key}	    => {app.borrow_mut().key_pressed(key);},
            KeyReleasedEvent{key}       => {app.borrow_mut().key_released(key);},
            MouseMovedEvent{pos}        => {app.borrow_mut().mouse_moved(pos);}
            MousePressedEvent{pos,button}   => {app.borrow_mut().mouse_pressed(pos,button);}
            MouseReleasedEvent{pos,button}  => {app.borrow_mut().mouse_released(pos,button);}
            WindowMovedEvent{pos}       => {app.borrow_mut().window_moved(pos);}
            WindowResizedEvent{size}    => {app.borrow_mut().window_resized(size);}
            ScrollEvent{scroll}         => {app.borrow_mut().scroll(scroll);}
            WindowClosingEvent          => {
                debug!("closing");
                app.borrow_mut().window_closing();
            }
            PauseEvent                  => {
                debug!("pause");
                app_data.borrow().window.borrow_mut().set_should_close();
                app.borrow_mut().pause();
            }
            StopEvent                   => {
                debug!("stop");
                app_data.borrow().window.borrow_mut().set_should_close();
                app.borrow_mut().stop();
            }
            ResumeEvent                 => {
                debug!("resume");
                app.borrow_mut().resume();
            }
            DestroyEvent                => {
                debug!("destroy");
                app_data.borrow().window.borrow_mut().set_should_close();
                app.borrow_mut().destroy();
            }
            GLReadyEvent                => {app.borrow_mut().gl_ready();}
            _ => {}
        }
    }
}