Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 59 additions & 62 deletions src/drivers/audio_driver.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,59 @@
use sdl2;
use sdl2::audio::{AudioDevice, AudioCallback, AudioSpecDesired};

pub struct AudioDriver {
device: AudioDevice<SquareWave>,
}

impl AudioDriver {
pub fn new(sdl_context: &sdl2::Sdl) -> Self {
let audio_subsystem = sdl_context.audio().unwrap();

let desired_spec = AudioSpecDesired {
freq: Some(44100),
channels: Some(1), // mono
samples: None, // default sample size
};

let device = audio_subsystem
.open_playback(None, &desired_spec, |spec| {
// Show obtained AudioSpec
println!("{:?}", spec);

// initialize the audio callback
SquareWave {
phase_inc: 240.0 / spec.freq as f32,
phase: 0.0,
volume: 0.25,
}
})
.unwrap();

AudioDriver { device: device }
}

pub fn start_beep(&self) {
self.device.resume();
}
pub fn stop_beep(&self) {
self.device.pause();
}
}




struct SquareWave {
phase_inc: f32,
phase: f32,
volume: f32,
}

impl AudioCallback for SquareWave {
type Channel = f32;

fn callback(&mut self, out: &mut [f32]) {
// Generate a square wave
for x in out.iter_mut() {
*x = self.volume * if self.phase < 0.5 { 1.0 } else { -1.0 };
self.phase = (self.phase + self.phase_inc) % 1.0;
}
}
}
use sdl2;
use sdl2::audio::{AudioCallback, AudioDevice, AudioSpecDesired};

pub struct AudioDriver {
device: AudioDevice<SquareWave>,
}

impl AudioDriver {
pub fn new(sdl_context: &sdl2::Sdl) -> Self {
let audio_subsystem = sdl_context.audio().unwrap();

let desired_spec = AudioSpecDesired {
freq: Some(44100),
channels: Some(1), // mono
samples: None, // default sample size
};

let device = audio_subsystem
.open_playback(None, &desired_spec, |spec| {
// Show obtained AudioSpec
println!("{:?}", spec);

// initialize the audio callback
SquareWave {
phase_inc: 240.0 / spec.freq as f32,
phase: 0.0,
volume: 0.25,
}
})
.unwrap();

AudioDriver { device: device }
}

pub fn start_beep(&self) {
self.device.resume();
}
pub fn stop_beep(&self) {
self.device.pause();
}
}

struct SquareWave {
phase_inc: f32,
phase: f32,
volume: f32,
}

impl AudioCallback for SquareWave {
type Channel = f32;

fn callback(&mut self, out: &mut [f32]) {
// Generate a square wave
for x in out.iter_mut() {
*x = self.volume * if self.phase < 0.5 { 1.0 } else { -1.0 };
self.phase = (self.phase + self.phase_inc) % 1.0;
}
}
}
43 changes: 17 additions & 26 deletions src/drivers/cartridge_driver.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
use std::fs::File;
use std::io::prelude::*;

pub struct CartridgeDriver {
pub rom: [u8; 3584],
pub size: usize,
}

impl CartridgeDriver {
pub fn new(filename: &str) -> Self {

let mut f = File::open(filename).expect("file not found");
let mut buffer = [0u8; 3584];

let bytes_read = if let Ok(bytes_read) = f.read(&mut buffer) {
bytes_read
} else {
0
};

CartridgeDriver {
rom: buffer,
size: bytes_read,
}
}
}
use std::fs::File;
use std::io::prelude::*;

pub struct CartridgeDriver {
pub rom: [u8; 3584],
}

impl CartridgeDriver {
pub fn new(filename: &str) -> Self {
let mut f = File::open(filename).expect("file not found");
let mut buffer = [0u8; 3584];

f.read(&mut buffer).expect("file couldn't be read");

CartridgeDriver { rom: buffer }
}
}
179 changes: 69 additions & 110 deletions src/drivers/display_driver.rs
Original file line number Diff line number Diff line change
@@ -1,110 +1,69 @@
use sdl2;
use sdl2::pixels;
use sdl2::rect::Rect;
use sdl2::render::Canvas;
use sdl2::video::Window;

use CHIP8_WIDTH;
use CHIP8_HEIGHT;

const SCALE_FACTOR: u32 = 20;
const SCREEN_WIDTH: u32 = (CHIP8_WIDTH as u32) * SCALE_FACTOR;
const SCREEN_HEIGHT: u32 = (CHIP8_HEIGHT as u32) * SCALE_FACTOR;

pub struct DisplayDriver {
canvas: Canvas<Window>,
}

