From c9e80f4f60100833cec3084567e9c9c45afb0268 Mon Sep 17 00:00:00 2001 From: Michel Heily Date: Fri, 27 Dec 2019 12:41:36 +0200 Subject: [PATCH] Remove dead code Former-commit-id: 5d5768ca6b73060197d9bacda61cba4c03804ad0 --- Cargo.toml | 1 - src/backend/cpal_backend.rs | 57 -------------- src/backend/minifb_backend.rs | 94 ----------------------- src/backend/mod.rs | 28 ------- src/backend/sdl2_backend.rs | 136 ---------------------------------- 5 files changed, 316 deletions(-) delete mode 100644 src/backend/cpal_backend.rs delete mode 100644 src/backend/minifb_backend.rs delete mode 100644 src/backend/mod.rs delete mode 100644 src/backend/sdl2_backend.rs diff --git a/Cargo.toml b/Cargo.toml index 7c04801..2c12dfe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,6 @@ bitfield = "0.13.1" bitflags = "1.1.0" zip = "0.5.3" ctrlc = "3.1.3" -cpal="0.10.0" spin_sleep="0.3.7" bit-set = "0.5.1" diff --git a/src/backend/cpal_backend.rs b/src/backend/cpal_backend.rs deleted file mode 100644 index 64c9cdc..0000000 --- a/src/backend/cpal_backend.rs +++ /dev/null @@ -1,57 +0,0 @@ -use std::thread; - -extern crate cpal; - -use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait}; -use cpal::EventLoop; - -pub struct CpalSoundBackend { - event_loop: EventLoop, - sample_rate: usize, - channels: usize, -} - -impl CpalSoundBackend { - pub fn new() -> CpalSoundBackend { - let host = cpal::default_host(); - let device = host.default_output_device().expect("failed to find a default output device"); - let format = device.default_output_format()?; - let event_loop = host.event_loop(); - let stream_id = event_loop.build_output_stream(&device, &format)?; - - event_loop.play_stream(stream_id.clone())?; - - CpalSoundBackend { - event_loop: event_loop, - sample_rate: format.sample_rate.0, - channels: format.channels, - } - } - - pub fn start(&self) { - thread::spawn(move || { - self.event_loop.run(move |id, result| { - let data = match result { - Ok(data) => data, - Err(err) => { - println!("an error occurred on stream {:?}: {}", id, err); - return; - } - }; - - match data { - cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::F32(mut buffer) } => { - for sample in buffer.chunks_mut(format.channels as usize) { - // TODO get samples from SoundController - sample[0] = 0.0; - sample[1] = 0.0; - } - }, - _ => { - panic!("expected F32"); - }, - } - }); - }); - } -} diff --git a/src/backend/minifb_backend.rs b/src/backend/minifb_backend.rs deleted file mode 100644 index 0391535..0000000 --- a/src/backend/minifb_backend.rs +++ /dev/null @@ -1,94 +0,0 @@ -use std::time; - -use crate::bit::BitIndex; - -extern crate minifb; - -use minifb::{Key, Window, WindowOptions}; - -use super::EmulatorBackend; -use crate::core::gpu::{DISPLAY_HEIGHT, DISPLAY_WIDTH}; -use crate::core::keypad; - -pub struct MinifbBackend { - window: Window, - frames_rendered: u32, - first_frame_start: time::Instant, -} - -impl MinifbBackend { - pub fn new() -> MinifbBackend { - let window = Window::new( - "rustboyadvance-ng", - DISPLAY_WIDTH, - DISPLAY_HEIGHT, - WindowOptions { - borderless: true, - scale: minifb::Scale::X4, - ..Default::default() - }, - ) - .unwrap(); - - MinifbBackend { - window: window, - frames_rendered: 0, - first_frame_start: time::Instant::now(), - } - } -} - -impl EmulatorBackend for MinifbBackend { - fn render(&mut self, buffer: &[u32]) { - self.frames_rendered += 1; - if self.first_frame_start.elapsed() >= time::Duration::from_secs(1) { - let title = format!("rustboyadvance-ng ({} fps)", self.frames_rendered); - self.window.set_title(&title); - self.first_frame_start = time::Instant::now(); - self.frames_rendered = 0; - } - self.window.update_with_buffer(buffer).unwrap(); - } - - fn get_key_state(&mut self) -> u16 { - let mut keyinput = keypad::KEYINPUT_ALL_RELEASED; - keyinput.set_bit(keypad::Keys::Up as usize, !self.window.is_key_down(Key::Up)); - keyinput.set_bit( - keypad::Keys::Down as usize, - !self.window.is_key_down(Key::Down), - ); - keyinput.set_bit( - keypad::Keys::Left as usize, - !self.window.is_key_down(Key::Left), - ); - keyinput.set_bit( - keypad::Keys::Right as usize, - !self.window.is_key_down(Key::Right), - ); - keyinput.set_bit( - keypad::Keys::ButtonB as usize, - !self.window.is_key_down(Key::Z), - ); - keyinput.set_bit( - keypad::Keys::ButtonA as usize, - !self.window.is_key_down(Key::X), - ); - keyinput.set_bit( - keypad::Keys::Start as usize, - !self.window.is_key_down(Key::Enter), - ); - keyinput.set_bit( - keypad::Keys::Select as usize, - !self.window.is_key_down(Key::Space), - ); - keyinput.set_bit( - keypad::Keys::ButtonL as usize, - !self.window.is_key_down(Key::A), - ); - keyinput.set_bit( - keypad::Keys::ButtonR as usize, - !self.window.is_key_down(Key::S), - ); - keyinput - } -} diff --git a/src/backend/mod.rs b/src/backend/mod.rs deleted file mode 100644 index 8b8e0be..0000000 --- a/src/backend/mod.rs +++ /dev/null @@ -1,28 +0,0 @@ -use super::core::keypad; - -mod minifb_backend; -pub use minifb_backend::MinifbBackend; - -mod sdl2_backend; -pub use sdl2_backend::Sdl2Backend; - -pub trait EmulatorBackend { - fn render(&mut self, buffer: &[u32]); - - fn get_key_state(&mut self) -> u16; -} - -pub struct DummyBackend; - -impl DummyBackend { - pub fn new() -> DummyBackend { - DummyBackend {} - } -} - -impl EmulatorBackend for DummyBackend { - fn get_key_state(&mut self) -> u16 { - keypad::KEYINPUT_ALL_RELEASED - } - fn render(&mut self, _buffer: &[u32]) {} -} diff --git a/src/backend/sdl2_backend.rs b/src/backend/sdl2_backend.rs deleted file mode 100644 index 9daa65f..0000000 --- a/src/backend/sdl2_backend.rs +++ /dev/null @@ -1,136 +0,0 @@ -use std::time; - -use crate::bit::BitIndex; - -extern crate sdl2; -use sdl2::event::Event; -use sdl2::keyboard::Keycode; -use sdl2::pixels::{Color, PixelFormatEnum}; -use sdl2::rect::{Point, Rect}; -use sdl2::render::{TextureCreator, WindowCanvas}; -use sdl2::video::WindowContext; - -use super::EmulatorBackend; -use crate::core::gpu::{DISPLAY_HEIGHT, DISPLAY_WIDTH}; -use crate::core::keypad; - -pub struct Sdl2Backend { - event_pump: sdl2::EventPump, - tc: TextureCreator, - canvas: WindowCanvas, - frames_rendered: u32, - fps_timer: time::Instant, - keyinput: u16, -} - -const SCREEN_WIDTH: u32 = DISPLAY_WIDTH as u32; -const SCREEN_HEIGHT: u32 = DISPLAY_HEIGHT as u32; - -impl Sdl2Backend { - pub fn new() -> Sdl2Backend { - let sdl_context = sdl2::init().unwrap(); - let video_subsystem = sdl_context.video().unwrap(); - - let window = video_subsystem - .window("RustBoyAdvance", SCREEN_WIDTH, SCREEN_HEIGHT) - .opengl() - .position_centered() - .build() - .unwrap(); - - let mut canvas = window.into_canvas().accelerated().build().unwrap(); - canvas.set_draw_color(Color::RGB(0, 0, 0)); - canvas.clear(); - let tc = canvas.texture_creator(); - let event_pump = sdl_context.event_pump().unwrap(); - - Sdl2Backend { - canvas: canvas, - event_pump: event_pump, - tc: tc, - frames_rendered: 0, - fps_timer: time::Instant::now(), - keyinput: keypad::KEYINPUT_ALL_RELEASED, - } - } -} - -impl EmulatorBackend for Sdl2Backend { - fn render(&mut self, buffer: &[u32]) { - let mut texture = self - .tc - .create_texture_target(PixelFormatEnum::RGB24, SCREEN_WIDTH, SCREEN_HEIGHT) - .unwrap(); - self.canvas - .with_texture_canvas(&mut texture, |texture_canvas| { - for y in 0i32..(SCREEN_HEIGHT as i32) { - for x in 0i32..(SCREEN_WIDTH as i32) { - let c = buffer[index2d!(x, y, SCREEN_WIDTH as i32) as usize]; - let color = Color::RGB((c >> 16) as u8, (c >> 8) as u8, c as u8); - texture_canvas.set_draw_color(color); - let _ = texture_canvas.draw_point(Point::from((x, y))); - } - } - }) - .unwrap(); - self.canvas - .copy( - &texture, - None, - Some(Rect::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)), - ) - .unwrap(); - self.canvas.present(); - - self.frames_rendered += 1; - if self.fps_timer.elapsed() >= time::Duration::from_secs(1) { - self.fps_timer = time::Instant::now(); - let title = format!("rustboyadvance-ng ({} fps)", self.frames_rendered); - self.canvas.window_mut().set_title(&title); - self.frames_rendered = 0; - } - } - - fn get_key_state(&mut self) -> u16 { - for event in self.event_pump.poll_iter() { - match event { - Event::KeyDown { - keycode: Some(keycode), - .. - } => { - if let Some(key) = keycode_to_keypad(keycode) { - self.keyinput.set_bit(key as usize, false); - } - } - Event::KeyUp { - keycode: Some(keycode), - .. - } => { - if let Some(key) = keycode_to_keypad(keycode) { - self.keyinput.set_bit(key as usize, true); - } - } - Event::Quit { .. } => panic!("quit!"), - _ => {} - } - } - - self.keyinput - } -} - -fn keycode_to_keypad(keycode: Keycode) -> Option { - match keycode { - Keycode::Up => Some(keypad::Keys::Up), - Keycode::Down => Some(keypad::Keys::Down), - Keycode::Left => Some(keypad::Keys::Left), - Keycode::Right => Some(keypad::Keys::Right), - Keycode::Z => Some(keypad::Keys::ButtonB), - Keycode::X => Some(keypad::Keys::ButtonA), - Keycode::Return => Some(keypad::Keys::Start), - Keycode::Space => Some(keypad::Keys::Select), - Keycode::A => Some(keypad::Keys::ButtonL), - Keycode::S => Some(keypad::Keys::ButtonR), - _ => None, - } -}