|
| 1 | +//! Example how to use pure `egui_glow` without [`epi`]. |
| 2 | +
|
| 3 | +fn create_display( |
| 4 | + event_loop: &glutin::event_loop::EventLoop<()>, |
| 5 | +) -> ( |
| 6 | + glutin::WindowedContext<glutin::PossiblyCurrent>, |
| 7 | + glow::Context, |
| 8 | +) { |
| 9 | + let window_builder = glutin::window::WindowBuilder::new() |
| 10 | + .with_resizable(true) |
| 11 | + .with_inner_size(glutin::dpi::LogicalSize { |
| 12 | + width: 800.0, |
| 13 | + height: 600.0, |
| 14 | + }) |
| 15 | + .with_title("egui_glow example"); |
| 16 | + |
| 17 | + let gl_window = unsafe { |
| 18 | + glutin::ContextBuilder::new() |
| 19 | + .with_depth_buffer(0) |
| 20 | + .with_srgb(true) |
| 21 | + .with_stencil_buffer(0) |
| 22 | + .with_vsync(true) |
| 23 | + .build_windowed(window_builder, event_loop) |
| 24 | + .unwrap() |
| 25 | + .make_current() |
| 26 | + .unwrap() |
| 27 | + }; |
| 28 | + |
| 29 | + let gl = unsafe { glow::Context::from_loader_function(|s| gl_window.get_proc_address(s)) }; |
| 30 | + |
| 31 | + unsafe { |
| 32 | + use glow::HasContext; |
| 33 | + gl.enable(glow::FRAMEBUFFER_SRGB); |
| 34 | + } |
| 35 | + |
| 36 | + (gl_window, gl) |
| 37 | +} |
| 38 | + |
| 39 | +fn main() { |
| 40 | + let event_loop = glutin::event_loop::EventLoop::with_user_event(); |
| 41 | + let (gl_window, gl) = create_display(&event_loop); |
| 42 | + |
| 43 | + let mut egui = egui_glow::EguiGlow::new(&gl_window, &gl); |
| 44 | + |
| 45 | + event_loop.run(move |event, _, control_flow| { |
| 46 | + let mut redraw = || { |
| 47 | + egui.begin_frame(gl_window.window()); |
| 48 | + |
| 49 | + let mut quit = false; |
| 50 | + |
| 51 | + egui::SidePanel::left("my_side_panel").show(egui.ctx(), |ui| { |
| 52 | + ui.heading("Hello World!"); |
| 53 | + if ui.button("Quit").clicked() { |
| 54 | + quit = true; |
| 55 | + } |
| 56 | + |
| 57 | + egui::ComboBox::from_label("Version") |
| 58 | + .width(150.0) |
| 59 | + .selected_text("foo") |
| 60 | + .show_ui(ui, |ui| { |
| 61 | + egui::CollapsingHeader::new("Dev") |
| 62 | + .default_open(true) |
| 63 | + .show(ui, |ui| { |
| 64 | + ui.label("contents"); |
| 65 | + }); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + let (needs_repaint, shapes) = egui.end_frame(gl_window.window()); |
| 70 | + |
| 71 | + *control_flow = if quit { |
| 72 | + glutin::event_loop::ControlFlow::Exit |
| 73 | + } else if needs_repaint { |
| 74 | + gl_window.window().request_redraw(); |
| 75 | + glutin::event_loop::ControlFlow::Poll |
| 76 | + } else { |
| 77 | + glutin::event_loop::ControlFlow::Wait |
| 78 | + }; |
| 79 | + |
| 80 | + { |
| 81 | + let clear_color = egui::Rgba::from_rgb(0.1, 0.3, 0.2); |
| 82 | + unsafe { |
| 83 | + use glow::HasContext; |
| 84 | + gl.clear_color( |
| 85 | + clear_color[0], |
| 86 | + clear_color[1], |
| 87 | + clear_color[2], |
| 88 | + clear_color[3], |
| 89 | + ); |
| 90 | + gl.clear(glow::COLOR_BUFFER_BIT); |
| 91 | + } |
| 92 | + |
| 93 | + // draw things behind egui here |
| 94 | + |
| 95 | + egui.paint(&gl_window, &gl, shapes); |
| 96 | + |
| 97 | + // draw things on top of egui here |
| 98 | + |
| 99 | + gl_window.swap_buffers().unwrap(); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + match event { |
| 104 | + // Platform-dependent event handlers to workaround a winit bug |
| 105 | + // See: https://github.com/rust-windowing/winit/issues/987 |
| 106 | + // See: https://github.com/rust-windowing/winit/issues/1619 |
| 107 | + glutin::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(), |
| 108 | + glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(), |
| 109 | + |
| 110 | + glutin::event::Event::WindowEvent { event, .. } => { |
| 111 | + if egui.is_quit_event(&event) { |
| 112 | + *control_flow = glutin::event_loop::ControlFlow::Exit; |
| 113 | + } |
| 114 | + |
| 115 | + if let glutin::event::WindowEvent::Resized(physical_size) = event { |
| 116 | + gl_window.resize(physical_size); |
| 117 | + } |
| 118 | + |
| 119 | + egui.on_event(&event); |
| 120 | + |
| 121 | + gl_window.window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead |
| 122 | + } |
| 123 | + glutin::event::Event::LoopDestroyed => { |
| 124 | + egui.destroy(&gl); |
| 125 | + } |
| 126 | + |
| 127 | + _ => (), |
| 128 | + } |
| 129 | + }); |
| 130 | +} |
0 commit comments