From c79aeeac76f4fef9f1f30ffac597dd47c49022d9 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Sun, 13 Feb 2022 19:14:20 +0100 Subject: [PATCH] Basic port of the romfs example --- ctru-rs/Cargo.toml | 1 + ctru-rs/examples/romfs.rs | 48 +++++++++++++++++++++++++ ctru-rs/examples/romfs/ファイル.txt | 1 + 3 files changed, 50 insertions(+) create mode 100644 ctru-rs/examples/romfs.rs create mode 100644 ctru-rs/examples/romfs/ファイル.txt diff --git a/ctru-rs/Cargo.toml b/ctru-rs/Cargo.toml index 4807f32..abc6f79 100644 --- a/ctru-rs/Cargo.toml +++ b/ctru-rs/Cargo.toml @@ -28,6 +28,7 @@ ferris-says = "0.2.1" futures = "0.3" time = "0.3.7" tokio = { version = "1.16", features = ["rt", "time", "sync", "macros"] } +cfg-if = "1.0.0" [features] default = ["romfs"] diff --git a/ctru-rs/examples/romfs.rs b/ctru-rs/examples/romfs.rs new file mode 100644 index 0000000..82afa24 --- /dev/null +++ b/ctru-rs/examples/romfs.rs @@ -0,0 +1,48 @@ +use ctru::console::Console; +use ctru::gfx::Gfx; +use ctru::services::apt::Apt; +use ctru::services::hid::{Hid, KeyPad}; + +fn main() { + ctru::init(); + let gfx = Gfx::default(); + let 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()); + + cfg_if::cfg_if! { + // Run this code ff RomFS are wanted and available + // This never fails as `ctru-rs` examples inherit all of the `ctru` features, + // but it might in a normal user application wasn't setup correctly + if #[cfg(all(feature = "romfs", romfs_exists))] { + let _romfs = ctru::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"); + + let f = std::fs::read_to_string("romfs:/ファイル.txt").unwrap(); + // While RomFS supports UTF-16 file paths, `Console` doesn't... + println!("Contents of [UTF-16 File]: \n{f}\n"); + } else { + println!("No RomFS was found, are you sure you included it?") + } + } + + println!("\nPress START to exit"); + + // Main loop + while apt.main_loop() { + //Scan all the inputs. This should be done once for each frame + hid.scan_input(); + + if hid.keys_down().contains(KeyPad::KEY_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/ファイル.txt b/ctru-rs/examples/romfs/ファイル.txt new file mode 100644 index 0000000..d60c842 --- /dev/null +++ b/ctru-rs/examples/romfs/ファイル.txt @@ -0,0 +1 @@ +This filename has UTF-16 exclusive characters! \ No newline at end of file