Skip to content

Commit 537ee53

Browse files
Csatinapppplecuds
andauthored
Make draw functional (#2)
* changed draw to a functional approach * remove comments * changed x to x_index and removed unused variable * fix warn about unused variable * cargo fmt --------- Co-authored-by: cuds <[email protected]>
1 parent 1ae205d commit 537ee53

File tree

9 files changed

+1238
-1385
lines changed

9 files changed

+1238
-1385
lines changed

src/drivers/audio_driver.rs

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,59 @@
1-
use sdl2;
2-
use sdl2::audio::{AudioDevice, AudioCallback, AudioSpecDesired};
3-
4-
pub struct AudioDriver {
5-
device: AudioDevice<SquareWave>,
6-
}
7-
8-
impl AudioDriver {
9-
pub fn new(sdl_context: &sdl2::Sdl) -> Self {
10-
let audio_subsystem = sdl_context.audio().unwrap();
11-
12-
let desired_spec = AudioSpecDesired {
13-
freq: Some(44100),
14-
channels: Some(1), // mono
15-
samples: None, // default sample size
16-
};
17-
18-
let device = audio_subsystem
19-
.open_playback(None, &desired_spec, |spec| {
20-
// Show obtained AudioSpec
21-
println!("{:?}", spec);
22-
23-
// initialize the audio callback
24-
SquareWave {
25-
phase_inc: 240.0 / spec.freq as f32,
26-
phase: 0.0,
27-
volume: 0.25,
28-
}
29-
})
30-
.unwrap();
31-
32-
AudioDriver { device: device }
33-
}
34-
35-
pub fn start_beep(&self) {
36-
self.device.resume();
37-
}
38-
pub fn stop_beep(&self) {
39-
self.device.pause();
40-
}
41-
}
42-
43-
44-
45-
46-
struct SquareWave {
47-
phase_inc: f32,
48-
phase: f32,
49-
volume: f32,
50-
}
51-
52-
impl AudioCallback for SquareWave {
53-
type Channel = f32;
54-
55-
fn callback(&mut self, out: &mut [f32]) {
56-
// Generate a square wave
57-
for x in out.iter_mut() {
58-
*x = self.volume * if self.phase < 0.5 { 1.0 } else { -1.0 };
59-
self.phase = (self.phase + self.phase_inc) % 1.0;
60-
}
61-
}
62-
}
1+
use sdl2;
2+
use sdl2::audio::{AudioCallback, AudioDevice, AudioSpecDesired};
3+
4+
pub struct AudioDriver {
5+
device: AudioDevice<SquareWave>,
6+
}
7+
8+
impl AudioDriver {
9+
pub fn new(sdl_context: &sdl2::Sdl) -> Self {
10+
let audio_subsystem = sdl_context.audio().unwrap();
11+
12+
let desired_spec = AudioSpecDesired {
13+
freq: Some(44100),
14+
channels: Some(1), // mono
15+
samples: None, // default sample size
16+
};
17+
18+
let device = audio_subsystem
19+
.open_playback(None, &desired_spec, |spec| {
20+
// Show obtained AudioSpec
21+
println!("{:?}", spec);
22+
23+
// initialize the audio callback
24+
SquareWave {
25+
phase_inc: 240.0 / spec.freq as f32,
26+
phase: 0.0,
27+
volume: 0.25,
28+
}
29+
})
30+
.unwrap();
31+
32+
AudioDriver { device: device }
33+
}
34+
35+
pub fn start_beep(&self) {
36+
self.device.resume();
37+
}
38+
pub fn stop_beep(&self) {
39+
self.device.pause();
40+
}
41+
}
42+
43+
struct SquareWave {
44+
phase_inc: f32,
45+
phase: f32,
46+
volume: f32,
47+
}
48+
49+
impl AudioCallback for SquareWave {
50+
type Channel = f32;
51+
52+
fn callback(&mut self, out: &mut [f32]) {
53+
// Generate a square wave
54+
for x in out.iter_mut() {
55+
*x = self.volume * if self.phase < 0.5 { 1.0 } else { -1.0 };
56+
self.phase = (self.phase + self.phase_inc) % 1.0;
57+
}
58+
}
59+
}

src/drivers/cartridge_driver.rs

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
use std::fs::File;
2-
use std::io::prelude::*;
3-
4-
pub struct CartridgeDriver {
5-
pub rom: [u8; 3584],
6-
pub size: usize,
7-
}
8-
9-
impl CartridgeDriver {
10-
pub fn new(filename: &str) -> Self {
11-
12-
let mut f = File::open(filename).expect("file not found");
13-
let mut buffer = [0u8; 3584];
14-
15-
let bytes_read = if let Ok(bytes_read) = f.read(&mut buffer) {
16-
bytes_read
17-
} else {
18-
0
19-
};
20-
21-
CartridgeDriver {
22-
rom: buffer,
23-
size: bytes_read,
24-
}
25-
}
26-
}
1+
use std::fs::File;
2+
use std::io::prelude::*;
3+
4+
pub struct CartridgeDriver {
5+
pub rom: [u8; 3584],
6+
}
7+
8+
impl CartridgeDriver {
9+
pub fn new(filename: &str) -> Self {
10+
let mut f = File::open(filename).expect("file not found");
11+
let mut buffer = [0u8; 3584];
12+
13+
f.read(&mut buffer).expect("file couldn't be read");
14+
15+
CartridgeDriver { rom: buffer }
16+
}
17+
}

src/drivers/display_driver.rs

Lines changed: 69 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,69 @@
1-
use sdl2;
2-
use sdl2::pixels;
3-
use sdl2::rect::Rect;
4-
use sdl2::render::Canvas;
5-
use sdl2::video::Window;
6-
7-
use CHIP8_WIDTH;
8-
use CHIP8_HEIGHT;
9-
10-
const SCALE_FACTOR: u32 = 20;
11-
const SCREEN_WIDTH: u32 = (CHIP8_WIDTH as u32) * SCALE_FACTOR;
12-
const SCREEN_HEIGHT: u32 = (CHIP8_HEIGHT as u32) * SCALE_FACTOR;
13-
14-
pub struct DisplayDriver {
15-
canvas: Canvas<Window>,
16-
}
17-
18-
impl DisplayDriver {
19-
pub fn new(sdl_context: &sdl2::Sdl) -> Self {
20-
let video_subsys = sdl_context.video().unwrap();
21-
let window = video_subsys
22-
.window(
23-
"rust-sdl2_gfx: draw line & FPSManager",
24-
SCREEN_WIDTH,
25-
SCREEN_HEIGHT,
26-
)
27-
.position_centered()
28-
.opengl()
29-
.build()
30-
.unwrap();
31-
32-
let mut canvas = window.into_canvas().build().unwrap();
33-
34-
canvas.set_draw_color(pixels::Color::RGB(0, 0, 0));
35-
canvas.clear();
36-
canvas.present();
37-
38-
DisplayDriver { canvas: canvas }
39-
}
40-
41-
pub fn draw(&mut self, pixels: &[u64; CHIP8_HEIGHT]) {
42-
43-
for y in 0usize..CHIP8_HEIGHT {
44-
for x in 0usize..CHIP8_WIDTH {
45-
let _x = (x as u32) * SCALE_FACTOR;
46-
let _y = (y as u32) * SCALE_FACTOR;
47-
48-
self.canvas.set_draw_color( color( (pixels[y] >> (63 - x) ) & 1 ));
49-
50-
let _ = self.canvas
51-
.fill_rect( Rect::new( _x as i32, _y as i32, SCALE_FACTOR, SCALE_FACTOR ) );
52-
}
53-
}
54-
self.canvas.present();
55-
}
56-
}
57-
58-
fn color(value: u64) -> pixels::Color {
59-
if value == 0 {
60-
pixels::Color::RGB(0, 0,100)
61-
} else {
62-
pixels::Color::RGB(0, 150, 0)
63-
}
64-
}
65-
66-
// pub fn run() {
67-
68-
69-
70-
// let mut lastx = 0;
71-
// let mut lasty = 0;
72-
73-
// let mut events = sdl_context.event_pump().unwrap();
74-
75-
// 'main: loop {
76-
// for event in events.poll_iter() {
77-
78-
// match event {
79-
80-
// Event::Quit {..} => break 'main,
81-
82-
// Event::KeyDown {keycode: Some(keycode), ..} => {
83-
// if keycode == Keycode::Escape {
84-
// break 'main
85-
// } else if keycode == Keycode::Space {
86-
// println!("space down");
87-
// for i in 0..400 {
88-
// driver.canvas.pixel(i as i16, i as i16, 0xFF000FFu32).unwrap();
89-
// }
90-
// driver.canvas.present();
91-
92-
// }
93-
// }
94-
95-
// Event::MouseButtonDown {x, y, ..} => {
96-
// let color = pixels::Color::RGB(x as u8, y as u8, 255);
97-
// let _ = driver.canvas.line(lastx, lasty, x as i16, y as i16, color);
98-
// let _ = driver.canvas.fill_rect(Rect::new(0,0,100,100));
99-
100-
// lastx = x as i16;
101-
// lasty = y as i16;
102-
// println!("mouse btn down at ({},{})", x, y);
103-
// driver.canvas.present();
104-
// }
105-
106-
// _ => {}
107-
// }
108-
// }
109-
// }
110-
// }
1+
use sdl2;
2+
use sdl2::pixels;
3+
use sdl2::rect::Rect;
4+
use sdl2::render::Canvas;
5+
use sdl2::video::Window;
6+
7+
use CHIP8_HEIGHT;
8+
use CHIP8_WIDTH;
9+
10+
const SCALE_FACTOR: u32 = 20;
11+
const SCREEN_WIDTH: u32 = (CHIP8_WIDTH as u32) * SCALE_FACTOR;
12+
const SCREEN_HEIGHT: u32 = (CHIP8_HEIGHT as u32) * SCALE_FACTOR;
13+
14+
pub struct DisplayDriver {
15+
canvas: Canvas<Window>,
16+
}
17+
18+
impl DisplayDriver {
19+
pub fn new(sdl_context: &sdl2::Sdl) -> Self {
20+
let video_subsys = sdl_context.video().unwrap();
21+
let window = video_subsys
22+
.window(
23+
"rust-sdl2_gfx: draw line & FPSManager",
24+
SCREEN_WIDTH,
25+
SCREEN_HEIGHT,
26+
)
27+
.position_centered()
28+
.opengl()
29+
.build()
30+
.unwrap();
31+
32+
let mut canvas = window.into_canvas().build().unwrap();
33+
34+
canvas.set_draw_color(pixels::Color::RGB(0, 0, 0));
35+
canvas.clear();
36+
canvas.present();
37+
38+
DisplayDriver { canvas: canvas }
39+
}
40+
41+
pub fn draw(&mut self, pixels: &[u64; CHIP8_HEIGHT]) {
42+
pixels.iter().enumerate().for_each(|(y_index, y)| {
43+
(0usize..CHIP8_WIDTH).for_each(|x_index| {
44+
let _x_start = (x_index as u32) * SCALE_FACTOR;
45+
let _y_start = (y_index as u32) * SCALE_FACTOR;
46+
47+
self.canvas
48+
.set_draw_color(color((*y >> (63 - x_index)) & 1));
49+
50+
let _ = self.canvas.fill_rect(Rect::new(
51+
_x_start as i32,
52+
_y_start as i32,
53+
SCALE_FACTOR,
54+
SCALE_FACTOR,
55+
));
56+
});
57+
});
58+
59+
self.canvas.present();
60+
}
61+
}
62+
63+
fn color(value: u64) -> pixels::Color {
64+
if value == 0 {
65+
pixels::Color::RGB(0, 0, 100)
66+
} else {
67+
pixels::Color::RGB(0, 150, 0)
68+
}
69+
}

0 commit comments

Comments
 (0)