From 7beeae01faaf2038b9b45be0e2458ba36c594df9 Mon Sep 17 00:00:00 2001 From: Franco Colmenarez Date: Fri, 12 Nov 2021 14:04:10 -0500 Subject: [PATCH] Frame counter --- src/bus.rs | 8 ++++++-- src/frames.rs | 37 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/render.rs | 12 ++++++++++-- 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/frames.rs diff --git a/src/bus.rs b/src/bus.rs index cc498ec..349eaae 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -59,11 +59,15 @@ impl Bus { pub fn new() -> Self { let args: Vec = std::env::args().collect(); if args.len() < 2 { - panic!("Please, specify a ROM"); + println!("Please, specify a ROM file"); + std::process::exit(1); } let game_rom = match ROM::load_file(&args[1]) { Ok(rom) => rom, - Err(_) => panic!("Could not read ROM"), + Err(err) => { + println!("Could not read ROM: {}", err); + std::process::exit(1); + }, }; let mut data = [0x00; 0x10000]; // Hardware registers after the bootrom diff --git a/src/frames.rs b/src/frames.rs new file mode 100644 index 0000000..33e02fd --- /dev/null +++ b/src/frames.rs @@ -0,0 +1,37 @@ +use std::time::Instant; + +pub struct Frames { + count: usize, + timer: Instant, + time_start: u128, +} + +impl Frames { + pub fn new() -> Self { + Self { + count: 0, + timer: Instant::now(), + time_start: 0, + } + } + + pub fn reset_count(&mut self) { + self.count = 0; + } + + pub fn reset_timer(&mut self) { + self.time_start = self.timer.elapsed().as_millis(); + } + + pub fn increment(&mut self) { + self.count = self.count.saturating_add(1); + } + + pub fn elapsed_ms(&self) -> u128 { + self.timer.elapsed().as_millis().saturating_sub(self.time_start) + } + + pub fn count(&self) -> usize { + self.count + } +} diff --git a/src/lib.rs b/src/lib.rs index b53c180..7a044bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,3 +7,4 @@ pub mod bus; pub mod joypad; pub mod emulator; pub mod render; +pub mod frames; diff --git a/src/render.rs b/src/render.rs index 433b987..d2968f9 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,4 +1,5 @@ use crate::emulator::Emulator; +use crate::frames::Frames; use crate::cpu::{Cycles}; use crate::ppu::{WIDTH, HEIGHT}; @@ -42,6 +43,9 @@ pub fn create_window(width: u32, height: u32, title: String, event_loop: &Eve } pub fn start_eventloop() { + let mut emulator = Emulator::new(); + let mut frames = Frames::new(); + env_logger::init(); let event_loop = EventLoop::new(); let mut input = WinitInputHelper::new(); @@ -49,8 +53,6 @@ pub fn start_eventloop() { let window = create_window(WIDTH, HEIGHT, "rmg-001".to_string(), &event_loop); let mut pixels = create_pixels(WIDTH, HEIGHT, &window); - let mut emulator = Emulator::new(); - event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; @@ -80,6 +82,12 @@ pub fn start_eventloop() { }, Event::MainEventsCleared => { emulator.run(Cycles(70224), pixels.get_frame()); + frames.increment(); + if frames.elapsed_ms() >= 1000 { + window.set_title(&format!("rmg-001 (FPS: {})", frames.count())); + frames.reset_count(); + frames.reset_timer(); + } // thread::sleep(time::Duration::from_millis(1)); window.request_redraw();