1 // Copyright 2016 The GLFW-RS Developers. For a full listing of the authors,
2 // refer to the AUTHORS file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 // NOTE: Make sure to resize the OpenGL viewport (or whatever else) after any size changes.
17
18 extern crate glfw;
19
20 use glfw::{Action, Context, Key};
21
main()22 fn main() {
23 let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
24
25 let (mut window, events) = glfw.create_window(600, 400, "Press F11 to toggle Fullscreen (it will be blank)", glfw::WindowMode::Windowed)
26 .expect("Failed to create GLFW window.");
27
28 window.set_key_polling(true);
29 window.make_current();
30 glfw.set_swap_interval(glfw::SwapInterval::Sync(1));
31
32 //Store fullscreen state
33 let mut is_fullscreen = false;
34
35 //Keep track of last position and size so we can restore the window to the originals
36 let mut last_pos = (0, 0);
37 let mut last_size = (0, 0);
38
39 while !window.should_close() {
40 glfw.poll_events();
41 for (_, event) in glfw::flush_messages(&events) {
42 match event {
43 glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => {
44 window.set_should_close(true)
45 }
46 //F11 is pretty standard for fullscreen
47 glfw::WindowEvent::Key(Key::F11, _, Action::Press, _) => {
48 if is_fullscreen {
49 window.set_monitor(glfw::WindowMode::Windowed, last_pos.0, last_pos.1, last_size.0 as u32, last_size.1 as u32, None);
50 println!("Window restored to {:?} at location {:?}", last_size, last_pos);
51 } else {
52 last_pos = window.get_pos();
53 last_size = window.get_size();
54
55 glfw.with_primary_monitor_mut(|_: &mut _, m: Option<&glfw::Monitor>| {
56 let monitor = m.unwrap();
57
58 let mode = monitor.get_video_mode().unwrap();
59
60 window.set_monitor(glfw::WindowMode::FullScreen(&monitor), 0, 0, mode.width, mode.height, Some(mode.refresh_rate));
61
62 println!("{}x{} fullscreen enabled at {}Hz on monitor {}", mode.width, mode.height, mode.refresh_rate, monitor.get_name().unwrap());
63 });
64 }
65
66 is_fullscreen = !is_fullscreen;
67 }
68 _ => {}
69 }
70 }
71 }
72 }