From 7b2483622d257d07646896467f9f7eea77343866 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Wed, 1 Jun 2016 16:33:50 -0700 Subject: [PATCH] Redesigned console API --- ctru-sys/src/console.rs | 25 +++++++++++++++---------- src/console.rs | 39 +++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/ctru-sys/src/console.rs b/ctru-sys/src/console.rs index 4d0c0e0..fcaa747 100644 --- a/ctru-sys/src/console.rs +++ b/ctru-sys/src/console.rs @@ -35,26 +35,31 @@ pub struct PrintConsole { pub consoleInitialised: u8, } -pub const CONSOLE_COLOR_BOLD: i32 = 1; -pub const CONSOLE_COLOR_FAINT: i32 = 2; -pub const CONSOLE_ITALIC: i32 = 4; -pub const CONSOLE_UNDERLINE: i32 = 8; -pub const CONSOLE_BLINK_SLOW: i32 = 16; -pub const CONSOLE_BLINK_FAST: i32 = 32; +pub const CONSOLE_COLOR_BOLD: i32 = 1; +pub const CONSOLE_COLOR_FAINT: i32 = 2; +pub const CONSOLE_ITALIC: i32 = 4; +pub const CONSOLE_UNDERLINE: i32 = 8; +pub const CONSOLE_BLINK_SLOW: i32 = 16; +pub const CONSOLE_BLINK_FAST: i32 = 32; pub const CONSOLE_COLOR_REVERSE: i32 = 64; -pub const CONSOLE_CONCEAL: i32 = 128; +pub const CONSOLE_CONCEAL: i32 = 128; #[repr(C)] pub enum debugDevice { - NULL = 0, - _3DMOO = 1, + NULL = 0, + _3DMOO = 1, CONSOLE = 2, } extern "C" { pub fn consoleSetFont(console: *mut PrintConsole, font: *mut ConsoleFont) -> (); - pub fn consoleSetWindow(console: *mut PrintConsole, x: i32, y: i32, width: i32, height: i32) -> (); + pub fn consoleSetWindow(console: *mut PrintConsole, + x: i32, + y: i32, + width: i32, + height: i32) + -> (); pub fn consoleGetDefault() -> *mut PrintConsole; pub fn consoleSelect(console: *mut PrintConsole) -> *mut PrintConsole; pub fn consoleInit(screen: gfxScreen_t, console: *mut PrintConsole) -> *mut PrintConsole; diff --git a/src/console.rs b/src/console.rs index c8c9601..6531c6d 100644 --- a/src/console.rs +++ b/src/console.rs @@ -1,31 +1,42 @@ -use libctru::console::{PrintConsole, consoleInit, consoleClear}; +use libctru::console::{consoleInit, consoleClear}; use libctru::gfx; +use core::default::Default; +use core::marker::PhantomData; use core::ptr; extern "C" { fn putchar(ch: u8) -> i32; } -pub fn console_default_init() -> *mut PrintConsole { - unsafe { consoleInit(gfx::gfxScreen_t::GFX_TOP, ptr::null_mut()) } +pub struct Console { + pd: PhantomData, } -pub fn console_write<'a>(s: &'a str) { - unsafe { - for c in s.as_bytes().iter() { - putchar(*c); +impl Console { + pub fn write<'a>(&mut self, s: &'a str) { + unsafe { + for ch in s.as_bytes().iter() { + putchar(*ch); + } } } -} -pub fn console_writeln<'a>(s: &'a str) { - unsafe { - console_write(s); - putchar('\n' as u8); + pub fn writeln<'a>(&mut self, s: &'a str) { + unsafe { + self.write(s); + putchar('\n' as u8); + } + } + + pub fn clear(&mut self) { + unsafe { consoleClear() } } } -pub fn console_clear() { - unsafe { consoleClear() } +impl Default for Console { + fn default() -> Self { + unsafe { consoleInit(gfx::gfxScreen_t::GFX_TOP, ptr::null_mut()); } + Console { pd: PhantomData } + } }