From 7f21946126a24c27a324779823a3413679f177a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sat, 30 Jan 2021 20:19:27 +0100 Subject: add pong --- pong/src/main.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 pong/src/main.rs (limited to 'pong/src') diff --git a/pong/src/main.rs b/pong/src/main.rs new file mode 100644 index 0000000..b29136b --- /dev/null +++ b/pong/src/main.rs @@ -0,0 +1,84 @@ +use macroquad::{Camera2D, DARKPURPLE, KeyCode, SKYBLUE, clear_background, draw_rectangle, get_frame_time, is_key_down, next_frame, set_camera, vec2}; +use std::path::Path; +use tihdy::{Type, Value}; +use tihdy_derive::extern_function; + +const SCREEN_WIDTH: f32 = 20.0; +const SCREEN_HEIGHT: f32 = 20.0; + +extern_function!(log + [Value::Float(f1), Value::Float(f2)] -> Type::Void => { + println!("({}, {})", f1, f2); + Ok(Value::Nil) + }, +); + +extern_function!(get_delta + [] -> Type::Float => { + Ok(Value::Float(get_frame_time() as f64)) + }, +); + +extern_function!(key_down + [Value::String(s)] -> Type::Bool => { + let s: &str = s; + Ok(Value::Bool(match s { + "w" => is_key_down(KeyCode::W), + "s" => is_key_down(KeyCode::S), + "i" => is_key_down(KeyCode::I), + "k" => is_key_down(KeyCode::K), + _ => false, + })) + }, +); + +extern_function!(my_next_frame + [] -> Type::Void => { + tokio::spawn(async { + next_frame().await + }); + Ok(Value::Nil) + }, +); + +extern_function!(my_draw_rectangle + [Value::Float(x), Value::Float(y), Value::Float(w), Value::Float(h)] -> Type::Void => { + println!("Drawing rectangle {} {} {} {}", x, y, w, h); + draw_rectangle(*x as f32, *y as f32, *w as f32, *h as f32, DARKPURPLE); + Ok(Value::Nil) + }, +); + +extern_function!(clear + [] -> Type::Void => { + clear_background(SKYBLUE); + Ok(Value::Nil) + }, +); + +#[macroquad::main("Pong")] +async fn main() { + set_camera(Camera2D { + zoom: vec2(1. / SCREEN_WIDTH * 2., -1. / SCREEN_HEIGHT * 2.), + target: vec2(SCREEN_WIDTH / 2., SCREEN_HEIGHT / 2.), + ..Default::default() + }); + + let rt = tokio::runtime::Runtime::new().unwrap(); + + let functions: Vec<(String, tihdy::RustFunction)> = vec![ + ("log".to_string(), log), + ("get_delta".to_string(), get_delta), + ("key_down".to_string(), key_down), + ("next_frame".to_string(), my_next_frame), + ("draw_rectangle".to_string(), my_draw_rectangle), + ("clear".to_string(), clear), + ]; + + let _guard = rt.enter(); // so we can async { next_frame().await } + if let Err(errs) = tihdy::run_file(Path::new("pong.tdy"), false, functions) { + for err in errs { + println!("{}", err); + } + } +} -- cgit v1.2.1 From 6b5e59311511b1462c7a10ba1389782f640e77a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sat, 30 Jan 2021 23:17:17 +0100 Subject: Yield --- pong/src/main.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'pong/src') diff --git a/pong/src/main.rs b/pong/src/main.rs index b29136b..449fee1 100644 --- a/pong/src/main.rs +++ b/pong/src/main.rs @@ -1,6 +1,7 @@ use macroquad::{Camera2D, DARKPURPLE, KeyCode, SKYBLUE, clear_background, draw_rectangle, get_frame_time, is_key_down, next_frame, set_camera, vec2}; use std::path::Path; use tihdy::{Type, Value}; +use tihdy::vm::OpResult; use tihdy_derive::extern_function; const SCREEN_WIDTH: f32 = 20.0; @@ -76,9 +77,26 @@ async fn main() { ]; let _guard = rt.enter(); // so we can async { next_frame().await } - if let Err(errs) = tihdy::run_file(Path::new("pong.tdy"), false, functions) { + let vm = tihdy::compile_file(Path::new("pong.tdy"), false, functions); + if let Err(errs) = vm { for err in errs { println!("{}", err); } + return; + } + let mut vm = vm.unwrap(); + loop { + match vm.run() { + Err(e) => { + println!("{:?}", e); + break; + } + Ok(OpResult::Yield) => { + next_frame().await + } + _ => { + break; + } + } } } -- cgit v1.2.1 From 43ba4b48e4f849fd7ef288a6ca27213ed7f4904d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sat, 30 Jan 2021 23:20:25 +0100 Subject: move second paddle --- pong/src/main.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'pong/src') diff --git a/pong/src/main.rs b/pong/src/main.rs index 449fee1..6382e92 100644 --- a/pong/src/main.rs +++ b/pong/src/main.rs @@ -44,7 +44,6 @@ extern_function!(my_next_frame extern_function!(my_draw_rectangle [Value::Float(x), Value::Float(y), Value::Float(w), Value::Float(h)] -> Type::Void => { - println!("Drawing rectangle {} {} {} {}", x, y, w, h); draw_rectangle(*x as f32, *y as f32, *w as f32, *h as f32, DARKPURPLE); Ok(Value::Nil) }, -- cgit v1.2.1 From 349055ef2533bf3268f37d3c04e5adb84814f50a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sat, 30 Jan 2021 23:24:05 +0100 Subject: remove tokio from pong --- pong/src/main.rs | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'pong/src') diff --git a/pong/src/main.rs b/pong/src/main.rs index 6382e92..c3cc0bf 100644 --- a/pong/src/main.rs +++ b/pong/src/main.rs @@ -33,15 +33,6 @@ extern_function!(key_down }, ); -extern_function!(my_next_frame - [] -> Type::Void => { - tokio::spawn(async { - next_frame().await - }); - Ok(Value::Nil) - }, -); - extern_function!(my_draw_rectangle [Value::Float(x), Value::Float(y), Value::Float(w), Value::Float(h)] -> Type::Void => { draw_rectangle(*x as f32, *y as f32, *w as f32, *h as f32, DARKPURPLE); @@ -64,18 +55,14 @@ async fn main() { ..Default::default() }); - let rt = tokio::runtime::Runtime::new().unwrap(); - let functions: Vec<(String, tihdy::RustFunction)> = vec![ ("log".to_string(), log), ("get_delta".to_string(), get_delta), ("key_down".to_string(), key_down), - ("next_frame".to_string(), my_next_frame), ("draw_rectangle".to_string(), my_draw_rectangle), ("clear".to_string(), clear), ]; - let _guard = rt.enter(); // so we can async { next_frame().await } let vm = tihdy::compile_file(Path::new("pong.tdy"), false, functions); if let Err(errs) = vm { for err in errs { -- cgit v1.2.1 From 40b9ec3f0f1273440d4405b531cd3290dd259b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 09:55:09 +0100 Subject: Add a ball, that kinda moves --- pong/src/main.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'pong/src') diff --git a/pong/src/main.rs b/pong/src/main.rs index c3cc0bf..2ab4b4a 100644 --- a/pong/src/main.rs +++ b/pong/src/main.rs @@ -49,11 +49,6 @@ extern_function!(clear #[macroquad::main("Pong")] async fn main() { - set_camera(Camera2D { - zoom: vec2(1. / SCREEN_WIDTH * 2., -1. / SCREEN_HEIGHT * 2.), - target: vec2(SCREEN_WIDTH / 2., SCREEN_HEIGHT / 2.), - ..Default::default() - }); let functions: Vec<(String, tihdy::RustFunction)> = vec![ ("log".to_string(), log), @@ -70,6 +65,13 @@ async fn main() { } return; } + + set_camera(Camera2D { + zoom: vec2(1. / SCREEN_WIDTH * 2., -1. / SCREEN_HEIGHT * 2.), + target: vec2(SCREEN_WIDTH / 2., SCREEN_HEIGHT / 2.), + ..Default::default() + }); + let mut vm = vm.unwrap(); loop { match vm.run() { -- cgit v1.2.1