diff --git a/egui_glium/src/epi_backend.rs b/egui_glium/src/epi_backend.rs index b22a5075..61dafe27 100644 --- a/egui_glium/src/epi_backend.rs +++ b/egui_glium/src/epi_backend.rs @@ -67,14 +67,10 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { std::thread::sleep(std::time::Duration::from_millis(10)); } - let (needs_repaint, mut textures_delta, shapes) = + let (needs_repaint, textures_delta, shapes) = integration.update(display.gl_window().window()); let clipped_meshes = integration.egui_ctx.tessellate(shapes); - for (id, image_delta) in textures_delta.set { - painter.set_texture(&display, id, &image_delta); - } - // paint: { use glium::Surface as _; @@ -82,20 +78,17 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { let color = integration.app.clear_color(); target.clear_color(color[0], color[1], color[2], color[3]); - painter.paint_meshes( + painter.paint_and_update_textures( &display, &mut target, integration.egui_ctx.pixels_per_point(), clipped_meshes, + &textures_delta, ); target.finish().unwrap(); } - for id in textures_delta.free.drain(..) { - painter.free_texture(id); - } - { *control_flow = if integration.should_quit() { glutin::event_loop::ControlFlow::Exit diff --git a/egui_glium/src/lib.rs b/egui_glium/src/lib.rs index 50426289..4bf8eb8e 100644 --- a/egui_glium/src/lib.rs +++ b/egui_glium/src/lib.rs @@ -157,22 +157,14 @@ impl EguiGlium { /// Paint the results of the last call to [`Self::run`]. pub fn paint(&mut self, display: &glium::Display, target: &mut T) { let shapes = std::mem::take(&mut self.shapes); - let mut textures_delta = std::mem::take(&mut self.textures_delta); - - for (id, image_delta) in textures_delta.set { - self.painter.set_texture(display, id, &image_delta); - } - + let textures_delta = std::mem::take(&mut self.textures_delta); let clipped_meshes = self.egui_ctx.tessellate(shapes); - self.painter.paint_meshes( + self.painter.paint_and_update_textures( display, target, self.egui_ctx.pixels_per_point(), clipped_meshes, + &textures_delta, ); - - for id in textures_delta.free.drain(..) { - self.painter.free_texture(id); - } } } diff --git a/egui_glium/src/painter.rs b/egui_glium/src/painter.rs index b176a2f7..26f74133 100644 --- a/egui_glium/src/painter.rs +++ b/egui_glium/src/painter.rs @@ -65,6 +65,25 @@ impl Painter { self.max_texture_side } + pub fn paint_and_update_textures( + &mut self, + display: &glium::Display, + target: &mut T, + pixels_per_point: f32, + clipped_meshes: Vec, + textures_delta: &egui::TexturesDelta, + ) { + for (id, image_delta) in &textures_delta.set { + self.set_texture(display, *id, image_delta); + } + + self.paint_meshes(display, target, pixels_per_point, clipped_meshes); + + for &id in &textures_delta.free { + self.free_texture(id); + } + } + /// Main entry-point for painting a frame. /// You should call `target.clear_color(..)` before /// and `target.finish()` after this. @@ -73,9 +92,9 @@ impl Painter { display: &glium::Display, target: &mut T, pixels_per_point: f32, - cipped_meshes: Vec, + clipped_meshes: Vec, ) { - for egui::ClippedMesh(clip_rect, mesh) in cipped_meshes { + for egui::ClippedMesh(clip_rect, mesh) in clipped_meshes { self.paint_mesh(target, display, pixels_per_point, clip_rect, &mesh); } } diff --git a/egui_glow/src/epi_backend.rs b/egui_glow/src/epi_backend.rs index fef06d10..54045e4e 100644 --- a/egui_glow/src/epi_backend.rs +++ b/egui_glow/src/epi_backend.rs @@ -83,14 +83,9 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { std::thread::sleep(std::time::Duration::from_millis(10)); } - let (needs_repaint, mut textures_delta, shapes) = - integration.update(gl_window.window()); + let (needs_repaint, textures_delta, shapes) = integration.update(gl_window.window()); let clipped_meshes = integration.egui_ctx.tessellate(shapes); - for (id, image_delta) in textures_delta.set { - painter.set_texture(&gl, id, &image_delta); - } - // paint: { let color = integration.app.clear_color(); @@ -100,20 +95,17 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { gl.clear_color(color[0], color[1], color[2], color[3]); gl.clear(glow::COLOR_BUFFER_BIT); } - painter.paint_meshes( + painter.paint_and_update_textures( &gl, gl_window.window().inner_size().into(), integration.egui_ctx.pixels_per_point(), clipped_meshes, + &textures_delta, ); gl_window.swap_buffers().unwrap(); } - for id in textures_delta.free.drain(..) { - painter.free_texture(&gl, id); - } - { *control_flow = if integration.should_quit() { winit::event_loop::ControlFlow::Exit diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index 0f9d5f44..c8c59b0b 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -270,6 +270,25 @@ impl Painter { (width_in_pixels, height_in_pixels) } + pub fn paint_and_update_textures( + &mut self, + gl: &glow::Context, + inner_size: [u32; 2], + pixels_per_point: f32, + clipped_meshes: Vec, + textures_delta: &egui::TexturesDelta, + ) { + for (id, image_delta) in &textures_delta.set { + self.set_texture(gl, *id, image_delta); + } + + self.paint_meshes(gl, inner_size, pixels_per_point, clipped_meshes); + + for &id in &textures_delta.free { + self.free_texture(gl, id); + } + } + /// Main entry-point for painting a frame. /// You should call `target.clear_color(..)` before /// and `target.finish()` after this. diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index 134a2bc2..08e22f9e 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -294,17 +294,14 @@ impl AppRunner { /// Paint the results of the last call to [`Self::logic`]. pub fn paint(&mut self, clipped_meshes: Vec) -> Result<(), JsValue> { let textures_delta = std::mem::take(&mut self.textures_delta); - for (id, image_delta) in textures_delta.set { - self.painter.set_texture(id, &image_delta); - } self.painter.clear(self.app.clear_color()); - self.painter - .paint_meshes(clipped_meshes, self.egui_ctx.pixels_per_point())?; - for id in textures_delta.free { - self.painter.free_texture(id); - } + self.painter.paint_and_update_textures( + clipped_meshes, + self.egui_ctx.pixels_per_point(), + &textures_delta, + )?; Ok(()) } diff --git a/egui_web/src/painter.rs b/egui_web/src/painter.rs index 16bc8e49..f0c26e7c 100644 --- a/egui_web/src/painter.rs +++ b/egui_web/src/painter.rs @@ -22,4 +22,23 @@ pub trait Painter { ) -> Result<(), JsValue>; fn name(&self) -> &'static str; + + fn paint_and_update_textures( + &mut self, + clipped_meshes: Vec, + pixels_per_point: f32, + textures_delta: &egui::TexturesDelta, + ) -> Result<(), JsValue> { + for (id, image_delta) in &textures_delta.set { + self.set_texture(*id, image_delta); + } + + self.paint_meshes(clipped_meshes, pixels_per_point)?; + + for &id in &textures_delta.free { + self.free_texture(id); + } + + Ok(()) + } }