Skip to content

Commit 52306ac

Browse files
committed
feat(example): add WASM example
1 parent b544b2f commit 52306ac

File tree

8 files changed

+126
-115
lines changed

8 files changed

+126
-115
lines changed

.github/renovate.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
3+
"extends": [
4+
"config:base",
5+
":automergeAll",
6+
":automergeBranch"
7+
]
8+
}

.github/workflows/rust.yml

Lines changed: 71 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -10,118 +10,112 @@ on:
1010
- "**.md"
1111

1212
jobs:
13-
# Run the `rustfmt` code formatter
1413
rustfmt:
15-
name: Rustfmt [Formatter]
14+
name: Formatter check
1615
runs-on: ubuntu-latest
1716
steps:
18-
- uses: actions/checkout@v1
17+
- uses: actions/checkout@v3
18+
1919
- uses: actions-rs/toolchain@v1
2020
with:
2121
profile: minimal
2222
toolchain: stable
2323
components: rustfmt
2424
override: true
25+
2526
- run: rustup component add rustfmt
2627
- uses: actions-rs/cargo@v1
2728
with:
2829
command: fmt
2930
args: --all -- --check
3031

31-
# Run the `clippy` linting tool
32-
clippy:
33-
name: Clippy [Linter]
34-
runs-on: ubuntu-latest
32+
# Run compile check on Linux, macOS, and Windows
33+
# On both Rust stable and Rust nightly
34+
compile:
35+
name: Compile
36+
runs-on: ${{ matrix.os }}
37+
strategy:
38+
fail-fast: true
39+
matrix:
40+
os: [ubuntu-latest, macOS-latest, windows-latest]
41+
toolchain: [stable, nightly]
3542
steps:
36-
- uses: actions/checkout@v1
37-
- uses: actions-rs/toolchain@v1
38-
with:
39-
profile: minimal
40-
toolchain: stable
41-
components: clippy
42-
override: true
43-
- uses: actions-rs/clippy-check@v1
44-
with:
45-
token: ${{ secrets.GITHUB_TOKEN }}
46-
args: --all-targets --all-features -- -D clippy::all
43+
# Checkout the branch being tested
44+
- uses: actions/checkout@v3
4745

48-
# Run a security audit on dependencies
49-
cargo_audit:
50-
name: Cargo Audit [Security]
51-
runs-on: ubuntu-latest
52-
steps:
53-
- uses: actions/checkout@v1
54-
- uses: actions-rs/toolchain@v1
46+
# Install rust stable
47+
- uses: dtolnay/rust-toolchain@master
5548
with:
56-
toolchain: stable
57-
override: true
58-
- run: cargo install --force cargo-audit
59-
- run: cargo generate-lockfile
60-
- uses: actions-rs/cargo@v1
61-
with:
62-
command: audit
49+
toolchain: ${{ matrix.toolchain }}
6350

64-
# Ensure that the project could be successfully compiled
65-
cargo_check:
66-
name: Compile
67-
runs-on: ubuntu-latest
68-
steps:
69-
- uses: actions/checkout@v1
70-
- uses: actions-rs/toolchain@v1
51+
# Cache the built dependencies
52+
- uses: Swatinem/[email protected]
7153
with:
72-
profile: minimal
73-
toolchain: stable
74-
override: true
75-
- uses: actions-rs/cargo@v1
76-
with:
77-
command: check
78-
args: --all
54+
save-if: ${{ github.event_name == 'push' }}
55+
56+
# Install cargo-hack
57+
- uses: taiki-e/install-action@cargo-hack
58+
59+
# Compile all feature combinations on the target platform
60+
- name: Compile
61+
run: cargo hack --feature-powerset check
7962

80-
# Run tests on Linux, macOS, and Windows
63+
# Run tests on Linux
8164
# On both Rust stable and Rust nightly
8265
test:
83-
name: Test Suite
84-
needs: [cargo_check]
85-
runs-on: ${{ matrix.os }}
66+
name: Test
67+
runs-on: ubuntu-latest
8668
strategy:
8769
fail-fast: false
8870
matrix:
89-
os: [ubuntu-latest, macOS-latest, windows-latest]
90-
rust: [stable, nightly]
71+
toolchain: [stable, nightly]
9172
steps:
9273
# Checkout the branch being tested
93-
- uses: actions/checkout@v1
74+
- uses: actions/checkout@v3
9475

