Browse Source

Update examples

pull/10/head
Fenrir 8 years ago
parent
commit
f70d1c9706
  1. 75
      examples/src/bin/buttons.rs
  2. 4
      examples/src/bin/hello-both-screens.rs
  3. 4
      examples/src/bin/hello-world.rs

75
examples/src/bin/buttons.rs

@ -0,0 +1,75 @@
extern crate ctru;
use ctru::gfx::Gfx;
use ctru::console::Console;
use ctru::services::apt::Apt;
use ctru::services::hid::{self, Hid, KeyPad};
fn main() {
// Setup services
let apt = Apt::init().unwrap();
let hid = Hid::init().unwrap();
let mut gfx = Gfx::default();
let mut console = Console::default();
println!("Hi there! Try pressing a button");
println!("\x1b[29;16HPress Start to exit");
// This struct will contain the keys that we held on the previous frame
let mut old_keys = KeyPad::empty();
while apt.main_loop() {
// Scan for user input on the current frame.
hid.scan_input();
// Get information about which keys were held down on this frame
let keys = hid.keys_held();
// We only want to print when the keys we're holding now are different
// from what they were on the previous frame
if keys != old_keys {
// Clear the screen
console.clear();
// We print these again because we just cleared the screen above
println!("Hi there! Try pressing a button");
println!("\x1b[29;16HPress Start to exit");
// Move the cursor back to the top of the screen
println!("\x1b[3;0H");
// Print to the screen depending on which keys were held.
//
// The .contains() method checks for all of the provided keys,
// and the .intersects() method checks for any of the provided keys.
//
// You can also use the .bits() method to do direct comparisons on
// the underlying bits
if keys.contains(hid::KEY_A) {
println!("You held A!");
}
if keys.bits() & hid::KEY_B.bits() != 0 {
println!("You held B!");
}
if keys.contains(hid::KEY_X | hid::KEY_Y) {
println!("You held X and Y!");
}
if keys.intersects(hid::KEY_L | hid::KEY_R | hid::KEY_ZL | hid::KEY_ZR) {
println!("You held a shoulder button!");
}
if keys.intersects(hid::KEY_START) {
println!("See ya!");
break
}
}
// Save our current key presses for the next frame
old_keys = keys;
// Flush and swap framebuffers
gfx.flush_buffers();
gfx.swap_buffers();
}
}

4
examples/src/bin/hello-both-screens.rs

@ -3,7 +3,7 @@ extern crate ctru;
use ctru::gfx::{Gfx, Screen}; use ctru::gfx::{Gfx, Screen};
use ctru::console::Console; use ctru::console::Console;
use ctru::services::apt::Apt; use ctru::services::apt::Apt;
use ctru::services::hid::{Hid, PadKey}; use ctru::services::hid::{self, Hid};
fn main() { fn main() {
// Initialize services // Initialize services
@ -35,7 +35,7 @@ fn main() {
gfx.swap_buffers(); gfx.swap_buffers();
hid.scan_input(); hid.scan_input();
if hid.key_down(PadKey::Start) { if hid.keys_down().contains(hid::KEY_START) {
break; break;
} }
} }

4
examples/src/bin/hello-world.rs

@ -3,7 +3,7 @@ extern crate ctru;
use ctru::gfx::Gfx; use ctru::gfx::Gfx;
use ctru::console::Console; use ctru::console::Console;
use ctru::services::apt::Apt; use ctru::services::apt::Apt;
use ctru::services::hid::{Hid, PadKey}; use ctru::services::hid::{self, Hid};
fn main() { fn main() {
// Initialize ctrulib service handles. // Initialize ctrulib service handles.
@ -45,7 +45,7 @@ fn main() {
hid.scan_input(); hid.scan_input();
// Check if the user has pressed the given button on this frame. // Check if the user has pressed the given button on this frame.
// If so, break out of the loop. // If so, break out of the loop.
if hid.key_down(PadKey::Start) { if hid.keys_down().contains(hid::KEY_START) {
break; break;
} }
} }

Loading…
Cancel
Save