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

Update winit and wgpu #62

Merged
merged 19 commits into from
Apr 24, 2020
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
interface.
- `Mesh::new_with_tolerance`, which allows to control the tolerance of line
segment approximations. [#100]
- `Game::cursor_icon`, which allows customization of the mouse cursor icon.

### Changed
- `Mesh::stroke` now takes an `f32` as `line_width` instead of a `u16`.
Expand Down
21 changes: 11 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ features = ["opengl", "debug"]

[features]
default = []
opengl = ["gfx", "gfx_core", "glutin", "gfx_device_gl", "gfx_window_glutin", "gfx_glyph", "gfx_winit"]
vulkan = ["wgpu", "wgpu/vulkan", "wgpu_glyph"]
metal = ["wgpu", "wgpu/metal", "wgpu_glyph"]
dx11 = ["wgpu", "wgpu/dx11", "wgpu_glyph"]
dx12 = ["wgpu", "wgpu/dx12", "wgpu_glyph"]
opengl = ["gfx", "gfx_core", "glutin", "gfx_device_gl", "gfx_glyph"]
vulkan = ["wgpu", "wgpu_glyph", "zerocopy", "futures"]
metal = ["wgpu", "wgpu_glyph", "zerocopy", "futures"]
dx11 = ["wgpu", "wgpu_glyph", "zerocopy", "futures"]
dx12 = ["wgpu", "wgpu_glyph", "zerocopy", "futures"]
debug = []

[dependencies]
Expand All @@ -35,19 +35,20 @@ stretch = "0.2"
twox-hash = "1.3"
lyon_tessellation = "0.13"
gilrs = "0.7"
winit = "0.22"

# gfx (OpenGL)
gfx = { version = "0.18", optional = true }
gfx_core = { version = "0.9", optional = true }
glutin = { version = "0.20", optional = true }
gfx_device_gl = { version = "0.16", optional = true }
gfx_window_glutin = { version = "0.30", optional = true }
gfx_glyph = { version = "0.15", optional = true }
gfx_winit = { package = "winit", version = "0.19", optional = true }
glutin = { version = "0.24", optional = true }

# wgpu (Vulkan, Metal, D3D)
wgpu = { version = "0.2", optional = true, git = "https://github.com/gfx-rs/wgpu-rs", rev = "5522c912f7e2f4f33a1167fb0c8ee4549f066dcf" }
wgpu_glyph = { version = "0.3", optional = true, git = "https://github.com/hecrj/wgpu_glyph", rev = "0577e4d2be6b035a14aa0c5d82b143aaf26c1bd3" }
wgpu = { version = "0.5", optional = true }
wgpu_glyph = { version = "0.8", optional = true }
zerocopy = { version = "0.3", optional = true }
futures = { version = "0.3", optional = true }

[dev-dependencies]
rand = "0.6"
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
feature = "vulkan",
feature = "metal",
feature = "dx11",
feature = "dx12"
feature = "dx12",
)))]
compile_error!(
"You need to enable a graphics backend feature. \
Expand Down
2 changes: 0 additions & 2 deletions src/debug/null.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::graphics;

// Null debug implementation
#[cfg(not(debug_assertions))]
#[allow(missing_debug_implementations)]
#[allow(missing_docs)]
pub struct Debug {}

#[cfg(not(debug_assertions))]
impl Debug {
pub(crate) fn new(_gpu: &mut graphics::Gpu) -> Self {
Self {}
Expand Down
167 changes: 15 additions & 152 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::graphics::window;
use crate::graphics::{Frame, Window, WindowSettings};
use crate::input::{self, gamepad, keyboard, mouse, Input};
mod r#loop;

pub(crate) use r#loop::Loop;

use crate::graphics::{CursorIcon, Frame, Window, WindowSettings};
use crate::input::{keyboard, Input};
use crate::load::{LoadingScreen, Task};
use crate::{Debug, Result, Timer};

Expand Down Expand Up @@ -121,6 +124,13 @@ pub trait Game {
/// [`Window`]: graphics/struct.Window.html
fn update(&mut self, _window: &Window) {}

/// Defines the cursor icon of the window.
///
/// By default, it returns platform-dependent default cursor.
fn cursor_icon(&self) -> CursorIcon {
CursorIcon::Default
}

/// Displays debug information.
///
/// This method is called after [`draw`] once per frame when debug has been
Expand Down Expand Up @@ -171,155 +181,8 @@ pub trait Game {
/// [`WindowSettings`]: graphics/struct.WindowSettings.html
fn run(window_settings: WindowSettings) -> Result<()>
where
Self: Sized + 'static,
Self: 'static + Sized,
{
// Set up window
let event_loop = &mut window::EventLoop::new();
let window = &mut Window::new(window_settings, &event_loop)?;
let mut debug = Debug::new(window.gpu());

// Load game
debug.loading_started();
let mut loading_screen = Self::LoadingScreen::new(window.gpu())?;
let game = &mut loading_screen.run(Self::load(window), window)?;
let input = &mut Self::Input::new();
let mut gamepads = gamepad::Tracker::new();
debug.loading_finished();

// Game loop
let mut timer = Timer::new(Self::TICKS_PER_SECOND);
let mut alive = true;

while alive && !game.is_finished() {
debug.frame_started();
timer.update();

while timer.tick() {
interact(
game,
input,
&mut debug,
window,
event_loop,
gamepads.as_mut(),
&mut alive,
);

debug.update_started();
game.update(window);
debug.update_finished();
}

if !timer.has_ticked() {
interact(
game,
input,
&mut debug,
window,
event_loop,
gamepads.as_mut(),
&mut alive,
);
}

debug.draw_started();
game.draw(&mut window.frame(), &timer);
debug.draw_finished();

if debug.is_enabled() {
debug.debug_started();
game.debug(input, &mut window.frame(), &mut debug);
debug.debug_finished();
}

window.swap_buffers();
debug.frame_finished();
}

Ok(())
}
}

fn interact<G: Game>(
game: &mut G,
input: &mut G::Input,
debug: &mut Debug,
window: &mut Window,
event_loop: &mut window::EventLoop,
gamepads: Option<&mut gamepad::Tracker>,
alive: &mut bool,
) {
debug.interact_started();

event_loop.poll(|event| {
process_window_event(game, input, debug, window, alive, event)
});

process_gamepad_events(gamepads, input);

game.interact(input, window);
input.clear();

debug.interact_finished();
}

pub(crate) fn process_window_event<G: Game, I: Input>(
game: &mut G,
input: &mut I,
debug: &mut Debug,
window: &mut Window,
alive: &mut bool,
event: window::Event,
) {
match event {
window::Event::Input(input_event) => {
input.update(input_event);

#[cfg(any(debug_assertions, feature = "debug"))]
match input_event {
input::Event::Keyboard(keyboard::Event::Input {
state: input::ButtonState::Released,
key_code,
}) if Some(key_code) == G::DEBUG_KEY => {
debug.toggle();
}
_ => {}
}
}
window::Event::CursorMoved(logical_position) => {
let position = logical_position.to_physical(window.dpi());

input.update(input::Event::Mouse(mouse::Event::CursorMoved {
x: position.x as f32,
y: position.y as f32,
}));
}
window::Event::Moved(logical_position) => {
let position = logical_position.to_physical(window.dpi());

input.update(input::Event::Window(input::window::Event::Moved {
x: position.x as f32,
y: position.y as f32,
}))
}
window::Event::CloseRequested => {
if game.on_close_request() {
*alive = false;
}
}
window::Event::Resized(new_size) => {
window.resize(new_size);
}
};
}

pub(crate) fn process_gamepad_events<I: Input>(
gamepads: Option<&mut gamepad::Tracker>,
input: &mut I,
) {
if let Some(tracker) = gamepads {
while let Some((id, event, time)) = tracker.next_event() {
input.update(input::Event::Gamepad { id, event, time });
}
<r#loop::Default as Loop<Self>>::run(window_settings)
}
}
Loading