twors 0.2.0

A simple Rust 2D game engine based on canvas and WASM
Documentation

TwoRS

development crates.io

Easily render 2D graphics in a canvas - entirely powered by Rust!!! 🦀

😎 Why Rust/WASM?

It's use case is either the reuse of code written in another language or to offload heavy computations to the near-native execution speeds of WASM.

However this is not free - traversing the WASM boundary means going trough some glue code and copying data from JS to the WASM module memory and back. In the case of strings it's even more expensive - because JS strings are UTF-16 and Rust strings use UTF-8 any string passtrough needs to go trough a copy AND a re-encode.

twors doesn't have any complicated physics to offload to WASM at this point in time - neither is it making use of some advanced pre-existing Rust library. So why Rust then?

Simply because Rust is the best!!!

⚡ Quick start

The examples/playground crate in this repo contains basic code for a moving rectangle and some input handling - there are two ways to run it:

  • Experiment by modifying examples/playground and running the code:

    cargo install cargo-make # only necessary once
    
    # compile, build the WASM module, run a HTTP server - rinse and repeat automatically on code changes
    cargo make watch
    
  • Set up your own crate similarly to examples/playground example by visiting the quick start guide

📚 Docs

  • Refer to the docs after you have a working setup to experiment with

📝 Roadmap

  • Adaptive canvas resolution on resize
  • Delta time
  • Keyboard and mouse input handling
  • Component system (with init() and update() lifecycle methods)
  • Transform (with inheritance) - translate, scale
  • Collision detection
  • Bezier curves
  • FPS benchmark

💻 Development

Experiments and manual testing are to be done in the examples/playground crate.

# convenience scripts - see "Makefile.toml" for full list of commands
cargo install cargo-make

# run local pre-commit checks - will be run on "build" automatically
cargo make install-git-hooks

cargo make build # build the "playground" crate as a WASM module
cargo make serve # like "build", but will also start a HTTP server
cargo make watch # like "serve", but will restart the server on changes

# other commands
cargo make test
cargo make clean
cargo make format
cargo make licenses # update licenses.html (run after dependency addition/removal)

Notes

  • Make sure to use wasm_assert! instead of assert! in non-test code to see error messages in the browser console.