From 5257d16aee9653b1df21c408d6744827ceb83476 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 6 Apr 2023 09:59:55 +0200 Subject: [PATCH 01/21] Renamed init functions to new --- ctru-rs/examples/audio-filters.rs | 10 +++++----- ctru-rs/examples/buttons.rs | 8 ++++---- ctru-rs/examples/camera-image.rs | 10 +++++----- ctru-rs/examples/file-explorer.rs | 14 +++++++------- ctru-rs/examples/futures-basic.rs | 8 ++++---- ctru-rs/examples/futures-tokio.rs | 8 ++++---- ctru-rs/examples/gfx-3d-mode.rs | 8 ++++---- ctru-rs/examples/gfx-wide-mode.rs | 10 +++++----- ctru-rs/examples/graphics-bitmap.rs | 8 ++++---- ctru-rs/examples/hashmaps.rs | 8 ++++---- ctru-rs/examples/hello-both-screens.rs | 10 +++++----- ctru-rs/examples/hello-world.rs | 8 ++++---- ctru-rs/examples/linear-memory.rs | 8 ++++---- ctru-rs/examples/mii-selector.rs | 10 +++++----- ctru-rs/examples/network-sockets.rs | 10 +++++----- ctru-rs/examples/output-3dslink.rs | 8 ++++---- ctru-rs/examples/romfs.rs | 10 +++++----- ctru-rs/examples/software-keyboard.rs | 10 +++++----- ctru-rs/examples/system-configuration.rs | 10 +++++----- ctru-rs/examples/thread-basic.rs | 8 ++++---- ctru-rs/examples/thread-info.rs | 8 ++++---- ctru-rs/examples/thread-locals.rs | 8 ++++---- ctru-rs/examples/time-rtc.rs | 8 ++++---- ctru-rs/examples/title-info.rs | 12 ++++++------ ctru-rs/src/applets/mii_selector.rs | 4 ++-- ctru-rs/src/applets/swkbd.rs | 4 ++-- ctru-rs/src/console.rs | 2 +- ctru-rs/src/lib.rs | 2 +- ctru-rs/src/services/am.rs | 2 +- ctru-rs/src/services/apt.rs | 2 +- ctru-rs/src/services/cam.rs | 2 +- ctru-rs/src/services/cfgu.rs | 2 +- ctru-rs/src/services/fs.rs | 18 +++++++++--------- ctru-rs/src/services/gfx.rs | 6 +++--- ctru-rs/src/services/hid.rs | 2 +- ctru-rs/src/services/ndsp/mod.rs | 2 +- ctru-rs/src/services/romfs.rs | 4 ++-- ctru-rs/src/services/soc.rs | 6 +++--- ctru-rs/src/services/sslc.rs | 2 +- ctru-rs/src/test_runner.rs | 8 ++++---- 40 files changed, 144 insertions(+), 144 deletions(-) diff --git a/ctru-rs/examples/audio-filters.rs b/ctru-rs/examples/audio-filters.rs index 327ede4..e425793 100644 --- a/ctru-rs/examples/audio-filters.rs +++ b/ctru-rs/examples/audio-filters.rs @@ -37,10 +37,10 @@ fn fill_buffer(audio_data: &mut [u8], frequency: f32) { fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let _console = Console::new(gfx.top_screen.borrow_mut()); let mut note: usize = 4; @@ -68,7 +68,7 @@ fn main() { let mut wave_info1 = Wave::new(audio_data1, AudioFormat::PCM16Stereo, false); let mut wave_info2 = Wave::new(audio_data2, AudioFormat::PCM16Stereo, false); - let mut ndsp = Ndsp::init().expect("Couldn't obtain NDSP controller"); + let mut ndsp = Ndsp::new().expect("Couldn't obtain NDSP controller"); // This line isn't needed since the default NDSP configuration already sets the output mode to `Stereo` ndsp.set_output_mode(OutputMode::Stereo); diff --git a/ctru-rs/examples/buttons.rs b/ctru-rs/examples/buttons.rs index 39f24d9..5645915 100644 --- a/ctru-rs/examples/buttons.rs +++ b/ctru-rs/examples/buttons.rs @@ -3,10 +3,10 @@ use ctru::prelude::*; fn main() { ctru::use_panic_handler(); - let apt = Apt::init().unwrap(); - let mut hid = Hid::init().unwrap(); - let gfx = Gfx::init().unwrap(); - let console = Console::init(gfx.top_screen.borrow_mut()); + let apt = Apt::new().unwrap(); + let mut hid = Hid::new().unwrap(); + let gfx = Gfx::new().unwrap(); + let console = Console::new(gfx.top_screen.borrow_mut()); println!("Hi there! Try pressing a button"); println!("\x1b[29;16HPress Start to exit"); diff --git a/ctru-rs/examples/camera-image.rs b/ctru-rs/examples/camera-image.rs index 46a96d4..88526be 100644 --- a/ctru-rs/examples/camera-image.rs +++ b/ctru-rs/examples/camera-image.rs @@ -16,22 +16,22 @@ const WAIT_TIMEOUT: Duration = Duration::from_millis(300); fn main() { ctru::use_panic_handler(); - let apt = Apt::init().expect("Failed to initialize Apt service."); - let mut hid = Hid::init().expect("Failed to initialize Hid service."); - let gfx = Gfx::init().expect("Failed to initialize GFX service."); + let apt = Apt::new().expect("Failed to initialize Apt service."); + let mut hid = Hid::new().expect("Failed to initialize Hid service."); + let gfx = Gfx::new().expect("Failed to initialize GFX service."); gfx.top_screen.borrow_mut().set_double_buffering(true); gfx.top_screen .borrow_mut() .set_framebuffer_format(FramebufferFormat::Rgb565); gfx.bottom_screen.borrow_mut().set_double_buffering(false); - let _console = Console::init(gfx.bottom_screen.borrow_mut()); + let _console = Console::new(gfx.bottom_screen.borrow_mut()); let mut keys_down; println!("Initializing camera"); - let mut cam = Cam::init().expect("Failed to initialize CAM service."); + let mut cam = Cam::new().expect("Failed to initialize CAM service."); { let camera = &mut cam.outer_right_cam; diff --git a/ctru-rs/examples/file-explorer.rs b/ctru-rs/examples/file-explorer.rs index 5b256c9..8a3d639 100644 --- a/ctru-rs/examples/file-explorer.rs +++ b/ctru-rs/examples/file-explorer.rs @@ -11,14 +11,14 @@ use std::path::{Path, PathBuf}; fn main() { ctru::use_panic_handler(); - let apt = Apt::init().unwrap(); - let mut hid = Hid::init().unwrap(); - let gfx = Gfx::init().unwrap(); + let apt = Apt::new().unwrap(); + let mut hid = Hid::new().unwrap(); + let gfx = Gfx::new().unwrap(); #[cfg(all(feature = "romfs", romfs_exists))] - let _romfs = ctru::services::romfs::RomFS::init().unwrap(); + let _romfs = ctru::services::romfs::RomFS::new().unwrap(); - FileExplorer::init(&apt, &mut hid, &gfx).run(); + FileExplorer::new(&apt, &mut hid, &gfx).run(); } struct FileExplorer<'a> { @@ -32,10 +32,10 @@ struct FileExplorer<'a> { } impl<'a> FileExplorer<'a> { - fn init(apt: &'a Apt, hid: &'a mut Hid, gfx: &'a Gfx) -> Self { + fn new(apt: &'a Apt, hid: &'a mut Hid, gfx: &'a Gfx) -> Self { let mut top_screen = gfx.top_screen.borrow_mut(); top_screen.set_wide_mode(true); - let console = Console::init(top_screen); + let console = Console::new(top_screen); FileExplorer { apt, diff --git a/ctru-rs/examples/futures-basic.rs b/ctru-rs/examples/futures-basic.rs index ee2e3be..cb6a804 100644 --- a/ctru-rs/examples/futures-basic.rs +++ b/ctru-rs/examples/futures-basic.rs @@ -15,10 +15,10 @@ use std::os::horizon::thread::BuilderExt; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let _console = Console::new(gfx.top_screen.borrow_mut()); // Give ourselves up to 30% of the system core's time apt.set_app_cpu_time_limit(30) diff --git a/ctru-rs/examples/futures-tokio.rs b/ctru-rs/examples/futures-tokio.rs index cbb446d..e42e9e9 100644 --- a/ctru-rs/examples/futures-tokio.rs +++ b/ctru-rs/examples/futures-tokio.rs @@ -8,10 +8,10 @@ use std::time::Duration; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let _console = Console::new(gfx.top_screen.borrow_mut()); // Give ourselves up to 30% of the system core's time apt.set_app_cpu_time_limit(30) diff --git a/ctru-rs/examples/gfx-3d-mode.rs b/ctru-rs/examples/gfx-3d-mode.rs index 13b4d3c..c5b194c 100644 --- a/ctru-rs/examples/gfx-3d-mode.rs +++ b/ctru-rs/examples/gfx-3d-mode.rs @@ -12,10 +12,10 @@ static ZERO: &[u8] = &[0; IMAGE.len()]; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.bottom_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let _console = Console::new(gfx.bottom_screen.borrow_mut()); println!("Press Start to exit.\nPress A to switch sides (be sure to have 3D mode enabled)."); diff --git a/ctru-rs/examples/gfx-wide-mode.rs b/ctru-rs/examples/gfx-wide-mode.rs index f781c89..53da6ea 100644 --- a/ctru-rs/examples/gfx-wide-mode.rs +++ b/ctru-rs/examples/gfx-wide-mode.rs @@ -3,10 +3,10 @@ use ctru::prelude::*; fn main() { ctru::use_panic_handler(); - let apt = Apt::init().unwrap(); - let mut hid = Hid::init().unwrap(); - let gfx = Gfx::init().unwrap(); - let mut console = Console::init(gfx.top_screen.borrow_mut()); + let apt = Apt::new().unwrap(); + let mut hid = Hid::new().unwrap(); + let gfx = Gfx::new().unwrap(); + let mut console = Console::new(gfx.top_screen.borrow_mut()); println!("Press A to enable/disable wide screen mode."); @@ -23,7 +23,7 @@ fn main() { let wide_mode = gfx.top_screen.borrow().is_wide(); gfx.top_screen.borrow_mut().set_wide_mode(!wide_mode); - console = Console::init(gfx.top_screen.borrow_mut()); + console = Console::new(gfx.top_screen.borrow_mut()); println!("Press A to enable/disable wide screen mode."); } diff --git a/ctru-rs/examples/graphics-bitmap.rs b/ctru-rs/examples/graphics-bitmap.rs index ebbf53e..55d4903 100644 --- a/ctru-rs/examples/graphics-bitmap.rs +++ b/ctru-rs/examples/graphics-bitmap.rs @@ -17,10 +17,10 @@ static IMAGE: &[u8] = include_bytes!("assets/ferris.rgb"); fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let _console = Console::new(gfx.top_screen.borrow_mut()); println!("\x1b[21;16HPress Start to exit."); diff --git a/ctru-rs/examples/hashmaps.rs b/ctru-rs/examples/hashmaps.rs index 87ad8be..e3726eb 100644 --- a/ctru-rs/examples/hashmaps.rs +++ b/ctru-rs/examples/hashmaps.rs @@ -8,10 +8,10 @@ fn main() { // HashMaps generate hashes thanks to the 3DS' cryptografically secure generator. // This generator is only active when activating the `PS` service. // This service is automatically initialized. - let apt = Apt::init().unwrap(); - let mut hid = Hid::init().unwrap(); - let gfx = Gfx::init().unwrap(); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let apt = Apt::new().unwrap(); + let mut hid = Hid::new().unwrap(); + let gfx = Gfx::new().unwrap(); + let _console = Console::new(gfx.top_screen.borrow_mut()); let mut map = std::collections::HashMap::new(); map.insert("A Key!", 102); diff --git a/ctru-rs/examples/hello-both-screens.rs b/ctru-rs/examples/hello-both-screens.rs index 7395967..fa2e16d 100644 --- a/ctru-rs/examples/hello-both-screens.rs +++ b/ctru-rs/examples/hello-both-screens.rs @@ -3,16 +3,16 @@ use ctru::prelude::*; fn main() { ctru::use_panic_handler(); - let apt = Apt::init().unwrap(); - let mut hid = Hid::init().unwrap(); - let gfx = Gfx::init().unwrap(); + let apt = Apt::new().unwrap(); + let mut hid = Hid::new().unwrap(); + let gfx = Gfx::new().unwrap(); // Start a console on the top screen - let top_screen = Console::init(gfx.top_screen.borrow_mut()); + let top_screen = Console::new(gfx.top_screen.borrow_mut()); // Start a console on the bottom screen. // The most recently initialized console will be active by default - let bottom_screen = Console::init(gfx.bottom_screen.borrow_mut()); + let bottom_screen = Console::new(gfx.bottom_screen.borrow_mut()); // Let's print on the top screen first top_screen.select(); diff --git a/ctru-rs/examples/hello-world.rs b/ctru-rs/examples/hello-world.rs index 697ec7c..345ee2e 100644 --- a/ctru-rs/examples/hello-world.rs +++ b/ctru-rs/examples/hello-world.rs @@ -5,10 +5,10 @@ use std::io::BufWriter; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let _console = Console::new(gfx.top_screen.borrow_mut()); let out = b"Hello fellow Rustaceans, I'm on the Nintendo 3DS!"; let width = 24; diff --git a/ctru-rs/examples/linear-memory.rs b/ctru-rs/examples/linear-memory.rs index b3afced..2373941 100644 --- a/ctru-rs/examples/linear-memory.rs +++ b/ctru-rs/examples/linear-memory.rs @@ -6,10 +6,10 @@ use ctru::prelude::*; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let _console = Console::new(gfx.top_screen.borrow_mut()); let linear_space_before = LinearAllocator::free_space(); diff --git a/ctru-rs/examples/mii-selector.rs b/ctru-rs/examples/mii-selector.rs index ef7d7d3..9ef86f6 100644 --- a/ctru-rs/examples/mii-selector.rs +++ b/ctru-rs/examples/mii-selector.rs @@ -4,12 +4,12 @@ use ctru::prelude::*; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let _console = Console::new(gfx.top_screen.borrow_mut()); - let mut mii_selector = MiiSelector::init(); + let mut mii_selector = MiiSelector::new(); mii_selector.set_initial_index(3); mii_selector.blacklist_user_mii(0.into()); mii_selector.set_title("Great Mii Selector!"); diff --git a/ctru-rs/examples/network-sockets.rs b/ctru-rs/examples/network-sockets.rs index ca2d163..bbea510 100644 --- a/ctru-rs/examples/network-sockets.rs +++ b/ctru-rs/examples/network-sockets.rs @@ -7,14 +7,14 @@ use std::time::Duration; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().unwrap(); - let _console = Console::init(gfx.top_screen.borrow_mut()); - let mut hid = Hid::init().unwrap(); - let apt = Apt::init().unwrap(); + let gfx = Gfx::new().unwrap(); + let _console = Console::new(gfx.top_screen.borrow_mut()); + let mut hid = Hid::new().unwrap(); + let apt = Apt::new().unwrap(); println!("\nlibctru sockets demo\n"); - let soc = Soc::init().unwrap(); + let soc = Soc::new().unwrap(); let server = TcpListener::bind("0.0.0.0:80").unwrap(); server.set_nonblocking(true).unwrap(); diff --git a/ctru-rs/examples/output-3dslink.rs b/ctru-rs/examples/output-3dslink.rs index 1fe0c6a..5649e6e 100644 --- a/ctru-rs/examples/output-3dslink.rs +++ b/ctru-rs/examples/output-3dslink.rs @@ -13,11 +13,11 @@ use ctru::prelude::*; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); - let mut soc = Soc::init().expect("Couldn't obtain SOC controller"); + let mut soc = Soc::new().expect("Couldn't obtain SOC controller"); soc.redirect_to_3dslink(true, true) .expect("unable to redirect stdout/err to 3dslink server"); diff --git a/ctru-rs/examples/romfs.rs b/ctru-rs/examples/romfs.rs index c729c32..f92d81d 100644 --- a/ctru-rs/examples/romfs.rs +++ b/ctru-rs/examples/romfs.rs @@ -3,17 +3,17 @@ use ctru::prelude::*; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let _console = Console::new(gfx.top_screen.borrow_mut()); cfg_if::cfg_if! { // Run this code if RomFS are wanted and available // This never fails as `ctru-rs` examples inherit all of the `ctru` features, // but it might if a normal user application wasn't setup correctly if #[cfg(all(feature = "romfs", romfs_exists))] { - let _romfs = ctru::services::romfs::RomFS::init().unwrap(); + let _romfs = ctru::services::romfs::RomFS::new().unwrap(); let f = std::fs::read_to_string("romfs:/test-file.txt").unwrap(); println!("Contents of test-file.txt: \n{f}\n"); diff --git a/ctru-rs/examples/software-keyboard.rs b/ctru-rs/examples/software-keyboard.rs index 954fdd0..77e877a 100644 --- a/ctru-rs/examples/software-keyboard.rs +++ b/ctru-rs/examples/software-keyboard.rs @@ -4,10 +4,10 @@ use ctru::prelude::*; fn main() { ctru::use_panic_handler(); - let apt = Apt::init().unwrap(); - let mut hid = Hid::init().unwrap(); - let gfx = Gfx::init().unwrap(); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let apt = Apt::new().unwrap(); + let mut hid = Hid::new().unwrap(); + let gfx = Gfx::new().unwrap(); + let _console = Console::new(gfx.top_screen.borrow_mut()); println!("Press A to enter some text or press Start to quit"); @@ -20,7 +20,7 @@ fn main() { if hid.keys_down().contains(KeyPad::A) { // Prepares a software keyboard with two buttons: One to cancel input and one - // to accept it. You can also use `Swkbd::init()` to launch the keyboard in different + // to accept it. You can also use `Swkbd::new()` to launch the keyboard in different // configurations. let mut keyboard = Swkbd::default(); diff --git a/ctru-rs/examples/system-configuration.rs b/ctru-rs/examples/system-configuration.rs index cd81a02..cb1e00f 100644 --- a/ctru-rs/examples/system-configuration.rs +++ b/ctru-rs/examples/system-configuration.rs @@ -4,11 +4,11 @@ use ctru::services::cfgu::Cfgu; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let cfgu = Cfgu::init().expect("Couldn't obtain CFGU controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let cfgu = Cfgu::new().expect("Couldn't obtain CFGU controller"); + let _console = Console::new(gfx.top_screen.borrow_mut()); println!("\x1b[0;0HRegion: {:?}", cfgu.region().unwrap()); println!("\x1b[10;0HLanguage: {:?}", cfgu.language().unwrap()); diff --git a/ctru-rs/examples/thread-basic.rs b/ctru-rs/examples/thread-basic.rs index 73be659..cc494f6 100644 --- a/ctru-rs/examples/thread-basic.rs +++ b/ctru-rs/examples/thread-basic.rs @@ -8,10 +8,10 @@ use std::time::Duration; fn main() { ctru::use_panic_handler(); - let apt = Apt::init().unwrap(); - let mut hid = Hid::init().unwrap(); - let gfx = Gfx::init().unwrap(); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let apt = Apt::new().unwrap(); + let mut hid = Hid::new().unwrap(); + let gfx = Gfx::new().unwrap(); + let _console = Console::new(gfx.top_screen.borrow_mut()); let prio = std::os::horizon::thread::current_priority(); println!("Main thread prio: {}\n", prio); diff --git a/ctru-rs/examples/thread-info.rs b/ctru-rs/examples/thread-info.rs index 968e858..9ab04fe 100644 --- a/ctru-rs/examples/thread-info.rs +++ b/ctru-rs/examples/thread-info.rs @@ -9,10 +9,10 @@ use std::os::horizon::thread::BuilderExt; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let _console = Console::new(gfx.top_screen.borrow_mut()); // Give ourselves up to 30% of the system core's time apt.set_app_cpu_time_limit(30) diff --git a/ctru-rs/examples/thread-locals.rs b/ctru-rs/examples/thread-locals.rs index 09e2082..d2a5e3e 100644 --- a/ctru-rs/examples/thread-locals.rs +++ b/ctru-rs/examples/thread-locals.rs @@ -12,11 +12,11 @@ std::thread_local! { fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); gfx.top_screen.borrow_mut().set_wide_mode(true); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let _console = Console::new(gfx.top_screen.borrow_mut()); // Give ourselves up to 30% of the system core's time apt.set_app_cpu_time_limit(30) diff --git a/ctru-rs/examples/time-rtc.rs b/ctru-rs/examples/time-rtc.rs index 86db923..66c9a63 100644 --- a/ctru-rs/examples/time-rtc.rs +++ b/ctru-rs/examples/time-rtc.rs @@ -3,11 +3,11 @@ use ctru::prelude::*; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); - let _console = Console::init(gfx.top_screen.borrow_mut()); + let _console = Console::new(gfx.top_screen.borrow_mut()); print!("\x1b[30;16HPress Start to exit."); diff --git a/ctru-rs/examples/title-info.rs b/ctru-rs/examples/title-info.rs index a18534e..07ba9f4 100644 --- a/ctru-rs/examples/title-info.rs +++ b/ctru-rs/examples/title-info.rs @@ -5,12 +5,12 @@ use ctru::services::fs::FsMediaType; fn main() { ctru::use_panic_handler(); - let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); - let mut hid = Hid::init().expect("Couldn't obtain HID controller"); - let apt = Apt::init().expect("Couldn't obtain APT controller"); - let am = Am::init().expect("Couldn't obtain AM controller"); - let top_screen = Console::init(gfx.top_screen.borrow_mut()); - let bottom_screen = Console::init(gfx.bottom_screen.borrow_mut()); + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + let am = Am::new().expect("Couldn't obtain AM controller"); + let top_screen = Console::new(gfx.top_screen.borrow_mut()); + let bottom_screen = Console::new(gfx.bottom_screen.borrow_mut()); let sd_count = am .title_count(FsMediaType::Sd) diff --git a/ctru-rs/src/applets/mii_selector.rs b/ctru-rs/src/applets/mii_selector.rs index 8d289ee..69175a2 100644 --- a/ctru-rs/src/applets/mii_selector.rs +++ b/ctru-rs/src/applets/mii_selector.rs @@ -41,7 +41,7 @@ bitflags! { /// ``` /// use ctru::applets::mii_selector::MiiSelector; /// -/// let mut mii_selector = MiiSelector::init(); +/// let mut mii_selector = MiiSelector::new(); /// mii_selector.set_title("Example Mii selector"); /// /// let result = mii_selector.launch().unwrap(); @@ -68,7 +68,7 @@ pub enum MiiLaunchError { impl MiiSelector { /// Initializes a Mii Selector - pub fn init() -> Self { + pub fn new() -> Self { let mut config = Box::::default(); unsafe { ctru_sys::miiSelectorInit(config.as_mut()); diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index 0af7dee..178a86e 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -89,7 +89,7 @@ bitflags! { impl Swkbd { /// Initializes a software keyboard of the specified type and the chosen number of buttons /// (from 1-3). - pub fn init(keyboard_type: Kind, num_buttons: i32) -> Self { + pub fn new(keyboard_type: Kind, num_buttons: i32) -> Self { unsafe { let mut state = Box::::default(); swkbdInit(state.as_mut(), keyboard_type.into(), num_buttons, -1); @@ -207,7 +207,7 @@ impl Swkbd { impl Default for Swkbd { fn default() -> Self { - Swkbd::init(Kind::Normal, 2) + Swkbd::new(Kind::Normal, 2) } } diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index c0a8765..73d3952 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -16,7 +16,7 @@ impl<'screen> Console<'screen> { /// 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: RefMut<'screen, dyn Screen>) -> Self { + pub fn new(screen: RefMut<'screen, dyn Screen>) -> Self { let mut context = Box::::default(); unsafe { consoleInit(screen.as_raw(), context.as_mut()) }; diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 2073fe3..57b61e9 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -54,7 +54,7 @@ fn panic_hook_setup() { if main_thread == std::thread::current().id() && console::Console::exists() { println!("\nPress SELECT to exit the software"); - match Hid::init() { + match Hid::new() { Ok(mut hid) => loop { hid.scan_input(); let keys = hid.keys_down(); diff --git a/ctru-rs/src/services/am.rs b/ctru-rs/src/services/am.rs index ada179e..6fcee8c 100644 --- a/ctru-rs/src/services/am.rs +++ b/ctru-rs/src/services/am.rs @@ -61,7 +61,7 @@ impl<'a> Title<'a> { pub struct Am(()); impl Am { - pub fn init() -> crate::Result { + pub fn new() -> crate::Result { unsafe { ResultCode(ctru_sys::amInit())?; Ok(Am(())) diff --git a/ctru-rs/src/services/apt.rs b/ctru-rs/src/services/apt.rs index aa1dd28..f7731be 100644 --- a/ctru-rs/src/services/apt.rs +++ b/ctru-rs/src/services/apt.rs @@ -3,7 +3,7 @@ use crate::error::ResultCode; pub struct Apt(()); impl Apt { - pub fn init() -> crate::Result { + pub fn new() -> crate::Result { unsafe { ResultCode(ctru_sys::aptInit())?; Ok(Apt(())) diff --git a/ctru-rs/src/services/cam.rs b/ctru-rs/src/services/cam.rs index 52ce926..261dbd3 100644 --- a/ctru-rs/src/services/cam.rs +++ b/ctru-rs/src/services/cam.rs @@ -770,7 +770,7 @@ impl Cam { /// This function will return an error if the service was unable to be initialized. /// Since this service requires no special or elevated permissions, errors are /// rare in practice. - pub fn init() -> crate::Result { + pub fn new() -> crate::Result { unsafe { ResultCode(ctru_sys::camInit())?; Ok(Cam { diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index 3f268c7..90edf44 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -61,7 +61,7 @@ impl Cfgu { /// ctrulib services are reference counted, so this function may be called /// as many times as desired and the service will not exit until all /// instances of Cfgu drop out of scope. - pub fn init() -> crate::Result { + pub fn new() -> crate::Result { ResultCode(unsafe { ctru_sys::cfguInit() })?; Ok(Cfgu(())) } diff --git a/ctru-rs/src/services/fs.rs b/ctru-rs/src/services/fs.rs index 9364bb6..f502afc 100644 --- a/ctru-rs/src/services/fs.rs +++ b/ctru-rs/src/services/fs.rs @@ -103,7 +103,7 @@ pub struct Fs(()); /// ```no_run /// use ctru::services::fs::Fs; /// -/// let fs = Fs::init().unwrap(); +/// let fs = Fs::new().unwrap(); /// let sdmc_archive = fs.sdmc().unwrap(); /// ``` pub struct Archive { @@ -126,7 +126,7 @@ pub struct Archive { /// use std::io::prelude::*; /// use ctru::services::fs::{Fs, File}; /// -/// let fs = Fs::init()?; +/// let fs = Fs::new()?; /// let sdmc = fs.sdmc()?; /// /// let mut file = File::create(&sdmc, "/foo.txt")?; @@ -139,7 +139,7 @@ pub struct Archive { /// use std::io::prelude::*; /// use ctru::services::fs::{Fs, File}; /// -/// let fs = Fs::init()?; +/// let fs = Fs::new()?; /// let sdmc = fs.sdmc()?; /// /// let mut file = File::open(&sdmc, "/foo.txt")?; @@ -156,7 +156,7 @@ pub struct Archive { /// use std::io::prelude::*; /// use ctru::services::fs::{Fs, File}; /// -/// let fs = Fs::init()?; +/// let fs = Fs::new()?; /// let sdmc = fs.sdmc()?; /// /// let file = File::open(&sdmc, "/foo.txt")?; @@ -209,7 +209,7 @@ pub struct Metadata { /// ```no_run /// use ctru::services::fs::{Fs, OpenOptions}; /// -/// let fs = Fs::init().unwrap(); +/// let fs = Fs::new().unwrap(); /// let sdmc_archive = fs.sdmc().unwrap(); /// let file = OpenOptions::new() /// .read(true) @@ -224,7 +224,7 @@ pub struct Metadata { /// ```no_run /// use ctru::services::fs::{Fs, OpenOptions}; /// -/// let fs = Fs::init().unwrap(); +/// let fs = Fs::new().unwrap(); /// let sdmc_archive = fs.sdmc().unwrap(); /// let file = OpenOptions::new() /// .read(true) @@ -298,7 +298,7 @@ impl Fs { /// ctrulib services are reference counted, so this function may be called /// as many times as desired and the service will not exit until all /// instances of Fs drop out of scope. - pub fn init() -> crate::Result { + pub fn new() -> crate::Result { unsafe { let r = ctru_sys::fsInit(); if r < 0 { @@ -351,7 +351,7 @@ impl File { /// ```no_run /// use ctru::services::fs::{Fs, File}; /// - /// let fs = Fs::init().unwrap(); + /// let fs = Fs::new().unwrap(); /// let sdmc_archive = fs.sdmc().unwrap(); /// let mut f = File::open(&sdmc_archive, "/foo.txt").unwrap(); /// ``` @@ -380,7 +380,7 @@ impl File { /// ```no_run /// use ctru::services::fs::{Fs, File}; /// - /// let fs = Fs::init().unwrap(); + /// let fs = Fs::new().unwrap(); /// let sdmc_archive = fs.sdmc().unwrap(); /// let mut f = File::create(&sdmc_archive, "/foo.txt").unwrap(); /// ``` diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index 40d4740..87be464 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -131,7 +131,7 @@ impl Gfx { /// Initialize the Gfx module with the chosen framebuffer formats for the top and bottom /// screens /// - /// Use `Gfx::init()` instead of this function to initialize the module with default parameters + /// Use `Gfx::new()` instead of this function to initialize the module with default parameters pub fn with_formats( top_fb_fmt: FramebufferFormat, bottom_fb_fmt: FramebufferFormat, @@ -158,7 +158,7 @@ impl Gfx { /// Creates a new [Gfx] instance with default init values /// It's the same as calling: /// `Gfx::with_formats(FramebufferFormat::Bgr8, FramebufferFormat::Bgr8, false)` - pub fn init() -> Result { + pub fn new() -> Result { Gfx::with_formats(FramebufferFormat::Bgr8, FramebufferFormat::Bgr8, false) } @@ -294,6 +294,6 @@ mod tests { #[test] fn gfx_duplicate() { // We don't need to build a `Gfx` because the test runner has one already - assert!(matches!(Gfx::init(), Err(Error::ServiceAlreadyActive))); + assert!(matches!(Gfx::new(), Err(Error::ServiceAlreadyActive))); } } diff --git a/ctru-rs/src/services/hid.rs b/ctru-rs/src/services/hid.rs index 31dd4d9..9577f42 100644 --- a/ctru-rs/src/services/hid.rs +++ b/ctru-rs/src/services/hid.rs @@ -61,7 +61,7 @@ pub struct CirclePosition(ctru_sys::circlePosition); /// Since this service requires no special or elevated permissions, errors are /// rare in practice. impl Hid { - pub fn init() -> crate::Result { + pub fn new() -> crate::Result { unsafe { ResultCode(ctru_sys::hidInit())?; Ok(Hid(())) diff --git a/ctru-rs/src/services/ndsp/mod.rs b/ctru-rs/src/services/ndsp/mod.rs index fa93cf6..3fff3b8 100644 --- a/ctru-rs/src/services/ndsp/mod.rs +++ b/ctru-rs/src/services/ndsp/mod.rs @@ -80,7 +80,7 @@ impl Ndsp { /// /// This function will return an error if an instance of the `Ndsp` struct already exists /// or if there are any issues during initialization. - pub fn init() -> crate::Result { + pub fn new() -> crate::Result { let _service_handler = ServiceReference::new( &NDSP_ACTIVE, false, diff --git a/ctru-rs/src/services/romfs.rs b/ctru-rs/src/services/romfs.rs index fd022e8..49369a6 100644 --- a/ctru-rs/src/services/romfs.rs +++ b/ctru-rs/src/services/romfs.rs @@ -25,7 +25,7 @@ pub struct RomFS { static ROMFS_ACTIVE: Mutex = Mutex::new(0); impl RomFS { - pub fn init() -> crate::Result { + pub fn new() -> crate::Result { let _service_handler = ServiceReference::new( &ROMFS_ACTIVE, true, @@ -50,7 +50,7 @@ mod tests { #[test] fn romfs_counter() { - let _romfs = RomFS::init().unwrap(); + let _romfs = RomFS::new().unwrap(); let value = *ROMFS_ACTIVE.lock().unwrap(); assert_eq!(value, 1); diff --git a/ctru-rs/src/services/soc.rs b/ctru-rs/src/services/soc.rs index c5df066..5b9c696 100644 --- a/ctru-rs/src/services/soc.rs +++ b/ctru-rs/src/services/soc.rs @@ -25,7 +25,7 @@ impl Soc { /// # Errors /// /// This function will return an error if the `Soc` service is already initialized - pub fn init() -> crate::Result { + pub fn new() -> crate::Result { Self::init_with_buffer_size(0x100000) } @@ -107,8 +107,8 @@ mod tests { #[test] fn soc_duplicate() { - let _soc = Soc::init().unwrap(); + let _soc = Soc::new().unwrap(); - assert!(matches!(Soc::init(), Err(Error::ServiceAlreadyActive))) + assert!(matches!(Soc::new(), Err(Error::ServiceAlreadyActive))) } } diff --git a/ctru-rs/src/services/sslc.rs b/ctru-rs/src/services/sslc.rs index 99eb456..405fb9b 100644 --- a/ctru-rs/src/services/sslc.rs +++ b/ctru-rs/src/services/sslc.rs @@ -8,7 +8,7 @@ pub struct SslC(()); impl SslC { /// Initialize the service - pub fn init() -> crate::Result { + pub fn new() -> crate::Result { unsafe { ResultCode(ctru_sys::sslcInit(0))?; Ok(SslC(())) diff --git a/ctru-rs/src/test_runner.rs b/ctru-rs/src/test_runner.rs index 6f157e3..713e0e6 100644 --- a/ctru-rs/src/test_runner.rs +++ b/ctru-rs/src/test_runner.rs @@ -12,13 +12,13 @@ use crate::prelude::*; /// runs all tests in series, "failing" on the first one to panic (really, the /// panic is just treated the same as any normal application panic). pub(crate) fn run(tests: &[&TestDescAndFn]) { - let gfx = Gfx::init().unwrap(); - let mut hid = Hid::init().unwrap(); - let apt = Apt::init().unwrap(); + let gfx = Gfx::new().unwrap(); + let mut hid = Hid::new().unwrap(); + let apt = Apt::new().unwrap(); let mut top_screen = gfx.top_screen.borrow_mut(); top_screen.set_wide_mode(true); - let _console = Console::init(top_screen); + let _console = Console::new(top_screen); let opts = TestOpts { force_run_in_process: true, From fe29c25c35780174a703a97a2e3db6c1e6367dd6 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 6 Apr 2023 11:22:23 +0200 Subject: [PATCH 02/21] Derive traits and Screen changes --- ctru-rs/examples/audio-filters.rs | 4 -- ctru-rs/examples/buttons.rs | 3 -- ctru-rs/examples/camera-image.rs | 17 ++++---- ctru-rs/examples/file-explorer.rs | 2 - ctru-rs/examples/futures-basic.rs | 2 - ctru-rs/examples/futures-tokio.rs | 2 - ctru-rs/examples/gfx-3d-mode.rs | 4 -- ctru-rs/examples/gfx-wide-mode.rs | 4 +- ctru-rs/examples/graphics-bitmap.rs | 4 +- ctru-rs/examples/hashmaps.rs | 2 - ctru-rs/examples/hello-both-screens.rs | 2 - ctru-rs/examples/hello-world.rs | 3 -- ctru-rs/examples/linear-memory.rs | 3 -- ctru-rs/examples/mii-selector.rs | 3 -- ctru-rs/examples/output-3dslink.rs | 3 -- ctru-rs/examples/romfs.rs | 3 -- ctru-rs/examples/software-keyboard.rs | 2 - ctru-rs/examples/system-configuration.rs | 3 -- ctru-rs/examples/thread-basic.rs | 2 - ctru-rs/examples/thread-info.rs | 4 +- ctru-rs/examples/thread-locals.rs | 2 - ctru-rs/examples/time-rtc.rs | 4 -- ctru-rs/examples/title-info.rs | 4 -- ctru-rs/src/applets/mii_selector.rs | 44 ++++++++++---------- ctru-rs/src/applets/swkbd.rs | 9 ++-- ctru-rs/src/console.rs | 4 ++ ctru-rs/src/services/am.rs | 1 + ctru-rs/src/services/cam.rs | 25 ++++++------ ctru-rs/src/services/cfgu.rs | 6 +-- ctru-rs/src/services/fs.rs | 14 +++---- ctru-rs/src/services/gfx.rs | 52 ++++++++++++------------ ctru-rs/src/services/gspgpu.rs | 4 +- ctru-rs/src/services/hid.rs | 3 +- ctru-rs/src/services/ndsp/mod.rs | 10 ++--- ctru-rs/src/services/ndsp/wave.rs | 2 +- ctru-rs/src/services/ps.rs | 4 +- ctru-rs/src/services/sslc.rs | 11 ----- ctru-rs/src/test_runner.rs | 2 - 38 files changed, 104 insertions(+), 169 deletions(-) diff --git a/ctru-rs/examples/audio-filters.rs b/ctru-rs/examples/audio-filters.rs index e425793..3ef76a6 100644 --- a/ctru-rs/examples/audio-filters.rs +++ b/ctru-rs/examples/audio-filters.rs @@ -154,10 +154,6 @@ fn main() { altern = !altern; } - // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); - //Wait for VBlank gfx.wait_for_vblank(); } diff --git a/ctru-rs/examples/buttons.rs b/ctru-rs/examples/buttons.rs index 5645915..8bba6c7 100644 --- a/ctru-rs/examples/buttons.rs +++ b/ctru-rs/examples/buttons.rs @@ -63,9 +63,6 @@ fn main() { // Save our current key presses for the next frame old_keys = keys; - // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); gfx.wait_for_vblank(); } } diff --git a/ctru-rs/examples/camera-image.rs b/ctru-rs/examples/camera-image.rs index 88526be..8f67b47 100644 --- a/ctru-rs/examples/camera-image.rs +++ b/ctru-rs/examples/camera-image.rs @@ -20,11 +20,10 @@ fn main() { let mut hid = Hid::new().expect("Failed to initialize Hid service."); let gfx = Gfx::new().expect("Failed to initialize GFX service."); - gfx.top_screen.borrow_mut().set_double_buffering(true); - gfx.top_screen - .borrow_mut() - .set_framebuffer_format(FramebufferFormat::Rgb565); - gfx.bottom_screen.borrow_mut().set_double_buffering(false); + let mut top_screen = gfx.top_screen.borrow_mut(); + top_screen.set_double_buffering(true); + top_screen.set_framebuffer_format(FramebufferFormat::Rgb565); + let _console = Console::new(gfx.bottom_screen.borrow_mut()); let mut keys_down; @@ -88,13 +87,15 @@ fn main() { rotate_image_to_screen( &buf, - gfx.top_screen.borrow_mut().raw_framebuffer().ptr, + top_screen.raw_framebuffer().ptr, WIDTH, HEIGHT, ); - gfx.flush_buffers(); - gfx.swap_buffers(); + // We will only flush the "camera" screen, since the other screen is handled by `Console` + top_screen.flush_buffer(); + top_screen.swap_buffers(); + gfx.wait_for_vblank(); } } diff --git a/ctru-rs/examples/file-explorer.rs b/ctru-rs/examples/file-explorer.rs index 8a3d639..6d30590 100644 --- a/ctru-rs/examples/file-explorer.rs +++ b/ctru-rs/examples/file-explorer.rs @@ -68,8 +68,6 @@ impl<'a> FileExplorer<'a> { self.get_input_and_run(Self::set_exact_path); } - self.gfx.flush_buffers(); - self.gfx.swap_buffers(); self.gfx.wait_for_vblank(); } } diff --git a/ctru-rs/examples/futures-basic.rs b/ctru-rs/examples/futures-basic.rs index cb6a804..c41245c 100644 --- a/ctru-rs/examples/futures-basic.rs +++ b/ctru-rs/examples/futures-basic.rs @@ -68,8 +68,6 @@ fn main() { frame_count = 0; } - gfx.flush_buffers(); - gfx.swap_buffers(); gfx.wait_for_vblank(); } } diff --git a/ctru-rs/examples/futures-tokio.rs b/ctru-rs/examples/futures-tokio.rs index e42e9e9..986e930 100644 --- a/ctru-rs/examples/futures-tokio.rs +++ b/ctru-rs/examples/futures-tokio.rs @@ -63,8 +63,6 @@ fn main() { break; } - gfx.flush_buffers(); - gfx.swap_buffers(); gfx.wait_for_vblank(); } } diff --git a/ctru-rs/examples/gfx-3d-mode.rs b/ctru-rs/examples/gfx-3d-mode.rs index c5b194c..8603a64 100644 --- a/ctru-rs/examples/gfx-3d-mode.rs +++ b/ctru-rs/examples/gfx-3d-mode.rs @@ -61,10 +61,6 @@ fn main() { buf.copy_from(IMAGE.as_ptr(), IMAGE.len()); } - // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); - //Wait for VBlank gfx.wait_for_vblank(); } diff --git a/ctru-rs/examples/gfx-wide-mode.rs b/ctru-rs/examples/gfx-wide-mode.rs index 53da6ea..0acf1b2 100644 --- a/ctru-rs/examples/gfx-wide-mode.rs +++ b/ctru-rs/examples/gfx-wide-mode.rs @@ -26,9 +26,7 @@ fn main() { console = Console::new(gfx.top_screen.borrow_mut()); println!("Press A to enable/disable wide screen mode."); } - - gfx.flush_buffers(); - gfx.swap_buffers(); + gfx.wait_for_vblank(); } } diff --git a/ctru-rs/examples/graphics-bitmap.rs b/ctru-rs/examples/graphics-bitmap.rs index 55d4903..4beacea 100644 --- a/ctru-rs/examples/graphics-bitmap.rs +++ b/ctru-rs/examples/graphics-bitmap.rs @@ -48,8 +48,8 @@ fn main() { } // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); + bottom_screen.flush_buffer(); + bottom_screen.swap_buffers(); //Wait for VBlank gfx.wait_for_vblank(); diff --git a/ctru-rs/examples/hashmaps.rs b/ctru-rs/examples/hashmaps.rs index e3726eb..7d4d8c4 100644 --- a/ctru-rs/examples/hashmaps.rs +++ b/ctru-rs/examples/hashmaps.rs @@ -21,8 +21,6 @@ fn main() { println!("{map:#?}"); while apt.main_loop() { - gfx.flush_buffers(); - gfx.swap_buffers(); gfx.wait_for_vblank(); hid.scan_input(); diff --git a/ctru-rs/examples/hello-both-screens.rs b/ctru-rs/examples/hello-both-screens.rs index fa2e16d..f2007f1 100644 --- a/ctru-rs/examples/hello-both-screens.rs +++ b/ctru-rs/examples/hello-both-screens.rs @@ -27,8 +27,6 @@ fn main() { println!("\x1b[29;16HPress Start to exit"); while apt.main_loop() { - gfx.flush_buffers(); - gfx.swap_buffers(); gfx.wait_for_vblank(); hid.scan_input(); diff --git a/ctru-rs/examples/hello-world.rs b/ctru-rs/examples/hello-world.rs index 345ee2e..a92cb90 100644 --- a/ctru-rs/examples/hello-world.rs +++ b/ctru-rs/examples/hello-world.rs @@ -29,9 +29,6 @@ fn main() { if hid.keys_down().contains(KeyPad::START) { break; } - // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); //Wait for VBlank gfx.wait_for_vblank(); diff --git a/ctru-rs/examples/linear-memory.rs b/ctru-rs/examples/linear-memory.rs index 2373941..e6c18dc 100644 --- a/ctru-rs/examples/linear-memory.rs +++ b/ctru-rs/examples/linear-memory.rs @@ -37,9 +37,6 @@ fn main() { if hid.keys_down().contains(KeyPad::START) { break; } - // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); //Wait for VBlank gfx.wait_for_vblank(); diff --git a/ctru-rs/examples/mii-selector.rs b/ctru-rs/examples/mii-selector.rs index 9ef86f6..d489e07 100644 --- a/ctru-rs/examples/mii-selector.rs +++ b/ctru-rs/examples/mii-selector.rs @@ -33,9 +33,6 @@ fn main() { if hid.keys_down().contains(KeyPad::START) { break; } - // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); //Wait for VBlank gfx.wait_for_vblank(); diff --git a/ctru-rs/examples/output-3dslink.rs b/ctru-rs/examples/output-3dslink.rs index 5649e6e..8314efa 100644 --- a/ctru-rs/examples/output-3dslink.rs +++ b/ctru-rs/examples/output-3dslink.rs @@ -33,9 +33,6 @@ fn main() { if hid.keys_down().contains(KeyPad::START) { break; } - // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); //Wait for VBlank gfx.wait_for_vblank(); diff --git a/ctru-rs/examples/romfs.rs b/ctru-rs/examples/romfs.rs index f92d81d..1138203 100644 --- a/ctru-rs/examples/romfs.rs +++ b/ctru-rs/examples/romfs.rs @@ -36,9 +36,6 @@ fn main() { if hid.keys_down().contains(KeyPad::START) { break; } - // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); //Wait for VBlank gfx.wait_for_vblank(); diff --git a/ctru-rs/examples/software-keyboard.rs b/ctru-rs/examples/software-keyboard.rs index 77e877a..39b5549 100644 --- a/ctru-rs/examples/software-keyboard.rs +++ b/ctru-rs/examples/software-keyboard.rs @@ -12,8 +12,6 @@ fn main() { println!("Press A to enter some text or press Start to quit"); while apt.main_loop() { - gfx.flush_buffers(); - gfx.swap_buffers(); gfx.wait_for_vblank(); hid.scan_input(); diff --git a/ctru-rs/examples/system-configuration.rs b/ctru-rs/examples/system-configuration.rs index cb1e00f..001233d 100644 --- a/ctru-rs/examples/system-configuration.rs +++ b/ctru-rs/examples/system-configuration.rs @@ -22,9 +22,6 @@ fn main() { if hid.keys_down().contains(KeyPad::START) { break; } - // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); //Wait for VBlank gfx.wait_for_vblank(); diff --git a/ctru-rs/examples/thread-basic.rs b/ctru-rs/examples/thread-basic.rs index cc494f6..33a8ea9 100644 --- a/ctru-rs/examples/thread-basic.rs +++ b/ctru-rs/examples/thread-basic.rs @@ -34,8 +34,6 @@ fn main() { } while apt.main_loop() { - gfx.flush_buffers(); - gfx.swap_buffers(); gfx.wait_for_vblank(); hid.scan_input(); diff --git a/ctru-rs/examples/thread-info.rs b/ctru-rs/examples/thread-info.rs index 9ab04fe..2316f16 100644 --- a/ctru-rs/examples/thread-info.rs +++ b/ctru-rs/examples/thread-info.rs @@ -44,9 +44,7 @@ fn main() { if hid.keys_down().contains(KeyPad::KEY_START) { break; } - - gfx.flush_buffers(); - gfx.swap_buffers(); + gfx.wait_for_vblank(); } } diff --git a/ctru-rs/examples/thread-locals.rs b/ctru-rs/examples/thread-locals.rs index d2a5e3e..80d8508 100644 --- a/ctru-rs/examples/thread-locals.rs +++ b/ctru-rs/examples/thread-locals.rs @@ -61,8 +61,6 @@ fn main() { break; } - gfx.flush_buffers(); - gfx.swap_buffers(); gfx.wait_for_vblank(); } } diff --git a/ctru-rs/examples/time-rtc.rs b/ctru-rs/examples/time-rtc.rs index 66c9a63..377fd5c 100644 --- a/ctru-rs/examples/time-rtc.rs +++ b/ctru-rs/examples/time-rtc.rs @@ -36,10 +36,6 @@ fn main() { println!("\x1b[1;1H{hours:0>2}:{minutes:0>2}:{seconds:0>2}"); println!("{weekday} {month} {day} {year}"); - // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); - //Wait for VBlank gfx.wait_for_vblank(); } diff --git a/ctru-rs/examples/title-info.rs b/ctru-rs/examples/title-info.rs index 07ba9f4..fee5cb6 100644 --- a/ctru-rs/examples/title-info.rs +++ b/ctru-rs/examples/title-info.rs @@ -106,10 +106,6 @@ fn main() { refresh = false; } - // Flush and swap framebuffers - gfx.flush_buffers(); - gfx.swap_buffers(); - //Wait for VBlank gfx.wait_for_vblank(); } diff --git a/ctru-rs/src/applets/mii_selector.rs b/ctru-rs/src/applets/mii_selector.rs index 69175a2..e7d0559 100644 --- a/ctru-rs/src/applets/mii_selector.rs +++ b/ctru-rs/src/applets/mii_selector.rs @@ -8,14 +8,14 @@ use std::ffi::CString; /// Index of a Mii used to configure some parameters of the Mii Selector /// Can be either a single index, or _all_ Miis -#[derive(Debug, Clone)] -pub enum MiiConfigIndex { +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub enum MiiIndex { Index(u32), All, } /// The type of a Mii with their respective data -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq, PartialEq)] pub enum MiiType { Guest { index: u32, name: String }, User, @@ -54,7 +54,7 @@ pub struct MiiSelector { /// Return value from a MiiSelector's launch #[non_exhaustive] #[derive(Clone, Debug)] -pub struct MiiSelectorReturn { +pub struct SelectionResult { pub mii_data: MiiData, pub is_mii_selected: bool, pub mii_type: MiiType, @@ -62,7 +62,7 @@ pub struct MiiSelectorReturn { /// Error type for the Mii selector #[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub enum MiiLaunchError { +pub enum LaunchError { InvalidChecksum, } @@ -94,40 +94,40 @@ impl MiiSelector { } /// Whitelist a guest Mii - pub fn whitelist_guest_mii(&mut self, mii_index: MiiConfigIndex) { + pub fn whitelist_guest_mii(&mut self, mii_index: MiiIndex) { let index = match mii_index { - MiiConfigIndex::Index(i) => i, - MiiConfigIndex::All => ctru_sys::MIISELECTOR_GUESTMII_SLOTS, + MiiIndex::Index(i) => i, + MiiIndex::All => ctru_sys::MIISELECTOR_GUESTMII_SLOTS, }; unsafe { ctru_sys::miiSelectorWhitelistGuestMii(self.config.as_mut(), index) } } /// Blacklist a guest Mii - pub fn blacklist_guest_mii(&mut self, mii_index: MiiConfigIndex) { + pub fn blacklist_guest_mii(&mut self, mii_index: MiiIndex) { let index = match mii_index { - MiiConfigIndex::Index(i) => i, - MiiConfigIndex::All => ctru_sys::MIISELECTOR_GUESTMII_SLOTS, + MiiIndex::Index(i) => i, + MiiIndex::All => ctru_sys::MIISELECTOR_GUESTMII_SLOTS, }; unsafe { ctru_sys::miiSelectorBlacklistGuestMii(self.config.as_mut(), index) } } /// Whitelist a user Mii - pub fn whitelist_user_mii(&mut self, mii_index: MiiConfigIndex) { + pub fn whitelist_user_mii(&mut self, mii_index: MiiIndex) { let index = match mii_index { - MiiConfigIndex::Index(i) => i, - MiiConfigIndex::All => ctru_sys::MIISELECTOR_USERMII_SLOTS, + MiiIndex::Index(i) => i, + MiiIndex::All => ctru_sys::MIISELECTOR_USERMII_SLOTS, }; unsafe { ctru_sys::miiSelectorWhitelistUserMii(self.config.as_mut(), index) } } /// Blacklist a user Mii - pub fn blacklist_user_mii(&mut self, mii_index: MiiConfigIndex) { + pub fn blacklist_user_mii(&mut self, mii_index: MiiIndex) { let index = match mii_index { - MiiConfigIndex::Index(i) => i, - MiiConfigIndex::All => ctru_sys::MIISELECTOR_USERMII_SLOTS, + MiiIndex::Index(i) => i, + MiiIndex::All => ctru_sys::MIISELECTOR_USERMII_SLOTS, }; unsafe { ctru_sys::miiSelectorBlacklistUserMii(self.config.as_mut(), index) } @@ -143,24 +143,24 @@ impl MiiSelector { /// Launch the Mii Selector. /// Returns an error when the checksum of the Mii is invalid. - pub fn launch(&mut self) -> Result { + pub fn launch(&mut self) -> Result { let mut return_val = Box::::default(); unsafe { ctru_sys::miiSelectorLaunch(self.config.as_mut(), return_val.as_mut()) } if unsafe { ctru_sys::miiSelectorChecksumIsValid(return_val.as_mut()) } { Ok((*return_val).into()) } else { - Err(MiiLaunchError::InvalidChecksum) + Err(LaunchError::InvalidChecksum) } } } -impl From for MiiSelectorReturn { +impl From for SelectionResult { fn from(ret: ctru_sys::MiiSelectorReturn) -> Self { let raw_mii_data = ret.mii; let mut guest_mii_name = ret.guest_mii_name; - MiiSelectorReturn { + SelectionResult { mii_data: raw_mii_data.into(), is_mii_selected: ret.no_mii_selected == 0, mii_type: if ret.guest_mii_index != 0xFFFFFFFF { @@ -179,7 +179,7 @@ impl From for MiiSelectorReturn { } } -impl From for MiiConfigIndex { +impl From for MiiIndex { fn from(v: u32) -> Self { Self::Index(v) } diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index 178a86e..8f4aab6 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -7,6 +7,7 @@ use std::iter::once; use std::str; /// An instance of the software keyboard. +#[derive(Clone)] pub struct Swkbd { state: Box, } @@ -18,7 +19,7 @@ pub struct Swkbd { /// Numpad is a number pad. /// Western is a text keyboard without japanese symbols (only applies to JPN systems). For other /// systems it's the same as a Normal keyboard. -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum Kind { Normal = ctru_sys::SWKBD_TYPE_NORMAL, @@ -28,7 +29,7 @@ pub enum Kind { } /// Represents which button the user pressed to close the software keyboard. -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum Button { Left = ctru_sys::SWKBD_BUTTON_LEFT, @@ -37,7 +38,7 @@ pub enum Button { } /// Error type for the software keyboard. -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(i32)] pub enum Error { InvalidInput = ctru_sys::SWKBD_INVALID_INPUT, @@ -51,7 +52,7 @@ pub enum Error { } /// Restrictions on keyboard input -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum ValidInput { Anything = ctru_sys::SWKBD_ANYTHING, diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index 73d3952..1891492 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -16,6 +16,10 @@ impl<'screen> Console<'screen> { /// 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. + /// + /// # Notes + /// + /// [Console] automatically takes care of flushing and swapping buffers for its screen when printing. pub fn new(screen: RefMut<'screen, dyn Screen>) -> Self { let mut context = Box::::default(); diff --git a/ctru-rs/src/services/am.rs b/ctru-rs/src/services/am.rs index 6fcee8c..70bd151 100644 --- a/ctru-rs/src/services/am.rs +++ b/ctru-rs/src/services/am.rs @@ -6,6 +6,7 @@ use std::mem::MaybeUninit; #[derive(Copy, Clone, Debug)] #[repr(transparent)] pub struct TitleInfo(ctru_sys::AM_TitleEntry); + impl TitleInfo { pub fn id(&self) -> u64 { self.0.titleID diff --git a/ctru-rs/src/services/cam.rs b/ctru-rs/src/services/cam.rs index 261dbd3..4a06421 100644 --- a/ctru-rs/src/services/cam.rs +++ b/ctru-rs/src/services/cam.rs @@ -21,7 +21,7 @@ pub struct Cam { } /// Flag to pass to [Camera::flip_image] -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum FlipMode { None = ctru_sys::FLIP_NONE, @@ -31,7 +31,7 @@ pub enum FlipMode { } /// Flag to pass to [Camera::set_view_size] -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum ViewSize { TopLCD = ctru_sys::SIZE_CTR_TOP_LCD, @@ -48,7 +48,7 @@ pub enum ViewSize { } /// Flag to pass to [Camera::set_frame_rate] -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum FrameRate { Fps15 = ctru_sys::FRAME_RATE_15, @@ -68,7 +68,7 @@ pub enum FrameRate { /// Flag to pass to [Camera::set_white_balance] or /// [Camera::set_white_balance_without_base_up] -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum WhiteBalance { /// Normal @@ -86,7 +86,7 @@ pub enum WhiteBalance { } /// Flag to pass to [Camera::set_photo_mode] -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum PhotoMode { Normal = ctru_sys::PHOTO_MODE_NORMAL, @@ -97,7 +97,7 @@ pub enum PhotoMode { } /// Flag to pass to [Camera::set_effect] -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum Effect { None = ctru_sys::EFFECT_NONE, @@ -109,7 +109,7 @@ pub enum Effect { } /// Flag to pass to [Camera::set_contrast] -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum Contrast { /// OFF @@ -121,7 +121,7 @@ pub enum Contrast { } /// Flag to pass to [Camera::set_lens_correction] -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum LensCorrection { Off = ctru_sys::LENS_CORRECTION_DARK, @@ -130,7 +130,7 @@ pub enum LensCorrection { } /// Flag to pass to [Camera::set_output_format] -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum OutputFormat { Yuv422 = ctru_sys::OUTPUT_YUV_422, @@ -138,7 +138,7 @@ pub enum OutputFormat { } /// Flag to pass to [Cam::play_shutter_sound] -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum ShutterSound { Normal = ctru_sys::SHUTTER_SOUND_TYPE_NORMAL, @@ -169,6 +169,7 @@ impl TryFrom for FramebufferFormat { } /// Struct containing coordinates passed to [Camera::set_trimming_params]. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct TrimmingParams { x_start: i16, y_start: i16, @@ -193,11 +194,11 @@ impl TrimmingParams { } /// Represents data used by the camera to calibrate image quality -#[derive(Default)] +#[derive(Default, Clone, Copy, Debug)] pub struct ImageQualityCalibrationData(pub ctru_sys::CAMU_ImageQualityCalibrationData); /// Represents data used by the camera to calibrate image quality when using both outward cameras -#[derive(Default)] +#[derive(Default, Clone, Copy, Debug)] pub struct StereoCameraCalibrationData(pub ctru_sys::CAMU_StereoCameraCalibrationData); /// Represents the camera on the inside of the 3DS diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index 90edf44..62052eb 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -4,7 +4,7 @@ use crate::error::ResultCode; -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum Region { Japan = ctru_sys::CFG_REGION_JPN, @@ -16,7 +16,7 @@ pub enum Region { Taiwan = ctru_sys::CFG_REGION_TWN, } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum Language { Japanese = ctru_sys::CFG_LANGUAGE_JP, @@ -33,7 +33,7 @@ pub enum Language { TraditionalChinese = ctru_sys::CFG_LANGUAGE_TW, } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum SystemModel { Model3DS = ctru_sys::CFG_MODEL_3DS, diff --git a/ctru-rs/src/services/fs.rs b/ctru-rs/src/services/fs.rs index f502afc..e1703c5 100644 --- a/ctru-rs/src/services/fs.rs +++ b/ctru-rs/src/services/fs.rs @@ -23,17 +23,13 @@ bitflags! { const FS_OPEN_WRITE = 2; const FS_OPEN_CREATE = 4; } -} -bitflags! { #[derive(Default)] struct FsWrite: u32 { const FS_WRITE_FLUSH = 1; const FS_WRITE_UPDATE_TIME = 256; } -} - -bitflags! { + #[derive(Default)] struct FsAttribute: u32 { const FS_ATTRIBUTE_DIRECTORY = 1; @@ -43,7 +39,7 @@ bitflags! { } } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum FsMediaType { Nand = ctru_sys::MEDIATYPE_NAND, @@ -51,7 +47,7 @@ pub enum FsMediaType { GameCard = ctru_sys::MEDIATYPE_GAME_CARD, } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum PathType { Invalid = ctru_sys::PATH_INVALID, @@ -61,7 +57,7 @@ pub enum PathType { UTF16 = ctru_sys::PATH_UTF16, } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum ArchiveID { RomFS = ctru_sys::ARCHIVE_ROMFS, @@ -234,7 +230,7 @@ pub struct Metadata { /// .open("foo.txt") /// .unwrap(); /// ``` -#[derive(Clone, Default)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct OpenOptions { read: bool, write: bool, diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index 87be464..a4c5c17 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -55,6 +55,23 @@ pub trait Screen: private::Sealed { unsafe { ctru_sys::gfxSetDoubleBuffering(self.as_raw(), enabled) } } + /// Swaps the video buffers. + /// + /// This should be used even if double buffering is disabled. + fn flush_buffer(&mut self) { + let framebuffer = self.raw_framebuffer(); + + // Flush the data array. `self.raw_framebuffer` should get the correct parameters for all kinds of screens + unsafe { ctru_sys::GSPGPU_FlushDataCache(framebuffer.ptr.cast(), (framebuffer.height * framebuffer.width).into()) }; + } + + /// Swaps the video buffers. + /// + /// This should be used even if double buffering is disabled. + fn swap_buffers(&mut self) { + unsafe { ctru_sys::gfxScreenSwapBuffers(self.side().into(), true) }; + } + /// Gets the framebuffer format fn framebuffer_format(&self) -> FramebufferFormat { unsafe { ctru_sys::gfxGetScreenFormat(self.as_raw()) }.into() @@ -103,7 +120,7 @@ pub struct RawFrameBuffer<'screen> { screen: PhantomData<&'screen mut dyn Screen>, } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] /// Side of top screen framebuffer /// @@ -128,6 +145,13 @@ pub struct Gfx { static GFX_ACTIVE: Mutex = Mutex::new(0); impl Gfx { + /// Creates a new [Gfx] instance with default init values + /// It's the same as calling: + /// `Gfx::with_formats(FramebufferFormat::Bgr8, FramebufferFormat::Bgr8, false)` + pub fn new() -> Result { + Gfx::with_formats(FramebufferFormat::Bgr8, FramebufferFormat::Bgr8, false) + } + /// Initialize the Gfx module with the chosen framebuffer formats for the top and bottom /// screens /// @@ -155,32 +179,6 @@ impl Gfx { }) } - /// Creates a new [Gfx] instance with default init values - /// It's the same as calling: - /// `Gfx::with_formats(FramebufferFormat::Bgr8, FramebufferFormat::Bgr8, false)` - pub fn new() -> Result { - Gfx::with_formats(FramebufferFormat::Bgr8, FramebufferFormat::Bgr8, false) - } - - /// Flushes the current framebuffers - pub fn flush_buffers(&self) { - unsafe { ctru_sys::gfxFlushBuffers() }; - } - - /// Swaps the framebuffers and sets the gsp state - /// - /// Use this function when working with software rendering - pub fn swap_buffers(&self) { - unsafe { ctru_sys::gfxSwapBuffers() }; - } - - /// Swaps the framebuffers without manipulating the gsp state - /// - /// Use this function when working with GPU rendering - pub fn swap_buffers_gpu(&self) { - unsafe { ctru_sys::gfxSwapBuffersGpu() }; - } - /// Waits for the vertical blank interrupt /// /// Use this to synchronize your application with the refresh rate of the LCD screens diff --git a/ctru-rs/src/services/gspgpu.rs b/ctru-rs/src/services/gspgpu.rs index 234c088..9f9219c 100644 --- a/ctru-rs/src/services/gspgpu.rs +++ b/ctru-rs/src/services/gspgpu.rs @@ -1,6 +1,6 @@ //! GSPGPU service -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum Event { Psc0 = ctru_sys::GSPGPU_EVENT_PSC0, @@ -13,7 +13,7 @@ pub enum Event { } /// Framebuffer formats supported by the 3DS -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum FramebufferFormat { /// RGBA8. 4 bytes per pixel diff --git a/ctru-rs/src/services/hid.rs b/ctru-rs/src/services/hid.rs index 9577f42..2db0d23 100644 --- a/ctru-rs/src/services/hid.rs +++ b/ctru-rs/src/services/hid.rs @@ -8,7 +8,6 @@ use crate::error::ResultCode; bitflags::bitflags! { /// A set of flags corresponding to the button and directional pad /// inputs on the 3DS - #[derive(Default)] pub struct KeyPad: u32 { const A = ctru_sys::KEY_A; const B = ctru_sys::KEY_B; @@ -48,9 +47,11 @@ bitflags::bitflags! { pub struct Hid(()); /// Represents user input to the touchscreen. +#[derive(Debug, Clone, Copy)] pub struct TouchPosition(ctru_sys::touchPosition); /// Represents the current position of the 3DS circle pad. +#[derive(Debug, Clone, Copy)] pub struct CirclePosition(ctru_sys::circlePosition); /// Initializes the HID service. diff --git a/ctru-rs/src/services/ndsp/mod.rs b/ctru-rs/src/services/ndsp/mod.rs index 3fff3b8..8f67b1b 100644 --- a/ctru-rs/src/services/ndsp/mod.rs +++ b/ctru-rs/src/services/ndsp/mod.rs @@ -14,7 +14,7 @@ use std::sync::Mutex; const NUMBER_OF_CHANNELS: u8 = 24; -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum OutputMode { Mono = ctru_sys::NDSP_OUTPUT_MONO, @@ -22,7 +22,7 @@ pub enum OutputMode { Surround = ctru_sys::NDSP_OUTPUT_SURROUND, } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum AudioFormat { PCM8Mono = ctru_sys::NDSP_FORMAT_MONO_PCM8, @@ -32,12 +32,12 @@ pub enum AudioFormat { } /// Representation of volume mix for a channel. -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq)] pub struct AudioMix { raw: [f32; 12], } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum InterpolationType { Polyphase = ctru_sys::NDSP_INTERP_POLYPHASE, @@ -45,7 +45,7 @@ pub enum InterpolationType { None = ctru_sys::NDSP_INTERP_NONE, } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum NdspError { /// Channel ID InvalidChannel(u8), diff --git a/ctru-rs/src/services/ndsp/wave.rs b/ctru-rs/src/services/ndsp/wave.rs index a47c113..feff4cd 100644 --- a/ctru-rs/src/services/ndsp/wave.rs +++ b/ctru-rs/src/services/ndsp/wave.rs @@ -11,7 +11,7 @@ pub struct Wave { played_on_channel: Option, } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u8)] /// Enum representing the playback status of a [Wave]. pub enum WaveStatus { diff --git a/ctru-rs/src/services/ps.rs b/ctru-rs/src/services/ps.rs index a14f2c4..1d5bd69 100644 --- a/ctru-rs/src/services/ps.rs +++ b/ctru-rs/src/services/ps.rs @@ -6,6 +6,7 @@ use crate::error::ResultCode; use crate::Result; +#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u32)] pub enum AESAlgorithm { CbcEnc = ctru_sys::PS_ALGORITHM_CBC_ENC, @@ -16,6 +17,7 @@ pub enum AESAlgorithm { CcmDec = ctru_sys::PS_ALGORITHM_CCM_DEC, } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u32)] pub enum AESKeyType { Keyslot0D = ctru_sys::PS_KEYSLOT_0D, @@ -56,7 +58,7 @@ impl Ps { pub fn generate_random_bytes(&self, out: &mut [u8]) -> crate::Result<()> { ResultCode(unsafe { - ctru_sys::PS_GenerateRandomBytes(out as *mut _ as *mut _, out.len()) + ctru_sys::PS_GenerateRandomBytes(out.as_mut_ptr().cast(), out.len()) })?; Ok(()) } diff --git a/ctru-rs/src/services/sslc.rs b/ctru-rs/src/services/sslc.rs index 405fb9b..341155f 100644 --- a/ctru-rs/src/services/sslc.rs +++ b/ctru-rs/src/services/sslc.rs @@ -14,17 +14,6 @@ impl SslC { Ok(SslC(())) } } - - /// Fill `buf` with `buf.len()` random bytes - pub fn generate_random_data(&self, buf: &mut [u8]) -> crate::Result<()> { - unsafe { - ResultCode(ctru_sys::sslcGenerateRandomData( - buf.as_ptr() as _, - buf.len() as u32, - ))?; - Ok(()) - } - } } impl Drop for SslC { diff --git a/ctru-rs/src/test_runner.rs b/ctru-rs/src/test_runner.rs index 713e0e6..97aa5f4 100644 --- a/ctru-rs/src/test_runner.rs +++ b/ctru-rs/src/test_runner.rs @@ -39,8 +39,6 @@ pub(crate) fn run(tests: &[&TestDescAndFn]) { println!("Press START to exit."); while apt.main_loop() { - gfx.flush_buffers(); - gfx.swap_buffers(); gfx.wait_for_vblank(); hid.scan_input(); From e06114d081ccc6dc0aa19f536aeea349cb801af0 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 6 Apr 2023 11:32:28 +0200 Subject: [PATCH 03/21] Use usize in RawFrameBuffer --- ctru-rs/examples/gfx-3d-mode.rs | 6 ++++++ ctru-rs/src/services/gfx.rs | 14 +++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/ctru-rs/examples/gfx-3d-mode.rs b/ctru-rs/examples/gfx-3d-mode.rs index 8603a64..d0f77b2 100644 --- a/ctru-rs/examples/gfx-3d-mode.rs +++ b/ctru-rs/examples/gfx-3d-mode.rs @@ -61,6 +61,12 @@ fn main() { buf.copy_from(IMAGE.as_ptr(), IMAGE.len()); } + left.flush_buffer(); + left.swap_buffers(); + + right.flush_buffer(); + right.swap_buffers(); + //Wait for VBlank gfx.wait_for_vblank(); } diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index a4c5c17..52638c2 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -34,15 +34,15 @@ pub trait Screen: private::Sealed { /// Note that the pointer of the framebuffer returned by this function can /// change after each call to this function if double buffering is enabled. fn raw_framebuffer(&mut self) -> RawFrameBuffer { - let mut width = 0; - let mut height = 0; + let mut width: u16 = 0; + let mut height: u16 = 0; let ptr = unsafe { ctru_sys::gfxGetFramebuffer(self.as_raw(), self.side().into(), &mut width, &mut height) }; RawFrameBuffer { ptr, - width, - height, + width: width.into(), + height: height.into(), screen: PhantomData, } } @@ -62,7 +62,7 @@ pub trait Screen: private::Sealed { let framebuffer = self.raw_framebuffer(); // Flush the data array. `self.raw_framebuffer` should get the correct parameters for all kinds of screens - unsafe { ctru_sys::GSPGPU_FlushDataCache(framebuffer.ptr.cast(), (framebuffer.height * framebuffer.width).into()) }; + unsafe { ctru_sys::GSPGPU_FlushDataCache(framebuffer.ptr.cast(), (framebuffer.height * framebuffer.width) as u32) }; } /// Swaps the video buffers. @@ -113,9 +113,9 @@ pub struct RawFrameBuffer<'screen> { /// Pointer to graphics data to be rendered. pub ptr: *mut u8, /// The width of the framebuffer in pixels. - pub width: u16, + pub width: usize, /// The height of the framebuffer in pixels. - pub height: u16, + pub height: usize, /// Keep a mutable reference to the Screen for which this framebuffer is tied. screen: PhantomData<&'screen mut dyn Screen>, } From 31ad7cbf8e741624d9f383018452700a68ce81e7 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 6 Apr 2023 11:46:08 +0200 Subject: [PATCH 04/21] Renamed SystemModel enum variants --- ctru-rs/src/services/cfgu.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index 62052eb..0c076a3 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -36,12 +36,12 @@ pub enum Language { #[derive(Copy, Clone, Debug, PartialEq, Eq)] #[repr(u32)] pub enum SystemModel { - Model3DS = ctru_sys::CFG_MODEL_3DS, - Model3DSXL = ctru_sys::CFG_MODEL_3DSXL, - ModelNew3DS = ctru_sys::CFG_MODEL_N3DS, - Model2DS = ctru_sys::CFG_MODEL_2DS, - ModelNew3DSXL = ctru_sys::CFG_MODEL_N3DSXL, - ModelNew2DSXL = ctru_sys::CFG_MODEL_N2DSXL, + N3DS = ctru_sys::CFG_MODEL_3DS, + N3DSXL = ctru_sys::CFG_MODEL_3DSXL, + NewN3DS = ctru_sys::CFG_MODEL_N3DS, + N2DS = ctru_sys::CFG_MODEL_2DS, + NewN3DSXL = ctru_sys::CFG_MODEL_N3DSXL, + NewN2DSXL = ctru_sys::CFG_MODEL_N2DSXL, } /// Represents the configuration service. No actions can be performed @@ -163,12 +163,12 @@ impl TryFrom for SystemModel { fn try_from(value: u8) -> Result { match value as u32 { - ctru_sys::CFG_MODEL_3DS => Ok(SystemModel::Model3DS), - ctru_sys::CFG_MODEL_3DSXL => Ok(SystemModel::Model3DSXL), - ctru_sys::CFG_MODEL_N3DS => Ok(SystemModel::ModelNew3DS), - ctru_sys::CFG_MODEL_2DS => Ok(SystemModel::Model2DS), - ctru_sys::CFG_MODEL_N3DSXL => Ok(SystemModel::ModelNew3DSXL), - ctru_sys::CFG_MODEL_N2DSXL => Ok(SystemModel::ModelNew2DSXL), + ctru_sys::CFG_MODEL_3DS => Ok(SystemModel::N3DS), + ctru_sys::CFG_MODEL_3DSXL => Ok(SystemModel::N3DSXL), + ctru_sys::CFG_MODEL_N3DS => Ok(SystemModel::NewN3DS), + ctru_sys::CFG_MODEL_2DS => Ok(SystemModel::N2DS), + ctru_sys::CFG_MODEL_N3DSXL => Ok(SystemModel::NewN3DSXL), + ctru_sys::CFG_MODEL_N2DSXL => Ok(SystemModel::NewN2DSXL), _ => Err(()), } } From ef074a66ad7986fc1918f0dea5081df42829112a Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 6 Apr 2023 11:48:58 +0200 Subject: [PATCH 05/21] Fmt and clippy --- ctru-rs/examples/camera-image.rs | 9 ++------- ctru-rs/examples/gfx-wide-mode.rs | 2 +- ctru-rs/examples/thread-info.rs | 2 +- ctru-rs/src/applets/mii_selector.rs | 6 ++++++ ctru-rs/src/console.rs | 4 ++-- ctru-rs/src/services/fs.rs | 2 +- ctru-rs/src/services/gfx.rs | 11 ++++++++--- 7 files changed, 21 insertions(+), 15 deletions(-) diff --git a/ctru-rs/examples/camera-image.rs b/ctru-rs/examples/camera-image.rs index 8f67b47..dcc92ff 100644 --- a/ctru-rs/examples/camera-image.rs +++ b/ctru-rs/examples/camera-image.rs @@ -23,7 +23,7 @@ fn main() { let mut top_screen = gfx.top_screen.borrow_mut(); top_screen.set_double_buffering(true); top_screen.set_framebuffer_format(FramebufferFormat::Rgb565); - + let _console = Console::new(gfx.bottom_screen.borrow_mut()); let mut keys_down; @@ -85,12 +85,7 @@ fn main() { cam.play_shutter_sound(ShutterSound::Normal) .expect("Failed to play shutter sound"); - rotate_image_to_screen( - &buf, - top_screen.raw_framebuffer().ptr, - WIDTH, - HEIGHT, - ); + rotate_image_to_screen(&buf, top_screen.raw_framebuffer().ptr, WIDTH, HEIGHT); // We will only flush the "camera" screen, since the other screen is handled by `Console` top_screen.flush_buffer(); diff --git a/ctru-rs/examples/gfx-wide-mode.rs b/ctru-rs/examples/gfx-wide-mode.rs index 0acf1b2..8036538 100644 --- a/ctru-rs/examples/gfx-wide-mode.rs +++ b/ctru-rs/examples/gfx-wide-mode.rs @@ -26,7 +26,7 @@ fn main() { console = Console::new(gfx.top_screen.borrow_mut()); println!("Press A to enable/disable wide screen mode."); } - + gfx.wait_for_vblank(); } } diff --git a/ctru-rs/examples/thread-info.rs b/ctru-rs/examples/thread-info.rs index 2316f16..28297ac 100644 --- a/ctru-rs/examples/thread-info.rs +++ b/ctru-rs/examples/thread-info.rs @@ -44,7 +44,7 @@ fn main() { if hid.keys_down().contains(KeyPad::KEY_START) { break; } - + gfx.wait_for_vblank(); } } diff --git a/ctru-rs/src/applets/mii_selector.rs b/ctru-rs/src/applets/mii_selector.rs index e7d0559..9d3b3ca 100644 --- a/ctru-rs/src/applets/mii_selector.rs +++ b/ctru-rs/src/applets/mii_selector.rs @@ -155,6 +155,12 @@ impl MiiSelector { } } +impl Default for MiiSelector { + fn default() -> Self { + Self::new() + } +} + impl From for SelectionResult { fn from(ret: ctru_sys::MiiSelectorReturn) -> Self { let raw_mii_data = ret.mii; diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index 1891492..e3ba62e 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -16,9 +16,9 @@ impl<'screen> Console<'screen> { /// 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. - /// + /// /// # Notes - /// + /// /// [Console] automatically takes care of flushing and swapping buffers for its screen when printing. pub fn new(screen: RefMut<'screen, dyn Screen>) -> Self { let mut context = Box::::default(); diff --git a/ctru-rs/src/services/fs.rs b/ctru-rs/src/services/fs.rs index e1703c5..7788373 100644 --- a/ctru-rs/src/services/fs.rs +++ b/ctru-rs/src/services/fs.rs @@ -29,7 +29,7 @@ bitflags! { const FS_WRITE_FLUSH = 1; const FS_WRITE_UPDATE_TIME = 256; } - + #[derive(Default)] struct FsAttribute: u32 { const FS_ATTRIBUTE_DIRECTORY = 1; diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index 52638c2..1412bb7 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -56,17 +56,22 @@ pub trait Screen: private::Sealed { } /// Swaps the video buffers. - /// + /// /// This should be used even if double buffering is disabled. fn flush_buffer(&mut self) { let framebuffer = self.raw_framebuffer(); // Flush the data array. `self.raw_framebuffer` should get the correct parameters for all kinds of screens - unsafe { ctru_sys::GSPGPU_FlushDataCache(framebuffer.ptr.cast(), (framebuffer.height * framebuffer.width) as u32) }; + unsafe { + ctru_sys::GSPGPU_FlushDataCache( + framebuffer.ptr.cast(), + (framebuffer.height * framebuffer.width) as u32, + ) + }; } /// Swaps the video buffers. - /// + /// /// This should be used even if double buffering is disabled. fn swap_buffers(&mut self) { unsafe { ctru_sys::gfxScreenSwapBuffers(self.side().into(), true) }; From f37ccaea13b1b67f920f92c736c0d5b1639f0f21 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 6 Apr 2023 12:14:39 +0200 Subject: [PATCH 06/21] Undo double flush in example --- ctru-rs/examples/gfx-3d-mode.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/ctru-rs/examples/gfx-3d-mode.rs b/ctru-rs/examples/gfx-3d-mode.rs index d0f77b2..5bd982f 100644 --- a/ctru-rs/examples/gfx-3d-mode.rs +++ b/ctru-rs/examples/gfx-3d-mode.rs @@ -64,9 +64,6 @@ fn main() { left.flush_buffer(); left.swap_buffers(); - right.flush_buffer(); - right.swap_buffers(); - //Wait for VBlank gfx.wait_for_vblank(); } From 7aef0de46bf35238c6a349bab630c345e0ad788b Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Wed, 5 Apr 2023 20:35:13 +0200 Subject: [PATCH 07/21] Update for linker-fix-3ds rename --- ctru-rs/Cargo.toml | 2 +- ctru-rs/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ctru-rs/Cargo.toml b/ctru-rs/Cargo.toml index d8b095a..a7a160c 100644 --- a/ctru-rs/Cargo.toml +++ b/ctru-rs/Cargo.toml @@ -15,7 +15,7 @@ name = "ctru" cfg-if = "1.0" ctru-sys = { path = "../ctru-sys", version = "21.2" } const-zero = "0.1.0" -linker-fix-3ds = { git = "https://github.com/rust3ds/rust-linker-fix-3ds.git" } +shim-3ds = { git = "https://github.com/rust3ds/shim-3ds.git" } pthread-3ds = { git = "https://github.com/rust3ds/pthread-3ds.git" } libc = "0.2.121" bitflags = "1.0.0" diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 57b61e9..12939d2 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -8,7 +8,7 @@ #![test_runner(test_runner::run)] // Nothing is imported from these crates but their inclusion here assures correct linking of the missing implementations. -extern crate linker_fix_3ds; +extern crate shim_3ds; extern crate pthread_3ds; #[no_mangle] From bca9aa791828dc7cd1232621085d34d792959c3a Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Wed, 5 Apr 2023 20:41:31 +0200 Subject: [PATCH 08/21] fmt --- ctru-rs/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 12939d2..210d37f 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -8,8 +8,8 @@ #![test_runner(test_runner::run)] // Nothing is imported from these crates but their inclusion here assures correct linking of the missing implementations. -extern crate shim_3ds; extern crate pthread_3ds; +extern crate shim_3ds; #[no_mangle] #[cfg(feature = "big-stack")] From a2a990b715c7b1bd715b2f85abd22ae614d4c741 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Sat, 8 Apr 2023 14:17:08 +0200 Subject: [PATCH 09/21] Compilation error when romfs can't be found --- ctru-rs/src/services/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ctru-rs/src/services/mod.rs b/ctru-rs/src/services/mod.rs index 6b9e115..ec554cb 100644 --- a/ctru-rs/src/services/mod.rs +++ b/ctru-rs/src/services/mod.rs @@ -34,6 +34,8 @@ cfg_if::cfg_if! { //! [package.metadata.cargo-3ds] //! romfs_dir = "romfs" //! ``` + + compile_error!("romfs feature is enabled but no romfs found!"); } } } From 93fdaffa6b018433049000194cd0dda770d0bf54 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Sat, 8 Apr 2023 15:04:42 +0200 Subject: [PATCH 10/21] fmt --- ctru-rs/src/services/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ctru-rs/src/services/mod.rs b/ctru-rs/src/services/mod.rs index ec554cb..c466f04 100644 --- a/ctru-rs/src/services/mod.rs +++ b/ctru-rs/src/services/mod.rs @@ -34,7 +34,7 @@ cfg_if::cfg_if! { //! [package.metadata.cargo-3ds] //! romfs_dir = "romfs" //! ``` - + compile_error!("romfs feature is enabled but no romfs found!"); } } From ef4ea1797ac199bd4c747678b20a59e7b8db7880 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Sat, 8 Apr 2023 20:20:01 +0200 Subject: [PATCH 11/21] Compilation error only if feature is set --- ctru-rs/src/services/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ctru-rs/src/services/mod.rs b/ctru-rs/src/services/mod.rs index c466f04..2c51d10 100644 --- a/ctru-rs/src/services/mod.rs +++ b/ctru-rs/src/services/mod.rs @@ -34,7 +34,9 @@ cfg_if::cfg_if! { //! [package.metadata.cargo-3ds] //! romfs_dir = "romfs" //! ``` - + + // If the feature is set, but no "romfs" directory was found: send an error during compilation. + #[cfg(feature = "romfs")] compile_error!("romfs feature is enabled but no romfs found!"); } } From a0e36f3753a612774b82fbb3443054e76987f4ef Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Sat, 8 Apr 2023 20:21:38 +0200 Subject: [PATCH 12/21] fmt --- ctru-rs/src/services/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ctru-rs/src/services/mod.rs b/ctru-rs/src/services/mod.rs index 2c51d10..1d31dea 100644 --- a/ctru-rs/src/services/mod.rs +++ b/ctru-rs/src/services/mod.rs @@ -34,7 +34,7 @@ cfg_if::cfg_if! { //! [package.metadata.cargo-3ds] //! romfs_dir = "romfs" //! ``` - + // If the feature is set, but no "romfs" directory was found: send an error during compilation. #[cfg(feature = "romfs")] compile_error!("romfs feature is enabled but no romfs found!"); From 8821ed4326e5328c162b2410c4bd42371eeebb69 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Tue, 11 Apr 2023 12:58:02 +0200 Subject: [PATCH 13/21] Explicit DPAD nomenclature --- ctru-rs/src/services/hid.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ctru-rs/src/services/hid.rs b/ctru-rs/src/services/hid.rs index 2db0d23..79a649b 100644 --- a/ctru-rs/src/services/hid.rs +++ b/ctru-rs/src/services/hid.rs @@ -13,10 +13,10 @@ bitflags::bitflags! { const B = ctru_sys::KEY_B; const SELECT = ctru_sys::KEY_SELECT; const START = ctru_sys::KEY_START; - const DRIGHT = ctru_sys::KEY_DRIGHT; - const DLEFT = ctru_sys::KEY_DLEFT; - const DUP = ctru_sys::KEY_DUP; - const DDOWN = ctru_sys::KEY_DDOWN; + const DPAD_RIGHT = ctru_sys::KEY_DRIGHT; + const DPAD_LEFT = ctru_sys::KEY_DLEFT; + const DPAD_UP = ctru_sys::KEY_DUP; + const DPAD_DOWN = ctru_sys::KEY_DDOWN; const R = ctru_sys::KEY_R; const L = ctru_sys::KEY_L; const X = ctru_sys::KEY_X; @@ -33,10 +33,10 @@ bitflags::bitflags! { const CPAD_UP = ctru_sys::KEY_CPAD_UP; const CPAD_DOWN = ctru_sys::KEY_CPAD_DOWN; // Convenience catch-all for the dpad and cpad - const UP = KeyPad::DUP.bits() | KeyPad::CPAD_UP.bits(); - const DOWN = KeyPad::DDOWN.bits() | KeyPad::CPAD_DOWN.bits(); - const LEFT = KeyPad::DLEFT.bits() | KeyPad::CPAD_LEFT.bits(); - const RIGHT = KeyPad::DRIGHT.bits() | KeyPad::CPAD_RIGHT.bits(); + const UP = KeyPad::DPAD_UP.bits() | KeyPad::CPAD_UP.bits(); + const DOWN = KeyPad::DPAD_DOWN.bits() | KeyPad::CPAD_DOWN.bits(); + const LEFT = KeyPad::DPAD_LEFT.bits() | KeyPad::CPAD_LEFT.bits(); + const RIGHT = KeyPad::DPAD_RIGHT.bits() | KeyPad::CPAD_RIGHT.bits(); } } From 80efa807b1889355ecc24c5fbce8a7133d7807f9 Mon Sep 17 00:00:00 2001 From: Meziu <55318903+Meziu@users.noreply.github.com> Date: Tue, 11 Apr 2023 20:21:14 +0200 Subject: [PATCH 14/21] Update flush_buffer docs Co-authored-by: Ian Chamberlain --- ctru-rs/src/services/gfx.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index 1412bb7..49dc6cd 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -55,7 +55,7 @@ pub trait Screen: private::Sealed { unsafe { ctru_sys::gfxSetDoubleBuffering(self.as_raw(), enabled) } } - /// Swaps the video buffers. + /// Flushes the video buffer for this screen. /// /// This should be used even if double buffering is disabled. fn flush_buffer(&mut self) { From 71ad3e94623e4e2b25fc46182f5ded074f396e69 Mon Sep 17 00:00:00 2001 From: Meziu <55318903+Meziu@users.noreply.github.com> Date: Wed, 12 Apr 2023 11:39:34 +0200 Subject: [PATCH 15/21] Update gfx.rs --- ctru-rs/src/services/gfx.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index 49dc6cd..9ac0a34 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -56,8 +56,6 @@ pub trait Screen: private::Sealed { } /// Flushes the video buffer for this screen. - /// - /// This should be used even if double buffering is disabled. fn flush_buffer(&mut self) { let framebuffer = self.raw_framebuffer(); From 683fad4aee346d73c731f0e7ef652f84abeac322 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Wed, 12 Apr 2023 11:06:20 -0400 Subject: [PATCH 16/21] Initial pass of Swap/Flush traits for screens --- ctru-rs/Cargo.toml | 4 +- ctru-rs/examples/camera-image.rs | 2 +- ctru-rs/examples/gfx-3d-mode.rs | 14 +++-- ctru-rs/examples/graphics-bitmap.rs | 2 +- ctru-rs/src/lib.rs | 2 +- ctru-rs/src/services/gfx.rs | 97 +++++++++++++++++++++-------- 6 files changed, 84 insertions(+), 37 deletions(-) diff --git a/ctru-rs/Cargo.toml b/ctru-rs/Cargo.toml index d8b095a..a79e7ac 100644 --- a/ctru-rs/Cargo.toml +++ b/ctru-rs/Cargo.toml @@ -1,5 +1,5 @@ [package] -authors = [ "Rust3DS Org", "Ronald Kinard " ] +authors = ["Rust3DS Org", "Ronald Kinard "] description = "A safe wrapper around smealum's ctrulib." license = "Zlib" name = "ctru-rs" @@ -15,7 +15,7 @@ name = "ctru" cfg-if = "1.0" ctru-sys = { path = "../ctru-sys", version = "21.2" } const-zero = "0.1.0" -linker-fix-3ds = { git = "https://github.com/rust3ds/rust-linker-fix-3ds.git" } +shim-3ds = { git = "https://github.com/rust3ds/shim-3ds.git" } pthread-3ds = { git = "https://github.com/rust3ds/pthread-3ds.git" } libc = "0.2.121" bitflags = "1.0.0" diff --git a/ctru-rs/examples/camera-image.rs b/ctru-rs/examples/camera-image.rs index dcc92ff..010ef95 100644 --- a/ctru-rs/examples/camera-image.rs +++ b/ctru-rs/examples/camera-image.rs @@ -1,6 +1,6 @@ use ctru::prelude::*; use ctru::services::cam::{Cam, Camera, OutputFormat, ShutterSound, ViewSize}; -use ctru::services::gfx::Screen; +use ctru::services::gfx::{Flush, Screen, Swap}; use ctru::services::gspgpu::FramebufferFormat; use std::time::Duration; diff --git a/ctru-rs/examples/gfx-3d-mode.rs b/ctru-rs/examples/gfx-3d-mode.rs index 5bd982f..4108694 100644 --- a/ctru-rs/examples/gfx-3d-mode.rs +++ b/ctru-rs/examples/gfx-3d-mode.rs @@ -1,5 +1,5 @@ use ctru::prelude::*; -use ctru::services::gfx::{Screen, Side, TopScreen3D}; +use ctru::services::gfx::{Screen, Side, Swap, TopScreen3D}; /// See `graphics-bitmap.rs` for details on how the image is generated. /// @@ -21,8 +21,7 @@ fn main() { gfx.top_screen.borrow_mut().set_double_buffering(true); - let top_screen = TopScreen3D::from(&gfx.top_screen); - let (mut left, mut right) = top_screen.split_mut(); + let mut top_screen = TopScreen3D::from(&gfx.top_screen); let mut current_side = Side::Left; @@ -35,6 +34,8 @@ fn main() { break; } + let (mut left, mut right) = top_screen.split_mut(); + let left_buf = left.raw_framebuffer(); let right_buf = right.raw_framebuffer(); @@ -61,8 +62,11 @@ fn main() { buf.copy_from(IMAGE.as_ptr(), IMAGE.len()); } - left.flush_buffer(); - left.swap_buffers(); + drop(left); + drop(right); + + top_screen.flush_buffers(); + top_screen.swap_buffers(); //Wait for VBlank gfx.wait_for_vblank(); diff --git a/ctru-rs/examples/graphics-bitmap.rs b/ctru-rs/examples/graphics-bitmap.rs index 4beacea..01358ac 100644 --- a/ctru-rs/examples/graphics-bitmap.rs +++ b/ctru-rs/examples/graphics-bitmap.rs @@ -1,5 +1,5 @@ use ctru::prelude::*; -use ctru::services::gfx::Screen; +use ctru::services::gfx::{Flush, Screen, Swap}; /// Ferris image taken from and scaled down to 320x240px. /// To regenerate the data, you will need to install `imagemagick` and run this diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 57b61e9..210d37f 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -8,8 +8,8 @@ #![test_runner(test_runner::run)] // Nothing is imported from these crates but their inclusion here assures correct linking of the missing implementations. -extern crate linker_fix_3ds; extern crate pthread_3ds; +extern crate shim_3ds; #[no_mangle] #[cfg(feature = "big-stack")] diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index 1412bb7..6bbb02d 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -9,11 +9,12 @@ use crate::services::gspgpu::{self, FramebufferFormat}; use crate::services::ServiceReference; mod private { - use super::{BottomScreen, TopScreen, TopScreenLeft, TopScreenRight}; + use super::{BottomScreen, TopScreen, TopScreen3D, TopScreenLeft, TopScreenRight}; pub trait Sealed {} impl Sealed for TopScreen {} + impl Sealed for TopScreen3D<'_> {} impl Sealed for TopScreenLeft {} impl Sealed for TopScreenRight {} impl Sealed for BottomScreen {} @@ -55,28 +56,6 @@ pub trait Screen: private::Sealed { unsafe { ctru_sys::gfxSetDoubleBuffering(self.as_raw(), enabled) } } - /// Swaps the video buffers. - /// - /// This should be used even if double buffering is disabled. - fn flush_buffer(&mut self) { - let framebuffer = self.raw_framebuffer(); - - // Flush the data array. `self.raw_framebuffer` should get the correct parameters for all kinds of screens - unsafe { - ctru_sys::GSPGPU_FlushDataCache( - framebuffer.ptr.cast(), - (framebuffer.height * framebuffer.width) as u32, - ) - }; - } - - /// Swaps the video buffers. - /// - /// This should be used even if double buffering is disabled. - fn swap_buffers(&mut self) { - unsafe { ctru_sys::gfxScreenSwapBuffers(self.side().into(), true) }; - } - /// Gets the framebuffer format fn framebuffer_format(&self) -> FramebufferFormat { unsafe { ctru_sys::gfxGetScreenFormat(self.as_raw()) }.into() @@ -101,13 +80,77 @@ pub struct TopScreen3D<'top_screen> { screen: &'top_screen RefCell, } -struct TopScreenLeft; +pub trait Swap: private::Sealed { + fn swap_buffers(&mut self); +} + +trait SwappableScreen: Screen {} +impl SwappableScreen for TopScreen {} +impl SwappableScreen for BottomScreen {} + +impl Swap for S { + fn swap_buffers(&mut self) { + unsafe { + ctru_sys::gfxScreenSwapBuffers(self.side().into(), false); + } + } +} + +pub trait Flush: private::Sealed { + fn flush_buffer(&mut self); +} -struct TopScreenRight; +trait FlushableScreen: Screen {} +impl FlushableScreen for TopScreen {} +impl FlushableScreen for BottomScreen {} +impl FlushableScreen for TopScreenLeft {} +impl FlushableScreen for TopScreenRight {} +impl Flush for S { + fn flush_buffer(&mut self) { + let framebuffer = self.raw_framebuffer(); + + // Flush the data array. `self.raw_framebuffer` should get the correct parameters for all kinds of screens + unsafe { + ctru_sys::GSPGPU_FlushDataCache( + framebuffer.ptr.cast(), + (framebuffer.height * framebuffer.width) as u32, + ) + }; + } +} + +impl Swap for TopScreen3D<'_> { + fn swap_buffers(&mut self) { + unsafe { + ctru_sys::gfxScreenSwapBuffers(Side::Left.into(), true); + } + } +} + +impl TopScreen3D<'_> { + /// Flush the buffers for both the left and right sides of the screen. + pub fn flush_buffers(&mut self) { + let (mut left, mut right) = self.split_mut(); + left.flush_buffer(); + right.flush_buffer(); + } +} + +/// The left side of the top screen, when using 3D mode. +#[derive(Debug)] +#[non_exhaustive] +pub struct TopScreenLeft; + +/// The right side of the top screen, when using 3D mode. +#[derive(Debug)] #[non_exhaustive] +pub struct TopScreenRight; + /// The bottom screen. Mutable access to this struct is required to write to the /// bottom screen's frame buffer. +#[derive(Debug)] +#[non_exhaustive] pub struct BottomScreen; /// Representation of a framebuffer for one [`Side`] of the top screen, or the @@ -194,14 +237,14 @@ impl Gfx { impl TopScreen3D<'_> { /// Immutably borrow the two sides of the screen as `(left, right)`. - pub fn split(&self) -> (Ref, Ref) { + pub fn split(&self) -> (Ref, Ref) { Ref::map_split(self.screen.borrow(), |screen| { (&screen.left as _, &screen.right as _) }) } /// Mutably borrow the two sides of the screen as `(left, right)`. - pub fn split_mut(&self) -> (RefMut, RefMut) { + pub fn split_mut(&self) -> (RefMut, RefMut) { RefMut::map_split(self.screen.borrow_mut(), |screen| { (&mut screen.left as _, &mut screen.right as _) }) From 0ef017cf4d312441160bb43b0ec14f6305fad550 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Wed, 12 Apr 2023 11:16:23 -0400 Subject: [PATCH 17/21] Do some cleanup and fix swap trait impls Oops! The argument to `gfxScreenSwapBuffers` is gfxScreen_t, not gfx3dSide_t ! Unfortunately they are both alias to libc::c_uint so we didn't get any help from the Rust compiler when passing the screen side into this function. --- ctru-rs/examples/gfx-3d-mode.rs | 3 +- ctru-rs/src/services/gfx.rs | 54 +++++++++++++++++---------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/ctru-rs/examples/gfx-3d-mode.rs b/ctru-rs/examples/gfx-3d-mode.rs index 596630e..582994e 100644 --- a/ctru-rs/examples/gfx-3d-mode.rs +++ b/ctru-rs/examples/gfx-3d-mode.rs @@ -62,8 +62,7 @@ fn main() { buf.copy_from(IMAGE.as_ptr(), IMAGE.len()); } - drop(left); - drop(right); + drop((left, right)); top_screen.flush_buffers(); top_screen.swap_buffers(); diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index 65c23c2..edc1607 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -81,20 +81,32 @@ pub struct TopScreen3D<'top_screen> { } pub trait Swap: private::Sealed { + /// Swaps the video buffers. + /// + /// This should be used even if double buffering is disabled. fn swap_buffers(&mut self); } -trait SwappableScreen: Screen {} -impl SwappableScreen for TopScreen {} -impl SwappableScreen for BottomScreen {} +impl Swap for TopScreen3D<'_> { + fn swap_buffers(&mut self) { + unsafe { + ctru_sys::gfxScreenSwapBuffers(ctru_sys::GFX_TOP, true); + } + } +} -impl Swap for S { - /// Swaps the video buffers. - /// - /// This should be used even if double buffering is disabled. +impl Swap for TopScreen { + fn swap_buffers(&mut self) { + unsafe { + ctru_sys::gfxScreenSwapBuffers(ctru_sys::GFX_TOP, false); + } + } +} + +impl Swap for BottomScreen { fn swap_buffers(&mut self) { unsafe { - ctru_sys::gfxScreenSwapBuffers(self.side().into(), false); + ctru_sys::gfxScreenSwapBuffers(ctru_sys::GFX_BOTTOM, false); } } } @@ -124,23 +136,6 @@ impl Flush for S { } } -impl Swap for TopScreen3D<'_> { - fn swap_buffers(&mut self) { - unsafe { - ctru_sys::gfxScreenSwapBuffers(Side::Left.into(), true); - } - } -} - -impl TopScreen3D<'_> { - /// Flush the buffers for both the left and right sides of the screen. - pub fn flush_buffers(&mut self) { - let (mut left, mut right) = self.split_mut(); - left.flush_buffer(); - right.flush_buffer(); - } -} - /// The left side of the top screen, when using 3D mode. #[derive(Debug)] #[non_exhaustive] @@ -253,6 +248,13 @@ impl TopScreen3D<'_> { (&mut screen.left as _, &mut screen.right as _) }) } + + /// Convenient helper to flush the buffers for both the left and right sides of the screen. + pub fn flush_buffers(&mut self) { + let (mut left, mut right) = self.split_mut(); + left.flush_buffer(); + right.flush_buffer(); + } } impl<'top_screen> From<&'top_screen RefCell> for TopScreen3D<'top_screen> { @@ -296,7 +298,7 @@ impl TopScreen { impl Screen for TopScreen { fn as_raw(&self) -> ctru_sys::gfxScreen_t { - self.left.as_raw() + ctru_sys::GFX_TOP } fn side(&self) -> Side { From a417218182f1090724245d317bcea5e6c845e5fc Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Wed, 12 Apr 2023 11:26:43 -0400 Subject: [PATCH 18/21] On second thought, it seems better to reuse traits Rename to flush_buffers, and just have TopScreen3D's impl do both. This still allows individual flushing of left and right if needed but most people would probably just use `TopScreen3D::flush_buffers`. --- ctru-rs/examples/camera-image.rs | 2 +- ctru-rs/examples/graphics-bitmap.rs | 2 +- ctru-rs/src/services/gfx.rs | 32 ++++++++++++++--------------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/ctru-rs/examples/camera-image.rs b/ctru-rs/examples/camera-image.rs index 010ef95..0042599 100644 --- a/ctru-rs/examples/camera-image.rs +++ b/ctru-rs/examples/camera-image.rs @@ -88,7 +88,7 @@ fn main() { rotate_image_to_screen(&buf, top_screen.raw_framebuffer().ptr, WIDTH, HEIGHT); // We will only flush the "camera" screen, since the other screen is handled by `Console` - top_screen.flush_buffer(); + top_screen.flush_buffers(); top_screen.swap_buffers(); gfx.wait_for_vblank(); diff --git a/ctru-rs/examples/graphics-bitmap.rs b/ctru-rs/examples/graphics-bitmap.rs index 01358ac..df9ce11 100644 --- a/ctru-rs/examples/graphics-bitmap.rs +++ b/ctru-rs/examples/graphics-bitmap.rs @@ -48,7 +48,7 @@ fn main() { } // Flush and swap framebuffers - bottom_screen.flush_buffer(); + bottom_screen.flush_buffers(); bottom_screen.swap_buffers(); //Wait for VBlank diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index edc1607..59cf829 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -112,18 +112,13 @@ impl Swap for BottomScreen { } pub trait Flush: private::Sealed { - /// Flushes the video buffer for this screen. - fn flush_buffer(&mut self); + /// Flushes the video buffer(s) for this screen. Note that you must still call + /// [`Swap::swap_buffers`] after this method for the buffer contents to be displayed. + fn flush_buffers(&mut self); } -trait FlushableScreen: Screen {} -impl FlushableScreen for TopScreen {} -impl FlushableScreen for BottomScreen {} -impl FlushableScreen for TopScreenLeft {} -impl FlushableScreen for TopScreenRight {} - -impl Flush for S { - fn flush_buffer(&mut self) { +impl Flush for S { + fn flush_buffers(&mut self) { let framebuffer = self.raw_framebuffer(); // Flush the data array. `self.raw_framebuffer` should get the correct parameters for all kinds of screens @@ -136,6 +131,16 @@ impl Flush for S { } } +impl Flush for TopScreen3D<'_> { + /// Unlike most other implementations of [`Flush`], this flushes the buffers for both + /// the left and right sides of the top screen. + fn flush_buffers(&mut self) { + let (mut left, mut right) = self.split_mut(); + left.flush_buffers(); + right.flush_buffers(); + } +} + /// The left side of the top screen, when using 3D mode. #[derive(Debug)] #[non_exhaustive] @@ -248,13 +253,6 @@ impl TopScreen3D<'_> { (&mut screen.left as _, &mut screen.right as _) }) } - - /// Convenient helper to flush the buffers for both the left and right sides of the screen. - pub fn flush_buffers(&mut self) { - let (mut left, mut right) = self.split_mut(); - left.flush_buffer(); - right.flush_buffer(); - } } impl<'top_screen> From<&'top_screen RefCell> for TopScreen3D<'top_screen> { From b239fb8d9f9074efd2bdb6660cd7ab63a151c996 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Fri, 14 Apr 2023 13:34:10 -0400 Subject: [PATCH 19/21] Update doc comments and bitmap example Clarify the behavior of `swap_buffers` for single buffering mode and update bitmap example to prove that this is actually how it works. --- .../{graphics-bitmap.rs => gfx-bitmap.rs} | 35 ++++++++++---- ctru-rs/src/services/gfx.rs | 47 +++++++++++++------ 2 files changed, 59 insertions(+), 23 deletions(-) rename ctru-rs/examples/{graphics-bitmap.rs => gfx-bitmap.rs} (58%) diff --git a/ctru-rs/examples/graphics-bitmap.rs b/ctru-rs/examples/gfx-bitmap.rs similarity index 58% rename from ctru-rs/examples/graphics-bitmap.rs rename to ctru-rs/examples/gfx-bitmap.rs index df9ce11..f680ff2 100644 --- a/ctru-rs/examples/graphics-bitmap.rs +++ b/ctru-rs/examples/gfx-bitmap.rs @@ -22,21 +22,20 @@ fn main() { let apt = Apt::new().expect("Couldn't obtain APT controller"); let _console = Console::new(gfx.top_screen.borrow_mut()); - println!("\x1b[21;16HPress Start to exit."); + println!("\x1b[21;4HPress Start to exit, or A to flip the image."); let mut bottom_screen = gfx.bottom_screen.borrow_mut(); // We don't need double buffering in this example. // In this way we can draw our image only once on screen. bottom_screen.set_double_buffering(false); + // Swapping buffers commits the change from the line above. + bottom_screen.swap_buffers(); - // We assume the image is the correct size already, so we drop width + height. - let frame_buffer = bottom_screen.raw_framebuffer(); + // 3 bytes per pixel, we just want to reverse the pixels but not individual bytes + let flipped_image: Vec<_> = IMAGE.chunks(3).rev().flatten().copied().collect(); - // Copy the image into the frame buffer - unsafe { - frame_buffer.ptr.copy_from(IMAGE.as_ptr(), IMAGE.len()); - } + let mut image_bytes = IMAGE; // Main loop while apt.main_loop() { @@ -47,9 +46,27 @@ fn main() { break; } - // Flush and swap framebuffers + // We assume the image is the correct size already, so we drop width + height. + let frame_buffer = bottom_screen.raw_framebuffer(); + + if hid.keys_down().contains(KeyPad::A) { + image_bytes = if std::ptr::eq(image_bytes, IMAGE) { + &flipped_image[..] + } else { + IMAGE + }; + } + + // this copies more than necessary (once per frame) but it's fine... + unsafe { + frame_buffer + .ptr + .copy_from(image_bytes.as_ptr(), image_bytes.len()); + } + + // Flush framebuffers. Since we're not using double buffering, + // this will render the pixels immediately bottom_screen.flush_buffers(); - bottom_screen.swap_buffers(); //Wait for VBlank gfx.wait_for_vblank(); diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index 59cf829..3a5ba37 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -50,18 +50,21 @@ pub trait Screen: private::Sealed { /// Sets whether to use double buffering. Enabled by default. /// - /// Note that even when double buffering is disabled, one should still use the `swap_buffers` - /// method on each frame to keep the gsp configuration up to date + /// [`Swap::swap_buffers`] must be called after this function for the configuration + /// change to take effect. fn set_double_buffering(&mut self, enabled: bool) { unsafe { ctru_sys::gfxSetDoubleBuffering(self.as_raw(), enabled) } } - /// Gets the framebuffer format + /// Gets the framebuffer format. fn framebuffer_format(&self) -> FramebufferFormat { unsafe { ctru_sys::gfxGetScreenFormat(self.as_raw()) }.into() } - /// Change the framebuffer format + /// Change the framebuffer format. + /// + /// [`Swap::swap_buffers`] must be called after this method for the configuration + /// change to take effect. fn set_framebuffer_format(&mut self, fmt: FramebufferFormat) { unsafe { ctru_sys::gfxSetScreenFormat(self.as_raw(), fmt.into()) } } @@ -76,14 +79,25 @@ pub struct TopScreen { /// A helper container for both sides of the top screen. Once the [`TopScreen`] is /// converted into this, 3D mode will be enabled until this struct is dropped. -pub struct TopScreen3D<'top_screen> { - screen: &'top_screen RefCell, +pub struct TopScreen3D<'screen> { + screen: &'screen RefCell, } +/// A screen that can have its frame buffers swapped, if double buffering is enabled. +/// +/// This trait applies to all [`Screen`]s that have swappable frame buffers. pub trait Swap: private::Sealed { /// Swaps the video buffers. /// - /// This should be used even if double buffering is disabled. + /// If double buffering is disabled, "swapping" the buffers has the side effect + /// of committing any configuration changes to the buffers (e.g. [`set_wide_mode`], + /// [`set_framebuffer_format`], [`set_double_buffering`]). + /// + /// This should be called once per frame at most. + /// + /// [`set_wide_mode`]: TopScreen::set_wide_mode + /// [`set_framebuffer_format`]: Screen::set_framebuffer_format + /// [`set_double_buffering`]: Screen::set_double_buffering fn swap_buffers(&mut self); } @@ -111,6 +125,8 @@ impl Swap for BottomScreen { } } +/// A screen with buffers that can be flushed. This trait applies to any [`Screen`] +/// that has data written to its frame buffer. pub trait Flush: private::Sealed { /// Flushes the video buffer(s) for this screen. Note that you must still call /// [`Swap::swap_buffers`] after this method for the buffer contents to be displayed. @@ -242,21 +258,19 @@ impl Gfx { impl TopScreen3D<'_> { /// Immutably borrow the two sides of the screen as `(left, right)`. pub fn split(&self) -> (Ref, Ref) { - Ref::map_split(self.screen.borrow(), |screen| { - (&screen.left as _, &screen.right as _) - }) + Ref::map_split(self.screen.borrow(), |screen| (&screen.left, &screen.right)) } /// Mutably borrow the two sides of the screen as `(left, right)`. pub fn split_mut(&self) -> (RefMut, RefMut) { RefMut::map_split(self.screen.borrow_mut(), |screen| { - (&mut screen.left as _, &mut screen.right as _) + (&mut screen.left, &mut screen.right) }) } } -impl<'top_screen> From<&'top_screen RefCell> for TopScreen3D<'top_screen> { - fn from(top_screen: &'top_screen RefCell) -> Self { +impl<'screen> From<&'screen RefCell> for TopScreen3D<'screen> { + fn from(top_screen: &'screen RefCell) -> Self { unsafe { ctru_sys::gfxSet3D(true); } @@ -282,6 +296,9 @@ impl TopScreen { } /// Enable or disable wide mode on the top screen. + /// + /// [`Swap::swap_buffers`] must be called after this method for the configuration + /// to take effect. pub fn set_wide_mode(&mut self, enable: bool) { unsafe { ctru_sys::gfxSetWide(enable); @@ -294,9 +311,11 @@ impl TopScreen { } } +// When 3D mode is disabled, only the left side is used, so this Screen impl +// just forwards everything to the TopScreenLeft. impl Screen for TopScreen { fn as_raw(&self) -> ctru_sys::gfxScreen_t { - ctru_sys::GFX_TOP + self.left.as_raw() } fn side(&self) -> Side { From 76ef8e637ef2ff11d981f58f2300d0f37ba7635d Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 4 May 2023 14:20:12 +0200 Subject: [PATCH 20/21] Fix merge messages --- ctru-rs/src/services/fs.rs | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/ctru-rs/src/services/fs.rs b/ctru-rs/src/services/fs.rs index d4b855e..04e0c1e 100644 --- a/ctru-rs/src/services/fs.rs +++ b/ctru-rs/src/services/fs.rs @@ -220,13 +220,13 @@ pub struct Metadata { /// ```no_run /// use ctru::services::fs::{Fs, OpenOptions}; /// -<<<<<<< HEAD -/// let fs = Fs::new().unwrap(); -/// let sdmc_archive = fs.sdmc().unwrap(); -======= /// let mut fs = Fs::new().unwrap(); +/// let mut sdmc_archive = fs.sdmc().unwrap(); +/// let file = OpenOptions::new() +/// .read(true) /// .archive(&sdmc_archive) /// .open("foo.txt") +/// .unwrap(); /// ``` /// /// Opening a file for both reading and writing, as well as creating it if it @@ -235,13 +235,8 @@ pub struct Metadata { /// ```no_run /// use ctru::services::fs::{Fs, OpenOptions}; /// -<<<<<<< HEAD -/// let fs = Fs::new().unwrap(); -/// let sdmc_archive = fs.sdmc().unwrap(); -======= /// let mut fs = Fs::new().unwrap(); /// let mut sdmc_archive = fs.sdmc().unwrap(); ->>>>>>> improve/api /// let file = OpenOptions::new() /// .read(true) /// .write(true) @@ -367,13 +362,8 @@ impl File { /// ```no_run /// use ctru::services::fs::{Fs, File}; /// -<<<<<<< HEAD - /// let fs = Fs::new().unwrap(); - /// let sdmc_archive = fs.sdmc().unwrap(); -======= /// let mut fs = Fs::new().unwrap(); /// let mut sdmc_archive = fs.sdmc().unwrap(); ->>>>>>> improve/api /// let mut f = File::open(&sdmc_archive, "/foo.txt").unwrap(); /// ``` pub fn open>(arch: &Archive, path: P) -> IoResult { @@ -401,15 +391,9 @@ impl File { /// ```no_run /// use ctru::services::fs::{Fs, File}; /// -<<<<<<< HEAD - /// let fs = Fs::new().unwrap(); - /// let sdmc_archive = fs.sdmc().unwrap(); - /// let mut f = File::create(&sdmc_archive, "/foo.txt").unwrap(); -======= /// let mut fs = Fs::new().unwrap(); /// let mut sdmc_archive = fs.sdmc().unwrap(); /// let mut f = File::create(&mut sdmc_archive, "/foo.txt").unwrap(); ->>>>>>> improve/api /// ``` pub fn create>(arch: &mut Archive, path: P) -> IoResult { OpenOptions::new() From 40e5cc9587288e3a0074deac2c71f01bb73ef51f Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 4 May 2023 14:22:02 +0200 Subject: [PATCH 21/21] Readd trait imports --- ctru-rs/examples/graphics-bitmap.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ctru-rs/examples/graphics-bitmap.rs b/ctru-rs/examples/graphics-bitmap.rs index 4beacea..df9ce11 100644 --- a/ctru-rs/examples/graphics-bitmap.rs +++ b/ctru-rs/examples/graphics-bitmap.rs @@ -1,5 +1,5 @@ use ctru::prelude::*; -use ctru::services::gfx::Screen; +use ctru::services::gfx::{Flush, Screen, Swap}; /// Ferris image taken from and scaled down to 320x240px. /// To regenerate the data, you will need to install `imagemagick` and run this @@ -48,7 +48,7 @@ fn main() { } // Flush and swap framebuffers - bottom_screen.flush_buffer(); + bottom_screen.flush_buffers(); bottom_screen.swap_buffers(); //Wait for VBlank