Skip to content

Commit

Permalink
Merge pull request #36 from osimarr/main
Browse files Browse the repository at this point in the history
Update egui to 0.26.2 and sdl2 to 0.36
  • Loading branch information
ArjunNair authored Mar 14, 2024
2 parents 4b4ee25 + e0b3b57 commit c2ac222
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 342 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"cmake.configureOnOpen": false
"cmake.configureOnOpen": false,
"rust-analyzer.cargo.features": [
"use_epi"
]
}
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "egui_sdl2_gl"
version = "0.23.0"
version = "0.26.2"
authors = ["Arjun Nair <[email protected]>"]
edition = "2018"
description = "Backend for Egui to use with sdl2-rs and open gl"
Expand All @@ -15,15 +15,16 @@ include = ["**/*.rs", "Cargo.toml"]
[dependencies]
ahash = "~0.8"
gl = "~0.14"
egui = "~0.23"
sdl2 = { version = "~0.35" }
egui = "~0.26"
sdl2 = { version = "~0.36" }
memoffset = "0.9.0"

[dependencies.epi]
version = "0.17"
optional = true

[features]
default-features = ["sdl2_bundled"]
default = ["sdl2_bundled"]

sdl2_unsafe_textures = ["sdl2/unsafe_textures"]
sdl2_gfx = ["sdl2/gfx"]
Expand All @@ -40,4 +41,4 @@ sdl2_static-link = ["sdl2/static-link"]
use_epi = ["epi"]

