Skip to content

Commit

Permalink
Reducing glutin dependency in egui_glow (#1151)
Browse files Browse the repository at this point in the history
Use winit wherever possible
  • Loading branch information
KentaTheBugMaker committed Jan 24, 2022
1 parent fa43d16 commit 9d59696
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
3 changes: 2 additions & 1 deletion egui_glow/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ All notable changes to the `egui_glow` integration will be noted in this file.
* `EguiGlow::run` no longer returns the shapes to paint, but stores them internally until you call `EguiGlow::paint` ([#1110](https://github.com/emilk/egui/pull/1110)).
* Added `set_texture_filter` method to `Painter` ([#1041](https://github.com/emilk/egui/pull/1041)).
* Fix failure to run in Chrome ([#1092](https://github.com/emilk/egui/pull/1092)).

* `EguiGlow::new` now takes `&winit::Window` because there are no reason to use `&glutin::WindowedContext` ([#1151](https://github.com/emilk/egui/pull/1151)).
* `EguiGlow::paint` now takes `&winit::Window` because there are no reason to use `&glutin::WindowedContext` ([#1151](https://github.com/emilk/egui/pull/1151)).

## 0.16.0 - 2021-12-29
* Made winit/glutin an optional dependency ([#868](https://github.com/emilk/egui/pull/868)).
Expand Down
4 changes: 2 additions & 2 deletions egui_glow/examples/pure_glow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {
let event_loop = glutin::event_loop::EventLoop::with_user_event();
let (gl_window, gl) = create_display(&event_loop);

let mut egui_glow = egui_glow::EguiGlow::new(&gl_window, &gl);
let mut egui_glow = egui_glow::EguiGlow::new(gl_window.window(), &gl);

event_loop.run(move |event, _, control_flow| {
let mut redraw = || {
Expand Down Expand Up @@ -78,7 +78,7 @@ fn main() {

// draw things behind egui here

egui_glow.paint(&gl_window, &gl);
egui_glow.paint(gl_window.window(), &gl);

// draw things on top of egui here

Expand Down
31 changes: 16 additions & 15 deletions egui_glow/src/epi_backend.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::*;
use egui_winit::winit;

struct RequestRepaintEvent;

struct GlowRepaintSignal(std::sync::Mutex<glutin::event_loop::EventLoopProxy<RequestRepaintEvent>>);
struct GlowRepaintSignal(std::sync::Mutex<winit::event_loop::EventLoopProxy<RequestRepaintEvent>>);

impl epi::backend::RepaintSignal for GlowRepaintSignal {
fn request_repaint(&self) {
Expand All @@ -12,8 +13,8 @@ impl epi::backend::RepaintSignal for GlowRepaintSignal {

#[allow(unsafe_code)]
fn create_display(
window_builder: glutin::window::WindowBuilder,
event_loop: &glutin::event_loop::EventLoop<RequestRepaintEvent>,
window_builder: winit::window::WindowBuilder,
event_loop: &winit::event_loop::EventLoop<RequestRepaintEvent>,
) -> (
glutin::WindowedContext<glutin::PossiblyCurrent>,
glow::Context,
Expand Down Expand Up @@ -51,7 +52,7 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
let window_settings = persistence.load_window_settings();
let window_builder =
egui_winit::epi::window_builder(native_options, &window_settings).with_title(app.name());
let event_loop = glutin::event_loop::EventLoop::with_user_event();
let event_loop = winit::event_loop::EventLoop::with_user_event();
let (gl_window, gl) = create_display(window_builder, &event_loop);

let repaint_signal = std::sync::Arc::new(GlowRepaintSignal(std::sync::Mutex::new(
Expand Down Expand Up @@ -116,12 +117,12 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {

{
*control_flow = if integration.should_quit() {
glutin::event_loop::ControlFlow::Exit
winit::event_loop::ControlFlow::Exit
} else if needs_repaint {
gl_window.window().request_redraw();
glutin::event_loop::ControlFlow::Poll
winit::event_loop::ControlFlow::Poll
} else {
glutin::event_loop::ControlFlow::Wait
winit::event_loop::ControlFlow::Wait
};
}

Expand All @@ -132,30 +133,30 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
// Platform-dependent event handlers to workaround a winit bug
// See: https://github.com/rust-windowing/winit/issues/987
// See: https://github.com/rust-windowing/winit/issues/1619
glutin::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
winit::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
winit::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),

glutin::event::Event::WindowEvent { event, .. } => {
if let glutin::event::WindowEvent::Focused(new_focused) = event {
winit::event::Event::WindowEvent { event, .. } => {
if let winit::event::WindowEvent::Focused(new_focused) = event {
is_focused = new_focused;
}

if let glutin::event::WindowEvent::Resized(physical_size) = event {
if let winit::event::WindowEvent::Resized(physical_size) = event {
gl_window.resize(physical_size);
}

integration.on_event(&event);
if integration.should_quit() {
*control_flow = glutin::event_loop::ControlFlow::Exit;
*control_flow = winit::event_loop::ControlFlow::Exit;
}

gl_window.window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
}
glutin::event::Event::LoopDestroyed => {
winit::event::Event::LoopDestroyed => {
integration.on_exit(gl_window.window());
painter.destroy(&gl);
}
glutin::event::Event::UserEvent(RequestRepaintEvent) => {
winit::event::Event::UserEvent(RequestRepaintEvent) => {
gl_window.window().request_redraw();
}
_ => (),
Expand Down
21 changes: 8 additions & 13 deletions egui_glow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
#![allow(clippy::manual_range_contains)]

pub mod painter;
#[cfg(feature = "winit")]
use egui_winit::winit;
pub use glow;
pub use painter::Painter;
#[cfg(feature = "winit")]
Expand Down Expand Up @@ -119,10 +121,7 @@ pub struct EguiGlow {

#[cfg(feature = "winit")]
impl EguiGlow {
pub fn new(
gl_window: &glutin::WindowedContext<glutin::PossiblyCurrent>,
gl: &glow::Context,
) -> Self {
pub fn new(window: &winit::window::Window, gl: &glow::Context) -> Self {
let painter = crate::Painter::new(gl, None, "")
.map_err(|error| {
crate::misc_util::glow_print_error(format!(
Expand All @@ -134,7 +133,7 @@ impl EguiGlow {

Self {
egui_ctx: Default::default(),
egui_winit: egui_winit::State::new(painter.max_texture_side(), gl_window.window()),
egui_winit: egui_winit::State::new(painter.max_texture_side(), window),
painter,
shapes: Default::default(),
textures_delta: Default::default(),
Expand All @@ -147,7 +146,7 @@ impl EguiGlow {
/// and only when this returns `false` pass on the events to your game.
///
/// Note that egui uses `tab` to move focus between elements, so this will always return `true` for tabs.
pub fn on_event(&mut self, event: &glutin::event::WindowEvent<'_>) -> bool {
pub fn on_event(&mut self, event: &winit::event::WindowEvent<'_>) -> bool {
self.egui_winit.on_event(&self.egui_ctx, event)
}

Expand All @@ -156,7 +155,7 @@ impl EguiGlow {
/// Call [`Self::paint`] later to paint.
pub fn run(
&mut self,
window: &glutin::window::Window,
window: &winit::window::Window,
run_ui: impl FnMut(&egui::Context),
) -> bool {
let raw_input = self.egui_winit.take_egui_input(window);
Expand All @@ -172,11 +171,7 @@ impl EguiGlow {
}

/// Paint the results of the last call to [`Self::run`].
pub fn paint(
&mut self,
gl_window: &glutin::WindowedContext<glutin::PossiblyCurrent>,
gl: &glow::Context,
) {
pub fn paint(&mut self, window: &winit::window::Window, gl: &glow::Context) {
let shapes = std::mem::take(&mut self.shapes);
let mut textures_delta = std::mem::take(&mut self.textures_delta);

Expand All @@ -185,7 +180,7 @@ impl EguiGlow {
}

let clipped_meshes = self.egui_ctx.tessellate(shapes);
let dimensions: [u32; 2] = gl_window.window().inner_size().into();
let dimensions: [u32; 2] = window.inner_size().into();
self.painter.paint_meshes(
gl,
dimensions,
Expand Down

0 comments on commit 9d59696

Please sign in to comment.