Skip to content

Commit a8a1577

Browse files
committed
Added a readme and re-ordered the crate a bit
1 parent 85b7864 commit a8a1577

File tree

2 files changed

+297
-283
lines changed

2 files changed

+297
-283
lines changed

README.md

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,84 @@
11
# imgui-winit-support
22

3-
See the examples for [`imgui-glium-renderer`][glium] or [`imgui-glow-renderer`][glow]
4-
for simple examples of this platform backend with different renderers
3+
This crate provides a winit-based backend platform for [`imgui-rs`].
54

6-
[glium]: ../../imgui-glium-renderer/examples
7-
[glow]: ../../imgui-glow-renderer/examples
5+
A backend platform handles window/input device events and manages their state.
6+
7+
# Using the library
8+
9+
There are five things you need to do to use this library correctly:
10+
11+
1. Initialize a [`WinitPlatform`] instance.
12+
2. Attach it to a winit [`Window`] with [`WinitPlatform::attach_window`].
13+
3. Pass events to the platform (every frame) with [`WinitPlatform::handle_event`].
14+
4. Call the frame preparation callback [`WinitPlatform::prepare_frame`] (every frame)
15+
5. Call the render preparation callback [`WinitPlatform::prepare_render`] (every frame)
16+
17+
## Complete example (without a renderer)
18+
19+
```no_run
20+
use imgui::Context;
21+
use imgui_winit_support::{HiDpiMode, WinitPlatform};
22+
use std::time::Instant;
23+
use winit::event::{Event, WindowEvent};
24+
use winit::event_loop::{ControlFlow, EventLoop};
25+
use winit::window::Window;
26+
27+
let mut event_loop = EventLoop::new().expect("Failed to create EventLoop");
28+
let mut window = Window::new(&event_loop).unwrap();
29+
30+
let mut imgui = Context::create();
31+
// configure imgui-rs Context if necessary
32+
33+
let mut platform = WinitPlatform::init(&mut imgui); // step 1
34+
platform.attach_window(imgui.io_mut(), &window, HiDpiMode::Default); // step 2
35+
36+
let mut last_frame = Instant::now();
37+
let mut run = true;
38+
event_loop.run(move |event, window_target| {
39+
match event {
40+
Event::NewEvents(_) => {
41+
// other application-specific logic
42+
let now = Instant::now();
43+
imgui.io_mut().update_delta_time(now - last_frame);
44+
last_frame = now;
45+
},
46+
Event::AboutToWait => {
47+
// other application-specific logic
48+
platform.prepare_frame(imgui.io_mut(), &window) // step 4
49+
.expect("Failed to prepare frame");
50+
window.request_redraw();
51+
}
52+
Event::WindowEvent { event: WindowEvent::RedrawRequested, .. } => {
53+
let ui = imgui.frame();
54+
// application-specific rendering *under the UI*
55+
56+
// construct the UI
57+
58+
platform.prepare_render(&ui, &window); // step 5
59+
// render the UI with a renderer
60+
let draw_data = imgui.render();
61+
// renderer.render(..., draw_data).expect("UI rendering failed");
62+
63+
// application-specific rendering *over the UI*
64+
},
65+
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
66+
window_target.exit();
67+
}
68+
// other application-specific event handling
69+
event => {
70+
platform.handle_event(imgui.io_mut(), &window, &event); // step 3
71+
// other application-specific event handling
72+
}
73+
}
74+
}).expect("EventLoop error");
75+
```
76+
77+
## License
78+
79+
Licensed under either of
80+
81+
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0)
82+
- MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT)
83+
84+
at your option.

0 commit comments

Comments
 (0)