95-
# Install all the required dependencies for testing
96-
- uses: actions-rs/toolchain@v1
76+
# Install rust stable
77+
- uses: dtolnay/rust-toolchain@master
9778
with:
98-
profile: minimal
99-
toolchain: stable
100-
override: true
79+
toolchain: ${{ matrix.toolchain }}
10180

102-
# Install Node.js at a fixed version
103-
- uses: actions/setup-node@v1
81+
# Cache the built dependencies
82+
- uses: Swatinem/[email protected]
10483
with:
105-
node-version: "12.0"
84+
save-if: ${{ github.event_name == 'push' }}
10685

107-
# Install Ruby at a fixed version
108-
- uses: actions/setup-ruby@v1
109-
with:
110-
ruby-version: "2.6"
86+
# Install cargo-hack
87+
- uses: taiki-e/install-action@cargo-hack
11188

112-
# Install Python at a fixed version
113-
- uses: actions/setup-python@v1
114-
with:
115-
python-version: "3.7"
89+
# Run the ignored tests that expect the above setup
90+
- name: Run all tests
91+
run: cargo hack --feature-powerset test
11692

117-
# Install dotnet at a fixed version
118-
- uses: actions/setup-dotnet@v1
93+
wasm:
94+
name: WASM examples
95+
runs-on: ubuntu-latest
96+
steps:
97+
- uses: actions/checkout@v3
98+
99+
# Install rust stable
100+
- uses: dtolnay/rust-toolchain@master
119101
with:
120-
dotnet-version: "2.2.402"
102+
toolchain: stable
103+
targets: wasm32-unknown-unknown
121104

