diff --git a/Cargo.toml b/Cargo.toml index 6df9dcc..9c6bfa8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "egui-baseview" -version = "0.0.0" +version = "0.1.0" authors = ["Billy Messenger "] -edition = "2018" +edition = "2021" description = "A baseview backend for egui" license = "MIT" repository = "https://github.com/BillyDM/egui-baseview" @@ -15,12 +15,11 @@ readme = "README.md" [features] default = ["opengl"] -opengl = ["raw-gl-context", "egui_glow"] +opengl = ["egui_glow", "baseview/opengl"] [dependencies] egui = "0.19" egui_glow = { version = "0.19", optional = true } -raw-gl-context = { version = "0.1", optional = true } keyboard-types = { version = "0.6.1", default-features = false } baseview = { git = "https://github.com/RustAudio/baseview.git", rev = "b3712638bacb3fdf2883cb5aa3f6caed0e91ac8c" } raw-window-handle = "0.4.2" diff --git a/README.md b/README.md index 33e2e04..fed02cb 100644 --- a/README.md +++ b/README.md @@ -14,16 +14,14 @@ A [`baseview`] backend for [`egui`]. ```rust use baseview::{Size, WindowOpenOptions, WindowScalePolicy}; use egui::Context; -use egui_baseview::{EguiWindow, Queue, RenderSettings, Settings}; +use egui_baseview::{EguiWindow, Queue}; fn main() { - let settings = Settings { - window: WindowOpenOptions { - title: String::from("egui-baseview simple demo"), - size: Size::new(400.0, 200.0), - scale: WindowScalePolicy::SystemScaleFactor, - }, - render_settings: RenderSettings::default(), + let settings = WindowOpenOptions { + title: String::from("egui-baseview simple demo"), + size: Size::new(400.0, 200.0), + scale: WindowScalePolicy::SystemScaleFactor, + gl_config: Some(Default::default()), }; let state = State::new(); diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 8521f62..3394faa 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -1,15 +1,13 @@ use baseview::{Size, WindowOpenOptions, WindowScalePolicy}; use egui::Context; -use egui_baseview::{EguiWindow, Queue, RenderSettings, Settings}; +use egui_baseview::{EguiWindow, Queue}; fn main() { - let settings = Settings { - window: WindowOpenOptions { - title: String::from("egui-baseview hello world"), - size: Size::new(300.0, 110.0), - scale: WindowScalePolicy::SystemScaleFactor, - }, - render_settings: RenderSettings::default(), + let settings = WindowOpenOptions { + title: String::from("egui-baseview hello world"), + size: Size::new(300.0, 110.0), + scale: WindowScalePolicy::SystemScaleFactor, + gl_config: Some(Default::default()), }; let state = (); diff --git a/examples/simple_demo.rs b/examples/simple_demo.rs index 2ad44da..dc33873 100644 --- a/examples/simple_demo.rs +++ b/examples/simple_demo.rs @@ -1,15 +1,13 @@ use baseview::{Size, WindowOpenOptions, WindowScalePolicy}; use egui::Context; -use egui_baseview::{EguiWindow, Queue, RenderSettings, Settings}; +use egui_baseview::{EguiWindow, Queue}; fn main() { - let settings = Settings { - window: WindowOpenOptions { - title: String::from("egui-baseview simple demo"), - size: Size::new(400.0, 200.0), - scale: WindowScalePolicy::SystemScaleFactor, - }, - render_settings: RenderSettings::default(), + let settings = WindowOpenOptions { + title: String::from("egui-baseview simple demo"), + size: Size::new(400.0, 200.0), + scale: WindowScalePolicy::SystemScaleFactor, + gl_config: Some(Default::default()), }; let state = State::new(); diff --git a/src/lib.rs b/src/lib.rs index 4ea5e98..9d4ee3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,4 @@ mod renderer; -mod settings; mod window; -pub use renderer::RenderSettings; -pub use settings::Settings; pub use window::{EguiWindow, Queue}; diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 2d87c9a..9e93321 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -1,6 +1,4 @@ #[cfg(feature = "opengl")] mod opengl_renderer; #[cfg(feature = "opengl")] -pub use opengl_renderer::RenderSettings; -#[cfg(feature = "opengl")] pub(crate) use opengl_renderer::Renderer; diff --git a/src/renderer/opengl_renderer.rs b/src/renderer/opengl_renderer.rs index 3427e42..a963381 100644 --- a/src/renderer/opengl_renderer.rs +++ b/src/renderer/opengl_renderer.rs @@ -1,20 +1,20 @@ use baseview::Window; use egui_glow::Painter; -use raw_gl_context::GlContext; use std::sync::Arc; -pub use raw_gl_context::GlConfig as RenderSettings; - pub struct Renderer { - context: GlContext, glow_context: Arc, painter: Painter, } impl Renderer { - pub fn new(window: &Window, render_settings: RenderSettings) -> Self { - let context = GlContext::create(window, render_settings).unwrap(); - context.make_current(); + pub fn new(window: &Window) -> Self { + let context = window + .gl_context() + .expect("failed to get baseview gl context"); + unsafe { + context.make_current(); + } let glow_context = Arc::new(unsafe { egui_glow::glow::Context::from_loader_function(|s| context.get_proc_address(s)) @@ -26,10 +26,11 @@ impl Renderer { }) .unwrap(); - context.make_not_current(); + unsafe { + context.make_not_current(); + } Self { - context, glow_context, painter, } @@ -37,6 +38,7 @@ impl Renderer { pub fn render( &mut self, + window: &Window, bg_color: egui::Rgba, canvas_width: u32, canvas_height: u32, @@ -48,7 +50,12 @@ impl Renderer { let shapes = std::mem::take(shapes); let mut textures_delta = std::mem::take(textures_delta); - self.context.make_current(); + let context = window + .gl_context() + .expect("failed to get baseview gl context"); + unsafe { + context.make_current(); + } unsafe { use egui_glow::glow::HasContext as _; @@ -71,8 +78,10 @@ impl Renderer { self.painter.free_texture(id); } - self.context.swap_buffers(); - self.context.make_not_current(); + unsafe { + context.swap_buffers(); + context.make_not_current(); + } } } diff --git a/src/settings.rs b/src/settings.rs deleted file mode 100644 index 0235863..0000000 --- a/src/settings.rs +++ /dev/null @@ -1,13 +0,0 @@ -//! Configure your application; - -use crate::RenderSettings; -use baseview::WindowOpenOptions; - -/// The settings of an application. -pub struct Settings { - /// The `baseview` window settings. - pub window: WindowOpenOptions, - - /// The settings for the rendering backend. - pub render_settings: RenderSettings, -} diff --git a/src/window.rs b/src/window.rs index a94e804..3fefdcf 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,11 +1,12 @@ -use baseview::{Event, EventStatus, Window, WindowHandle, WindowHandler, WindowScalePolicy}; +use baseview::{ + Event, EventStatus, Window, WindowHandle, WindowHandler, WindowOpenOptions, WindowScalePolicy, +}; use copypasta::ClipboardProvider; use egui::{pos2, vec2, Pos2, Rect, Rgba}; use raw_window_handle::HasRawWindowHandle; use std::time::Instant; -use crate::renderer::{RenderSettings, Renderer}; -use crate::Settings; +use crate::renderer::Renderer; pub struct Queue<'a> { bg_color: &'a mut Rgba, @@ -40,17 +41,17 @@ struct OpenSettings { } impl OpenSettings { - fn new(settings: &Settings) -> Self { + fn new(settings: &WindowOpenOptions) -> Self { // WindowScalePolicy does not implement copy/clone. - let scale_policy = match &settings.window.scale { + let scale_policy = match &settings.scale { WindowScalePolicy::SystemScaleFactor => WindowScalePolicy::SystemScaleFactor, WindowScalePolicy::ScaleFactor(scale) => WindowScalePolicy::ScaleFactor(*scale), }; Self { scale_policy, - logical_width: settings.window.size.width as f64, - logical_height: settings.window.size.height as f64, + logical_width: settings.size.width as f64, + logical_height: settings.size.height as f64, } } } @@ -90,7 +91,6 @@ where fn new( window: &mut baseview::Window<'_>, open_settings: OpenSettings, - mut render_settings: Option, mut build: B, update: U, mut state: State, @@ -129,7 +129,7 @@ where let physical_width = (open_settings.logical_width * scale as f64).round() as u32; let physical_height = (open_settings.logical_height * scale as f64).round() as u32; - let renderer = Renderer::new(window, render_settings.take().unwrap()); + let renderer = Renderer::new(window); let mut bg_color = Rgba::BLACK; let mut close_requested = false; @@ -181,7 +181,7 @@ where /// application and build the UI. pub fn open_parented( parent: &P, - settings: Settings, + mut settings: WindowOpenOptions, state: State, build: B, update: U, @@ -191,14 +191,17 @@ where B: FnMut(&egui::Context, &mut Queue, &mut State), B: 'static + Send, { + if settings.gl_config.is_none() { + settings.gl_config = Some(Default::default()); + } + let open_settings = OpenSettings::new(&settings); - let render_settings = Some(settings.render_settings); Window::open_parented( parent, - settings.window, + settings, move |window: &mut baseview::Window<'_>| -> EguiWindow { - EguiWindow::new(window, open_settings, render_settings, build, update, state) + EguiWindow::new(window, open_settings, build, update, state) }, ) } @@ -212,7 +215,7 @@ where /// * `update` - Called before each frame. Here you should update the state of your /// application and build the UI. pub fn open_as_if_parented( - settings: Settings, + mut settings: WindowOpenOptions, state: State, build: B, update: U, @@ -221,13 +224,16 @@ where B: FnMut(&egui::Context, &mut Queue, &mut State), B: 'static + Send, { + if settings.gl_config.is_none() { + settings.gl_config = Some(Default::default()); + } + let open_settings = OpenSettings::new(&settings); - let render_settings = Some(settings.render_settings); Window::open_as_if_parented( - settings.window, + settings, move |window: &mut baseview::Window<'_>| -> EguiWindow { - EguiWindow::new(window, open_settings, render_settings, build, update, state) + EguiWindow::new(window, open_settings, build, update, state) }, ) } @@ -240,18 +246,21 @@ where /// call `ctx.set_fonts()`. Optional. /// * `update` - Called before each frame. Here you should update the state of your /// application and build the UI. - pub fn open_blocking(settings: Settings, state: State, build: B, update: U) + pub fn open_blocking(mut settings: WindowOpenOptions, state: State, build: B, update: U) where B: FnMut(&egui::Context, &mut Queue, &mut State), B: 'static + Send, { + if settings.gl_config.is_none() { + settings.gl_config = Some(Default::default()); + } + let open_settings = OpenSettings::new(&settings); - let render_settings = Some(settings.render_settings); Window::open_blocking( - settings.window, + settings, move |window: &mut baseview::Window<'_>| -> EguiWindow { - EguiWindow::new(window, open_settings, render_settings, build, update, state) + EguiWindow::new(window, open_settings, build, update, state) }, ) } @@ -294,6 +303,7 @@ where if do_repaint_now { self.renderer.render( + window, self.bg_color, self.physical_width, self.physical_height,