use anyhow::{Context, Ok, Result}; use bevy::{ diagnostic::FrameTimeDiagnosticsPlugin, input::keyboard::KeyboardInput, prelude::*, window::PresentMode, }; use log::info; use rusty_chip8::{Audio, Chip8, Graphics, FPS, TERMINAL_HEIGHT, TERMINAL_WIDTH}; use std::{ collections::HashMap, fs::File, io::Read, path::{Path, PathBuf}, time::Duration, vec, }; use structopt::StructOpt; #[derive(Resource)] struct Chip8Resource(Chip8); struct BevyGraphics<'w, 's> { commands: Commands<'w, 's>, } impl BevyGraphics<'_, '_> { /// Draws/turns on a pixel on a specific coordinate. /// /// If the coordinates is out of the screen area it returns an Error. fn draw_pixel(&mut self, x: usize, y: usize, color: Option) { let x = x as i32; let y = y as i32; let rectangle = SpriteBundle { sprite: Sprite { color: color.unwrap_or(Color::WHITE), custom_size: Some(Vec2::new(10.0, 10.0)), ..default() }, transform: Transform::from_xyz(((x - 32) * 10) as f32, ((16 - y) * 10) as f32, 0.), ..default() }; self.commands.spawn(rectangle); } } impl Graphics for BevyGraphics<'_, '_> { fn clear_pixel(&mut self, x: usize, y: usize) { self.draw_pixel(x, y, Some(Color::BLACK)) } fn draw_pixel(&mut self, x: usize, y: usize) { self.draw_pixel(x, y, None) } } fn setup(mut commands: Commands) { commands.spawn(Camera2dBundle::default()); } struct AudioEmulator; impl Audio for AudioEmulator { fn start_beep(&mut self) { info!("Starting BEEEEP!") } fn stop_beep(&mut self) { info!("Stopping BEEEEP!") } } #[derive(Resource)] struct CPUClock(Timer); #[derive(Resource)] struct TimerClock(Timer); fn tick( commands: Commands, time: Res