diff --git a/README.md b/README.md index 7c2c232..c4a3d93 100644 --- a/README.md +++ b/README.md @@ -255,6 +255,34 @@ fn main() { } ``` +### Terminal ~ Rust usage: + +tl;dr: You can see the destop example in the example folder ([`/examples/terminal`](/examples/terminal)) + +1. Add `gameboy` as dependency: + +```toml +[dependencies] +gameboy = { version = "0.1.2" } +``` + +2. There you go: + +> Default scale is `1` (160x144). + +```rust +use gameboy::gameboy::{Gameboy, RenderMode::Terminal}; + +fn main() { + if let Ok((data, filepath)) = load_rom("./my-rom.gb") { + let gb = Gameboy::new(data, Some(filepath)); + gb.render(Terminal); + } else { + panic!("error loading rom"); + } +} +``` + ## Tests The tests are based on Blargg's Gameboy hardware test ROMs. diff --git a/examples/desktop/bin.rs b/examples/desktop/bin.rs index de54b2e..fcd739c 100644 --- a/examples/desktop/bin.rs +++ b/examples/desktop/bin.rs @@ -7,11 +7,6 @@ fn main() { // let gb = Gameboy::new("./../../tests/cpu_instrs/cpu_instrs.gb"); if let Ok((data, filepath)) = load_rom("./../pokemon-red.gb") { // if let Ok((data, filepath)) = load_rom("./../bakery.gb") { - // if let Ok((data, filepath)) = load_rom("./../unearthed.gb") { - // if let Ok((data, filepath)) = load_rom("./../../tests/cpu_instrs/cpu_instrs.gb") { - // if let Ok((data, filepath)) = load_rom("./../../tests/dmg_sound/dmg_sound.gb") { - // na web: - // let gb = Gameboy::new(data, None); let gb = Gameboy::new(data, Some(filepath)); gb.render(Desktop); } else { diff --git a/src/screen/tui.rs b/src/screen/tui.rs index 83f5f9f..d26678d 100644 --- a/src/screen/tui.rs +++ b/src/screen/tui.rs @@ -117,6 +117,7 @@ fn run_app( struct App { should_quit: bool, + scale: u32, last_key: Option, tick_rate: Duration, split_percent: u16, @@ -133,7 +134,7 @@ fn size() -> Rect { Rect::new(0, 0, 30, 16) } -fn get_image(gameboy: &mut Gameboy) -> image::DynamicImage { +fn get_image(gameboy: &mut Gameboy, scale: u32) -> image::DynamicImage { // let harvest_moon = "/Users/rapha/harvest-moon.png"; // image::io::Reader::open(harvest_moon).unwrap().decode().unwrap() @@ -154,13 +155,21 @@ fn get_image(gameboy: &mut Gameboy) -> image::DynamicImage { i += 3; } - let buffer = image::ImageBuffer::from_raw(width, height, output_data).unwrap(); + let mut buffer = image::ImageBuffer::from_raw(width, height, output_data).unwrap(); + if scale > 1 { + buffer = image::imageops::resize( + &buffer, + width * scale, + height * scale, + image::imageops::FilterType::Nearest, + ); + } image::DynamicImage::ImageRgb8(buffer) } impl App { pub fn new(_: &mut Terminal, gameboy: &mut Gameboy) -> Self { - let image_source = get_image(gameboy); + let image_source = get_image(gameboy, 1); let mut picker = Picker::from_query_stdio().unwrap(); picker.set_background_color([0, 0, 0, 0]); @@ -172,6 +181,7 @@ impl App { Self { should_quit: false, + scale: 1, tick_rate: Duration::from_millis(5), split_percent: 40, picker, @@ -194,6 +204,13 @@ impl App { .set_protocol_type(self.picker.protocol_type().next()); self.reset_images(); } + 'o' => { + if self.scale >= 3 { + self.scale = 1; + } else { + self.scale += 1; + } + } 'H' => { if self.split_percent >= 10 { self.split_percent -= 10; @@ -250,7 +267,7 @@ impl App { #[inline] pub fn on_tick(&mut self, gameboy: &mut Gameboy) { - self.image_source = get_image(gameboy); + self.image_source = get_image(gameboy, self.scale); self.image_static = self .picker .new_protocol(self.image_source.clone(), size(), Resize::Fit(None)) @@ -295,11 +312,12 @@ fn ui(f: &mut Frame<'_>, app: &mut App) { paragraph(vec![ Line::from("Controls:"), Line::from("arrows: movement"), - Line::from("Key a: A"), - Line::from("Key s: B"), - Line::from("Key z: select"), - Line::from("Key x: start"), + Line::from("Key a/A: A"), + Line::from("Key s/S: B"), + Line::from("Key z/Z: select"), + Line::from("Key x/X: start"), Line::from("H/L: resize splits"), + Line::from(format!("o: scale image (current: {:?})", app.scale)), Line::from(format!( "i: cycle image protocols (current: {:?})", app.picker.protocol_type()