-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub enum Action { | ||
CharacterInput(char), | ||
Restart, | ||
Quit, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,30 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
pub mod action; | ||
pub mod model; | ||
pub mod terminal; | ||
pub mod view; | ||
|
||
use model::Model; | ||
|
||
use terminal::*; | ||
|
||
fn main() -> std::io::Result<()> { | ||
let mut model = Model::default(); | ||
|
||
let mut terminal = create_terminal()?; | ||
|
||
while !model.should_quit { | ||
model.frame_statistics.new_frame(); | ||
|
||
terminal.draw(|frame| { | ||
model.view(frame); | ||
})?; | ||
|
||
if let Some(action) = poll_terminal() { | ||
model.update(action); | ||
} | ||
} | ||
|
||
destroy_terminal()?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::action::Action; | ||
|
||
#[derive(Default)] | ||
pub struct Model { | ||
pub frame_statistics: FrameStatistics, | ||
pub config: Config, | ||
pub should_quit: bool, | ||
} | ||
|
||
impl Model { | ||
pub fn update(&mut self, action: Action) { | ||
match action { | ||
Action::Quit => self.should_quit = true, | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
pub struct Config { | ||
pub show_frame_statistics: bool, | ||
} | ||
|
||
impl Default for Config { | ||
fn default() -> Self { | ||
Config { | ||
show_frame_statistics: true, | ||
} | ||
} | ||
} | ||
|
||
pub struct FrameStatistics { | ||
pub frame_begin: std::time::Instant, | ||
pub last_frame_duration: std::time::Duration, | ||
pub average_frame_duration: std::time::Duration, | ||
} | ||
|
||
impl FrameStatistics { | ||
pub fn new_frame(&mut self) { | ||
let new_frame_begin = std::time::Instant::now(); | ||
self.last_frame_duration = new_frame_begin - self.frame_begin; | ||
self.frame_begin = new_frame_begin; | ||
self.average_frame_duration = | ||
self.average_frame_duration.mul_f64(0.8) + self.last_frame_duration.mul_f64(0.2); | ||
} | ||
} | ||
|
||
impl Default for FrameStatistics { | ||
fn default() -> Self { | ||
FrameStatistics { | ||
frame_begin: std::time::Instant::now(), | ||
last_frame_duration: std::time::Duration::default(), | ||
average_frame_duration: std::time::Duration::default(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crossterm::{ | ||
event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}, | ||
ExecutableCommand, | ||
}; | ||
|
||
use crate::action::Action; | ||
|
||
pub fn create_terminal( | ||
) -> std::io::Result<ratatui::prelude::Terminal<ratatui::prelude::CrosstermBackend<std::io::Stdout>>> | ||
{ | ||
std::io::stdout().execute(crossterm::terminal::EnterAlternateScreen)?; | ||
crossterm::terminal::enable_raw_mode()?; | ||
|
||
let mut terminal = ratatui::prelude::Terminal::new(ratatui::prelude::CrosstermBackend::new( | ||
std::io::stdout(), | ||
))?; | ||
|
||
terminal.clear()?; | ||
|
||
Ok(terminal) | ||
} | ||
|
||
pub fn poll_terminal() -> Option<Action> { | ||
if let Ok(true) = event::poll(std::time::Duration::from_millis(10)) { | ||
} else { | ||
return None; | ||
} | ||
|
||
let Ok(Event::Key(KeyEvent { | ||
code, | ||
modifiers, | ||
kind: KeyEventKind::Press, | ||
state: _, | ||
})) = event::read() | ||
else { | ||
return None; | ||
}; | ||
|
||
match (modifiers, code) { | ||
(KeyModifiers::NONE, KeyCode::Esc) | ||
| (KeyModifiers::CONTROL, KeyCode::Char('c')) | ||
| (KeyModifiers::CONTROL, KeyCode::Char('q')) => Some(Action::Quit), | ||
(KeyModifiers::NONE, KeyCode::Char(c)) => Some(Action::CharacterInput(c)), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub fn destroy_terminal() -> std::io::Result<()> { | ||
std::io::stdout().execute(crossterm::terminal::LeaveAlternateScreen)?; | ||
crossterm::terminal::disable_raw_mode()?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::model::Model; | ||
|
||
use ratatui::{prelude::*, style::*, widgets::*}; | ||
|
||
impl Model { | ||
pub fn view(&self, frame: &mut ratatui::Frame) { | ||
let average_frametime = self.frame_statistics.average_frame_duration.as_millis(); | ||
let average_fps = 1_000 / std::cmp::max(average_frametime, 1); | ||
|
||
frame.render_widget( | ||
Block::default() | ||
.title(format!( | ||
"Frametime {}ms ({}FPS)", | ||
average_frametime, average_fps | ||
)) | ||
.title_alignment(Alignment::Right) | ||
.borders(Borders::ALL), | ||
frame.size(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[default.extend-words] | ||
"ratatui" = "ratatui" |