e2rcore/implement/window/
winglutin.rs

1extern crate gl;
2extern crate glutin;
3
4use std::ops::FnMut;
5
6use self::glutin::Context;
7use self::glutin::dpi::{LogicalSize,PhysicalSize,PhysicalPosition};
8use self::glutin::ContextTrait;
9
10use interface::i_window::IWindow;
11
12pub struct WinGlutinBase {
13    pub _eventsloop: glutin::EventsLoop,
14}
15
16pub struct WinGlutinWin {
17    pub _wingl: glutin::WindowedContext,
18}
19
20pub struct WinGlutin {
21    pub _base: WinGlutinBase,
22    pub _win: WinGlutinWin,
23}
24
25pub struct DummySignalRequestType {
26}
27
28impl IWindow for WinGlutin {
29
30    type EventType = glutin::Event;
31    type SignalRequestType = DummySignalRequestType;
32
33    fn new( w: u64, h: u64 ) -> WinGlutin {
34        let gl_request = glutin::GlRequest::Latest;
35
36        let wb = glutin::WindowBuilder::new().with_dimensions(
37            LogicalSize::from( (w as u32,
38                                h as u32) ) );
39
40        let base = WinGlutinBase {
41            _eventsloop: glutin::EventsLoop::new(),
42        };
43        
44        let c = glutin::ContextBuilder::new()
45            .with_vsync( true )
46            .with_gl( gl_request )
47            .build_windowed( wb, &base._eventsloop )
48            .unwrap();
49        
50        // let w = WinGlutinWin {
51        //     _wingl: glutin::Window::new( wb, c, &base._eventsloop ).unwrap(),
52        // };
53        
54        // let w = WinGlutinWin {
55        //     _wingl: wb.build( &base._eventsloop ).unwrap(),
56        // };
57
58        let w = WinGlutinWin {
59            _wingl: c, //windowed context
60        };
61        
62        WinGlutin {
63            _base: base,
64            _win: w,
65        }
66    }
67    fn make_current( & self ) -> Result< (), & 'static str > {
68        unsafe {
69            self._win._wingl.make_current().unwrap();
70        }
71        gl::load_with( |symbol| self._win._wingl.get_proc_address(symbol) as * const _ );
72        Ok( () )
73    }
74    fn handle_events < F > ( & mut self, cb: F ) -> ()
75        where F : FnMut( Self::EventType ) -> () {
76        self._base._eventsloop.poll_events( cb );
77        ()
78    }
79    fn handle_events_pass_thru( & mut self ) -> Option< Self::EventType > {
80        //todo: handle specific events as requested via handle_signal_request
81        let mut e = None;
82        self._base._eventsloop.poll_events( |event| {
83            e = Some(event);
84            ()
85        } );
86        e
87    }
88    fn swap_buf( & self ) -> () {
89        self._win._wingl.swap_buffers().unwrap();
90        ()
91    }
92    fn handle_signal_request( & mut self, _sig: & [ Self::SignalRequestType ] ) -> Result< (), & 'static str > {
93        //todo
94        Ok( () )
95    }
96    fn per_frame_setup( & mut self ) -> Result< (), & 'static str > {
97        unsafe {
98            gl::ClearColor( 0.9, 0.9, 0.9, 1.0 );
99            gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
100        }
101        Ok( () )
102    }
103
104    fn get_offset( & self ) -> Option<(i32,i32)> {
105        match self._win._wingl.get_position() {
106            Some( logical_pos ) => {
107                // let dpi = self._win._wingl.get_current_monitor().get_hidpi_factor();
108                let dpi = self._win._wingl.get_hidpi_factor();
109                Some( logical_pos.to_physical( dpi ).into() )
110                // Some( logical_pos.into() )
111            },
112            _ => None,
113        }
114    }
115    
116    fn get_size( & self ) -> Option<(u32,u32)> {
117        match self._win._wingl.get_inner_size() {
118            Some( logical_size ) => {
119                // let dpi = self._win._wingl.get_hidpi_factor();
120                let dpi = self._win._wingl.get_current_monitor().get_hidpi_factor();
121                Some( logical_size.to_physical( dpi ).into() )                
122                // Some( logical_size.into() )
123            },
124            _ => None,
125        }
126    }
127
128}