2021-10-18 21:13:32 +00:00
|
|
|
//! Example how to use pure `egui_glow` without [`epi`].
|
|
|
|
|
|
|
|
fn create_display(
|
|
|
|
event_loop: &glutin::event_loop::EventLoop<()>,
|
|
|
|
) -> (
|
|
|
|
glutin::WindowedContext<glutin::PossiblyCurrent>,
|
|
|
|
glow::Context,
|
|
|
|
) {
|
|
|
|
let window_builder = glutin::window::WindowBuilder::new()
|
|
|
|
.with_resizable(true)
|
|
|
|
.with_inner_size(glutin::dpi::LogicalSize {
|
|
|
|
width: 800.0,
|
|
|
|
height: 600.0,
|
|
|
|
})
|
|
|
|
.with_title("egui_glow example");
|
|
|
|
|
|
|
|
let gl_window = unsafe {
|
|
|
|
glutin::ContextBuilder::new()
|
|
|
|
.with_depth_buffer(0)
|
|
|
|
.with_srgb(true)
|
|
|
|
.with_stencil_buffer(0)
|
|
|
|
.with_vsync(true)
|
|
|
|
.build_windowed(window_builder, event_loop)
|
|
|
|
.unwrap()
|
|
|
|
.make_current()
|
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
let gl = unsafe { glow::Context::from_loader_function(|s| gl_window.get_proc_address(s)) };
|
|
|
|
|
|
|
|
unsafe {
|
2021-10-19 19:40:55 +00:00
|
|
|
use glow::HasContext as _;
|
2021-10-18 21:13:32 +00:00
|
|
|
gl.enable(glow::FRAMEBUFFER_SRGB);
|
|
|
|
}
|
|
|
|
|
|
|
|
(gl_window, gl)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-11-27 10:44:23 +00:00
|
|
|
let mut clear_color = [0.1, 0.1, 0.1];
|
|
|
|
|
2021-10-18 21:13:32 +00:00
|
|
|
let event_loop = glutin::event_loop::EventLoop::with_user_event();
|
|
|
|
let (gl_window, gl) = create_display(&event_loop);
|
|
|
|
|
2021-11-03 12:45:51 +00:00
|
|
|
let mut egui_glow = egui_glow::EguiGlow::new(&gl_window, &gl);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
let mut redraw = || {
|
|
|
|
let mut quit = false;
|
|
|
|
|
2021-11-03 12:45:51 +00:00
|
|
|
let (needs_repaint, shapes) = egui_glow.run(gl_window.window(), |egui_ctx| {
|
|
|
|
egui::SidePanel::left("my_side_panel").show(egui_ctx, |ui| {
|
|
|
|
ui.heading("Hello World!");
|
|
|
|
if ui.button("Quit").clicked() {
|
|
|
|
quit = true;
|
|
|
|
}
|
2021-11-27 10:44:23 +00:00
|
|
|
ui.color_edit_button_rgb(&mut clear_color);
|
2021-11-03 12:45:51 +00:00
|
|
|
});
|
2021-10-18 21:13:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
*control_flow = if quit {
|
|
|
|
glutin::event_loop::ControlFlow::Exit
|
|
|
|
} else if needs_repaint {
|
|
|
|
gl_window.window().request_redraw();
|
|
|
|
glutin::event_loop::ControlFlow::Poll
|
|
|
|
} else {
|
|
|
|
glutin::event_loop::ControlFlow::Wait
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
unsafe {
|
2021-10-19 19:40:55 +00:00
|
|
|
use glow::HasContext as _;
|
2021-11-27 10:44:23 +00:00
|
|
|
gl.clear_color(clear_color[0], clear_color[1], clear_color[2], 1.0);
|
2021-10-18 21:13:32 +00:00
|
|
|
gl.clear(glow::COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw things behind egui here
|
|
|
|
|
2021-11-03 12:45:51 +00:00
|
|
|
egui_glow.paint(&gl_window, &gl, shapes);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
// draw things on top of egui here
|
|
|
|
|
|
|
|
gl_window.swap_buffers().unwrap();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match event {
|
|
|
|
// 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(),
|
|
|
|
|
|
|
|
glutin::event::Event::WindowEvent { event, .. } => {
|
2021-11-07 19:58:02 +00:00
|
|
|
use glutin::event::WindowEvent;
|
|
|
|
if matches!(event, WindowEvent::CloseRequested | WindowEvent::Destroyed) {
|
2021-10-18 21:13:32 +00:00
|
|
|
*control_flow = glutin::event_loop::ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let glutin::event::WindowEvent::Resized(physical_size) = event {
|
|
|
|
gl_window.resize(physical_size);
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:45:51 +00:00
|
|
|
egui_glow.on_event(&event);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
gl_window.window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
|
|
|
|
}
|
|
|
|
glutin::event::Event::LoopDestroyed => {
|
2021-11-03 12:45:51 +00:00
|
|
|
egui_glow.destroy(&gl);
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|