Skip to content

Commit

Permalink
Remove tokio, use pollster
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Apr 30, 2024
1 parent 03ed461 commit dbfea33
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 116 deletions.
101 changes: 7 additions & 94 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ clap = { version = "4.5", features = ["derive"] }
bitflags = "2.5"
log = "0.4"
wgpu = "0.20"
tokio = { version = "1.37", features = ["rt-multi-thread", "sync", "time", "macros"] }
bytemuck = { version = "1.15", features = ["derive"] }
num-traits = "0.2"
num-derive = "0.4"
serde = { version = "1.0", features = ["derive"] }
toml = "0.8"
toml = "0.8"
pollster = "0.3.0"
30 changes: 13 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use crate::context::Context;
use clap::Parser;
use std::fs::File;
use std::io::{Read, Write};
use std::process;
use std::{process, thread};
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;
use tokio::sync::mpsc::UnboundedSender;
use tokio::time::{sleep, Duration, Instant};
use std::sync::mpsc::{Sender};
use std::sync::mpsc;
use std::time::{Duration, Instant};
use pollster::FutureExt;
use wgpu::SurfaceError;
use winit::event::{ElementState, Event, WindowEvent};
use winit::event_loop::ControlFlow;
Expand All @@ -36,8 +37,7 @@ struct Args {
boot_rom: Option<String>,
}

#[tokio::main]
async fn main() -> Result<(), impl std::error::Error> {
fn main() -> Result<(), impl std::error::Error> {
let config = match File::open("./config.toml") {
Ok(mut file) => {
let mut config_data = String::new();
Expand Down Expand Up @@ -84,12 +84,6 @@ async fn main() -> Result<(), impl std::error::Error> {
let event_loop = EventLoop::new().unwrap();
event_loop.set_control_flow(ControlFlow::Poll);

let panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
panic(info);
process::exit(1);
}));

let window = WindowBuilder::new()
.with_title(format!("tetsuyu - {:}", game_name))
.with_inner_size(winit::dpi::LogicalSize::new(
Expand All @@ -99,14 +93,16 @@ async fn main() -> Result<(), impl std::error::Error> {
.build(&event_loop)
.unwrap();

let context = Arc::new(Mutex::new(Context::new(Arc::new(window), config.clone().shader).await));
let (input_tx, mut input_rx) = mpsc::unbounded_channel::<(JoypadButton, bool)>();
let context_future = Context::new(Arc::new(window), config.clone().shader);
let context = Arc::new(Mutex::new(context_future.block_on()));

let (input_tx, input_rx) = mpsc::channel::<(JoypadButton, bool)>();

{
let config = config.clone();
let context = Arc::clone(&context);
// Start CPU
tokio::spawn(async move {
thread::spawn( move || {
let mut cpu = CPU::new(buffer, config);
let mut step_cycles = 0;
let mut step_zero = Instant::now();
Expand All @@ -119,7 +115,7 @@ async fn main() -> Result<(), impl std::error::Error> {
let duration = now.duration_since(step_zero);
let milliseconds = STEP_TIME.saturating_sub(duration.as_millis() as u32);
// println!("[CPU] Sleeping {}ms", milliseconds);
sleep(Duration::from_millis(milliseconds as u64)).await;
thread::sleep(Duration::from_millis(milliseconds as u64));
step_zero = now;
}

Expand Down Expand Up @@ -204,7 +200,7 @@ pub fn send_input(
key: Key,
pressed: bool,
input: Input,
input_tx: UnboundedSender<(JoypadButton, bool)>,
input_tx: Sender<(JoypadButton, bool)>,
) {
match key {
key if key == input.up => input_tx.send((JoypadButton::UP, pressed)).unwrap(),
Expand Down
7 changes: 4 additions & 3 deletions src/sound/synth.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::thread;
use std::time::Duration;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{Device, FromSample, SizedSample, StreamConfig};
use fundsp::hacker::*;
use std::time::Duration;

pub struct Synth {
pub ch1_freq: Shared<f64>,
Expand Down Expand Up @@ -194,7 +195,7 @@ impl Synth {
) where
T: SizedSample + FromSample<f64>,
{
tokio::spawn(async move {
thread::spawn(move || {
let sample_rate = config.sample_rate.0 as f64;
let channels = config.channels as usize;

Expand Down Expand Up @@ -236,7 +237,7 @@ impl Synth {
stream.play().unwrap();

loop {
std::thread::sleep(Duration::from_millis(1));
std::thread::sleep(Duration::from_millis(120_000));
}
});
}
Expand Down

0 comments on commit dbfea33

Please sign in to comment.