Skip to content

Commit

Permalink
cargo fmt (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elabajaba authored Dec 20, 2024
1 parent 7255fab commit c5394ed
Show file tree
Hide file tree
Showing 32 changed files with 292 additions and 259 deletions.
48 changes: 24 additions & 24 deletions examples/audio-capture-and-replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,30 @@ impl AudioCallback<i16> for Recording {
}
}

fn record(
audio_subsystem: &AudioSubsystem,
desired_spec: &AudioSpec,
) -> Result<Vec<i16>, String> {
fn record(audio_subsystem: &AudioSubsystem, desired_spec: &AudioSpec) -> Result<Vec<i16>, String> {
println!(
"Capturing {:} seconds... Please rock!",
RECORDING_LENGTH_SECONDS
);

let (done_sender, done_receiver) = mpsc::channel();

let capture_device = audio_subsystem.open_recording_stream(desired_spec, Recording {
record_buffer: vec![
0;
desired_spec.freq.unwrap() as usize
* RECORDING_LENGTH_SECONDS
* desired_spec.channels.unwrap() as usize
],
pos: 0,
done_sender,
done: false,
})?;

println!(
"AudioDriver: {:?}",
audio_subsystem.current_audio_driver()
);
let capture_device = audio_subsystem.open_recording_stream(
desired_spec,
Recording {
record_buffer: vec![
0;
desired_spec.freq.unwrap() as usize
* RECORDING_LENGTH_SECONDS
* desired_spec.channels.unwrap() as usize
],
pos: 0,
done_sender,
done: false,
},
)?;

println!("AudioDriver: {:?}", audio_subsystem.current_audio_driver());
capture_device.resume()?;

// Wait until the recording is done.
Expand Down Expand Up @@ -115,10 +112,13 @@ fn replay_recorded_vec(
) -> Result<(), String> {
println!("Playing...");

let playback_device = audio_subsystem.open_playback_stream(desired_spec, SoundPlayback {
data: recorded_vec,
pos: 0,
})?;
let playback_device = audio_subsystem.open_playback_stream(
desired_spec,
SoundPlayback {
data: recorded_vec,
pos: 0,
},
)?;

// Start playback
playback_device.resume()?;
Expand Down
13 changes: 8 additions & 5 deletions examples/audio-squarewave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ fn main() -> Result<(), String> {
format: None,
};

let device = audio_subsystem.open_playback_stream(&desired_spec, SquareWave {
phase_inc: 440.0 / desired_spec.freq.unwrap() as f32,
phase: 0.0,
volume: 0.25,
})?;
let device = audio_subsystem.open_playback_stream(
&desired_spec,
SquareWave {
phase_inc: 440.0 / desired_spec.freq.unwrap() as f32,
phase: 0.0,
volume: 0.25,
},
)?;

// Start playback
device.resume()?;
Expand Down
1 change: 0 additions & 1 deletion examples/audio-wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,3 @@ fn main() -> Result<(), String> {

Ok(())
}

19 changes: 15 additions & 4 deletions examples/audio-whitenoise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ extern crate rand;
extern crate sdl3;

use sdl3::audio::{AudioCallback, AudioSpec};
use std::{sync::{Arc, Mutex}, time::Duration};
use std::{
sync::{Arc, Mutex},
time::Duration,
};

struct MyCallback {
volume: Arc<Mutex<f32>>, // it seems like AudioStreamWithCallback<> is supposed to have a lock() method which allows us to modify MyCallback, but that function no longer seems to exist
Expand All @@ -12,10 +15,12 @@ impl AudioCallback<f32> for MyCallback {
use self::rand::{thread_rng, Rng};
let mut rng = thread_rng();

let Ok(volume) = self.volume.lock() else {return};
let Ok(volume) = self.volume.lock() else {
return;
};
// Generate white noise
for x in out.iter_mut() {
*x = (rng.gen_range(0.0 ..= 2.0) - 1.0) * *volume;
*x = (rng.gen_range(0.0..=2.0) - 1.0) * *volume;
}
}
}
Expand All @@ -33,7 +38,13 @@ fn main() -> Result<(), String> {

// None: use default device
let volume = Arc::new(Mutex::new(0.5));
let device = audio_subsystem.open_playback_stream_with_callback(&device, &desired_spec, MyCallback { volume: volume.clone() })?;
let device = audio_subsystem.open_playback_stream_with_callback(
&device,
&desired_spec,
MyCallback {
volume: volume.clone(),
},
)?;

// Start playback
device.resume()?;
Expand Down
81 changes: 52 additions & 29 deletions examples/dialog.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extern crate sdl3;

use sdl3::dialog::{show_open_file_dialog, show_open_folder_dialog, show_save_file_dialog, DialogFileFilter};
use sdl3::dialog::{
show_open_file_dialog, show_open_folder_dialog, show_save_file_dialog, DialogFileFilter,
};
use sdl3::event::Event;
use sdl3::keyboard::Keycode;
use sdl3::pixels::Color;
Expand Down Expand Up @@ -51,8 +53,11 @@ pub fn main() -> Result<(), String> {
..
} => {
break 'running;
},
Event::KeyDown {keycode: Some(Keycode::O), ..} => {
}
Event::KeyDown {
keycode: Some(Keycode::O),
..
} => {
show_open_file_dialog(
&filters,
None::<PathBuf>,
Expand All @@ -68,32 +73,50 @@ pub fn main() -> Result<(), String> {
}
};
}),
).unwrap_or_else(|e| panic!("Failed to show open file dialog: {e}"));
},
Event::KeyDown {keycode: Some(Keycode::D), ..} => {
show_open_folder_dialog(Some(&default_path_path), false, canvas.window(), Box::new(|result, _| {
match result {
Ok(result) => {
println!("Folder: {result:?}");
}
Err(error) => {
eprintln!("Folder dialog error {error}");
}
};
}));
},
Event::KeyDown {keycode: Some(Keycode::S), ..} => {
show_save_file_dialog(&filters, Some("/home"), canvas.window(), Box::new(|result, filter| {
match result {
Ok(result) => {
println!("Save File: {result:?} Filter: {filter:?}");
}
Err(error) => {
eprintln!("Save dialog error {error}");
}
};
})).unwrap_or_else(|e| panic!("Failed to show save file dialog: {e}"));
},
)
.unwrap_or_else(|e| panic!("Failed to show open file dialog: {e}"));
}
Event::KeyDown {
keycode: Some(Keycode::D),
..
} => {
show_open_folder_dialog(
Some(&default_path_path),
false,
canvas.window(),
Box::new(|result, _| {
match result {
Ok(result) => {
println!("Folder: {result:?}");
}
Err(error) => {
eprintln!("Folder dialog error {error}");
}
};
}),
);
}
Event::KeyDown {
keycode: Some(Keycode::S),
..
} => {
show_save_file_dialog(
&filters,
Some("/home"),
canvas.window(),
Box::new(|result, filter| {
match result {
Ok(result) => {
println!("Save File: {result:?} Filter: {filter:?}");
}
Err(error) => {
eprintln!("Save dialog error {error}");
}
};
}),
)
.unwrap_or_else(|e| panic!("Failed to show save file dialog: {e}"));
}
_ => {}
}
}
Expand Down
17 changes: 8 additions & 9 deletions examples/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub fn main() -> Result<(), String> {
let path_info = get_path_info(base_path).unwrap();
println!("Base path info: {path_info:?}");


enumerate_directory(base_path, |directory, file| {
println!("Enumerate {directory:?}: {file:?}");
return EnumerationResult::CONTINUE;
}).ok();
})
.ok();

