Frame counter

This commit is contained in:
Franco Colmenarez 2021-11-12 14:04:10 -05:00
parent 6404593f2c
commit 7beeae01fa
4 changed files with 54 additions and 4 deletions

View File

@ -59,11 +59,15 @@ impl Bus {
pub fn new() -> Self {
let args: Vec<String> = 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

37
src/frames.rs Normal file
View File

@ -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
}
}

View File

@ -7,3 +7,4 @@ pub mod bus;
pub mod joypad;
pub mod emulator;
pub mod render;
pub mod frames;

View File

@ -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<T>(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();