Skip to content

Commit

Permalink
use baseview's built-in opengl context
Browse files Browse the repository at this point in the history
  • Loading branch information
Billy Messenger authored and Billy Messenger committed Sep 22, 2022
1 parent 4aaaa2e commit 679b8cd
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 79 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "egui-baseview"
version = "0.0.0"
version = "0.1.0"
authors = ["Billy Messenger <[email protected]>"]
edition = "2018"
edition = "2021"
description = "A baseview backend for egui"
license = "MIT"
repository = "https://github.com/BillyDM/egui-baseview"
Expand All @@ -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"
Expand Down
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 6 additions & 8 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
@@ -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 = ();
Expand Down
14 changes: 6 additions & 8 deletions examples/simple_demo.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
mod renderer;
mod settings;
mod window;

pub use renderer::RenderSettings;
pub use settings::Settings;
pub use window::{EguiWindow, Queue};
2 changes: 0 additions & 2 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
33 changes: 21 additions & 12 deletions src/renderer/opengl_renderer.rs
Original file line number Diff line number Diff line change
@@ -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<egui_glow::glow::Context>,
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))
Expand All @@ -26,17 +26,19 @@ impl Renderer {
})
.unwrap();

context.make_not_current();
unsafe {
context.make_not_current();
}

Self {
context,
glow_context,
painter,
}
}

pub fn render(
&mut self,
window: &Window,
bg_color: egui::Rgba,
canvas_width: u32,
canvas_height: u32,
Expand All @@ -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 _;
Expand All @@ -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();
}
}
}

Expand Down
13 changes: 0 additions & 13 deletions src/settings.rs

This file was deleted.

52 changes: 31 additions & 21 deletions src/window.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -90,7 +91,6 @@ where
fn new<B>(
window: &mut baseview::Window<'_>,
open_settings: OpenSettings,
mut render_settings: Option<RenderSettings>,
mut build: B,
update: U,
mut state: State,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -181,7 +181,7 @@ where
/// application and build the UI.
pub fn open_parented<P, B>(
parent: &P,
settings: Settings,
mut settings: WindowOpenOptions,
state: State,
build: B,
update: U,
Expand All @@ -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<State, U> {
EguiWindow::new(window, open_settings, render_settings, build, update, state)
EguiWindow::new(window, open_settings, build, update, state)
},
)
}
Expand All @@ -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<B>(
settings: Settings,
mut settings: WindowOpenOptions,
state: State,
build: B,
update: U,
Expand All @@ -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<State, U> {
EguiWindow::new(window, open_settings, render_settings, build, update, state)
EguiWindow::new(window, open_settings, build, update, state)
},
)
}
Expand All @@ -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<B>(settings: Settings, state: State, build: B, update: U)
pub fn open_blocking<B>(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<State, U> {
EguiWindow::new(window, open_settings, render_settings, build, update, state)
EguiWindow::new(window, open_settings, build, update, state)
},
)
}
Expand Down Expand Up @@ -294,6 +303,7 @@ where

if do_repaint_now {
self.renderer.render(
window,
self.bg_color,
self.physical_width,
self.physical_height,
Expand Down

0 comments on commit 679b8cd

Please sign in to comment.