diff options
| author | Mica White <botahamec@outlook.com> | 2026-07-03 15:33:08 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-07-03 15:33:08 -0400 |
| commit | 73ffbee638c03da132aea0e5d600ca5e8144fd8e (patch) | |
| tree | 4ea897e8b3aaad2876625e241e740493e9b5b8b8 /examples | |
| parent | bb7525206da1782fd9af49621aa52d87bd227838 (diff) | |
Support WASM, kinda
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/snake.rs | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/examples/snake.rs b/examples/snake.rs index 2163acf..b284dba 100644 --- a/examples/snake.rs +++ b/examples/snake.rs @@ -4,10 +4,10 @@ use bytemuck::{Pod, Zeroable, bytes_of, from_bytes_mut}; use crossterm::{event::KeyCode, style::ContentStyle}; use tortuise::{ buffer::{Buffer, Cell, Event}, - file::FileManager, + file::{FileManager, GameManifest}, input::InputManager, - scene::switch_scenes, - script::{ScriptContext, ScriptDeclaration, ScriptManager}, + scene::{ResourceRequirements, Scene, switch_scenes}, + script::{ScriptContext, ScriptDeclaration, ScriptManager, WasmRunner}, }; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Pod, Zeroable)] @@ -26,7 +26,7 @@ struct GameState { next_move: u128, } -fn init_scene(_: &mut ScriptContext, state: &mut Box<[u8]>) { +fn init_scene(_: &mut ScriptContext<'_>, state: &mut Box<[u8]>) { *state = Box::from(bytes_of(&GameState { apple_location: Position { x: 2, y: 2 }, snake_cells: [Position { x: 10, y: 5 }, Position { x: 9, y: 5 }], @@ -40,7 +40,7 @@ fn init_scene(_: &mut ScriptContext, state: &mut Box<[u8]>) { })) } -fn render(ctx: &mut ScriptContext, byte_state: &mut Box<[u8]>) { +fn render(ctx: &mut ScriptContext<'_>, byte_state: &mut Box<[u8]>) { let state: &mut GameState = from_bytes_mut(byte_state); if ctx.input.is_key_pressed_this_frame(KeyCode::Up) { state.direction = Position { x: 0, y: -1 }; @@ -94,13 +94,25 @@ fn render(ctx: &mut ScriptContext, byte_state: &mut Box<[u8]>) { } } -fn exit_on_escape(ctx: &mut ScriptContext, _: &mut Box<[u8]>) { +fn exit_on_escape(ctx: &mut ScriptContext<'_>, _: &mut Box<[u8]>) { if ctx.input.is_key_pressed_this_frame(KeyCode::Esc) { ctx.exit(); } } fn main() { + let manifest = GameManifest { + name: Box::from("snake"), + first_scene: 0, + scenes: Box::from([Scene { + name: Box::from("main"), + scripts: ResourceRequirements { + required: Box::from([0, 1]), + preload: Box::from([]), + }, + }]), + }; + let stdout = Box::new(std::io::stdout().lock()); let mut buffer = Buffer::new(stdout).unwrap(); let mut input = InputManager::new(); @@ -108,6 +120,7 @@ fn main() { buffer: &mut buffer, input: &input, }; + let scripts = [ ScriptDeclaration::Static { start: None, @@ -120,19 +133,17 @@ fn main() { end: None, }, ]; - let mut scripts = ScriptManager::new(&scripts); let mut files = FileManager::default(); - let manifest = files - .load_file(Box::from("manifest.toml".as_ref())) - .unwrap() - .unwrap_manifest() - .clone(); + let mut scripts = ScriptManager::new(&scripts); + let wasm_runner = WasmRunner::new(); + switch_scenes( &manifest.scenes, &mut scripts, &mut context, &mut files, + &wasm_runner, manifest.first_scene, ); @@ -148,7 +159,7 @@ fn main() { buffer, input: &input, }; - scripts.run_scripts(&mut context); + scripts.run_scripts(&mut context, &wasm_runner, &mut files); input.reset_next_frame(); } }) |
