diff --git a/src/app/app.rs b/src/app/app.rs index 1fe22e5..6dcbf1d 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -1,13 +1,14 @@ use crate::{ - app::{ - GameStates, handle_keybindings, - threads::{AppChannels, AudioCmd}, - view::View, - }, + app::{GameStates, handle_keybindings, threads::AudioCmd, view::View}, cli::Cli, }; use ratatui::{DefaultTerminal, Frame, crossterm::event::KeyEvent, layout::Rect}; -use std::{io::Result, sync::mpsc::Sender, thread::sleep, time::Duration}; +use std::{ + io::Result, + sync::mpsc::{Receiver, Sender}, + thread::sleep, + time::Duration, +}; pub struct App { pub exit: bool, @@ -38,15 +39,21 @@ impl App { self.states.as_mut() } - pub fn run(&mut self, terminal: &mut DefaultTerminal, channels: AppChannels) -> Result<()> { + pub fn run( + &mut self, + terminal: &mut DefaultTerminal, + input_rx: Receiver, + resize_rx: Receiver<(u16, u16)>, + tick_rx: Receiver<()>, + ) -> Result<()> { while !self.exit { terminal.draw(|frame: &mut Frame<'_>| self.draw(frame))?; - if let Ok(key) = channels.input_rx.try_recv() { + if let Ok(key) = input_rx.try_recv() { self.handle_key_event(key)?; } - if let Ok((_, _)) = channels.resize_rx.try_recv() { + if let Ok((_, _)) = resize_rx.try_recv() { let window_area: Rect = self.window_area; if let Some(state) = self.states_mut() { state @@ -56,7 +63,7 @@ impl App { } } - if let Ok(()) = channels.tick_rx.try_recv() { + if let Ok(()) = tick_rx.try_recv() { self.update()?; } diff --git a/src/app/threads/app_channels.rs b/src/app/threads/app_channels.rs deleted file mode 100644 index 1ff00cb..0000000 --- a/src/app/threads/app_channels.rs +++ /dev/null @@ -1,37 +0,0 @@ -use crate::app::threads::AudioCmd; -use ratatui::crossterm::event::KeyEvent; -use std::sync::mpsc::{Receiver, Sender, channel}; - -pub struct AppChannels { - pub input_tx: Sender, - pub input_rx: Receiver, - - pub resize_tx: Sender<(u16, u16)>, - pub resize_rx: Receiver<(u16, u16)>, - - pub tick_tx: Sender<()>, - pub tick_rx: Receiver<()>, - - pub audio_tx: Sender, - pub audio_rx: Receiver, -} - -impl AppChannels { - pub fn new() -> Self { - let (input_tx, input_rx) = channel::(); - let (resize_tx, resize_rx) = channel::<(u16, u16)>(); - let (tick_tx, tick_rx) = channel::<()>(); - let (audio_tx, audio_rx) = channel::(); - - Self { - input_tx, - input_rx, - resize_tx, - resize_rx, - tick_tx, - tick_rx, - audio_tx, - audio_rx, - } - } -} diff --git a/src/app/threads/mod.rs b/src/app/threads/mod.rs index a08ac80..24050fd 100644 --- a/src/app/threads/mod.rs +++ b/src/app/threads/mod.rs @@ -1,9 +1,7 @@ -mod app_channels; mod audio; mod events; mod tick; -pub use app_channels::AppChannels; pub use audio::{AudioCmd, SoundrackParts, Soundtrack, handle_audio}; pub use events::handle_ct_events; pub use tick::handle_tick_event; diff --git a/src/cli.rs b/src/cli.rs index 472171a..e8aa35c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -148,7 +148,7 @@ pub struct Cli { default_value_t = Soundtrack::Default, value_enum )] - pub sound_track: Soundtrack, + pub soundtrack: Soundtrack, /// Mute soundtrack (default: disabled). #[arg( diff --git a/src/main.rs b/src/main.rs index 75a7b3e..301fd5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,13 @@ use ratatui::{Terminal, crossterm::event::KeyEvent, prelude::CrosstermBackend}; use std::{ io::{Result, Stdout}, - mem::replace, - sync::mpsc::{Receiver, Sender, channel}, + sync::mpsc::channel, thread, }; use war_in_tunnels::{ app::{ App, - threads::{AppChannels, AudioCmd, handle_audio, handle_ct_events, handle_tick_event}, + threads::{AudioCmd, handle_audio, handle_ct_events, handle_tick_event}, }, cli::{Cli, get_args}, }; @@ -23,24 +22,21 @@ const TICK_MS: u8 = 33; fn main() -> Result<()> { let args: Cli = get_args(); - let mut channels: AppChannels = AppChannels::new(); + let (input_tx, input_rx) = channel::(); + let (resize_tx, resize_rx) = channel::<(u16, u16)>(); + let (tick_tx, tick_rx) = channel::<()>(); + let (audio_tx, audio_rx) = channel::(); - let input_tx: Sender = channels.input_tx.clone(); - let resize_tx: Sender<(u16, u16)> = channels.resize_tx.clone(); thread::spawn(move || handle_ct_events(input_tx, resize_tx)); - - let tick_tx: Sender<()> = channels.tick_tx.clone(); thread::spawn(move || handle_tick_event(tick_tx, TICK_MS)); - - let audio_rx: Receiver = replace(&mut channels.audio_rx, channel().1); thread::spawn(move || { - handle_audio(audio_rx, args.mute, args.sound_track); + handle_audio(audio_rx, args.mute, args.soundtrack); }); let mut terminal: Terminal> = ratatui::init(); - let mut app: App = App::new(args, channels.audio_tx.clone()); + let mut app: App = App::new(args, audio_tx); - let app_result: Result<()> = app.run(&mut terminal, channels); + let app_result: Result<()> = app.run(&mut terminal, input_rx, resize_rx, tick_rx); ratatui::restore();