aboutsummaryrefslogtreecommitdiff
path: root/ctru-rs/src/console.rs
blob: 9f6643a823c41f252df1e60194ab9299aacf2a7f (plain) (blame)
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
use std::default::Default;

use libctru::{PrintConsole, consoleInit, consoleSelect, consoleClear, consoleSetWindow};

use gfx::Screen;

pub struct Console {
    context: Box<PrintConsole>,
}

impl Console {
    /// Initialize a console on the chosen screen, overwriting whatever was on the screen
    /// previously (including other consoles). The new console is automatically selected for
    /// printing.
    pub fn init(screen: Screen) -> Self {
        unsafe {
            let mut context = Box::new(PrintConsole::default());
            consoleInit(screen.into(), context.as_mut());
            Console { context, }
        }
    }

    /// Select this console as the current target for stdout
    pub fn select(&self) {
        unsafe { consoleSelect(self.context.as_ref() as *const _ as *mut _); }
    }

    /// Clears all text from the console
    pub fn clear(&self) {
        unsafe { consoleClear() }
    }

    /// Resizes the active console to fit in a smaller portion of the screen.
    ///
    /// The first two arguments are the desired coordinates of the top-left corner
    /// of the console, and the second pair is the new width and height
    ///
    /// This function is unsafe because it does not validate that the input will produce
    /// a console that actually fits on the screen
    pub unsafe fn set_window(&mut self, x: i32, y: i32, width: i32, height: i32) {
        consoleSetWindow(self.context.as_mut(), x, y, width, height);
    } 
}

impl Default for Console {
    fn default() -> Self {
        Console::init(Screen::Top)
    }
}