Browse Source

Got a basic image drawing program working

Thomas Johnson 7 months ago
commit
881dd4a55e
7 changed files with 119 additions and 0 deletions
  1. 4
    0
      .cargo/config
  2. 2
    0
      .gitignore
  3. 14
    0
      Cargo.toml
  4. 8
    0
      index.html
  5. 38
    0
      index.js
  6. 52
    0
      src/lib.rs
  7. 1
    0
      wt.wasm

+ 4
- 0
.cargo/config View File

@@ -0,0 +1,4 @@
1
+
2
+[build]
3
+target = "wasm32-unknown-unknown"
4
+

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+/target
2
+Cargo.lock

+ 14
- 0
Cargo.toml View File

@@ -0,0 +1,14 @@
1
+[package]
2
+name = "wt"
3
+version = "0.1.0"
4
+authors = ["thajohns <thajohns@clarkson.edu>"]
5
+edition = "2018"
6
+
7
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
+
9
+[dependencies]
10
+wee_alloc = "0.4.5"
11
+#wasm-bindgen = "0.2.59"
12
+
13
+[lib]
14
+crate-type = ["cdylib"]

+ 8
- 0
index.html View File

@@ -0,0 +1,8 @@
1
+<html>
2
+  <head>
3
+    <script defer src="index.js"></script>
4
+  </head>
5
+  <body id="body">
6
+    <canvas id="canvas" height=1000 width=1500></canvas>
7
+  </body>
8
+</html>

+ 38
- 0
index.js View File

@@ -0,0 +1,38 @@
1
+canvas = document.getElementById("canvas");
2
+ctx = canvas.getContext("2d");
3
+
4
+function get_string(memory, pointer, size)
5
+{
6
+  var array = new Int8Array(memory.buffer, pointer, size);
7
+  return (new TextDecoder()).decode(array);
8
+}
9
+
10
+function paint(memory, pointer, width, height)
11
+{
12
+  array = new Uint8ClampedArray(memory.buffer, pointer, 4 * width * height);
13
+  data = new ImageData(array, width, height);
14
+  ctx.putImageData(data, 0, 0);
15
+}
16
+
17
+fetch("wt.wasm").then(response => response.arrayBuffer()).then(bytes =>
18
+  {
19
+    var memory;
20
+    WebAssembly.instantiate(bytes, { env:
21
+      {
22
+        console_log: function(str, len)
23
+        {
24
+          console.log(get_string(memory, str, len));
25
+        },
26
+        extern_paint: function(pointer, width, height)
27
+        {
28
+          paint(memory, pointer, width, height);
29
+        }
30
+      } }).then(mod =>
31
+      {
32
+        memory = mod.instance.exports.memory;
33
+        mod.instance.exports.entrypoint();
34
+        mod.instance.exports.paint_red(canvas.width, canvas.height);
35
+      }
36
+    )
37
+  });
38
+

+ 52
- 0
src/lib.rs View File

@@ -0,0 +1,52 @@
1
+
2
+extern crate wee_alloc;
3
+
4
+use core::mem::transmute;
5
+
6
+#[global_allocator]
7
+pub static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
8
+
9
+extern "C"
10
+{
11
+  #[no_mangle]
12
+  fn console_log(_: *const u8, _: usize);
13
+  #[no_mangle]
14
+  fn extern_paint(ptr: *const u8, w: usize, h: usize);
15
+}
16
+
17
+fn log(string: &str)
18
+{
19
+  unsafe
20
+  {
21
+    let (ptr, size) = transmute::<_, (*const u8, usize)>(string);
22
+    console_log(ptr, size);
23
+  }
24
+}
25
+
26
+fn paint(buffer: &[u8], w: usize, h: usize)
27
+{
28
+  unsafe { extern_paint(buffer.as_ptr(), w, h); }
29
+}
30
+
31
+#[no_mangle]
32
+pub extern "C" fn entrypoint()
33
+{
34
+  log("Test from Rust");
35
+}
36
+
37
+#[no_mangle]
38
+pub extern "C" fn paint_red(w: usize, h: usize)
39
+{
40
+  let mut buffer = vec![0; 4 * w * h];
41
+  let mut c: (u8, u8, u8) = (0, 0, 0);
42
+  for ii in 0 .. w * h
43
+  {
44
+    buffer[4 * ii] = c.0;
45
+    buffer[4 * ii + 1] = c.1;
46
+    buffer[4 * ii + 2] = c.2;
47
+    buffer[4 * ii + 3] = 255;
48
+    c = (c.0.wrapping_add(1), c.1.wrapping_add(254), c.2.wrapping_add(2));
49
+  }
50
+  paint(&buffer, w, h);
51
+}
52
+

+ 1
- 0
wt.wasm View File

@@ -0,0 +1 @@
1
+/home/primary/rust/dabble/webasm/target/wasm32-unknown-unknown/debug/wt.wasm

Loading…
Cancel
Save