1
Fork 0

got keyboard input working by using a static buffer (hack)

todo: remove code bloat
This commit is contained in:
Andy Killorin 2023-09-13 01:49:33 -05:00
parent b56934f0a5
commit 92f801df31
Signed by: ank
GPG key ID: B6241CA3B552BCA4
5 changed files with 273 additions and 18 deletions

View file

@ -5,7 +5,7 @@
</script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<body id="body">
<canvas id="window"></canvas>
</body>
</html>

View file

@ -1,6 +1,7 @@
var ctx;
var image;
var memory;
var exports;
const width = 160;
const height = 144;
@ -15,6 +16,18 @@ function blit_text(text, len, x, y, size) {
ctx.fillText(decoded,x,y);
}
function keyboard_callback(e) {
const keycode_address = exports.KEYCODE.value;
const value = new Uint8ClampedArray(exports.memory.buffer, keycode_address, 2);
value[0] = e.keyCode;
console.log("thing" + value[0]);
console.log(keycode_address);
console.log(value[0]);
exports.keyboard_input();
}
async function init() {
const canvas = document.getElementById("window");
canvas.width = width;
@ -33,6 +46,9 @@ async function init() {
}
);
exports = instance.exports;
document.getElementById("body").onkeydown=keyboard_callback;
memory = instance.exports.memory
const buffer_address = instance.exports.BUFFER.value;
image = new ImageData(
@ -48,16 +64,11 @@ async function init() {
ctx.textBaseline = 'top'
ctx.textAlign = 'left';
var frame = 1;
const render = () => {
frame += 1;
console.log(frame);
instance.exports.frame_entry();
requestAnimationFrame(render);
}
canvas.onclick = render;
render();
}

180
pirates/Cargo.lock generated
View file

@ -2,6 +2,12 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]]
name = "approx"
version = "0.5.1"
@ -11,18 +17,81 @@ dependencies = [
"num-traits",
]
[[package]]
name = "arraydeque"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0ffd3d69bd89910509a5d31d1f1353f38ccffdd116dd0099bbd6627f7bd8ad8"
[[package]]
name = "arrayvec"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9"
dependencies = [
"nodrop",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "keycode"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b07873c3182aec8a0eb1a5a4e7b197d42e9d167ba78497a6ee932a82d94673ed"
dependencies = [
"arraydeque",
"arrayvec",
"bitflags",
"keycode_macro",
]
[[package]]
name = "keycode_macro"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e521ea802f5b3c7194e169d75cab431b0ff08d022f2b6047b08754b4988b89df"
dependencies = [
"anyhow",
"heck",
"proc-macro2",
"quote",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
dependencies = [
"spin 0.5.2",
]
[[package]]
name = "libc"
version = "0.2.147"
@ -35,6 +104,16 @@ version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4"
[[package]]
name = "lock_api"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "memory_units"
version = "0.4.0"
@ -55,6 +134,12 @@ dependencies = [
"typenum",
]
[[package]]
name = "nodrop"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
[[package]]
name = "num-complex"
version = "0.4.4"
@ -101,15 +186,63 @@ version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c"
[[package]]
name = "pin-project"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fda4ed1c6c173e3fc7a83629421152e01d7b1f9b7f65fb301e490e8cfc656422"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "pirates"
version = "0.1.0"
dependencies = [
"keycode",
"lazy_static",
"libm",
"nalgebra",
"spin 0.9.8",
"thingbuf",
"wee_alloc",
]
[[package]]
name = "proc-macro2"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "simba"
version = "0.8.1"
@ -122,12 +255,59 @@ dependencies = [
"paste",
]
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
dependencies = [
"lock_api",
]
[[package]]
name = "syn"
version = "2.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thingbuf"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4706f1bfb859af03f099ada2de3cea3e515843c2d3e93b7893f16d94a37f9415"
dependencies = [
"pin-project",
]
[[package]]
name = "typenum"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]]
name = "unicode-ident"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
[[package]]
name = "unicode-segmentation"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
[[package]]
name = "wee_alloc"
version = "0.4.5"

View file

@ -11,10 +11,21 @@ opt-level = 's'
crate-type = ["cdylib"]
[dependencies]
keycode = "0.4.0"
libm = "0.2.7"
spin = "0.9.8"
wee_alloc = "0.4.5"
[dependencies.nalgebra]
version = "0.32.3"
default-features = false
features = ["libm"]
[dependencies.thingbuf]
version = "0.1.4"
default-features = false
features = ["static"]
[dependencies.lazy_static]
version = "1.4.0"
features = ["spin_no_std"]

