Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use egui::RawInput;
- use winit::event::{Event, WindowEvent};
- use winit::event_loop::{ControlFlow, EventLoop};
- use winit::window::WindowBuilder;
- use wgpu::{Instance, Device, Queue, RequestAdapterOptions};
- use egui_wgpu::Renderer;
- use egui_winit::{State, egui, egui::viewport::ViewportId};
- use std::process;
- async fn run(event_loop: EventLoop<()>, window: winit::window::Window) {
- let instance = Instance::new(wgpu::InstanceDescriptor {
- backends: wgpu::Backends::all(),
- ..Default::default()
- });
- let adapter = instance.request_adapter(&RequestAdapterOptions {
- power_preference: wgpu::PowerPreference::HighPerformance,
- compatible_surface: None,
- force_fallback_adapter: false,
- })
- .await
- .expect("Failed to find an adapter");
- let (device, queue) = adapter.request_device(&wgpu::DeviceDescriptor {
- label: None,
- required_features: wgpu::Features::empty(),
- required_limits: wgpu::Limits::default(),
- }, None)
- .await
- .expect("Failed to create device and queue");
- let mut state = State::new(
- egui::Context::default(),
- ViewportId::ROOT,
- &window,
- None,
- None,
- );
- let mut renderer = Renderer::new(&device, wgpu::TextureFormat::Bgra8UnormSrgb, None, 1);
- event_loop.run(move |event, control_flow| {
- match event {
- Event::WindowEvent { event, .. } => match event {
- WindowEvent::CloseRequested => {
- std::process::exit(0);
- }
- WindowEvent::RedrawRequested => {
- let raw_input = RawInput::default();
- state.egui_ctx().run(raw_input, move |ctx| {
- egui::CentralPanel::default().show(ctx, |ui| {
- ui.label("Hello from Egui!");
- if ui.button("Close").clicked() {
- std::process::exit(0);
- }
- });
- });
- state.handle_platform_output(&window, Default::default());
- }
- _ => {}
- },
- _ => {}
- }
- });
- }
- fn main() {
- let event_loop = EventLoop::new().expect("Failed to create event loop");
- let window = WindowBuilder::new()
- .with_title("Egui + WGPU")
- .build(&event_loop)
- .expect("Failed to create window");
- pollster::block_on(run(event_loop, window));
- }
Advertisement
Add Comment
Please, Sign In to add comment