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
use sdl2::video::FullscreenType;
use crate::graphics::{self, ScreenScaling};
use crate::{Context, Result, TetraError};
pub fn quit(ctx: &mut Context) {
ctx.running = false;
}
pub fn get_title(ctx: &Context) -> &str {
ctx.window.title()
}
pub fn set_title(ctx: &mut Context, title: &str) {
ctx.window.set_title(title).unwrap();
}
pub fn get_width(ctx: &Context) -> i32 {
ctx.window_width
}
pub fn set_width(ctx: &mut Context, width: i32) {
set_size_ex(ctx, width, ctx.window_height, false);
}
pub fn get_height(ctx: &Context) -> i32 {
ctx.window_height
}
pub fn set_height(ctx: &mut Context, height: i32) {
set_size_ex(ctx, ctx.window_width, height, false);
}
pub fn get_size(ctx: &Context) -> (i32, i32) {
(ctx.window_width, ctx.window_height)
}
pub fn set_size(ctx: &mut Context, width: i32, height: i32) {
set_size_ex(ctx, width, height, false);
}
pub(crate) fn set_size_ex(ctx: &mut Context, width: i32, height: i32, from_sdl: bool) {
ctx.window_width = width;
ctx.window_height = height;
graphics::set_window_projection(ctx, width, height);
if let ScreenScaling::Resize = graphics::get_scaling(ctx) {
graphics::set_backbuffer_size(ctx, width, height);
}
graphics::update_screen_rect(ctx);
if !from_sdl {
ctx.window.set_size(width as u32, height as u32).unwrap();
}
}
pub fn toggle_fullscreen(ctx: &mut Context) -> Result {
if ctx.fullscreen {
disable_fullscreen(ctx)
} else {
enable_fullscreen(ctx)
}
}
pub fn enable_fullscreen(ctx: &mut Context) -> Result {
if !ctx.fullscreen {
ctx.window
.display_mode()
.and_then(|m| {
set_size_ex(ctx, m.w, m.h, false);
ctx.window.set_fullscreen(FullscreenType::Desktop)
})
.map(|_| ())
.map_err(TetraError::Sdl)
} else {
Ok(())
}
}
pub fn disable_fullscreen(ctx: &mut Context) -> Result {
if ctx.fullscreen {
ctx.window
.set_fullscreen(FullscreenType::Off)
.map(|_| {
let size = ctx.window.drawable_size();
set_size_ex(ctx, size.0 as i32, size.1 as i32, false);
})
.map_err(TetraError::Sdl)
} else {
Ok(())
}
}
pub fn is_fullscreen(ctx: &Context) -> bool {
ctx.fullscreen
}
pub fn show_mouse(ctx: &mut Context) {
ctx.sdl.mouse().show_cursor(true);
}
pub fn hide_mouse(ctx: &mut Context) {
ctx.sdl.mouse().show_cursor(false);
}
pub fn is_mouse_visible(ctx: &mut Context) {
ctx.sdl.mouse().is_cursor_showing();
}