Skip to content

Commit

Permalink
Merge pull request imgui-rs#619 from Rob2309/main
Browse files Browse the repository at this point in the history
Expose Viewport functionality
  • Loading branch information
dbr authored Feb 2, 2023
2 parents e08707e + 29978d3 commit a2ad8dd
Show file tree
Hide file tree
Showing 16 changed files with 2,162 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

strategy:
matrix:
rust: ["1.57"]
rust: ["1.60"]

env:
RUSTFLAGS: -D warnings
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:
matrix:
rust:
- stable
- "1.57"
- "1.60"
os:
- ubuntu-latest
- macos-latest
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"imgui-glow-renderer",
"imgui-sdl2-support",
"imgui-winit-support",
"imgui-winit-glow-renderer-viewports",
"imgui-examples",
"xtask",
]
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ be applicable to usage with any backend/renderer.

## Minimum Support Rust Version (MSRV)

The MSRV for `imgui-rs` and all of the backend crates is **1.57**. We update our MSRV periodically, and issue a minor bump for it.
The MSRV for `imgui-rs` and all of the backend crates is **1.60**. We update our MSRV periodically, and issue a minor bump for it.

## Choosing a backend platform and a renderer

Expand Down
2 changes: 2 additions & 0 deletions imgui-examples/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ mod support;

fn main() {
let system = support::init(file!());

let mut value = 0;
let choices = ["test test this is 1", "test test this is 2"];

system.main_loop(move |_, ui| {
ui.window("Hello world")
.size([300.0, 110.0], Condition::FirstUseEver)
Expand Down
2 changes: 1 addition & 1 deletion imgui-examples/examples/tables_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ struct HumanData {
}

impl HumanData {
pub fn sort_humans(humans: &mut Vec<Self>, specs: Specs<'_>) {
pub fn sort_humans(humans: &mut [Self], specs: Specs<'_>) {
let spec = specs.iter().next().unwrap();
if let Some(kind) = spec.sort_direction() {
match kind {
Expand Down
16 changes: 16 additions & 0 deletions imgui-winit-glow-renderer-viewports/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "imgui-winit-glow-renderer-viewports"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
imgui = { version="0.10.0", path="../imgui", features=["docking"] }

glow = "0.11.2"
glutin = "0.30.3"
raw-window-handle = "0.5.0"
winit = "0.27.5"
thiserror = "1.0.38"
glutin-winit = "0.2.1"
162 changes: 162 additions & 0 deletions imgui-winit-glow-renderer-viewports/examples/viewports_basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
use std::{ffi::CString, num::NonZeroU32, time::Instant};

use glow::{Context, HasContext};
use glutin::{
config::ConfigTemplateBuilder,
context::ContextAttributesBuilder,
display::GetGlDisplay,
prelude::{
GlDisplay, NotCurrentGlContextSurfaceAccessor, PossiblyCurrentContextGlSurfaceAccessor,
},
surface::{GlSurface, SurfaceAttributesBuilder, WindowSurface},
};
use glutin_winit::DisplayBuilder;
use imgui::ConfigFlags;
use imgui_winit_glow_renderer_viewports::Renderer;
use raw_window_handle::HasRawWindowHandle;
use winit::{dpi::LogicalSize, event::WindowEvent, event_loop::EventLoop, window::WindowBuilder};

fn main() {
let event_loop = EventLoop::new();

let window_builder = WindowBuilder::new()
.with_inner_size(LogicalSize::new(800.0, 600.0))
.with_visible(true)
.with_resizable(true)
.with_title("Viewports example");

let template_builder = ConfigTemplateBuilder::new();
let (window, gl_config) = DisplayBuilder::new()
.with_window_builder(Some(window_builder))
.build(&event_loop, template_builder, |mut configs| {
configs.next().unwrap()
})
.expect("Failed to create main window");

let window = window.unwrap();

let context_attribs = ContextAttributesBuilder::new().build(Some(window.raw_window_handle()));
let context = unsafe {
gl_config
.display()
.create_context(&gl_config, &context_attribs)
.expect("Failed to create main context")
};

let size = window.inner_size();
let surface_attribs = SurfaceAttributesBuilder::<WindowSurface>::new().build(
window.raw_window_handle(),
NonZeroU32::new(size.width).unwrap(),
NonZeroU32::new(size.height).unwrap(),
);
let surface = unsafe {
gl_config
.display()
.create_window_surface(&gl_config, &surface_attribs)
.expect("Failed to create main surface")
};

let context = context
.make_current(&surface)
.expect("Failed to make current");

let glow = unsafe {
Context::from_loader_function(|name| {
let name = CString::new(name).unwrap();
context.display().get_proc_address(&name)
})
};

let mut imgui = imgui::Context::create();
imgui
.io_mut()
.config_flags
.insert(ConfigFlags::DOCKING_ENABLE);
imgui
.io_mut()
.config_flags
.insert(ConfigFlags::VIEWPORTS_ENABLE);
imgui.set_ini_filename(None);

let mut renderer = Renderer::new(&mut imgui, &window, &glow).expect("Failed to init Renderer");

let mut last_frame = Instant::now();

event_loop.run(move |event, window_target, control_flow| {
control_flow.set_poll();

renderer.handle_event(&mut imgui, &window, &event);

match event {
winit::event::Event::NewEvents(_) => {
let now = Instant::now();
imgui.io_mut().update_delta_time(now - last_frame);
last_frame = now;
}
winit::event::Event::WindowEvent {
window_id,
event: WindowEvent::CloseRequested,
} if window_id == window.id() => {
control_flow.set_exit();
}
winit::event::Event::WindowEvent {
window_id,
event: WindowEvent::Resized(new_size),
} if window_id == window.id() => {
surface.resize(
&context,
NonZeroU32::new(new_size.width).unwrap(),
NonZeroU32::new(new_size.height).unwrap(),
);
}
winit::event::Event::MainEventsCleared => {
window.request_redraw();
}
winit::event::Event::RedrawRequested(_) => {
let ui = imgui.frame();

ui.dockspace_over_main_viewport();

ui.show_demo_window(&mut true);
ui.window("Style Editor").build(|| {
ui.show_default_style_editor();
});

ui.end_frame_early();

renderer.prepare_render(&mut imgui, &window);

imgui.update_platform_windows();
renderer
.update_viewports(&mut imgui, window_target, &glow)
.expect("Failed to update viewports");

let draw_data = imgui.render();

if let Err(e) = context.make_current(&surface) {
// For some reason make_current randomly throws errors on windows.
// Until the reason for this is found, we just print it out instead of panicing.
eprintln!("Failed to make current: {e}");
}

unsafe {
glow.disable(glow::SCISSOR_TEST);
glow.clear(glow::COLOR_BUFFER_BIT);
}

renderer
.render(&window, &glow, draw_data)
.expect("Failed to render main viewport");

surface
.swap_buffers(&context)
.expect("Failed to swap buffers");

renderer
.render_viewports(&glow, &mut imgui)
.expect("Failed to render viewports");
}
_ => {}
}
});
}
13 changes: 13 additions & 0 deletions imgui-winit-glow-renderer-viewports/src/fragment_shader.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 330

in vec2 v2f_UV;
in vec4 v2f_Color;

uniform sampler2D u_FontTexture;

layout(location = 0) out vec4 out_Color;

void main() {
vec4 tex = texture(u_FontTexture, v2f_UV);
out_Color = v2f_Color * tex;
}
Loading

0 comments on commit a2ad8dd

Please sign in to comment.