122-
# Run the ignored tests that expect the above setup
123-
- name: Run all tests
124-
uses: actions-rs/cargo@v1
105+
# Cache the built dependencies
106+
- uses: Swatinem/[email protected]
125107
with:
126-
command: test
127-
args: -- -Z unstable-options --include-ignored
108+
save-if: ${{ github.event_name == 'push' }}
109+
110+
# Build the WASM examples
111+
- name: Build example
112+
run: cargo run --package run-wasm -- --release --build-only --example window
113+
114+
# Deploy to GitHub pages
115+
- name: Deploy to GitHub Pages
116+
uses: s0/git-publish-subdir-action@master
117+
env:
118+
REPO: self
119+
BRANCH: gh-pages
120+
FOLDER: target/wasm-examples
121+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ web-sys = { version = "0.3.61", features = ["CanvasRenderingContext2d", "Documen
3737
wasm-bindgen = "0.2.84"
3838
console_log = { version = "1.0.0", features = ["wasm-bindgen", "color"] }
3939

40+
[[example]]
41+
name = "window"
42+
required-features = ["blit"]
43+
4044
[[bench]]
4145
name = "rotsprite"
4246
harness = false
@@ -48,3 +52,6 @@ harness = false
4852
[[bench]]
4953
name = "rotate"
5054
harness = false
55+
56+
[workspace]
57+
members = ["run-wasm"]

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Rust implementation/library of the RotSprite algorithm.
99

1010
Works with many types of pixel buffers.
1111

12-
## Example
1312

1413
![Large](docs/example-large.png?raw=true)
1514
![Small](docs/example-small.png?raw=true)
@@ -18,12 +17,20 @@ Works with many types of pixel buffers.
1817
|-|-|-|
1918
| Source Image | Rotated 30° using RotSprite | Rotated 30° using naive rotation |
2019

21-
### Run the example
20+
## Example
21+
22+
## Demos
23+
24+
### [WASM Demo](https://tversteeg.nl/rotsprite/window/)
25+
26+
Web: https://tversteeg.nl/rotsprite/window
27+
28+
Uses the `["blit"]` feature flag.
2229

23-
On Linux you need the `xorg-dev` package as required by minifb. `sudo apt install xorg-dev`
30+
#### Local
2431

25-
```sh
26-
cargo run --example minifb
32+
```console
33+
cargo run --example window
2734
```
2835

2936
## Credits

benches/rotsprite.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use blit::*;
1+
use blit::BlitExt;
22
use criterion::{criterion_group, criterion_main, Criterion};
3-
use image::*;
3+
use image::GenericImageView;
44
use rotsprite::rotsprite;
55

66
fn load_image(path: &str) -> (usize, Vec<u32>) {
@@ -10,14 +10,9 @@ fn load_image(path: &str) -> (usize, Vec<u32>) {
1010
let size = img.dimensions();
1111
// Create a new buffer for this image that can be passed to the rotate function
1212
let mut img_buf: Vec<u32> = vec![0xFF_FF_FF; (size.0 * size.1) as usize];
13-
img.as_rgba8()
14-
.expect("Could not convert image to RGBA8 array")
15-
.blit(
16-
&mut img_buf,
17-
size.0 as usize,
18-
(0, 0),
19-
blit::Color::from_u32(0xFF_00_FF),
20-
);
13+
img.into_rgba8()
14+
.to_blit_buffer_with_mask_color(0xFF_00_FF)
15+
.blit(&mut img_buf, size.0 as usize, (0, 0));
2116

2217
(size.0 as usize, img_buf)
2318
}

examples/simple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use image::{GenericImageView, Rgba, RgbaImage};
1+
use image::{Rgba, RgbaImage};
22
use rotsprite::rotsprite;
33

44
fn main() {

examples/window.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
use blit::{BlitBuffer, BlitExt};
2-
use image::GenericImageView;
1+
use blit::BlitExt;
32
use rotsprite::Rotsprite;
43
use softbuffer::GraphicsContext;
4+
use web_time::SystemTime;
55
use winit::{
66
event::{Event, WindowEvent},
77
event_loop::{ControlFlow, EventLoop},
88
window::WindowBuilder,
99
};
1010

11-
use std::time::Duration;
12-
1311
const BACKGROUND_COLOR: u32 = 0xFF_CC_FF;
1412
const MASK_COLOR: u32 = 0xFF_FF_FF;
1513

@@ -45,22 +43,30 @@ fn main() {
4543
let width = window.inner_size().width as usize;
4644
let height = window.inner_size().height as usize;
4745

48-
// Clear the buffer first
49-
buffer.fill(BACKGROUND_COLOR);
46+
// Redraw the rotation every 5 steps
47+
if rotation % 15.0 == 0.0 {
48+
// Clear the buffer first
49+
buffer.fill(BACKGROUND_COLOR);
5050

51-
// Redraw the whole buffer if it resized
52-
if buffer.len() != width * height {
53-
log::info!("Buffer resized to {width}x{height}, redrawing");
51+
// Redraw the whole buffer if it resized
52+
if buffer.len() != width * height {
53+
log::info!("Buffer resized to {width}x{height}, redrawing");
5454

55-
// Resize the buffer with empty values
56-
buffer.resize(width * height, BACKGROUND_COLOR);
57-
}
55+
// Resize the buffer with empty values
56+
buffer.resize(width * height, BACKGROUND_COLOR);
57+
}
58+
59+
let now = SystemTime::now();
5860

59-
// Rotate the sprite
60-
let rotated_blit_buffer = img.rotsprite((rotation / 15.0).round() * 15.0).unwrap();
61+
// Rotate the sprite
62+
let rotated_blit_buffer =
63+
img.rotsprite((rotation / 15.0).round() * 15.0).unwrap();
6164

62-
// Draw the rotated sprite
63-
rotated_blit_buffer.blit(&mut buffer, width, (0, 0));
65+
log::info!("Rotated sprite in {}ms", now.elapsed().unwrap().as_millis());
66+
67+
// Draw the rotated sprite
68+
rotated_blit_buffer.blit(&mut buffer, width, (0, 0));
69+
}
6470

6571
rotation += 0.5;
6672

renovate.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)