Start Sdl2AudioPlayer

Former-commit-id: 905b846d751db577bc213f1ca38a4c0e72558cfa
This commit is contained in:
Michel Heily 2019-12-09 22:53:11 +02:00
parent 04d2edfc01
commit aee86d85c8
5 changed files with 65 additions and 13 deletions

View file

@ -34,7 +34,7 @@ pub trait VideoInterface {
}
pub trait AudioInterface {
fn get_sample_rate(&self);
fn get_sample_rate(&self) -> u32;
}
pub trait InputInterface {

View file

@ -1 +1,51 @@
const SAMPLE_RATE: u32 = 44100;
use sdl2;
use sdl2::audio::{AudioDevice, AudioSpec, AudioSpecDesired};
use rustboyadvance_ng::AudioInterface;
pub struct Sdl2AudioPlayer {
device: AudioDevice<GbaAudioCallback>,
freq: u32,
}
impl AudioInterface for Sdl2AudioPlayer {
fn get_sample_rate(&self) -> u32 {
self.freq
}
}
struct GbaAudioCallback {
spec: AudioSpec,
}
impl sdl2::audio::AudioCallback for GbaAudioCallback {
type Channel = f32;
fn callback(&mut self, out: &mut [f32]) {
// TODO audio
for x in out.iter_mut() {
*x = 0.0;
}
}
}
pub fn create_audio_player(sdl: &sdl2::Sdl) -> Sdl2AudioPlayer {
let audio_subsystem = sdl.audio().unwrap();
let desired_spec = AudioSpecDesired {
freq: Some(44100),
channels: Some(1), // stereo
samples: None,
};
let mut device = audio_subsystem
.open_playback(None, &desired_spec, |spec| {
println!("Obtained {:?}", spec);
GbaAudioCallback { spec: spec }
})
.unwrap();
let freq = (*device.lock()).spec.freq as u32;
Sdl2AudioPlayer { device, freq }
}

View file

@ -7,12 +7,12 @@ use rustboyadvance_ng::InputInterface;
extern crate bit;
use bit::BitIndex;
pub struct PlatformSdl2_Keyboard {
pub struct Sdl2Keyboard {
event_pump: EventPump,
keyinput: u16,
}
impl InputInterface for PlatformSdl2_Keyboard {
impl InputInterface for Sdl2Keyboard {
fn poll(&mut self) -> u16 {
for event in self.event_pump.poll_iter() {
match event {
@ -56,8 +56,8 @@ fn keycode_to_keypad(keycode: Keycode) -> Option<gba_keypad::Keys> {
}
}
pub fn create_keyboard(sdl: &sdl2::Sdl) -> PlatformSdl2_Keyboard {
PlatformSdl2_Keyboard {
pub fn create_keyboard(sdl: &sdl2::Sdl) -> Sdl2Keyboard {
Sdl2Keyboard {
event_pump: sdl.event_pump().unwrap(),
keyinput: gba_keypad::KEYINPUT_ALL_RELEASED,
}

View file

@ -11,8 +11,9 @@ mod audio;
mod keyboard;
mod video;
use keyboard::{create_keyboard, PlatformSdl2_Keyboard};
use video::{create_video_interface, PlatformSdl2_VideoInterface};
use keyboard::create_keyboard;
use video::create_video_interface;
use audio::create_audio_player;
#[macro_use]
extern crate rustboyadvance_ng;
@ -41,6 +42,7 @@ fn main() {
let sdl_context = sdl2::init().unwrap();
let mut video = create_video_interface(&sdl_context);
let mut audio = create_audio_player(&sdl_context);
let mut keyboard = create_keyboard(&sdl_context);
let mut fps_counter = FpsCounter::default();

View file

@ -12,19 +12,19 @@ const SCREEN_WIDTH: u32 = DISPLAY_WIDTH as u32;
const SCREEN_HEIGHT: u32 = DISPLAY_HEIGHT as u32;
const SCALE: u32 = 3; // TODO control via CLI & support window resize
pub struct PlatformSdl2_VideoInterface {
pub struct Sdl2VideoInterface {
tc: TextureCreator<WindowContext>,
canvas: WindowCanvas,
fps_counter: FpsCounter,
}
impl PlatformSdl2_VideoInterface {
impl Sdl2VideoInterface {
pub fn set_window_title(&mut self, title: &str) {
self.canvas.window_mut().set_title(&title);
}
}
impl VideoInterface for PlatformSdl2_VideoInterface {
impl VideoInterface for Sdl2VideoInterface {
fn render(&mut self, buffer: &[u32]) {
let mut texture = self
.tc
@ -53,7 +53,7 @@ impl VideoInterface for PlatformSdl2_VideoInterface {
}
}
pub fn create_video_interface(sdl: &Sdl) -> PlatformSdl2_VideoInterface {
pub fn create_video_interface(sdl: &Sdl) -> Sdl2VideoInterface {
let video_subsystem = sdl.video().unwrap();
let window = video_subsystem
.window(
@ -69,7 +69,7 @@ pub fn create_video_interface(sdl: &Sdl) -> PlatformSdl2_VideoInterface {
canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.clear();
let tc = canvas.texture_creator();
PlatformSdl2_VideoInterface {
Sdl2VideoInterface {
tc: tc,
canvas: canvas,
fps_counter: Default::default(),