From 5b313af9aa1665d7cdbe39e849db77ce9937ef4c Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Mon, 11 May 2020 10:55:45 +0200 Subject: [PATCH] Use winit also when targeting wasm This allows unifying the code paths more. --- examples/graphicstest/Cargo.toml | 2 ++ examples/graphicstest/src/main.rs | 48 +++++++++++++++---------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/examples/graphicstest/Cargo.toml b/examples/graphicstest/Cargo.toml index 6dcf64674..88afb2bbb 100644 --- a/examples/graphicstest/Cargo.toml +++ b/examples/graphicstest/Cargo.toml @@ -12,11 +12,13 @@ sixtyfps_gl_backend = { package = "gl", path = "../../sixtyfps_runtime/rendering kurbo = "0.5.11" image = { version = "0.23.4", default-features = false, features = [ "png" ] } cgmath = "0.17.0" +instant = "0.1" [target.'cfg(target_arch = "wasm32")'.dependencies] web_sys = { version = "0.3", package = "web-sys", features=["console"] } wasm-bindgen = { version = "0.2" } glow = { version = "0.4.0", default-features = false } +winit = { version = "0.22.1", features = ["web-sys"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] glutin = { version = "0.24" } diff --git a/examples/graphicstest/src/main.rs b/examples/graphicstest/src/main.rs index 3a6d5c5b3..d636d9bb0 100644 --- a/examples/graphicstest/src/main.rs +++ b/examples/graphicstest/src/main.rs @@ -1,14 +1,12 @@ use cgmath::{Matrix4, SquareMatrix, Vector3}; -#[cfg(target_arch = "wasm32")] -use glow::HasRenderLoop; #[cfg(not(target_arch = "wasm32"))] use glutin; +use instant; use kurbo::{BezPath, Rect}; use sixtyfps_corelib::graphics::{Color, FillStyle, GraphicsBackend, RenderTree}; use sixtyfps_gl_backend::{GLRenderer, OpaqueRenderingPrimitive}; #[cfg(target_arch = "wasm32")] use wasm_bindgen::prelude::*; -#[cfg(not(target_arch = "wasm32"))] use winit::{event, event_loop, window::WindowBuilder}; fn create_rect( @@ -32,10 +30,11 @@ pub fn wasm_main() { } fn main() { + let event_loop = event_loop::EventLoop::new(); + let wb = WindowBuilder::new(); + #[cfg(not(target_arch = "wasm32"))] - let (event_loop, windowed_context, gl_context) = { - let event_loop = event_loop::EventLoop::new(); - let wb = WindowBuilder::new(); + let (windowed_context, gl_context) = { let windowed_context = glutin::ContextBuilder::new().with_vsync(true).build_windowed(wb, &event_loop).unwrap(); let windowed_context = unsafe { windowed_context.make_current().unwrap() }; @@ -44,12 +43,11 @@ fn main() { windowed_context.get_proc_address(s) as *const _ }); - (event_loop, windowed_context, gl_context) + (windowed_context, gl_context) }; #[cfg(target_arch = "wasm32")] - let (event_loop, windowed_context, gl_context) = { - use wasm_bindgen::JsCast; + let (window, gl_context) = { let canvas = web_sys::window() .unwrap() .document() @@ -58,17 +56,21 @@ fn main() { .unwrap() .dyn_into::() .unwrap(); - let webgl1_context = canvas + + use winit::platform::web::WindowBuilderExtWebSys; + use winit::platform::web::WindowExtWebSys; + + let window = wb.with_canvas(Some(canvas)).build(&event_loop).unwrap(); + + use wasm_bindgen::JsCast; + let webgl1_context = window + .canvas() .get_context("webgl") .unwrap() .unwrap() .dyn_into::() .unwrap(); - ( - glow::RenderLoop::from_request_animation_frame(), - (canvas.width(), canvas.height()), - glow::Context::from_webgl1_context(webgl1_context), - ) + (window, glow::Context::from_webgl1_context(webgl1_context)) }; let mut renderer = GLRenderer::new(gl_context); @@ -124,10 +126,8 @@ fn main() { render_tree.node_at_mut(root).append_child(image_node); - #[cfg(not(target_arch = "wasm32"))] event_loop.run(move |event, _, control_flow| { - let next_frame_time = - std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667); + let next_frame_time = instant::Instant::now() + std::time::Duration::from_nanos(16_666_667); *control_flow = event_loop::ControlFlow::WaitUntil(next_frame_time); match event { @@ -146,14 +146,14 @@ fn main() { _ => return, } - let size = windowed_context.window().inner_size(); + #[cfg(not(target_arch = "wasm32"))] + let window = windowed_context.window(); + + let size = window.inner_size(); // TODO #4: ensure GO context is current -- see if this can be done within the runtime render_tree.render(&mut renderer, size.width, size.height, root); + + #[cfg(not(target_arch = "wasm32"))] windowed_context.swap_buffers().unwrap(); }); - - #[cfg(target_arch = "wasm32")] - event_loop.run(move |_running: &mut bool| { - render_tree.render(&mut renderer, windowed_context.0, windowed_context.1, root); - }); }