diff --git a/.gitignore b/.gitignore index 60e88f4..4165f5c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /games -/.vscode \ No newline at end of file +/.vscode +/.idea \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 16c4e2f..d38fdf5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,9 @@ // and https://en.wikipedia.org/wiki/CHIP-8#Opcode_table // for information about how to implement a CHIP-8 Emulator use chip8::emu::Chip8; -use std::path::Path; - use pixels::{wgpu::Surface, Pixels, SurfaceTexture}; +use std::path::Path; +use std::time::{Duration, Instant}; use winit::dpi::LogicalSize; use winit::event::{Event, VirtualKeyCode}; use winit::event_loop::{ControlFlow, EventLoop}; @@ -13,6 +13,7 @@ use winit_input_helper::WinitInputHelper; static WIDTH: u32 = 64; static HEIGHT: u32 = 32; +static OPCODES_PER_SECOND: u64 = 500; fn main() { let mut chip8: Chip8 = Default::default(); @@ -24,6 +25,9 @@ fn main() { let rom_path = Path::new("./games/c8games/INVADERS"); chip8.load_rom(rom_path).expect("Unable to load ROM"); + let mut start = Instant::now(); + let frametime = Duration::from_nanos(1e+9 as u64 / OPCODES_PER_SECOND); + event_loop.run(move |event, _, control_flow| { if let Event::RedrawRequested(_) = event { draw(&chip8.display.buf, pixels.get_frame()); @@ -44,7 +48,11 @@ fn main() { pixels.resize(size.width, size.height); } - chip8.execute_cycle(); + if Instant::now().duration_since(start) > frametime { + chip8.execute_cycle(); + start = Instant::now(); + } + if chip8.request_redraw { window.request_redraw(); }