-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
211 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
/target | ||
*.ch8 | ||
*.rom | ||
/roms | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1 @@ | ||
[package] | ||
name = "chip-8" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
rand = "0.8.5" | ||
workspace = { members = ["desktop", "core"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "core" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
rand = "0.8.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "desktop" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
core = {path = "../core"} | ||
sdl2 = "0.36.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use core::{Interpreter, SCREEN_HEIGHT, SCREEN_WIDTH}; | ||
use sdl2::{ | ||
event::Event, keyboard::Keycode, pixels::Color, rect::Rect, render::Canvas, video::Window, | ||
}; | ||
use std::{env, fs::File, io::Read}; | ||
|
||
const SCALE: u32 = 24; | ||
const WINDOW_WIDTH: u32 = (SCREEN_WIDTH as u32) * SCALE; | ||
const WINDOW_HEIGHT: u32 = (SCREEN_HEIGHT as u32) * SCALE; | ||
const TICKS_PER_FRAME: usize = 5; | ||
|
||
fn main() { | ||
let args: Vec<_> = env::args().collect(); | ||
if args.len() != 2 { | ||
println!("Hello"); | ||
return; | ||
} | ||
|
||
let sdl_context = sdl2::init().unwrap(); | ||
let video_subsystem = sdl_context.video().unwrap(); | ||
let window = video_subsystem | ||
.window("Chip-8", WINDOW_WIDTH, WINDOW_HEIGHT) | ||
.position_centered() | ||
.opengl() | ||
.build() | ||
.unwrap(); | ||
|
||
let mut canvas = window.into_canvas().present_vsync().build().unwrap(); | ||
canvas.clear(); | ||
canvas.present(); | ||
|
||
let mut event_pump = sdl_context.event_pump().unwrap(); | ||
|
||
let mut chip8 = Interpreter::new(); | ||
|
||
let mut rom = File::open(&args[1]).expect("Unable to open file"); | ||
let mut buffer = Vec::new(); | ||
rom.read_to_end(&mut buffer).unwrap(); | ||
chip8.load(&buffer); | ||
|
||
'gameloop: loop { | ||
for event in event_pump.poll_iter() { | ||
match event { | ||
Event::Quit { .. } | ||
| Event::KeyDown { | ||
keycode: Some(Keycode::Escape), | ||
.. | ||
} => { | ||
break 'gameloop; | ||
} | ||
Event::KeyDown { | ||
keycode: Some(key), .. | ||
} => { | ||
if let Some(k) = match_key_to_btn(key) { | ||
chip8.keypress(k, true) | ||
} | ||
} | ||
Event::KeyUp { | ||
keycode: Some(key), .. | ||
} => { | ||
if let Some(k) = match_key_to_btn(key) { | ||
chip8.keypress(k, false) | ||
} | ||
} | ||
_ => (), | ||
} | ||
} | ||
|
||
for _ in 0..TICKS_PER_FRAME { | ||
chip8.tick(); | ||
} | ||
chip8.tick_timers(); | ||
draw_screen(&chip8, &mut canvas) | ||
} | ||
} | ||
|
||
fn draw_screen(interpreter: &Interpreter, canvas: &mut Canvas<Window>) { | ||
canvas.set_draw_color(Color::RGB(0, 0, 0)); | ||
canvas.clear(); | ||
|
||
let screen_buffer = interpreter.get_screen(); | ||
canvas.set_draw_color(Color::RGB(255, 255, 255)); | ||
for row in 0..screen_buffer.len() { | ||
for col in 0..screen_buffer[0].len() { | ||
if screen_buffer[row][col] { | ||
let rect = Rect::new( | ||
(col as u32 * SCALE) as i32, | ||
(row as u32 * SCALE) as i32, | ||
SCALE, | ||
SCALE, | ||
); | ||
canvas.fill_rect(rect).unwrap(); | ||
} | ||
} | ||
} | ||
canvas.present(); | ||
} | ||
|
||
fn match_key_to_btn(key: Keycode) -> Option<usize> { | ||
match key { | ||
Keycode::Num1 => Some(0x1), | ||
Keycode::Num2 => Some(0x2), | ||
Keycode::Num3 => Some(0x3), | ||
Keycode::Num4 => Some(0xC), | ||
Keycode::Q => Some(0x4), | ||
Keycode::W => Some(0x5), | ||
Keycode::E => Some(0x6), | ||
Keycode::R => Some(0xD), | ||
Keycode::A => Some(0x7), | ||
Keycode::S => Some(0x8), | ||
Keycode::D => Some(0x9), | ||
Keycode::F => Some(0xE), | ||
Keycode::Z => Some(0xA), | ||
Keycode::X => Some(0x0), | ||
Keycode::C => Some(0xB), | ||
Keycode::V => Some(0xF), | ||
_ => None, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.