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

expose vsync toggle #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ fn main() -> Result<()> {
resizable: true,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down
1 change: 1 addition & 0 deletions examples/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn main() -> Result<()> {
resizable: false,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down
1 change: 1 addition & 0 deletions examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn main() -> Result<()> {
resizable: false,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down
1 change: 1 addition & 0 deletions examples/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn main() -> Result<()> {
resizable: false,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down
1 change: 1 addition & 0 deletions examples/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn main() -> Result<()> {
resizable: false,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down
1 change: 1 addition & 0 deletions examples/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() -> Result<()> {
resizable: false,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down
1 change: 1 addition & 0 deletions examples/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() -> Result<()> {
resizable: false,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down
1 change: 1 addition & 0 deletions examples/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn main() -> Result<()> {
resizable: false,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down
1 change: 1 addition & 0 deletions examples/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn main() -> Result<()> {
resizable: false,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down
1 change: 1 addition & 0 deletions examples/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn main() -> coffee::Result<()> {
resizable: true,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down
4 changes: 3 additions & 1 deletion examples/snake.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
extern crate coffee;

use coffee::graphics::{
Color, Font, Frame, Mesh, Point, Rectangle, Shape, Text, Window, WindowSettings,
Color, Font, Frame, Mesh, Point, Rectangle, Shape, Text, Window,
WindowSettings,
};
use coffee::input::keyboard::KeyCode;
use coffee::input::{self, keyboard, Input};
Expand All @@ -18,6 +19,7 @@ fn main() {
resizable: false,
maximized: false,
fullscreen: false,
vsync: false,
})
.expect("An error occured while starting the game");
}
Expand Down
1 change: 1 addition & 0 deletions examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn main() -> Result<()> {
resizable: false,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down
8 changes: 4 additions & 4 deletions src/graphics/backend_gfx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use types::TargetView;
use gfx::{self, Device};
use gfx_device_gl as gl;

use crate::graphics::{Color, Transformation};
use crate::graphics::{Color, Transformation, WindowSettings};
use crate::Result;

/// A link between your game and a graphics processor.
Expand All @@ -40,11 +40,11 @@ pub struct Gpu {

impl Gpu {
pub(super) fn for_window(
builder: winit::window::WindowBuilder,
events_loop: &winit::event_loop::EventLoop<()>,
settings: WindowSettings,
event_loop: &winit::event_loop::EventLoop<()>,
) -> Result<(Gpu, Surface)> {
let (surface, device, mut factory) =
Surface::new(builder, events_loop)?;
Surface::new(settings, event_loop)?;

let mut encoder: gfx::Encoder<gl::Resources, gl::CommandBuffer> =
factory.create_command_buffer().into();
Expand Down
7 changes: 4 additions & 3 deletions src/graphics/backend_gfx/surface.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use gfx_device_gl as gl;

use super::{format, Gpu, TargetView};
use crate::{Error, Result};
use crate::{graphics::WindowSettings, Error, Result};

pub struct Surface {
context: glutin::WindowedContext<glutin::PossiblyCurrent>,
Expand All @@ -10,7 +10,7 @@ pub struct Surface {

impl Surface {
pub(super) fn new(
builder: winit::window::WindowBuilder,
settings: WindowSettings,
event_loop: &winit::event_loop::EventLoop<()>,
) -> Result<(Self, gl::Device, gl::Factory)> {
let gl_builder = glutin::ContextBuilder::new()
Expand All @@ -19,8 +19,9 @@ impl Surface {
.with_multisampling(0)
// 24 color bits, 8 alpha bits
.with_pixel_format(24, 8)
.with_vsync(true);
.with_vsync(settings.vsync);

let builder = settings.into_builder(event_loop);
let (context, device, factory, target, _depth) = init_raw(
builder,
gl_builder,
Expand Down
8 changes: 5 additions & 3 deletions src/graphics/backend_wgpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use texture::Texture;
pub use triangle::Vertex;
pub use types::TargetView;

use crate::graphics::{Color, Transformation};
use crate::graphics::{Color, Transformation, WindowSettings};
use crate::{Error, Result};

#[allow(missing_debug_implementations)]
Expand All @@ -27,9 +27,11 @@ pub struct Gpu {

impl Gpu {
pub(super) fn for_window(
builder: winit::window::WindowBuilder,
settings: WindowSettings,
event_loop: &winit::event_loop::EventLoop<()>,
) -> Result<(Gpu, Surface)> {
let vsync = settings.vsync;
let builder = settings.into_builder(event_loop);
let window = builder
.build(event_loop)
.map_err(|error| Error::WindowCreation(error.to_string()))?;
Expand Down Expand Up @@ -57,7 +59,7 @@ impl Gpu {
(device, queue)
});

let surface = Surface::new(window, &device);
let surface = Surface::new(window, &device, vsync);

let quad_pipeline = quad::Pipeline::new(&mut device);
let triangle_pipeline = triangle::Pipeline::new(&mut device);
Expand Down
17 changes: 14 additions & 3 deletions src/graphics/backend_wgpu/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ pub struct Surface {
swap_chain: wgpu::SwapChain,
extent: wgpu::Extent3d,
output: Option<wgpu::SwapChainOutput>,
vsync: bool,
}

impl Surface {
pub fn new(
window: winit::window::Window,
device: &wgpu::Device,
vsync: bool,
) -> Surface {
let surface = wgpu::Surface::create(&window);
let size = window.inner_size();

let (swap_chain, extent) = new_swap_chain(device, &surface, size);
let (swap_chain, extent) =
new_swap_chain(device, &surface, size, vsync);

Surface {
window,
surface,
swap_chain,
extent,
output: None,
vsync,
}
}

Expand All @@ -50,7 +54,7 @@ impl Surface {
size: winit::dpi::PhysicalSize<u32>,
) {
let (swap_chain, extent) =
new_swap_chain(&gpu.device, &self.surface, size);
new_swap_chain(&gpu.device, &self.surface, size, self.vsync);

self.swap_chain = swap_chain;
self.extent = extent;
Expand Down Expand Up @@ -82,15 +86,22 @@ fn new_swap_chain(
device: &wgpu::Device,
surface: &wgpu::Surface,
size: winit::dpi::PhysicalSize<u32>,
vsync: bool,
) -> (wgpu::SwapChain, wgpu::Extent3d) {
let present_mode = if vsync {
wgpu::PresentMode::Mailbox
} else {
wgpu::PresentMode::Fifo
};

let swap_chain = device.create_swap_chain(
surface,
&wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
format: wgpu::TextureFormat::Bgra8UnormSrgb,
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Mailbox,
present_mode,
},
);

Expand Down
3 changes: 1 addition & 2 deletions src/graphics/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ impl Window {
let (width, height) = settings.size;
let is_fullscreen = settings.fullscreen;

let (gpu, surface) =
Gpu::for_window(settings.into_builder(event_loop), event_loop)?;
let (gpu, surface) = Gpu::for_window(settings, event_loop)?;

Ok(Window {
is_fullscreen,
Expand Down
5 changes: 4 additions & 1 deletion src/graphics/window/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ pub struct Settings {

/// Defines whether or not the window should start maximized.
pub maximized: bool,

/// Defines whether or not to enable vsync.
pub vsync: bool,
}

impl Settings {
pub(super) fn into_builder(
pub(crate) fn into_builder(
self,
events_loop: &winit::event_loop::EventLoop<()>,
) -> winit::window::WindowBuilder {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
//! resizable: true,
//! fullscreen: false,
//! maximized: false,
//! vsync: false,
//! })
//! }
//!
Expand Down
1 change: 1 addition & 0 deletions tests/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn graphics() -> Result<()> {
resizable: false,
fullscreen: false,
maximized: false,
vsync: false,
})
}

Expand Down