impl DisplayDriver {
pub fn new(sdl_context: &sdl2::Sdl) -> Self {
let video_subsys = sdl_context.video().unwrap();
let window = video_subsys
.window(
"rust-sdl2_gfx: draw line & FPSManager",
SCREEN_WIDTH,
SCREEN_HEIGHT,
)
.position_centered()
.opengl()
.build()
.unwrap();

let mut canvas = window.into_canvas().build().unwrap();

canvas.set_draw_color(pixels::Color::RGB(0, 0, 0));
canvas.clear();
canvas.present();

DisplayDriver { canvas: canvas }
}

pub fn draw(&mut self, pixels: &[u64; CHIP8_HEIGHT]) {

for y in 0usize..CHIP8_HEIGHT {
for x in 0usize..CHIP8_WIDTH {
let _x = (x as u32) * SCALE_FACTOR;
let _y = (y as u32) * SCALE_FACTOR;

self.canvas.set_draw_color( color( (pixels[y] >> (63 - x) ) & 1 ));

let _ = self.canvas
.fill_rect( Rect::new( _x as i32, _y as i32, SCALE_FACTOR, SCALE_FACTOR ) );
}
}
self.canvas.present();
}
}

fn color(value: u64) -> pixels::Color {
if value == 0 {
pixels::Color::RGB(0, 0,100)
} else {
pixels::Color::RGB(0, 150, 0)
}
}

// pub fn run() {



// let mut lastx = 0;
// let mut lasty = 0;

// let mut events = sdl_context.event_pump().unwrap();

// 'main: loop {
// for event in events.poll_iter() {

// match event {

// Event::Quit {..} => break 'main,

// Event::KeyDown {keycode: Some(keycode), ..} => {
// if keycode == Keycode::Escape {
// break 'main
// } else if keycode == Keycode::Space {
// println!("space down");
// for i in 0..400 {
// driver.canvas.pixel(i as i16, i as i16, 0xFF000FFu32).unwrap();
// }
// driver.canvas.present();

// }
// }

// Event::MouseButtonDown {x, y, ..} => {
// let color = pixels::Color::RGB(x as u8, y as u8, 255);
// let _ = driver.canvas.line(lastx, lasty, x as i16, y as i16, color);
// let _ = driver.canvas.fill_rect(Rect::new(0,0,100,100));

// lastx = x as i16;
// lasty = y as i16;
// println!("mouse btn down at ({},{})", x, y);
// driver.canvas.present();
// }

// _ => {}
// }
// }
// }
// }
use sdl2;
use sdl2::pixels;
use sdl2::rect::Rect;
use sdl2::render::Canvas;
use sdl2::video::Window;

use CHIP8_HEIGHT;
use CHIP8_WIDTH;

const SCALE_FACTOR: u32 = 20;
const SCREEN_WIDTH: u32 = (CHIP8_WIDTH as u32) * SCALE_FACTOR;
const SCREEN_HEIGHT: u32 = (CHIP8_HEIGHT as u32) * SCALE_FACTOR;

pub struct DisplayDriver {
canvas: Canvas<Window>,
}

impl DisplayDriver {
pub fn new(sdl_context: &sdl2::Sdl) -> Self {
let video_subsys = sdl_context.video().unwrap();
let window = video_subsys
.window(
"rust-sdl2_gfx: draw line & FPSManager",
SCREEN_WIDTH,
SCREEN_HEIGHT,
)
.position_centered()
.opengl()
.build()
.unwrap();

let mut canvas = window.into_canvas().build().unwrap();

canvas.set_draw_color(pixels::Color::RGB(0, 0, 0));
canvas.clear();
canvas.present();

DisplayDriver { canvas: canvas }
}

pub fn draw(&mut self, pixels: &[u64; CHIP8_HEIGHT]) {
pixels.iter().enumerate().for_each(|(y_index, y)| {
(0usize..CHIP8_WIDTH).for_each(|x_index| {
let _x_start = (x_index as u32) * SCALE_FACTOR;
let _y_start = (y_index as u32) * SCALE_FACTOR;

self.canvas
.set_draw_color(color((*y >> (63 - x_index)) & 1));

let _ = self.canvas.fill_rect(Rect::new(
_x_start as i32,
_y_start as i32,
SCALE_FACTOR,
SCALE_FACTOR,
));
});
});

self.canvas.present();
}
}

fn color(value: u64) -> pixels::Color {
if value == 0 {
pixels::Color::RGB(0, 0, 100)
} else {
pixels::Color::RGB(0, 150, 0)
}
}
Loading