improve terminal display & emulation fluidity
This commit is contained in:
parent
652b11cd04
commit
6b2a6e288d
2 changed files with 26 additions and 31 deletions
|
@ -1,7 +1,5 @@
|
||||||
use ratatui::layout::Rect;
|
use ratatui::layout::Rect;
|
||||||
use ratatui::style::Color;
|
use ratatui::style::{Style, Stylize};
|
||||||
use ratatui::symbols::Marker;
|
|
||||||
use ratatui::widgets::canvas::Canvas;
|
|
||||||
use ratatui::widgets::{Block, Borders};
|
use ratatui::widgets::{Block, Borders};
|
||||||
use ratatui::DefaultTerminal;
|
use ratatui::DefaultTerminal;
|
||||||
|
|
||||||
|
@ -71,37 +69,34 @@ impl Display for RatatuiDisplay {
|
||||||
fn render(&mut self, display_data: &[bool; DISPLAY_WIDTH * DISPLAY_HEIGHT]) {
|
fn render(&mut self, display_data: &[bool; DISPLAY_WIDTH * DISPLAY_HEIGHT]) {
|
||||||
self.terminal
|
self.terminal
|
||||||
.draw(|frame| {
|
.draw(|frame| {
|
||||||
let canvas = Canvas::default()
|
|
||||||
.block(
|
|
||||||
Block::default()
|
|
||||||
.title("Chip8 Emulator by nuculabs.dev")
|
|
||||||
.borders(Borders::ALL),
|
|
||||||
)
|
|
||||||
.marker(Marker::Braille)
|
|
||||||
.paint(|ctx| {
|
|
||||||
for row in 0..DISPLAY_HEIGHT {
|
|
||||||
for column in 0..DISPLAY_WIDTH {
|
|
||||||
if display_data[row * DISPLAY_WIDTH + column] {
|
|
||||||
ctx.draw(&ratatui::widgets::canvas::Rectangle {
|
|
||||||
x: column as f64,
|
|
||||||
y: DISPLAY_HEIGHT as f64 - row as f64,
|
|
||||||
width: 1.0,
|
|
||||||
height: 1.0,
|
|
||||||
color: Color::White,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.x_bounds([0.0, DISPLAY_WIDTH as f64])
|
|
||||||
.y_bounds([0.0, DISPLAY_HEIGHT as f64]);
|
|
||||||
|
|
||||||
// Render the canvas widget
|
// Render the canvas widget
|
||||||
frame.render_widget(
|
frame.render_widget(
|
||||||
canvas,
|
Block::default()
|
||||||
Rect::new(0, 0, DISPLAY_WIDTH as u16, DISPLAY_HEIGHT as u16),
|
.title("Chip8 Emulator by nuculabs.dev")
|
||||||
|
.borders(Borders::ALL),
|
||||||
|
Rect::new(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
(DISPLAY_WIDTH * 2 + 2) as u16,
|
||||||
|
(DISPLAY_HEIGHT * 2) as u16,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
display_data.iter().enumerate().for_each(|(index, pixel)| {
|
||||||
|
if *pixel {
|
||||||
|
let x = (index % DISPLAY_WIDTH) as u16;
|
||||||
|
let y = (index / DISPLAY_WIDTH) as u16;
|
||||||
|
let area = Rect::new(x * 2, y, 2, 1);
|
||||||
|
let block = Block::default().style(Style::new().on_white());
|
||||||
|
frame.render_widget(block, area);
|
||||||
|
}
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.expect("failed to draw");
|
.expect("failed to draw");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Drop for RatatuiDisplay {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
ratatui::restore();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -146,7 +146,7 @@ where
|
||||||
// Handle sound and delay timer.
|
// Handle sound and delay timer.
|
||||||
self.handle_timers();
|
self.handle_timers();
|
||||||
|
|
||||||
for _ in 0..=7 {
|
for _ in 0..=14 {
|
||||||
// fetch instruction & decode it
|
// fetch instruction & decode it
|
||||||
let instruction = self.fetch_instruction()?;
|
let instruction = self.fetch_instruction()?;
|
||||||
self.program_counter += 2;
|
self.program_counter += 2;
|
||||||
|
|
Loading…
Reference in a new issue