View file

@ -9,10 +9,15 @@ extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
use lazy_static::lazy_static;
use noise::PerlinBuf;
use core::sync::atomic::{AtomicU32, Ordering};
use libm::{self, Libm};
extern crate nalgebra as na;
use nalgebra::{Vector2};
use thingbuf::mpsc::{self, errors::TryRecvError};
use spin::Mutex;
use keycode::KeyMap;
mod sampler;
mod noise;
@ -37,6 +42,12 @@ const HEIGHT: usize = 144;
#[no_mangle]
static mut BUFFER: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT];
// hack, js -> rust ffi in weird shape without wasm_bindgen
#[no_mangle]
static mut KEYCODE: [u8; 2] = [0; 2];
static LAST_KEY: Mutex<Option<[u32; 2]>> = Mutex::new(None);
static FRAME: AtomicU32 = AtomicU32::new(0);
static mut RAND: noise::PerlinBuf = [0; 512];
@ -56,6 +67,14 @@ static MAP: [u8; MAP_WIDTH * MAP_HEIGHT] = [ // should deflate to smaller than b
1,1,1,1,1,1,1,1,1,1,1,1,
];
static CAMERA: Mutex<[f32; 3]> = Mutex::new([0.0, 0.0, 4.0]);
#[no_mangle]
pub unsafe extern fn keyboard_input() {
let keycode = KEYCODE[0];
LAST_KEY.lock().insert([keycode as u32, 0]);
}
#[no_mangle]
pub unsafe extern fn frame_entry() {
// calling from multiple threads is ub
@ -74,20 +93,32 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
RAND
};
let mut camera = CAMERA.lock();
//camera[0] += 1.0;
if let Some(key) = LAST_KEY.lock().take() {
match key[0] {
38 => camera[1] -= 10.0, // up
40 => camera[1] += 10.0, // down
37 => camera[0] -= 10.0, // left
39 => camera[0] += 10.0, // right
61 => camera[2] *= 0.9, // +
173 => camera[2] *= 1.1, // -
_ => {}
}
}
let camera_vec = Vector2::new(camera[0],camera[1]);
const half: Vector2<f32> = Vector2::new(WIDTH as f32 / 2.0, HEIGHT as f32 / 2.0);
for y in 0..HEIGHT {
for x in 0..WIDTH {
let point = Vector2::new(x as f32, y as f32)* 4.0;
let offset = Vector2::new(64120.0, 7320.0);
let point = point + offset;
let mut n = 0.0;
n += (sampler::sample_map_inter(point / 64.0, &MAP)-0.5)* 0.6;
n += noise::noise(point / 64.0, rand) / 1.0;
n += noise::noise(point / 32.0, rand) / 2.0;
n += noise::noise(point / 16.0, rand) / 4.0;
n += noise::noise(point / 8.0, rand) / 8.0;
n += noise::noise(point / 4.0, rand) / 16.0;
n += noise::noise(point / 2.0, rand) / 32.0;
//buffer[y*WIDTH + x] = (((n*0.5+0.5)*256.0) as u32) << 16| 0xFF005000;
let mut point = Vector2::new(x as f32, y as f32);
point -= half;
point *= camera[2];
point += half;
let n = sample_world(point+camera_vec, rand);
buffer[y*WIDTH + x] =
if n > 0.1 {
0xFF00FF00
@ -105,6 +136,28 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
//draw_text("hi from rust", 0,100,30);
}
fn sample_world(point: Vector2<f32>, rand: PerlinBuf) -> f32 {
let offset = Vector2::new(64480.0, 7870.0);
//240.0,240.0
let point = point + offset;
let mut n = 0.0;
n += (sampler::sample_map_inter(point / 64.0, &MAP)-0.5)* 0.6;
n += noise::noise(point / 64.0, rand) / 1.0;
n += noise::noise(point / 32.0, rand) / 2.0;
n += noise::noise(point / 16.0, rand) / 4.0;
n += noise::noise(point / 8.0, rand) / 8.0;
n += noise::noise(point / 4.0, rand) / 16.0;
n += noise::noise(point / 2.0, rand) / 32.0;
n
}
#[repr(u32)]
enum Keys {
Up = 38,
Down = 40,
Left = 37,
Right = 39
}
#[cfg(test)]
mod tests {