Formatting: cargo fmt
This commit is contained in:
@@ -10,20 +10,19 @@ pub struct Cartridge {
|
|||||||
|
|
||||||
impl Cartridge {
|
impl Cartridge {
|
||||||
pub fn new(filename: &str) -> Self {
|
pub fn new(filename: &str) -> Self {
|
||||||
let mut file = File::open(filename).expect("Error while opening file!");
|
let mut file = File::open(filename).expect("Error while opening file!");
|
||||||
let mut buffer = [0u8; BUFFER_SIZE];
|
let mut buffer = [0u8; BUFFER_SIZE];
|
||||||
|
|
||||||
// either read a byte, or noting (0)
|
// either read a byte, or noting (0)
|
||||||
let bytes = if let Ok(bytes) = file.read(&mut buffer) {
|
let bytes = if let Ok(bytes) = file.read(&mut buffer) {
|
||||||
bytes
|
bytes
|
||||||
}
|
} else {
|
||||||
else {
|
0
|
||||||
0
|
};
|
||||||
};
|
|
||||||
|
|
||||||
Cartridge {
|
Cartridge {
|
||||||
rom: buffer,
|
rom: buffer,
|
||||||
size: bytes,
|
size: bytes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use sdl2;
|
use sdl2;
|
||||||
use sdl2::{render::Canvas, video::Window, pixels, rect::Rect};
|
use sdl2::{pixels, rect::Rect, render::Canvas, video::Window};
|
||||||
|
|
||||||
pub struct Display {
|
pub struct Display {
|
||||||
canvas: Canvas<Window>,
|
canvas: Canvas<Window>,
|
||||||
@@ -7,49 +7,43 @@ pub struct Display {
|
|||||||
|
|
||||||
impl Display {
|
impl Display {
|
||||||
pub fn new(sdl_ctx: &sdl2::Sdl) -> Self {
|
pub fn new(sdl_ctx: &sdl2::Sdl) -> Self {
|
||||||
let video = sdl_ctx.video().unwrap();
|
let video = sdl_ctx.video().unwrap();
|
||||||
let window = video
|
let window = video
|
||||||
.window(
|
.window(
|
||||||
"chip8-rs",
|
"chip8-rs",
|
||||||
(crate::SCREEN_WIDTH * crate::SCREEN_SCALE) as u32,
|
(crate::SCREEN_WIDTH * crate::SCREEN_SCALE) as u32,
|
||||||
(crate::SCREEN_HEIGHT * crate::SCREEN_SCALE) as u32)
|
(crate::SCREEN_HEIGHT * crate::SCREEN_SCALE) as u32,
|
||||||
|
)
|
||||||
.position_centered()
|
.position_centered()
|
||||||
.opengl()
|
.opengl()
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut canvas = window
|
let mut canvas = window.into_canvas().build().unwrap();
|
||||||
.into_canvas()
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
canvas.set_draw_color(pixels::Color::RGB(0, 0, 0));
|
canvas.set_draw_color(pixels::Color::RGB(0, 0, 0));
|
||||||
canvas.clear();
|
canvas.clear();
|
||||||
canvas.present();
|
canvas.present();
|
||||||
|
|
||||||
Display {
|
Display { canvas }
|
||||||
canvas,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&mut self, pixel: &[[u8; crate::SCREEN_WIDTH]; crate::SCREEN_HEIGHT]) {
|
pub fn draw(&mut self, pixel: &[[u8; crate::SCREEN_WIDTH]; crate::SCREEN_HEIGHT]) {
|
||||||
for (y, &row) in pixel.iter().enumerate() {
|
for (y, &row) in pixel.iter().enumerate() {
|
||||||
for (x, &column) in row.iter().enumerate() {
|
for (x, &column) in row.iter().enumerate() {
|
||||||
self.canvas.set_draw_color(
|
self.canvas.set_draw_color(if column == 1 {
|
||||||
if column == 1 {
|
pixels::Color::RGB(255, 255, 255)
|
||||||
pixels::Color::RGB(255, 255, 255)
|
} else {
|
||||||
}
|
pixels::Color::RGB(0, 0, 0)
|
||||||
else {
|
});
|
||||||
pixels::Color::RGB(0, 0, 0)
|
let _ = self.canvas.fill_rect(Rect::new(
|
||||||
});
|
(x * crate::SCREEN_SCALE) as i32,
|
||||||
let _ = self.canvas
|
(y * crate::SCREEN_SCALE) as i32,
|
||||||
.fill_rect(
|
crate::SCREEN_SCALE as u32,
|
||||||
Rect::new((x * crate::SCREEN_SCALE) as i32,
|
crate::SCREEN_SCALE as u32,
|
||||||
(y * crate::SCREEN_SCALE) as i32,
|
));
|
||||||
crate::SCREEN_SCALE as u32,
|
}
|
||||||
crate::SCREEN_SCALE as u32));
|
}
|
||||||
}
|
self.canvas.present();
|
||||||
}
|
|
||||||
self.canvas.present();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
pub (crate) const FONT: [u8; 80] = [
|
pub(crate) const FONT: [u8; 80] = [
|
||||||
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
|
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
|
||||||
0x20, 0x60, 0x20, 0x20, 0x70, // 1
|
0x20, 0x60, 0x20, 0x20, 0x70, // 1
|
||||||
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
|
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
|
||||||
|
|||||||
@@ -21,7 +21,8 @@ impl Input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get all inputs
|
// get all inputs
|
||||||
let input: Vec<Keycode> = self.events
|
let input: Vec<Keycode> = self
|
||||||
|
.events
|
||||||
.keyboard_state()
|
.keyboard_state()
|
||||||
.pressed_scancodes()
|
.pressed_scancodes()
|
||||||
.filter_map(Keycode::from_scancode)
|
.filter_map(Keycode::from_scancode)
|
||||||
|
|||||||
10
src/main.rs
10
src/main.rs
@@ -3,14 +3,14 @@ extern crate sdl2;
|
|||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
mod processor;
|
|
||||||
mod fontset;
|
|
||||||
mod display;
|
|
||||||
mod input;
|
|
||||||
mod cartridge;
|
mod cartridge;
|
||||||
|
mod display;
|
||||||
|
mod fontset;
|
||||||
|
mod input;
|
||||||
|
mod processor;
|
||||||
|
|
||||||
use crate::processor::Processor;
|
|
||||||
use crate::cartridge::Cartridge;
|
use crate::cartridge::Cartridge;
|
||||||
|
use crate::processor::Processor;
|
||||||
|
|
||||||
const MEMORY_SIZE: usize = 4096;
|
const MEMORY_SIZE: usize = 4096;
|
||||||
const GAME_ENTRY: usize = 0x200; // most games load into 0x200
|
const GAME_ENTRY: usize = 0x200; // most games load into 0x200
|
||||||
|
|||||||
1042
src/processor.rs
1042
src/processor.rs
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user