Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web support #63

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
Cargo.lock
.cargo
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ before_install:
if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
sudo apt-get -qq update
sudo apt-get install -y libudev-dev
rustup target add wasm32-unknown-unknown
fi
- |
if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
Expand All @@ -30,5 +31,6 @@ matrix:
script:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then chmod +x spirvtest && ./spirvtest; fi
#- cargo test --verbose --features opengl
#- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then cargo test --verbose --features vulkan --release; fi
- cargo test --verbose
#- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then cargo test --verbose --features vulkan --release; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then cargo test --verbose --features web --target wasm32-unknown-unknown --no-run; fi
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ vulkan = ["wgpu", "wgpu/vulkan", "wgpu_glyph"]
metal = ["wgpu", "wgpu/metal", "wgpu_glyph"]
dx11 = ["wgpu", "wgpu/dx11", "wgpu_glyph"]
dx12 = ["wgpu", "wgpu/dx12", "wgpu_glyph"]
web = ["wgpu", "wgpu/web", "wgpu_glyph", "instant/wasm-bindgen", "instant/now"]
empty = ["wgpu", "wgpu_glyph"]
debug = []

Expand All @@ -34,8 +35,10 @@ nalgebra = "0.18"
rayon = "1.0"
stretch = "0.2"
twox-hash = "1.3"
seahash = "3.0"
lyon_tessellation = "0.13"
gilrs = "0.7"
instant = "0.1"

# gfx (OpenGL)
gfx = { version = "0.18", optional = true }
Expand All @@ -52,7 +55,13 @@ wgpu_glyph = { version = "0.3", optional = true }

[dev-dependencies]
# Example dependencies
rand = "0.6"
rand = { version = "0.6", features = ["wasm-bindgen"] }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
log = "0.4"
wasm-bindgen = "0.2"
console_error_panic_hook = "0.1.6"
console_log = "0.1.2"

