Skip to content

Commit

Permalink
examples: use glutin_winit::GlWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic committed Jan 27, 2023
1 parent 939191d commit c0abf6d
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 104 deletions.
1 change: 1 addition & 0 deletions gfx-glyph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cgmath = "0.18"
env_logger = { version = "0.10", default-features = false }
gfx_device_gl = "0.16"
glutin = "0.30.3"
glutin-winit = "0.2.2"
old_school_gfx_glutin_ext = "0.30"
spin_sleep = "1"
winit = "0.27"
3 changes: 2 additions & 1 deletion gfx-glyph/examples/depth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use gfx::{
};
use gfx_glyph::{ab_glyph::*, *};
use glutin::surface::GlSurface;
use init::{init_example, WindowExt};
use glutin_winit::GlWindow;
use init::init_example;
use std::error::Error;
use winit::{
event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent},
Expand Down
29 changes: 0 additions & 29 deletions gfx-glyph/examples/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,6 @@ pub fn init_example(example_name: &str) {
}
}

/// [`Window`] extensions for working with [`glutin`] surfaces.
pub trait WindowExt {
/// Resize the surface to the window inner size.
///
/// No-op if either window size is zero.
fn resize_surface(&self, surface: &Surface<WindowSurface>, context: &PossiblyCurrentContext);
}

impl WindowExt for Window {
fn resize_surface(&self, surface: &Surface<WindowSurface>, context: &PossiblyCurrentContext) {
if let Some((w, h)) = self.inner_size().non_zero() {
surface.resize(context, w, h)
}
}
}

/// [`winit::dpi::PhysicalSize<u32>`] non-zero extensions.
pub trait NonZeroU32PhysicalSize {
/// Converts to non-zero `(width, height)`.
fn non_zero(self) -> Option<(NonZeroU32, NonZeroU32)>;
}
impl NonZeroU32PhysicalSize for winit::dpi::PhysicalSize<u32> {
fn non_zero(self) -> Option<(NonZeroU32, NonZeroU32)> {
let w = NonZeroU32::new(self.width)?;
let h = NonZeroU32::new(self.height)?;
Some((w, h))
}
}

fn main() {
eprintln!("\"init\" isn't an example. Try: cargo run --example paragraph");
}
3 changes: 2 additions & 1 deletion gfx-glyph/examples/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use gfx::{
};
use gfx_glyph::ab_glyph;
use glutin::surface::GlSurface;
use init::{init_example, WindowExt};
use glutin_winit::GlWindow;
use init::init_example;
use std::{
error::Error,
f32::consts::PI as PI32,
Expand Down
3 changes: 2 additions & 1 deletion gfx-glyph/examples/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use gfx::{
};
use gfx_glyph::{ab_glyph::*, *};
use glutin::surface::GlSurface;
use init::{init_example, WindowExt};
use glutin_winit::GlWindow;
use init::init_example;
use std::{env, error::Error};
use winit::{
event::{ElementState, Event, KeyboardInput, MouseScrollDelta, VirtualKeyCode, WindowEvent},
Expand Down
3 changes: 2 additions & 1 deletion gfx-glyph/examples/pre_positioned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use gfx::{
};
use gfx_glyph::{ab_glyph::*, *};
use glutin::surface::GlSurface;
use init::{init_example, WindowExt};
use glutin_winit::GlWindow;
use init::init_example;
use std::error::Error;
use winit::{
event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent},
Expand Down
3 changes: 2 additions & 1 deletion gfx-glyph/examples/varied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use gfx::{
};
use gfx_glyph::{ab_glyph::*, *};
use glutin::surface::GlSurface;
use init::{init_example, WindowExt};
use glutin_winit::GlWindow;
use init::init_example;
use std::error::Error;
use winit::{
event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent},
Expand Down
2 changes: 1 addition & 1 deletion glyph-brush/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ criterion = "0.4"
env_logger = { version = "0.10", default-features = false }
gl = "0.14"
glutin = "0.30.3"
glutin-winit = "0.2.1"
glutin-winit = "0.2.2"
once_cell = "1.3"
raw-window-handle = "0.5"
spin_sleep = "1"
Expand Down
3 changes: 2 additions & 1 deletion glyph-brush/examples/draw_cache_guts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use glutin::{
prelude::{GlConfig, GlDisplay, NotCurrentGlContextSurfaceAccessor},
surface::GlSurface,
};
use glutin_winit::GlWindow;
use glyph_brush::{ab_glyph::*, *};
use opengl::{glutin_winit2::GlWindow, GlGlyphTexture, GlTextPipe, Res, Vertex};
use opengl::{GlGlyphTexture, GlTextPipe, Res, Vertex};
use raw_window_handle::HasRawWindowHandle;
use std::{env, ffi::CString, mem};
use winit::{
Expand Down
69 changes: 1 addition & 68 deletions glyph-brush/examples/opengl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use glutin::{
prelude::{GlConfig, GlDisplay, NotCurrentGlContextSurfaceAccessor},
surface::GlSurface,
};
use glutin_winit2::GlWindow;
use glutin_winit::GlWindow;
use glyph_brush::{ab_glyph::*, *};
use raw_window_handle::HasRawWindowHandle;
use std::{
Expand Down Expand Up @@ -635,70 +635,3 @@ impl Drop for GlTextPipe {
}
}
}

// glutin-winit helpers
pub mod glutin_winit2 {
use glutin::{
context::PossiblyCurrentContext,
surface::{GlSurface, Surface, SurfaceAttributes, SurfaceAttributesBuilder, WindowSurface},
};
use raw_window_handle::HasRawWindowHandle;
use std::num::NonZeroU32;
use winit::window::Window;

/// [`Window`] extensions for working with [`glutin`] surfaces.
pub trait GlWindow {
/// Build the surface attributes suitable to create a window surface.
///
/// Panics if either window inner dimension is zero.
fn build_surface_attributes(
&self,
builder: SurfaceAttributesBuilder<WindowSurface>,
) -> SurfaceAttributes<WindowSurface>;

/// Resize the surface to the window inner size.
///
/// No-op if either window size is zero.
fn resize_surface(
&self,
surface: &Surface<WindowSurface>,
context: &PossiblyCurrentContext,
);
}

impl GlWindow for Window {
fn build_surface_attributes(
&self,
builder: SurfaceAttributesBuilder<WindowSurface>,
) -> SurfaceAttributes<WindowSurface> {
let (w, h) = self
.inner_size()
.non_zero()
.expect("invalid zero inner size");
builder.build(self.raw_window_handle(), w, h)
}

fn resize_surface(
&self,
surface: &Surface<WindowSurface>,
context: &PossiblyCurrentContext,
) {
if let Some((w, h)) = self.inner_size().non_zero() {
surface.resize(context, w, h)
}
}
}

/// [`winit::dpi::PhysicalSize<u32>`] non-zero extensions.
pub trait NonZeroU32PhysicalSize {
/// Converts to non-zero `(width, height)`.
fn non_zero(self) -> Option<(NonZeroU32, NonZeroU32)>;
}
impl NonZeroU32PhysicalSize for winit::dpi::PhysicalSize<u32> {
fn non_zero(self) -> Option<(NonZeroU32, NonZeroU32)> {
let w = NonZeroU32::new(self.width)?;
let h = NonZeroU32::new(self.height)?;
Some((w, h))
}
}
}

0 comments on commit c0abf6d

Please sign in to comment.