From 2fa23dbbc37f61ca625e0bfbe388b2baaf474151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 6 Oct 2020 12:28:49 +0200 Subject: [PATCH] start program without spawning thread so screen works on mac. improve mouse demo --- crsn/src/runtime/run_thread.rs | 6 +++-- examples/mouse.csn | 40 ++++++++++++++++++---------------- launcher/src/main.rs | 5 ++--- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/crsn/src/runtime/run_thread.rs b/crsn/src/runtime/run_thread.rs index 3c9be78..2c5dc12 100644 --- a/crsn/src/runtime/run_thread.rs +++ b/crsn/src/runtime/run_thread.rs @@ -48,13 +48,15 @@ impl RunThread { self.info.cycle_time = cycle_time; } - pub fn start(self) -> JoinHandle<()> { + /// Spawn as a thread + pub fn spawn(self) -> JoinHandle<()> { std::thread::spawn(move || { self.run(); }) } - fn run(mut self) { + /// Start synchronously + pub fn run(mut self) { 'run: loop { match self.eval_op() { Ok(EvalRes { cycles, advance }) => { diff --git a/examples/mouse.csn b/examples/mouse.csn index 355b9a4..96cebd2 100644 --- a/examples/mouse.csn +++ b/examples/mouse.csn @@ -1,18 +1,25 @@ ( - (sc-init 640 480) + (sc-init 800 600) (sym x r6) (sym y r7) - (def BOXSIZE 10) + (def BOXSIZE 7) (:loop) (sc-poll) - (sc-mouse x y - (ov? (j :next))) + (sc-blit 0) + (sc-mouse x y (ov? (j :next))) + (sc-mbtn _ 1 (nz? (sc-erase) (sc-blit 1))) + (sc-mbtn _ 0 (z? (j :next))) - (sc-mbtn r0 0 (z? (j :next))) + (call draw_box x y) + (:next) + (sleep 500) + (j :loop) + + (proc draw_box x y (sym vx r0) (sym vy r1) (sym endx r2) @@ -20,21 +27,16 @@ (add endx x BOXSIZE) (add endy y BOXSIZE) - (sub vy y BOXSIZE) (:nextline) - (cmp vy endy (eq? (j :endbox))) - (sub vx x BOXSIZE) - (:nextpx) - (sc-px vx vy 0xffffff) - (inc vx) - (cmp vx endx - (eq? (inc vy)(j :nextline))) - (j :nextpx) - (:endbox) - - (:next) - (sleep 20000) - (j :loop) + (cmp vy endy (eq? (ret))) + (sub vx x BOXSIZE) + (:nextpx) + (sc-px vx vy 0xffffff) + (inc vx) + (cmp vx endx + (eq? (inc vy)(j :nextline))) + (j :nextpx) + ) ) diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 7beee45..95e0965 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -148,9 +148,8 @@ fn main() -> anyhow::Result<()> { let mut thread = RunThread::new(ThreadToken(0), None, parsed.clone(), Addr(0), args); thread.set_speed(config.cycle_time); - let a = thread.start(); - // ... - let _ = a.join(); + // run without spawning, so it is on the main thread - required by some extensions + thread.run(); info!("Runtime shut down.");