Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Sep 21, 2024
1 parent 18ea6a4 commit f56ba58
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ use crate::{

pub struct App {
config: Config,
// maybe move these two into config instead of passing them
// via the cli?
tick_rate: f64,
frame_rate: f64,
components: Arc<Mutex<Vec<Box<dyn Component>>>>,
Expand All @@ -69,7 +71,7 @@ impl App {
pub fn new(tick_rate: f64, frame_rate: f64) -> Result<Self> {
let (action_tx, action_rx) = mpsc::unbounded_channel();
let (render_tx, _) = mpsc::unbounded_channel();
let event_loop = EventLoop::new(Some(std::time::Duration::from_millis(250)), true);
let event_loop = EventLoop::new(tick_rate, true);

Ok(Self {
tick_rate,
Expand All @@ -92,7 +94,6 @@ impl App {

pub async fn run(&mut self) -> Result<()> {
let mut tui = Tui::new()?.frame_rate(self.frame_rate);
// this starts the event handling loop in Tui

// Rendering loop
let (render_tx, render_rx) = mpsc::unbounded_channel();
Expand All @@ -105,7 +106,7 @@ impl App {
render(
&mut tui,
render_rx,
render_tx,
//render_tx,
action_tx_r,
config_r,
components_r,
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::config::{get_config_dir, get_data_dir};
#[command(author, version = version(), about)]
pub struct Cli {
/// Tick rate, i.e. number of ticks per second
#[arg(short, long, value_name = "FLOAT", default_value_t = 4.0)]
#[arg(short, long, value_name = "FLOAT", default_value_t = 8.0)]
pub tick_rate: f64,

/// Frame rate, i.e. number of frames per second
Expand Down
11 changes: 4 additions & 7 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,19 @@ pub struct EventLoop {
//tick_rate: std::time::Duration,
}

const EVENT_LOOP_TICK_RATE: std::time::Duration = std::time::Duration::from_millis(250);

impl EventLoop {
pub fn new(tick_rate: Option<std::time::Duration>, init: bool) -> Self {
pub fn new(tick_rate: f64, init: bool) -> Self {
let (tx, rx) = mpsc::unbounded_channel();
let _tx = tx.clone();
let should_tick = tick_rate.is_some();
let tick_rate = tick_rate.unwrap_or(EVENT_LOOP_TICK_RATE);
let tick_interval = tokio::time::Duration::from_secs_f64(1.0 / tick_rate);

let (abort, mut abort_recv) = mpsc::unbounded_channel();

if init {
let mut reader = crossterm::event::EventStream::new();
tokio::spawn(async move {
loop {
let delay = tokio::time::sleep(tick_rate);
let delay = tokio::time::sleep(tick_interval);
let event = reader.next();

tokio::select! {
Expand All @@ -80,7 +77,7 @@ impl EventLoop {
break;
},
// if `delay` completes, pass to the next event "frame"
_ = delay, if should_tick => {
_ = delay => {
_tx.send(Event::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event"));
},
// if the receiver dropped the channel, stop the event loop
Expand Down
1 change: 0 additions & 1 deletion src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub enum RenderingTask {
pub async fn render(
tui: &mut Tui,
mut render_rx: mpsc::UnboundedReceiver<RenderingTask>,
render_tx: mpsc::UnboundedSender<RenderingTask>,
action_tx: mpsc::UnboundedSender<Action>,
config: Config,
components: Arc<Mutex<Vec<Box<dyn Component>>>>,
Expand Down

0 comments on commit f56ba58

Please sign in to comment.