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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use crate::gl;
use glutin::dpi::*;
use glutin::*;
use std::default::Default;
use std::env;
use std::error::Error;
pub use glutin;
/// Defines a window.
pub struct WindowSettings {
/// Title of the window. Default value: Name of the executable file
pub title: String,
/// Width of the window in logical pixels. Default value: `640.0`
pub width: f32,
/// Height of the window in logical pixels. Default value: `480.0`
pub height: f32,
/// Whether or not the application is a dialog. Default value: `true`
///
/// This only affects x11 environments, where it sets the window
/// type to dialog. In [tiling
/// environments](https://en.wikipedia.org/wiki/Tiling_window_manager),
/// like i3 and sway, this can cause the window to pop up as a
/// floating window, not a tiled one. This is useful for
/// applications that are supposed to be opened for very short
/// amounts of time.
pub is_dialog: bool,
/// This should always be true for everything except benchmarks.
pub vsync: bool,
}
impl Default for WindowSettings {
fn default() -> WindowSettings {
WindowSettings {
title: env::current_exe()
.ok()
.and_then(|p| p.file_name().map(|s| s.to_os_string()))
.and_then(|s| s.into_string().ok())
.unwrap_or_default(),
width: 640.0,
height: 480.0,
is_dialog: false,
vsync: true,
}
}
}
/// Manages the window and propagates events to the UI system.
pub struct Window {
/// The width of the window.
pub width: f32,
/// The height of the window.
pub height: f32,
/// The dpi of the window.
pub dpi_factor: f32,
gl_window: GlWindow,
events_loop: EventsLoop,
/// The opengl legacy status for Renderer.
pub opengl21: bool,
}
impl Window {
/// Creates a new `Window`.
///
/// Can result in an error if window creation fails or OpenGL
/// context creation fails.
pub fn create(settings: &WindowSettings) -> Result<Window, Box<Error>> {
// Note: At the time of writing, wayland support in winit
// seems to be buggy. Default to x11, since xwayland at least
// works.
if cfg!(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "openbsd",
)) {
env::set_var("WINIT_UNIX_BACKEND", "x11");
}
let events_loop = EventsLoop::new();
let opengl21;
let gl_window = {
let create_window = |gl_request, gl_profile| {
let mut window = WindowBuilder::new()
.with_title(settings.title.clone())
.with_dimensions(LogicalSize::new(
f64::from(settings.width),
f64::from(settings.height),
));
if settings.is_dialog {
window = Window::window_as_dialog(window);
}
let context = ContextBuilder::new()
.with_vsync(settings.vsync)
.with_srgb(true)
.with_gl(gl_request)
.with_gl_profile(gl_profile);
GlWindow::new(window, context, &events_loop)
};
if env::var_os("FAE_OPENGL_LEGACY").is_some() {
opengl21 = true;
create_window(
GlRequest::GlThenGles {
opengl_version: (2, 1),
opengles_version: (2, 0),
},
GlProfile::Compatibility,
)?
} else if let Ok(result) = create_window(
GlRequest::GlThenGles {
opengl_version: (3, 3),
opengles_version: (3, 0),
},
GlProfile::Core,
) {
opengl21 = false;
result
} else {
opengl21 = true;
create_window(
GlRequest::GlThenGles {
opengl_version: (2, 1),
opengles_version: (2, 0),
},
GlProfile::Compatibility,
)?
}
};
unsafe {
gl_window.make_current()?;
gl::load_with(|symbol| gl_window.get_proc_address(symbol) as *const _);
/* use std::ffi::CStr;
Uncomment in case of opengl shenanigans
let opengl_version_string = String::from_utf8_lossy(
CStr::from_ptr(gl::GetString(gl::VERSION) as *const _).to_bytes(),
);
if cfg!(debug_assertions) {
println!("OpenGL version: {}", opengl_version_string);
}*/
}
Ok(Window {
width: settings.width,
height: settings.height,
dpi_factor: 1.0,
gl_window,
events_loop,
opengl21,
})
}
/// Re-renders the window, polls for new events and passes them on
/// to the UI system, and clears the screen with the
/// `background_*` colors, which consist of 0.0 - 1.0
/// values. **Note**: Because of vsync, this function will hang
/// for a while (usually 16ms at max).
pub fn refresh<F: FnMut(&Event)>(&mut self, mut event_handler: F) -> bool {
let _ = self.gl_window.swap_buffers();
let mut running = true;
let mut resized_logical_size = None;
self.events_loop.poll_events(|event| {
event_handler(&event);
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => running = false,
WindowEvent::Resized(logical_size) => resized_logical_size = Some(logical_size),
_ => {}
}
}
});
/* Resize event handling */
if let Some(logical_size) = resized_logical_size {
let dpi_factor = self.gl_window.get_hidpi_factor();
let physical_size = logical_size.to_physical(dpi_factor);
let (width, height): (u32, u32) = physical_size.into();
unsafe {
gl::Viewport(0, 0, width as i32, height as i32);
}
self.gl_window.resize(physical_size);
self.width = logical_size.width as f32;
self.height = logical_size.height as f32;
self.dpi_factor = dpi_factor as f32;
}
running
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "openbsd"
))]
fn window_as_dialog(window: WindowBuilder) -> WindowBuilder {
use glutin::os::unix::{WindowBuilderExt, XWindowType};
window.with_x11_window_type(XWindowType::Dialog)
}
#[cfg(not(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "openbsd"
)))]
fn window_as_dialog(window: WindowBuilder) -> WindowBuilder {
window
}
}