From 44bf8c5d7ec70ae666977d7d72cc5ed4e8df2558 Mon Sep 17 00:00:00 2001 From: Arjun Nair Date: Sun, 22 Oct 2023 22:08:38 +0530 Subject: [PATCH] Refactored code for dpi calculation so that default just works. Fixed clippy complaint. --- examples/basic.rs | 6 +++--- examples/demo_lib.rs | 4 ++-- src/lib.rs | 19 +++++++++++++------ src/painter.rs | 5 +++-- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 10fa1b7..fa7df6a 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,7 +1,7 @@ use egui::Checkbox; +use egui_backend::egui::FullOutput; use egui_backend::sdl2::video::GLProfile; use egui_backend::{egui, gl, sdl2}; -use egui_backend::egui::FullOutput; use egui_backend::{sdl2::event::Event, DpiScaling, ShaderVersion}; use std::time::Instant; // Alias the backend to something less mouthful @@ -40,7 +40,7 @@ fn main() { // On linux use GLES SL 100+, like so: // let shader_ver = ShaderVersion::Adaptive; let (mut painter, mut egui_state) = - egui_backend::with_sdl2(&window, shader_ver, DpiScaling::Custom(2.0)); + egui_backend::with_sdl2(&window, shader_ver, DpiScaling::Default); let egui_ctx = egui::Context::default(); let mut event_pump = sdl_context.event_pump().unwrap(); @@ -65,7 +65,7 @@ fn main() { .gl_set_swap_interval(SwapInterval::Immediate) .unwrap() } - + unsafe { // Clear the screen to green gl::ClearColor(0.3, 0.6, 0.3, 1.0); diff --git a/examples/demo_lib.rs b/examples/demo_lib.rs index 55b5dd3..1772910 100644 --- a/examples/demo_lib.rs +++ b/examples/demo_lib.rs @@ -56,7 +56,7 @@ fn main() { // Init egui stuff let (mut painter, mut egui_state) = - egui_backend::with_sdl2(&window, ShaderVersion::Default, DpiScaling::Custom(1.25)); + egui_backend::with_sdl2(&window, ShaderVersion::Default, DpiScaling::Default); let mut demo_windows = egui_demo_lib::DemoWindows::default(); let egui_ctx = egui::Context::default(); let mut event_pump = sdl_context.event_pump().unwrap(); @@ -81,7 +81,7 @@ fn main() { }); demo_windows.ui(&egui_ctx); - + let FullOutput { platform_output, repaint_after, diff --git a/src/lib.rs b/src/lib.rs index efe9277..483866a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -88,14 +88,21 @@ pub fn with_sdl2( shader_ver: ShaderVersion, scale: DpiScaling, ) -> (Painter, EguiStateHandler) { - let display_dpi = window - .subsystem() - .display_dpi(0) - .unwrap_or((96.0, 96.0, 96.0)); + let standard_dpi = 96.0; + let display_dpi = + window + .subsystem() + .display_dpi(0) + .unwrap_or((standard_dpi, standard_dpi, standard_dpi)); + let dpi_scale = standard_dpi / display_dpi.0; + let normalized_scale = 1.0 / dpi_scale; + let default_scale = dpi_scale * normalized_scale; + println!("Display DPI detected: {:?}", display_dpi); let scale = match scale { - DpiScaling::Default => 96.0 / display_dpi.0, - DpiScaling::Custom(custom) => (96.0 / display_dpi.0) * custom, + DpiScaling::Default => default_scale, + DpiScaling::Custom(custom) => default_scale * custom, }; + println!("Scaling display to {}", scale); let painter = painter::Painter::new(window, scale, shader_ver); EguiStateHandler::new(painter) } diff --git a/src/painter.rs b/src/painter.rs index 84651f5..103c27f 100644 --- a/src/painter.rs +++ b/src/painter.rs @@ -514,8 +514,9 @@ impl Painter { { match primitive { Primitive::Mesh(mesh) => { - if let Some(Texture { gl_id, .. }) = self.textures.get(&mesh.texture_id) { - if let Some(texture_gl_id) = gl_id { + if let Some(Texture {gl_id: Some(texture_gl_id), .. }) = self.textures.get(&mesh.texture_id) { + //if let Some(texture_gl_id) = gl_id + { gl::BindTexture(gl::TEXTURE_2D, *texture_gl_id); let clip_min_x = pixels_per_point * clip_rect.min.x;