aboutsummaryrefslogtreecommitdiff
path: root/examples/src
diff options
context:
space:
mode:
authorFenrir <[email protected]>2017-07-25 20:12:21 -0600
committerFenrir <[email protected]>2017-07-25 21:03:02 -0600
commitc159576e6d4a84e4958257f9b87ea9e06d4bbcb3 (patch)
treee3014df2d64dcb842f4b7850d4f252f4b6a7708e /examples/src
parentMerge pull request #36 from FenrirWolf/errDisp (diff)
downloadctru-rs-c159576e6d4a84e4958257f9b87ea9e06d4bbcb3.tar.xz
ctru-rs-c159576e6d4a84e4958257f9b87ea9e06d4bbcb3.zip
Add examples directory
Diffstat (limited to 'examples/src')
-rw-r--r--examples/src/bin/hello-both-screens.rs42
-rw-r--r--examples/src/bin/hello-world.rs55
2 files changed, 97 insertions, 0 deletions
diff --git a/examples/src/bin/hello-both-screens.rs b/examples/src/bin/hello-both-screens.rs
new file mode 100644
index 0000000..ade33d4
--- /dev/null
+++ b/examples/src/bin/hello-both-screens.rs
@@ -0,0 +1,42 @@
+extern crate ctru;
+
+use ctru::gfx::{Gfx, Screen};
+use ctru::console::Console;
+use ctru::services::apt::Apt;
+use ctru::services::hid::{Hid, PadKey};
+
+fn main() {
+ // Initialize services
+ let apt = Apt::init().unwrap();
+ let hid = Hid::init().unwrap();
+ let mut gfx = Gfx::default();
+
+ // Start a console on the top screen
+ let mut top_screen = Console::init(Screen::Top);
+
+ // Start a console on the bottom screen.
+ // The most recently initialized console will be active by default
+ let mut bottom_screen = Console::init(Screen::Bottom);
+
+ // Let's print on the top screen first
+ top_screen.select();
+ println!("This is the top screen! We have a lot of space up here!");
+
+ // Now let's print something on the bottom screen
+ bottom_screen.select();
+ println!("\x1b[14;00HThis is the bottom screen.");
+ println!("There's not as much space down here, but that's okay.");
+
+ top_screen.select();
+ println!("\x1b[29;16HPress Start to exit");
+
+ while apt.main_loop() {
+ gfx.flush_buffers();
+ gfx.swap_buffers();
+
+ hid.scan_input();
+ if hid.key_down(PadKey::Start) {
+ break;
+ }
+ }
+}
diff --git a/examples/src/bin/hello-world.rs b/examples/src/bin/hello-world.rs
new file mode 100644
index 0000000..cb63510
--- /dev/null
+++ b/examples/src/bin/hello-world.rs
@@ -0,0 +1,55 @@
+extern crate ctru;
+
+use ctru::gfx::Gfx;
+use ctru::console::Console;
+use ctru::services::apt::Apt;
+use ctru::services::hid::{Hid, PadKey};
+
+fn main() {
+ // Initialize ctrulib service handles.
+ // Service handles are internally reference-counted. When all instances of a
+ // service handle go out of scope, the service will be closed.
+
+ // The APT service handles application management functions, such as
+ // syncing our main loop with the graphics hardware.
+ let apt = Apt::init().unwrap();
+
+ // The HID service handles button and touch screen inputs.
+ let hid = Hid::init().unwrap();
+
+ // The GFX service manages the framebuffers for the top and bottom screens.
+ let mut gfx = Gfx::default();
+
+ // Initialize a ctrulib console and direct standard output to it.
+ // Consoles can be initialized on both the top and bottom screens.
+ // The top screen is initialized by default.
+ let _console = Console::default();
+
+ // Now we can print to stdout!
+ println!("Hello, world!");
+
+ // We can use escape sequences to move the cursor around the terminal.
+ // The following text will be moved down 29 rows and right 16 characters
+ // before printing begins.
+ println!("\x1b[29;16HPress Start to exit");
+
+ // Main application loop.
+ while apt.main_loop() {
+
+ // Flushes and swaps the framebuffers when double-buffering
+ // is enabled
+ gfx.flush_buffers();
+ gfx.swap_buffers();
+
+ // Scan for user input.
+ hid.scan_input();
+ // Check if the user has pressed the given button on this frame.
+ // If so, break out of the loop.
+ if hid.key_down(PadKey::Start) {
+ break;
+ }
+ }
+
+ // All of our service handles will drop out of scope at this point,
+ // triggering the end of our application.
+}