include rust
This commit is contained in:
parent
86b8c7f899
commit
c62e9fa15c
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
**/*.rs.bk
|
|
@ -0,0 +1,20 @@
|
|||
[package]
|
||||
name = "webgl"
|
||||
version = "0.1.0"
|
||||
authors = ["Magnus Ulimoen <flymagnus@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[features]
|
||||
default = ["console_error_panic_hook", "wee_alloc"]
|
||||
|
||||
[dependencies]
|
||||
wasm-bindgen = "*"
|
||||
console_error_panic_hook = { version = "*", optional = true }
|
||||
wee_alloc = { version = "*", optional = true }
|
||||
|
||||
[profile.release]
|
||||
opt-level = "s"
|
||||
lto = "full"
|
|
@ -11,5 +11,5 @@
|
|||
<canvas id="glCanvas"></canvas>
|
||||
</body>
|
||||
<link rel="stylesheet" type="text/css" href="style.css">
|
||||
<script src="main.js"></script>
|
||||
<script async type="module" src="main.js"></script>
|
||||
</html>
|
||||
|
|
16
main.js
16
main.js
|
@ -1,8 +1,8 @@
|
|||
"use strict";
|
||||
import { Universe, default as init } from "./webgl.js";
|
||||
|
||||
main();
|
||||
async function run() {
|
||||
let wasm = await init("./webgl_bg.wasm");
|
||||
|
||||
function main() {
|
||||
const canvas = document.getElementById("glCanvas");
|
||||
|
||||
const gl = canvas.getContext("webgl");
|
||||
|
@ -82,6 +82,8 @@ function main() {
|
|||
|
||||
const width = 4;
|
||||
const height = 5;
|
||||
let universe = Universe.new(width, height);
|
||||
/*
|
||||
const field = new Uint8Array(width*height);
|
||||
for (let i = 0; i < height; i += 1) {
|
||||
for (let j = 0; j < width; j += 1) {
|
||||
|
@ -91,6 +93,12 @@ function main() {
|
|||
field[i*width + j] = Math.floor(Math.random() * (255 + 1));
|
||||
}
|
||||
}
|
||||
*/
|
||||
universe.set_something();
|
||||
const field = new Uint8Array(wasm.memory.buffer, universe.get_field(), width*height);
|
||||
console.log(field);
|
||||
console.log(wasm.memory.buffer);
|
||||
console.log(universe.get_field());
|
||||
|
||||
const texture = gl.createTexture();
|
||||
{
|
||||
|
@ -156,3 +164,5 @@ function main() {
|
|||
window.addEventListener('resize', resizeCanvas, false);
|
||||
resizeCanvas();
|
||||
}
|
||||
|
||||
run();
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
#! /usr/bin/env python3
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from subprocess import call
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = ArgumentParser(description="Build js and wasm")
|
||||
parser.add_argument("-r", help="Build release type",
|
||||
dest="release", action="store_true")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.release:
|
||||
call(["cargo", "build", "--release",
|
||||
"--target", "wasm32-unknown-unknown"])
|
||||
target = "target/wasm32-unknown-unknown/release/webgl.wasm"
|
||||
else:
|
||||
call(["cargo", "build",
|
||||
"--target", "wasm32-unknown-unknown"])
|
||||
target = "target/wasm32-unknown-unknown/debug/webgl.wasm"
|
||||
|
||||
call(["wasm-bindgen", target, "--out-dir", ".",
|
||||
"--no-typescript", "--target", "web"])
|
|
@ -0,0 +1,43 @@
|
|||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[cfg(feature = "wee_alloc")]
|
||||
#[global_allocator]
|
||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||
|
||||
pub fn set_panic_hook() {
|
||||
#[cfg(feature = "console_error_panic_hook")]
|
||||
console_error_panic_hook::set_once();
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct Universe {
|
||||
width: u32,
|
||||
height: u32,
|
||||
field: Vec<u8>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl Universe {
|
||||
pub fn new(width: u32, height: u32) -> Self {
|
||||
set_panic_hook();
|
||||
|
||||
Universe {
|
||||
width,
|
||||
height,
|
||||
field: vec![0u8; width as usize * height as usize],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_something(&mut self) {
|
||||
for j in 0..self.height {
|
||||
for i in 0..self.width {
|
||||
self.field[(self.width * j + i) as usize] =
|
||||
((10 * i + 100 * j) % (u8::max_value() as u32)) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_field(&mut self) -> *mut u8 {
|
||||
self.field.as_mut_ptr()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue