Skip to content

Commit

Permalink
Use audio subsystem lock API to tell the callback what to play
Browse files Browse the repository at this point in the history
This is in preparation for actually playing multiple intros one after
another – I need *some* way to change what's being played.
  • Loading branch information
jstasiak committed Oct 22, 2019
1 parent 687d1b6 commit 3a79a89
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/bin/openpol.rs
@@ -1,6 +1,6 @@
use flic::{FlicFile, RasterMut};
use openpol::image13h;
use sdl2::audio::{AudioCallback, AudioSpecDesired};
use sdl2::audio::{AudioCallback, AudioDevice, AudioSpecDesired};
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::{Color, PixelFormatEnum};
Expand Down Expand Up @@ -86,22 +86,15 @@ impl Game {
.map_err(|e| e.to_string())?;

let mut timer = sdl.timer()?;

let mut audio_file =
fs::File::open(&data_dir.join("I002.DAT")).map_err(|e| e.to_string())?;
let mut audio_data = Vec::new();
audio_file
.read_to_end(&mut audio_data)
.map_err(|e| e.to_string())?;
let audio = sdl.audio()?;
let desired_spec = AudioSpecDesired {
freq: Some(22_050),
channels: Some(1),
samples: None,
};

let audio_device = audio.open_playback(None, &desired_spec, |_spec| Audio {
data: audio_data,
let mut audio_device = audio.open_playback(None, &desired_spec, |_spec| Audio {
data: Vec::new(),
position: 0,
})?;
audio_device.resume();
Expand All @@ -112,6 +105,7 @@ impl Game {
&data_dir,
&mut canvas,
&mut texture,
&mut audio_device,
)
}

Expand All @@ -122,6 +116,7 @@ impl Game {
data_dir: &path::Path,
canvas: &mut WindowCanvas,
texture: &mut Texture,
audio_device: &mut AudioDevice<Audio>,
) -> Result<(), String> {
let mut flic = FlicFile::open(&data_dir.join("S002.DAT")).map_err(|e| e.to_string())?;
assert_eq!(flic.width() as usize, image13h::SCREEN_WIDTH);
Expand All @@ -130,6 +125,19 @@ impl Game {
let mut flic_buffer = vec![0; image13h::SCREEN_PIXELS];
let mut flic_palette = vec![0; 3 * image13h::COLORS];

let mut audio_file =
fs::File::open(&data_dir.join("I002.DAT")).map_err(|e| e.to_string())?;
let mut audio_data = Vec::new();
audio_file
.read_to_end(&mut audio_data)
.map_err(|e| e.to_string())?;

{
let mut audio_lock = audio_device.lock();
audio_lock.data = audio_data;
audio_lock.position = 0;
}

let ms_per_frame = flic.speed_msec();
let mut last_render = timer.ticks();
'running: loop {
Expand Down

0 comments on commit 3a79a89

Please sign in to comment.