mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 11:16:42 +01:00
add support for game input
This commit is contained in:
60
examples/curlywas/skipahead.cwa
Normal file
60
examples/curlywas/skipahead.cwa
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import "env.memory" memory(4);
|
||||||
|
|
||||||
|
import "env.rectangle" fn rect(f32, f32, f32, f32, i32);
|
||||||
|
import "env.circle" fn circle(f32, f32, f32, i32);
|
||||||
|
import "env.isButtonPressed" fn btn(i32) -> i32;
|
||||||
|
import "env.random" fn random() -> i32;
|
||||||
|
import "env.randomSeed" fn randomSeed(i32);
|
||||||
|
import "env.cls" fn cls(i32);
|
||||||
|
|
||||||
|
global mut pz: i32 = 4;
|
||||||
|
global mut px: f32 = 2.0;
|
||||||
|
global mut py: f32 = 2.0;
|
||||||
|
global mut s: f32 = 2.0;
|
||||||
|
global mut f: f32 = 2.0;
|
||||||
|
|
||||||
|
export fn upd() {
|
||||||
|
let y: i32;
|
||||||
|
let inline zero = 0.0;
|
||||||
|
|
||||||
|
let lazy control_speed = 0.03;
|
||||||
|
s = s + 0.1 - (f + control_speed) * btn(4 <| cls(4)) as f32;
|
||||||
|
f = f * 0.7;
|
||||||
|
|
||||||
|
loop lines {
|
||||||
|
let lazy z = (4000 / (y := y + 1) + pz) / 20;
|
||||||
|
let lazy x = px - ({randomSeed(z); random()} >> 30) as f32;
|
||||||
|
let lazy w = 9 as f32 / sqrt(z as f32);
|
||||||
|
let lazy rx = 160 as f32 - (y as f32 * x);
|
||||||
|
let inline rw = y as f32 * w;
|
||||||
|
|
||||||
|
let inline c = (z & 1) * -2;
|
||||||
|
let inline yf = y as f32;
|
||||||
|
rect(rx, yf, rw, yf / 6 as f32, c + 1);
|
||||||
|
rect(rx, yf, rw, 1 as f32, c - 4);
|
||||||
|
|
||||||
|
if y == 180 & py > zero {
|
||||||
|
if x > w | x < zero {
|
||||||
|
|
||||||
|
if btn(5) {
|
||||||
|
pz = 5;
|
||||||
|
px = 2 as f32;
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
py = zero;
|
||||||
|
s = zero;
|
||||||
|
f = 0.8;
|
||||||
|
}
|
||||||
|
|
||||||
|
branch_if y < 240: lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
circle(160 as f32, 160 as f32 + py, 22 as f32, -28);
|
||||||
|
circle((160 - 6) as f32, (160 - 6) as f32 + py, 6 as f32, -26);
|
||||||
|
|
||||||
|
px = px + (btn(3) - btn(2)) as f32 * control_speed;
|
||||||
|
py = py + s;
|
||||||
|
pz = pz + 1;
|
||||||
|
}
|
||||||
25
examples/curlywas/technotunnel.cwa
Normal file
25
examples/curlywas/technotunnel.cwa
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import "env.memory" memory(4);
|
||||||
|
import "env.sin" fn sin(f32) -> f32;
|
||||||
|
import "env.time" fn time() -> f32;
|
||||||
|
|
||||||
|
export fn upd() {
|
||||||
|
let i: i32;
|
||||||
|
loop screen {
|
||||||
|
let inline t = time() / 2 as f32;
|
||||||
|
let lazy o = sin(t) * 0.8;
|
||||||
|
let lazy q = (i % 320) as f32 - 160.1;
|
||||||
|
let lazy w = (i / 320 - 120) as f32;
|
||||||
|
let lazy r = sqrt(q*q + w*w);
|
||||||
|
let lazy z = q / r;
|
||||||
|
let lazy s = z * o + sqrt(z * z * o * o + 1 as f32 - o * o);
|
||||||
|
let lazy q2 = (z * s - o) * 10 as f32 + t;
|
||||||
|
let lazy w2 = w / r * s * 10 as f32 + t;
|
||||||
|
let lazy s2 = s * 100 as f32 / r;
|
||||||
|
i?120 = max(
|
||||||
|
0 as f32,
|
||||||
|
((q2 as i32 ^ w2 as i32 & ((s2 + time()) * 10 as f32) as i32) & 5) as f32 *
|
||||||
|
(4 as f32 - s2) as f32
|
||||||
|
) as i32 - 32;
|
||||||
|
branch_if (i := i + 1) < 320*240: screen
|
||||||
|
}
|
||||||
|
}
|
||||||
27
examples/curlywas/trainride.cwa
Normal file
27
examples/curlywas/trainride.cwa
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import "env.memory" memory(2);
|
||||||
|
|
||||||
|
import "env.fmod" fn fmod(f32, f32) -> f32;
|
||||||
|
import "env.time" fn time() -> f32;
|
||||||
|
|
||||||
|
export fn upd() {
|
||||||
|
let i: i32;
|
||||||
|
loop pixels {
|
||||||
|
let lazy x = (i % 320 - 160) as f32;
|
||||||
|
let lazy y = (i / 320) as f32 - 120.5;
|
||||||
|
let lazy z = time() + 20 as f32 / sqrt(x*x + y*y);
|
||||||
|
let inline z_int = z as i32;
|
||||||
|
let lazy q = select(z_int % 9 >= 6, z, (z_int - (z_int % 9 - 6)) as f32);
|
||||||
|
let lazy w = 9 as f32 / y + time();
|
||||||
|
let inline s = q - time();
|
||||||
|
let inline m = x * s / 50 as f32;
|
||||||
|
|
||||||
|
i?120 = select(y > 0 as f32 & w < q,
|
||||||
|
select(abs(x * (w - time())) < 9 as f32, -2, -18) - w as i32 % 2,
|
||||||
|
select(y * s > -99 as f32 / (m * m + 1 as f32),
|
||||||
|
select(z_int % 9 >= 6, z_int % 2 - 31, -27),
|
||||||
|
(-10 as f32 + y / 23 as f32 + fmod(y / 4 as f32, 1 as f32)) as i32
|
||||||
|
)
|
||||||
|
);
|
||||||
|
branch_if (i := i + 1) < 320*240: pixels;
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
@@ -2,12 +2,16 @@ import "env.memory" memory(4);
|
|||||||
|
|
||||||
import "env.cos" fn cos(f32) -> f32;
|
import "env.cos" fn cos(f32) -> f32;
|
||||||
|
|
||||||
export fn time() -> i32 {
|
export fn time() -> f32 {
|
||||||
0!64
|
(0!64) as f32 / 1000 as f32
|
||||||
}
|
}
|
||||||
|
|
||||||
export fn ftime() -> f32 {
|
export fn isButtonPressed(btn: i32) -> i32 {
|
||||||
(0!64) as f32 / 1000 as f32
|
(68!0 >> btn) & 1
|
||||||
|
}
|
||||||
|
|
||||||
|
export fn isButtonTriggered(btn: i32) -> i32 {
|
||||||
|
((68!0 & (-1 - 68!4)) >> btn) & 1
|
||||||
}
|
}
|
||||||
|
|
||||||
global mut randomState: i64 = 37i64;
|
global mut randomState: i64 = 37i64;
|
||||||
@@ -117,6 +121,10 @@ export fn circle(cx: f32, cy: f32, radius: f32, col: i32) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export fn endFrame() {
|
||||||
|
68!4 = 68!0;
|
||||||
|
}
|
||||||
|
|
||||||
start fn setup() {
|
start fn setup() {
|
||||||
let i: i32 = 12*16*3-1;
|
let i: i32 = 12*16*3-1;
|
||||||
let avg: f32;
|
let avg: f32;
|
||||||
|
|||||||
36
src/lib.rs
36
src/lib.rs
@@ -8,6 +8,8 @@ use wasmtime::{
|
|||||||
Engine, GlobalType, Memory, MemoryType, Module, Mutability, Store, TypedFunc, ValType,
|
Engine, GlobalType, Memory, MemoryType, Module, Mutability, Store, TypedFunc, ValType,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static GAMEPAD_KEYS: &'static [Key] = &[Key::Up, Key::Down, Key::Left, Key::Right, Key::Z, Key::X, Key::A, Key::S];
|
||||||
|
|
||||||
pub struct MicroW8 {
|
pub struct MicroW8 {
|
||||||
engine: Engine,
|
engine: Engine,
|
||||||
loader_module: Module,
|
loader_module: Module,
|
||||||
@@ -19,7 +21,8 @@ pub struct MicroW8 {
|
|||||||
struct UW8Instance {
|
struct UW8Instance {
|
||||||
store: Store<()>,
|
store: Store<()>,
|
||||||
memory: Memory,
|
memory: Memory,
|
||||||
upd: TypedFunc<(), ()>,
|
end_frame: TypedFunc<(), ()>,
|
||||||
|
update: TypedFunc<(), ()>,
|
||||||
start_time: Instant,
|
start_time: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,7 +33,11 @@ impl MicroW8 {
|
|||||||
let loader_module =
|
let loader_module =
|
||||||
wasmtime::Module::new(&engine, include_bytes!("../platform/bin/loader.wasm"))?;
|
wasmtime::Module::new(&engine, include_bytes!("../platform/bin/loader.wasm"))?;
|
||||||
|
|
||||||
let mut window = Window::new("MicroW8", 320, 240, WindowOptions::default())?;
|
let mut options = WindowOptions::default();
|
||||||
|
options.scale = minifb::Scale::X2;
|
||||||
|
options.scale_mode = minifb::ScaleMode::AspectRatioStretch;
|
||||||
|
options.resize = true;
|
||||||
|
let mut window = Window::new("MicroW8", 320, 240, options)?;
|
||||||
window.limit_update_rate(Some(std::time::Duration::from_micros(16666)));
|
window.limit_update_rate(Some(std::time::Duration::from_micros(16666)));
|
||||||
|
|
||||||
Ok(MicroW8 {
|
Ok(MicroW8 {
|
||||||
@@ -123,12 +130,14 @@ impl MicroW8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let instance = linker.instantiate(&mut store, &module)?;
|
let instance = linker.instantiate(&mut store, &module)?;
|
||||||
let upd = instance.get_typed_func::<(), (), _>(&mut store, "upd")?;
|
let end_frame = platform_instance.get_typed_func::<(), (), _>(&mut store, "endFrame")?;
|
||||||
|
let update = instance.get_typed_func::<(), (), _>(&mut store, "upd")?;
|
||||||
|
|
||||||
self.instance = Some(UW8Instance {
|
self.instance = Some(UW8Instance {
|
||||||
store,
|
store,
|
||||||
memory,
|
memory,
|
||||||
upd,
|
end_frame,
|
||||||
|
update,
|
||||||
start_time: Instant::now(),
|
start_time: Instant::now(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -137,9 +146,22 @@ impl MicroW8 {
|
|||||||
|
|
||||||
pub fn run_frame(&mut self) -> Result<()> {
|
pub fn run_frame(&mut self) -> Result<()> {
|
||||||
if let Some(mut instance) = self.instance.take() {
|
if let Some(mut instance) = self.instance.take() {
|
||||||
instance.memory.data_mut(&mut instance.store)[64..68]
|
{
|
||||||
.copy_from_slice(&(instance.start_time.elapsed().as_millis() as i32).to_le_bytes());
|
let time = instance.start_time.elapsed().as_millis() as i32;
|
||||||
instance.upd.call(&mut instance.store, ())?;
|
let mut gamepad: u32 = 0;
|
||||||
|
for key in self.window.get_keys().unwrap_or(Vec::new()) {
|
||||||
|
if let Some(index) = GAMEPAD_KEYS.iter().enumerate().find(|(_, &k)| k == key).map(|(i, _)| i) {
|
||||||
|
gamepad |= 1 << index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mem = instance.memory.data_mut(&mut instance.store);
|
||||||
|
mem[64..68].copy_from_slice(&time.to_le_bytes());
|
||||||
|
mem[68..72].copy_from_slice(&gamepad.to_le_bytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
instance.update.call(&mut instance.store, ())?;
|
||||||
|
instance.end_frame.call(&mut instance.store, ())?;
|
||||||
|
|
||||||
let framebuffer = &instance.memory.data(&instance.store)[120..];
|
let framebuffer = &instance.memory.data(&instance.store)[120..];
|
||||||
let palette = &framebuffer[320 * 240..];
|
let palette = &framebuffer[320 * 240..];
|
||||||
|
|||||||
@@ -77,8 +77,9 @@ impl BaseModule {
|
|||||||
add_function(&mut functions, &type_map, "rectangle", &[F32, F32, F32, F32, I32], None);
|
add_function(&mut functions, &type_map, "rectangle", &[F32, F32, F32, F32, I32], None);
|
||||||
add_function(&mut functions, &type_map, "circle", &[F32, F32, F32, I32], None);
|
add_function(&mut functions, &type_map, "circle", &[F32, F32, F32, I32], None);
|
||||||
|
|
||||||
add_function(&mut functions, &type_map, "time", &[], Some(I32));
|
add_function(&mut functions, &type_map, "time", &[], Some(F32));
|
||||||
add_function(&mut functions, &type_map, "ftime", &[], Some(F32));
|
add_function(&mut functions, &type_map, "isButtonPressed", &[I32], Some(I32));
|
||||||
|
add_function(&mut functions, &type_map, "isButtonTriggered", &[I32], Some(I32));
|
||||||
|
|
||||||
for i in functions.len()..64 {
|
for i in functions.len()..64 {
|
||||||
add_function(
|
add_function(
|
||||||
|
|||||||
Reference in New Issue
Block a user