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
//! Functions and types relating to the game window.

use sdl2::video::FullscreenType;

use crate::graphics::{self, ScreenScaling};
use crate::{Context, Result, TetraError};

/// Quits the game, if it is currently running.
///
/// Note that currently, quitting the game does not take effect until the end of the current
/// cycle of the game loop. This will probably change later.
pub fn quit(ctx: &mut Context) {
    ctx.running = false;
}

/// Gets the current title of the window.
pub fn get_title(ctx: &Context) -> &str {
    ctx.window.title()
}

/// Sets the title of the window.
pub fn set_title(ctx: &mut Context, title: &str) {
    ctx.window.set_title(title).unwrap();
}

/// Gets the width of the window.
pub fn get_width(ctx: &Context) -> i32 {
    ctx.window_width
}

/// Sets the width of the window.
pub fn set_width(ctx: &mut Context, width: i32) {
    set_size_ex(ctx, width, ctx.window_height, false);
}

/// Gets the height of the window.
pub fn get_height(ctx: &Context) -> i32 {
    ctx.window_height
}

/// Sets the height of the window.
pub fn set_height(ctx: &mut Context, height: i32) {
    set_size_ex(ctx, ctx.window_width, height, false);
}

/// Gets the size of the window.
pub fn get_size(ctx: &Context) -> (i32, i32) {
    (ctx.window_width, ctx.window_height)
}

/// Sets the size of the window.
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();
    }
}

/// Enables fullscreen if it is currently disabled, or vice-versa.
pub fn toggle_fullscreen(ctx: &mut Context) -> Result {
    if ctx.fullscreen {
        disable_fullscreen(ctx)
    } else {
        enable_fullscreen(ctx)
    }
}

/// Enables fullscreen.
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(())
    }
}

/// Disables fullscreen.
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(())
    }
}

/// Returns whether or not the window is currently in fullscreen mode.
pub fn is_fullscreen(ctx: &Context) -> bool {
    ctx.fullscreen
}

/// Makes the mouse cursor visible.
pub fn show_mouse(ctx: &mut Context) {
    ctx.sdl.mouse().show_cursor(true);
}

/// Hides the mouse cursor.
pub fn hide_mouse(ctx: &mut Context) {
    ctx.sdl.mouse().show_cursor(false);
}

/// Returns whether or not the mouse cursor is currently visible.
pub fn is_mouse_visible(ctx: &mut Context) {
    ctx.sdl.mouse().is_cursor_showing();
}