From c11d81de866c2e3ea4d3de3ff9df633d6b2ce951 Mon Sep 17 00:00:00 2001 From: ghostly_zsh Date: Thu, 20 Mar 2025 18:35:39 -0500 Subject: [PATCH] not quite working egui --- crates/client/Cargo.toml | 5 ++-- crates/client/src/rendering/mod.rs | 39 ++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 77bb2ba2c7b98a8dd0610181e128319e60ec1d88..74272bea2242163b5a9c696e513e95b22e3465ee 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -11,12 +11,13 @@ crate-type = ["cdylib", "rlib"] tracing = "0.1" # Log system tracing-subscriber = "0.3" # Log layers bevy_ecs = "0.15" -egui = "0.30" +egui = "0.31.1" +egui_glow = { version = "0.31.1", features = ["winit"] } winit = "0.30" glow = "0.16.0" thiserror = "2" image = "0.25" -egui-winit = { version = "0.30", default-features = false, features = ["links", "wayland", "x11"] } +egui-winit = { version = "0.31.1", default-features = false, features = ["links", "wayland", "x11"] } web-time = "1" futures = "0.3" nalgebra = "0.33" diff --git a/crates/client/src/rendering/mod.rs b/crates/client/src/rendering/mod.rs index 245fb2df7065161bcd116f8938f1bd96c92eb28b..d7fe0ffd1e690fcb473c4fa9f38c5a2b02e29825 100644 --- a/crates/client/src/rendering/mod.rs +++ b/crates/client/src/rendering/mod.rs @@ -1,5 +1,7 @@ use std::num::NonZeroU32; +use std::sync::Arc; +use egui_glow::EguiGlow; use glow::{HasContext, PixelUnpackData}; #[cfg(not(target_arch = "wasm32"))] use glutin::surface::{Surface, WindowSurface, GlSurface, SwapInterval}; @@ -28,7 +30,8 @@ pub struct App { gl_surface: Option>, #[cfg(not(target_arch = "wasm32"))] gl_context: Option, - gl: Option, + egui_glow: Option, + gl: Option>, } const VERTICES: [f32; 16] = [ @@ -69,7 +72,7 @@ impl ApplicationHandler for App { .dyn_into::() .unwrap(); #[cfg(target_arch = "wasm32")] - let (gl, shader_version) = (glow::Context::from_webgl2_context(context), "#version 300 es"); + let (gl, shader_version) = (Arc::new(glow::Context::from_webgl2_context(context)), "#version 300 es"); #[cfg(not(target_arch = "wasm32"))] let (gl, shader_version) = unsafe { @@ -79,7 +82,9 @@ impl ApplicationHandler for App { let (window, gl_config) = display_builder.build(event_loop, template, |configs| { configs.reduce(|accum, config| { - if config.num_samples() > accum.num_samples() { + let supports_transparency = config.supports_transparency().unwrap_or(false) + && !accum.supports_transparency().unwrap_or(false); + if supports_transparency || config.num_samples() > accum.num_samples() { config } else { accum @@ -109,7 +114,7 @@ impl ApplicationHandler for App { self.gl_surface = Some(gl_surface); self.gl_context = Some(gl_context); - (gl, "#version 300 es") + (Arc::new(gl), "#version 300 es") }; unsafe { let shaders = [ @@ -142,7 +147,7 @@ impl ApplicationHandler for App { std::slice::from_raw_parts(VERTICES.as_ptr() as *const u8, size_of_val(&VERTICES)), glow::STATIC_DRAW); gl.buffer_data_u8_slice(glow::ELEMENT_ARRAY_BUFFER, - std::slice::from_raw_parts(INDICES.as_ptr() as *const u8, 6*4), + std::slice::from_raw_parts(INDICES.as_ptr() as *const u8, size_of_val(&INDICES)), glow::STATIC_DRAW); gl.vertex_attrib_pointer_f32(0, 2, glow::FLOAT, false, 4*size_of::() as i32, 0); @@ -177,6 +182,9 @@ impl ApplicationHandler for App { canvas.set_width(web_sys::window().unwrap().inner_width().unwrap().as_f64().unwrap() as u32); canvas.set_height(web_sys::window().unwrap().inner_height().unwrap().as_f64().unwrap() as u32); }).into_js_value().as_ref().unchecked_ref())); + + let egui_glow = egui_glow::EguiGlow::new(event_loop, gl.clone(), None, None, true); + self.egui_glow = Some(egui_glow); self.gl = Some(gl); } fn window_event( @@ -202,16 +210,33 @@ impl ApplicationHandler for App { let window = self.window.as_ref().unwrap(); let gl = self.gl.as_ref().unwrap(); + self.egui_glow.as_mut().unwrap().run( + self.window.as_ref().unwrap(), + |ctx| { + egui::Window::new("Main Menu").resizable(false).show(ctx, |ui| { + ui.heading("Starkingdoms.tk"); + }); + }, + ); + unsafe { gl.clear(glow::COLOR_BUFFER_BIT); + gl.bind_buffer(glow::ARRAY_BUFFER, self.vertex_buffer); + gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, self.element_buffer); + gl.bind_vertex_array(self.vertex_array); gl.draw_elements(glow::TRIANGLES, 6, glow::UNSIGNED_INT, 0); - #[cfg(not(target_arch = "wasm32"))] - self.gl_surface.as_ref().unwrap().swap_buffers(self.gl_context.as_ref().unwrap()).unwrap(); } + self.egui_glow.as_mut().unwrap().paint(self.window.as_ref().unwrap()); + + #[cfg(not(target_arch = "wasm32"))] + self.gl_surface.as_ref().unwrap().swap_buffers(self.gl_context.as_ref().unwrap()).unwrap(); + window.request_redraw(); } _ => {} } + let event_response = self.egui_glow.as_mut().unwrap() + .on_window_event(self.window.as_ref().unwrap(), &event); } }