Skip to content

Commit

Permalink
update tui to add scale
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Dec 28, 2024
1 parent 8bbf86e commit 99a4eb3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 0 additions & 5 deletions examples/desktop/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 26 additions & 8 deletions src/screen/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ fn run_app<B: Backend>(

struct App {
should_quit: bool,
scale: u32,
last_key: Option<KeypadKey>,
tick_rate: Duration,
split_percent: u16,
Expand All @@ -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()

Expand All @@ -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<B: Backend>(_: &mut Terminal<B>, 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]);
Expand All @@ -172,6 +181,7 @@ impl App {

Self {
should_quit: false,
scale: 1,
tick_rate: Duration::from_millis(5),
split_percent: 40,
picker,
Expand All @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 99a4eb3

Please sign in to comment.