[patch.crates-io]
wgpu = { git = "https://github.com/hecrj/wgpu-rs", branch = "web" }
Expand All @@ -62,3 +71,4 @@ gfx-hal = { git = "https://github.com/hecrj/gfx", branch = "web" }
gfx-backend-empty = { git = "https://github.com/hecrj/gfx", branch = "web" }
gfx-backend-gl = { git = "https://github.com/hecrj/gfx", branch = "web" }
winit = { git = "https://github.com/hecrj/winit", branch = "web" }
instant = { git = "https://github.com/hecrj/instant", branch = "fix/illegal-invocation" }
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
feature = "metal",
feature = "dx11",
feature = "dx12",
feature = "web",
all(debug_assertions, feature = "empty")
)))]
compile_error!(
Expand Down
20 changes: 18 additions & 2 deletions examples/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use coffee::graphics::{
WindowSettings,
};
use coffee::input::KeyboardAndMouse;
use coffee::load::Task;
use coffee::load::{loading_screen::ProgressBar, Task};
use coffee::ui::{
slider, Align, Column, Element, Justify, Radio, Renderer, Row, Slider,
Text, UserInterface,
Expand All @@ -12,6 +12,22 @@ use coffee::{Game, Result, Timer};

use std::ops::RangeInclusive;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(start)]
pub fn wasm_main() {
#[cfg(debug_assertions)]
{
use log::Level;
console_log::init_with_level(Level::Trace).expect("error initializing log");
}

std::panic::set_hook(Box::new(console_error_panic_hook::hook));
main().expect("Run main");
}

fn main() -> Result<()> {
<Example as UserInterface>::run(WindowSettings {
title: String::from("Mesh - Coffee"),
Expand Down Expand Up @@ -52,7 +68,7 @@ enum ModeOption {

impl Game for Example {
type Input = KeyboardAndMouse;
type LoadingScreen = ();
type LoadingScreen = ProgressBar;

fn load(_window: &Window) -> Task<Example> {
Task::new(move || Example {
Expand Down
34 changes: 22 additions & 12 deletions examples/particles.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
//! A particle gravity simulator that showcases a loading screen, input
//! handling, and graphics interpolation with batched drawing and font
//! rendering. Move the mouse around to attract the particles.
use rand::Rng;
use rayon::prelude::*;
use std::{thread, time};

use coffee::graphics::{
Batch, Color, Frame, Image, Point, Rectangle, Sprite, Vector, Window,
WindowSettings,
Expand All @@ -14,6 +10,24 @@ use coffee::load::{loading_screen::ProgressBar, Join, Task};
use coffee::ui::{Checkbox, Column, Element, Justify, Renderer, UserInterface};
use coffee::{Game, Result, Timer};

use rand::Rng;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(start)]
pub fn wasm_main() {
#[cfg(debug_assertions)]
{
use log::Level;
console_log::init_with_level(Level::Trace).expect("error initializing log");
}

std::panic::set_hook(Box::new(console_error_panic_hook::hook));
main().expect("Run main");
}

fn main() -> Result<()> {
<Particles as UserInterface>::run(WindowSettings {
title: String::from("Particles - Coffee"),
Expand Down Expand Up @@ -74,13 +88,9 @@ impl Game for Particles {
Self::generate(window.width(), window.height()),
),
Task::stage("Loading assets...", Self::load_palette()),
Task::stage(
"Showing off the loading screen for a bit...",
Task::new(|| thread::sleep(time::Duration::from_secs(2))),
),
)
.join()
.map(|(particles, palette, _)| Particles {
.map(|(particles, palette)| Particles {
particles,
gravity_centers: vec![Point::new(0.0, 0.0)],
batch: Batch::new(palette),
Expand All @@ -106,7 +116,7 @@ impl Game for Particles {
let gravity_centers = self.gravity_centers.clone();

// Update particles in parallel! <3 rayon
self.particles.par_iter_mut().for_each(move |particle| {
self.particles.iter_mut().for_each(move |particle| {
particle.acceleration = gravity_centers
.iter()
.map(|gravity_center| {
Expand All @@ -133,7 +143,7 @@ impl Game for Particles {
};

// Generate sprites in parallel! <3 rayon
let sprites = self.particles.par_iter().map(|particle| {
let sprites = self.particles.iter().map(|particle| {
let velocity =
particle.velocity + particle.acceleration * delta_factor;

Expand All @@ -153,7 +163,7 @@ impl Game for Particles {
self.batch.clear();

// Use the parallel iterator to populate the batch efficiently
self.batch.par_extend(sprites);
self.batch.extend(sprites);

// Draw particles all at once!
self.batch.draw(&mut frame.as_target());
Expand Down
20 changes: 18 additions & 2 deletions examples/ui.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
use coffee::graphics::{
Color, Frame, HorizontalAlignment, Window, WindowSettings,
};
use coffee::load::Task;
use coffee::load::{loading_screen::ProgressBar, Task};
use coffee::ui::{
button, slider, Align, Button, Checkbox, Column, Element, Justify, Radio,
Renderer, Row, Slider, Text, UserInterface,
};
use coffee::{Game, Result, Timer};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(start)]
pub fn wasm_main() {
#[cfg(debug_assertions)]
{
use log::Level;
console_log::init_with_level(Level::Trace).expect("error initializing log");
}

std::panic::set_hook(Box::new(console_error_panic_hook::hook));
main().expect("Run main");
}

fn main() -> Result<()> {
<Tour as UserInterface>::run(WindowSettings {
title: String::from("User Interface - Coffee"),
Expand All @@ -25,7 +41,7 @@ struct Tour {

impl Game for Tour {
type Input = ();
type LoadingScreen = ();
type LoadingScreen = ProgressBar;

fn load(_window: &Window) -> Task<Tour> {
Task::new(|| Tour {
Expand Down
47 changes: 23 additions & 24 deletions src/debug/basic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use instant::Instant;
use std::time;

use crate::graphics;
Expand All @@ -19,19 +20,19 @@ use crate::graphics;
pub struct Debug {
font: graphics::Font,
enabled: bool,
load_start: time::Instant,
load_start: Instant,
load_duration: time::Duration,
frame_start: time::Instant,
frame_start: Instant,
frame_durations: TimeBuffer,
interact_start: time::Instant,
interact_start: Instant,
interact_duration: time::Duration,
update_start: time::Instant,
update_start: Instant,
update_durations: TimeBuffer,
draw_start: time::Instant,
draw_start: Instant,
draw_durations: TimeBuffer,
ui_start: time::Instant,
ui_start: Instant,
ui_durations: TimeBuffer,
debug_start: time::Instant,
debug_start: Instant,
debug_durations: TimeBuffer,
text: Vec<(String, String)>,
draw_rate: u16,
Expand All @@ -40,7 +41,7 @@ pub struct Debug {

impl Debug {
pub(crate) fn new(gpu: &mut graphics::Gpu) -> Self {
let now = time::Instant::now();
let now = Instant::now();

Self {
font: graphics::Font::from_bytes(gpu, graphics::Font::DEFAULT)
Expand All @@ -67,11 +68,11 @@ impl Debug {
}

pub(crate) fn loading_started(&mut self) {
self.load_start = time::Instant::now();
self.load_start = Instant::now();
}

pub(crate) fn loading_finished(&mut self) {
self.load_duration = time::Instant::now() - self.load_start;
self.load_duration = Instant::now() - self.load_start;
}

/// Returns the time spent loading your [`Game`].
Expand All @@ -82,11 +83,10 @@ impl Debug {
}

pub(crate) fn frame_started(&mut self) {
self.frame_start = time::Instant::now();
self.frame_start = Instant::now();
}
pub(crate) fn frame_finished(&mut self) {
self.frame_durations
.push(time::Instant::now() - self.frame_start);
self.frame_durations.push(Instant::now() - self.frame_start);
}

/// Returns the average time spent per frame.
Expand All @@ -97,11 +97,11 @@ impl Debug {
}

pub(crate) fn interact_started(&mut self) {
self.interact_start = time::Instant::now();
self.interact_start = Instant::now();
}

pub(crate) fn interact_finished(&mut self) {
self.interact_duration = time::Instant::now() - self.interact_start;
self.interact_duration = Instant::now() - self.interact_start;
}

/// Returns the average time spent processing events and running
Expand All @@ -113,12 +113,12 @@ impl Debug {
}

pub(crate) fn update_started(&mut self) {
self.update_start = time::Instant::now();
self.update_start = Instant::now();
}

pub(crate) fn update_finished(&mut self) {
self.update_durations
.push(time::Instant::now() - self.update_start);
.push(Instant::now() - self.update_start);
}

/// Returns the average time spent running [`Game::update`].
Expand All @@ -129,11 +129,11 @@ impl Debug {
}

pub(crate) fn draw_started(&mut self) {
self.draw_start = time::Instant::now();
self.draw_start = Instant::now();
}

pub(crate) fn draw_finished(&mut self) {
let duration = time::Instant::now() - self.draw_start;
let duration = Instant::now() - self.draw_start;

if duration.subsec_micros() > 0 {
self.draw_durations.push(duration);
Expand All @@ -148,11 +148,11 @@ impl Debug {
}

pub(crate) fn ui_started(&mut self) {
self.ui_start = time::Instant::now();
self.ui_start = Instant::now();
}

pub(crate) fn ui_finished(&mut self) {
self.ui_durations.push(time::Instant::now() - self.ui_start);
self.ui_durations.push(Instant::now() - self.ui_start);
}

/// Returns the average time spent rendering the [`UserInterface`].
Expand All @@ -168,12 +168,11 @@ impl Debug {
}

pub(crate) fn debug_started(&mut self) {
self.debug_start = time::Instant::now();
self.debug_start = Instant::now();
}

pub(crate) fn debug_finished(&mut self) {
self.debug_durations
.push(time::Instant::now() - self.debug_start);
self.debug_durations.push(Instant::now() - self.debug_start);
}

/// Returns the average time spent running [`Game::debug`].
Expand Down
2 changes: 2 additions & 0 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ use backend_gfx as gpu;
feature = "metal",
feature = "dx11",
feature = "dx12",
feature = "web",
all(debug_assertions, feature = "empty")
))]
mod backend_wgpu;
Expand All @@ -103,6 +104,7 @@ mod backend_wgpu;
feature = "metal",
feature = "dx11",
feature = "dx12",
feature = "web",
all(debug_assertions, feature = "empty")
))]
use backend_wgpu as gpu;
Expand Down
Loading