if let Ok(results) = glob_directory(base_path, Some("filesystem*"), GlobFlags::NONE) {
for result in &results {
Expand All @@ -29,37 +29,36 @@ pub fn main() -> Result<(), String> {
let test_path2 = base_path.join("testpath2");
match get_path_info(&test_path) {
Ok(info) => println!("Test path info: {info:?}"),
Err(e) => println!("Test path error: {e:?}")
Err(e) => println!("Test path error: {e:?}"),
}
create_directory(&test_path).ok();
match get_path_info(&test_path) {
Ok(info) => println!("Test path info: {info:?}"),
Err(e) => println!("Test path error: {e:?}")
Err(e) => println!("Test path error: {e:?}"),
}

match rename_path(&test_path, &test_path2) {
Ok(()) => println!("Renamed {test_path:?} to {test_path2:?}"),
Err(e) => eprintln!("Failed to rename: {e:?}")
Err(e) => eprintln!("Failed to rename: {e:?}"),
}

match remove_path(&test_path2) {
Ok(()) => println!("Removed {test_path2:?}"),
Err(e) => eprintln!("Failed to remove: {e:?}")
Err(e) => eprintln!("Failed to remove: {e:?}"),
}

match get_pref_path("sdl-rs", "filesystem") {
Ok(path) => {
println!("Got pref path for org 'sdl-rs' app 'filesystem' as {path:?}");
match remove_path(&path) {
Ok(()) => println!("Removed {path:?}"),
Err(e) => eprintln!("Failed to remove: {e:?}")
Err(e) => eprintln!("Failed to remove: {e:?}"),
}
},
}
Err(error) => {
eprintln!("Failed to get pref path for org 'sdl-rs' app 'filesystem': {error:?}")
}
}


Ok(())
}
3 changes: 1 addition & 2 deletions examples/game-controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ fn main() -> Result<(), String> {
// Iterate over all available joysticks and look for game controllers.
let mut controller = (0..available)
.find_map(|id| {

println!("Attempting to open gamepad {}", id);

match gamepad_subsystem.open(id) {
Expand All @@ -40,8 +39,8 @@ fn main() -> Result<(), String> {
let (mut lo_freq, mut hi_freq) = (0, 0);

for event in sdl_context.event_pump()?.wait_iter() {
use sdl3::gamepad::Axis;
use sdl3::event::Event;
use sdl3::gamepad::Axis;

match event {
Event::ControllerAxisMotion {
Expand Down
3 changes: 2 additions & 1 deletion examples/haptic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ fn main() -> Result<(), String> {
println!("{} joysticks available", joysticks.len());

// Iterate over all available joysticks and stop once we manage to open one.
let (_joystick, joystick_id) = joysticks.into_iter()
let (_joystick, joystick_id) = joysticks
.into_iter()
.find_map(|joystick| {
let id = joystick.id;
match joystick_subsystem.open(joystick) {
Expand Down
11 changes: 8 additions & 3 deletions examples/joystick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ fn main() -> Result<(), String> {
println!("{} joysticks available", joysticks.len());

// Iterate over all available joysticks and stop once we manage to open one.
let mut joystick = joysticks.into_iter()
let mut joystick = joysticks
.into_iter()
.find_map(|joystick| match joystick_subsystem.open(joystick) {
Ok(c) => {
println!("Success: opened \"{}\"", c.name());
Expand Down Expand Up @@ -65,7 +66,9 @@ fn main() -> Result<(), String> {
} else {
println!(
"Error setting rumble to ({}, {}): {:?}",
lo_freq, hi_freq, get_error()
lo_freq,
hi_freq,
get_error()
);
}
}
Expand All @@ -83,7 +86,9 @@ fn main() -> Result<(), String> {
} else {
println!(
"Error setting rumble to ({}, {}): {:?}",
lo_freq, hi_freq, get_error()
lo_freq,
hi_freq,
get_error()
);
}
}
Expand Down
Loading

0 comments on commit c5394ed

Please sign in to comment.