Skip to content

Commit

Permalink
Add vtf package
Browse files Browse the repository at this point in the history
  • Loading branch information
cooolbros committed Dec 10, 2024
1 parent 6a35078 commit 791106c
Show file tree
Hide file tree
Showing 9 changed files with 676 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
dist
node_modules
out
target
tsconfig.tsbuildinfo
206 changes: 206 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]
members = ["packages/vtf"]
resolver = "2"

[profile.release]
opt-level = 3
13 changes: 13 additions & 0 deletions packages/vtf/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "vtf"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
bincode = { version = "2.0.0-rc.3", features = ["derive", "serde"] }
texpresso = "2.0.1"
wasm-bindgen = "0.2.95"
web-sys = { version = "0.3.74", features = ["CanvasRenderingContext2d", "ImageData"] }
12 changes: 12 additions & 0 deletions packages/vtf/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "vtf",
"main": "./pkg/vtf.js",
"types": "./pkg/vtf.d.ts",
"files": [
"pkg"
],
"scripts": {
"dev": "cargo watch -- wasm-pack build --dev --target web",
"build": "wasm-pack build --release --target web"
}
}
1 change: 1 addition & 0 deletions packages/vtf/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_width = 180
16 changes: 16 additions & 0 deletions packages/vtf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use vtf::{VTFData, VTFError, VTF};
use wasm_bindgen::{prelude::wasm_bindgen, Clamped};
use web_sys::{CanvasRenderingContext2d, ImageData};

pub mod vtf;

#[wasm_bindgen]
impl VTF {
#[wasm_bindgen(js_name = "putImageData")]
pub fn put_image_data(self, context: &CanvasRenderingContext2d, mipmap_index: usize, frame_index: usize) -> Result<(), VTFError> {
let VTFData { width, height, rgba } = self.extract(mipmap_index, frame_index)?;
let data = ImageData::new_with_u8_clamped_array_and_sh(Clamped(&rgba), width as u32, height as u32).unwrap();
context.put_image_data(&data, 0.0, 0.0).unwrap();
Ok(())
}
}
Loading

0 comments on commit 791106c

Please sign in to comment.