summaryrefslogtreecommitdiff
path: root/examples/snake.rs
diff options
context:
space:
mode:
authorMica White <botahamec@outlook.com>2026-07-03 16:35:05 -0400
committerMica White <botahamec@outlook.com>2026-07-03 16:35:05 -0400
commitbdbd5faeb7be5e5f81c1f4a952c8110df9e9b990 (patch)
tree324a84ed9cba6b4ac5427775362ae25c6588b4db /examples/snake.rs
parentf31fda79035077a9dab8dee07a11d71fac3ba791 (diff)
parentcbbe1ca7f7b36b03e813fbf7e0147341090762bb (diff)
merge
Diffstat (limited to 'examples/snake.rs')
-rw-r--r--examples/snake.rs37
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();
}
})