wasm_rgame/
lib.rs

1//! # wasm-rgame
2//! A Rust framework for building browser games with WASM.
3//!
4//! This framework was inspired by the [Game of Life Rust + WASM Tutorial](https://rust-lang-nursery.github.io/rust-wasm/game-of-life/introduction.html). You can dive right in and start writing a wasm-rgame application with the [tutorial](TODO link tutorial)!
5//!
6//! ### How It Works
7//! This framework abstracts away writing custom HTML/Javascript and provides Rust types that interface with the Javascript bundled in the  [wasm-rgame-js](https://github.com/DarrenTsung/wasm-rgame-js) respository.
8//!
9//! The Rust API provides types for:
10//! * Keyboard events
11//! * Mouse position and events
12//! * Rendering API to a 2D canvas
13//! * Spawning new objects in the Application
14//!
15//! Also, a build tool ([wasm-rgame-tools](https://github.com/DarrenTsung/wasm-rgame-tools)) is provided that builds the project (targeting wasm32-unknown-unknown), runs wasm-bindgen, and bundles together the generated WASM binaries and Javascript/HTML.
16//!
17//! ### Goals
18//! This project is mainly experimental, but has been used to make non-trivial applications (see [wrg-snake](https://github.com/DarrenTsung/wrg-snake)).
19//!
20//! * Users only need to write Rust.
21//! * Minimal calls between Rust / Javascript.
22//!
23//! ### Example API Usage
24//! These examples are taken from [wrg-snake](https://github.com/DarrenTsung/wrg-snake). Also note that these examples can't be run stand-alone as they require the Javascript/HTML framework.
25//!
26//! Rendering a transparent overlay:
27//! ```rust,ignore
28//! use wasm_rgame::{Delegate, Graphics, Canvas};
29//!
30//! impl Delegate for MyObject {
31//!     fn tick(..) {}
32//!
33//!     fn render(&self, graphics: &mut Graphics) {
34//!         let canvas = Canvas::instance();
35//!         // draw a transparent overlay over the game
36//!         graphics.draw_rect(
37//!             0.0,
38//!             0.0,
39//!             canvas.width() as f32,
40//!             canvas.height() as f32,
41//!             [255, 255, 255, 150]
42//!         );
43//!     }
44//! }
45//! ```
46//!
47//! Checking keyboard input:
48//! ```rust,ignore
49//! use wasm_rgame::{KeyManager, key_codes};
50//!
51//! pub fn store_direction_change(&mut self, key_manager: &KeyManager) {
52//!     // Don't let direction change if already going opposite direction
53//!     let wanted_direction = if key_manager.key_down(key_codes::W) {
54//!         Direction::Up
55//!     } else if key_manager.key_down(key_codes::D) {
56//!         Direction::Right
57//!     } else if key_manager.key_down(key_codes::S) {
58//!         Direction::Down
59//!     } else if key_manager.key_down(key_codes::A) {
60//!         Direction::Left
61//!     } else {
62//!         return;
63//!     };
64//!
65//!     ...
66//! }
67//! ```
68
69#[macro_use] extern crate lazy_static;
70
71extern crate dmsort;
72extern crate raii_counter;
73
74mod application;
75mod canvas;
76mod graphics;
77pub mod delegate_prelude;
78
79pub use application::{
80    Application,
81    ApplicationContext,
82
83    DelegateSpawner,
84    Delegate,
85    SpawnableDelegate,
86    SpawnHandle,
87    SpawnHandles,
88
89    KeyManager,
90    key_codes,
91
92    MouseState,
93    MouseButton,
94};
95
96pub use canvas::Canvas;
97
98pub use graphics::{
99    Graphics,
100    Color,
101};