[dev-dependencies]
egui_demo_lib = "0.23.0"
egui_demo_lib = "~0.26"
39 changes: 25 additions & 14 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,26 @@ fn main() {
let mut quit = false;
let mut slider = 0.0;

if enable_vsync {
if let Err(error) = window.subsystem().gl_set_swap_interval(SwapInterval::VSync) {
println!(
"Failed to gl_set_swap_interval(SwapInterval::VSync): {}",
error
);
}
} else if let Err(error) = window
.subsystem()
.gl_set_swap_interval(SwapInterval::Immediate)
{
println!(
"Failed to gl_set_swap_interval(SwapInterval::Immediate): {}",
error
);
}

let start_time = Instant::now();

'running: loop {
if enable_vsync {
window
.subsystem()
.gl_set_swap_interval(SwapInterval::VSync)
.unwrap()
} else {
window
.subsystem()
.gl_set_swap_interval(SwapInterval::Immediate)
.unwrap()
}

unsafe {
// Clear the screen to green
gl::ClearColor(0.3, 0.6, 0.3, 1.0);
Expand All @@ -90,9 +95,10 @@ fn main() {

let FullOutput {
platform_output,
repaint_after,
textures_delta,
shapes,
pixels_per_point,
viewport_output,
} = egui_ctx.end_frame();

// Process ouput
Expand All @@ -106,10 +112,15 @@ fn main() {
// window.set_size(w, h).unwrap();
// }

let paint_jobs = egui_ctx.tessellate(shapes);
let paint_jobs = egui_ctx.tessellate(shapes, pixels_per_point);
painter.paint_jobs(None, textures_delta, paint_jobs);
window.gl_swap_window();

let repaint_after = viewport_output
.get(&egui::ViewportId::ROOT)
.expect("Missing ViewportId::ROOT")
.repaint_delay;

if !repaint_after.is_zero() {
if let Some(event) = event_pump.wait_event_timeout(5) {
match event {
Expand Down
21 changes: 15 additions & 6 deletions examples/demo_lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(not(feature = "use_epi"))]
compile_error!("feature \"use_epi\" must be used");

use egui::ViewportId;
use egui_backend::{
egui::{self, FullOutput},
epi::{Frame, IntegrationInfo},
Expand Down Expand Up @@ -49,10 +50,12 @@ fn main() {
debug_assert_eq!(gl_attr.context_version(), (3, 2));

// Enable vsync
window
.subsystem()
.gl_set_swap_interval(SwapInterval::VSync)
.unwrap();
if let Err(error) = window.subsystem().gl_set_swap_interval(SwapInterval::VSync) {
println!(
"Failed to gl_set_swap_interval(SwapInterval::VSync): {}",
error
);
}

// Init egui stuff
let (mut painter, mut egui_state) =
Expand Down Expand Up @@ -84,9 +87,10 @@ fn main() {

let FullOutput {
platform_output,
repaint_after,
textures_delta,
shapes,
pixels_per_point,
viewport_output,
} = egui_ctx.end_frame();
// Process ouput
egui_state.process_output(&window, &platform_output);
Expand All @@ -95,6 +99,11 @@ fn main() {
break 'running;
}

let repaint_after = viewport_output
.get(&ViewportId::ROOT)
.expect("Missing ViewportId::ROOT")
.repaint_delay;

if !repaint_after.is_zero() {
// Reactive every 1 second.
if let Some(event) = event_pump.wait_event_timeout(1000) {
Expand Down Expand Up @@ -126,7 +135,7 @@ fn main() {
// window.set_size(w, h).unwrap();
// }

let paint_jobs = egui_ctx.tessellate(shapes);
let paint_jobs = egui_ctx.tessellate(shapes, pixels_per_point);

// An example of how OpenGL can be used to draw custom stuff with egui
// overlaying it:
Expand Down
21 changes: 15 additions & 6 deletions examples/mix/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::time::Instant;

//Alias the backend to something less mouthful
use egui::load::SizedTexture;
use egui::ViewportId;
use egui_backend::egui::{vec2, Color32, FullOutput, Image};
use egui_backend::sdl2::video::GLProfile;
use egui_backend::{egui, gl, sdl2};
Expand Down Expand Up @@ -48,10 +49,12 @@ fn main() {
debug_assert_eq!(gl_attr.context_version(), (3, 2));

// Enable vsync
window
.subsystem()
.gl_set_swap_interval(SwapInterval::VSync)
.unwrap();
if let Err(error) = window.subsystem().gl_set_swap_interval(SwapInterval::VSync) {
println!(
"Failed to gl_set_swap_interval(SwapInterval::VSync): {}",
error
);
};

// Init egui stuff
let (mut painter, mut egui_state) =
Expand Down Expand Up @@ -139,14 +142,15 @@ fn main() {

let FullOutput {
platform_output,
repaint_after,
textures_delta,
shapes,
pixels_per_point,
viewport_output,
} = egui_ctx.end_frame();
// Process output
egui_state.process_output(&window, &platform_output);

let paint_jobs = egui_ctx.tessellate(shapes);
let paint_jobs = egui_ctx.tessellate(shapes, pixels_per_point);

// Note: passing a bg_color to paint_jobs will clear any previously drawn stuff.
// Use this only if egui is being used for all drawing and you aren't mixing your own Open GL
Expand All @@ -156,6 +160,11 @@ fn main() {

window.gl_swap_window();

let repaint_after = viewport_output
.get(&ViewportId::ROOT)
.expect("Missing ViewportId::ROOT")
.repaint_delay;

if !repaint_after.is_zero() {
if let Some(event) = event_pump.wait_event_timeout(5) {
match event {
Expand Down
4 changes: 2 additions & 2 deletions examples/mix/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use std::mem;
use std::ptr;
use std::str;

const VS_SRC: &'static str = "
const VS_SRC: &str = "
#version 150
in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}";

const FS_SRC: &'static str = "
const FS_SRC: &str = "
#version 150
out vec4 out_color;
Expand Down
19 changes: 14 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,20 @@ pub fn with_sdl2(

impl EguiStateHandler {
pub fn new(painter: Painter) -> (Painter, EguiStateHandler) {
let mut input = egui::RawInput {
screen_rect: Some(painter.screen_rect),
..Default::default()
};
input
.viewports
.entry(ViewportId::ROOT)
.or_default()
.native_pixels_per_point = Some(painter.pixels_per_point);
let native_pixels_per_point = painter.pixels_per_point;
let _self = EguiStateHandler {
fused_cursor: FusedCursor::default(),
pointer_pos: Pos2::new(0f32, 0f32),
input: egui::RawInput {
screen_rect: Some(painter.screen_rect),
pixels_per_point: Some(native_pixels_per_point),
..Default::default()
},
input,
modifiers: Modifiers::default(),
native_pixels_per_point,
};
Expand Down Expand Up @@ -251,6 +256,8 @@ pub fn input_to_egui(
pressed: false,
repeat,
modifiers: state.modifiers,
// TODO: implement support for physical_key
physical_key: None,
});
}

Expand Down Expand Up @@ -288,6 +295,8 @@ pub fn input_to_egui(
pressed: true,
repeat,
modifiers: state.modifiers,
// TODO: implement support for physical_key
physical_key: None,
});

if state.modifiers.command && key == Key::C {
Expand Down
Loading

0 comments on commit c2ac222

Please sign in to comment.