Skip to content

Commit 1297dbc

Browse files
triplejamJames Johnson
andauthored
Update documentation for using SDL with ImGui (#650)
Co-authored-by: James Johnson <[email protected]>
1 parent 080cef4 commit 1297dbc

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

doc/SDL2.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,88 @@ if (device->GetDeviceInfo().IsGLDevice())
353353
- Look at [GetSurfacePretransformMatrix()](https://github.com/DiligentGraphics/DiligentSamples/blob/9be93225a7fdf135583146c1175c232217f310b2/SampleBase/src/SampleBase.cpp#L140) for an example of handling the view matrix.
354354
- `SDL_GetDisplayOrientation()` can be used to get the device's orientation. Alternatively, listen for the `SDL_DISPLAYEVENT_ORIENTATION` event.
355355
- `SDL_GetDisplayMode()` can be used to determine max resolution.
356+
357+
## ImGui
358+
### Compiling and Linking
359+
Whatever is most convenient for your build system, compile and link [imgui_impl_sdl.cpp](https://github.com/DiligentGraphics/imgui/blob/66ad2ad5398cb61433009553e10fd326d13acb84/backends/imgui_impl_sdl.cpp) into your project
360+
361+
### Using the ImGuiImplSDL implementation
362+
#### Setup
363+
Add the [ImGuiImplSDL.hpp](https://github.com/DiligentGraphics/DiligentTools/blob/fbd6c1054744724387e37ae69b1636782f703ca0/Imgui/interface/ImGuiImplSDL.hpp) header included in Diligent. After creating the swapchain, call `ImGuiImplSDL::Create(CI, sdl_window)` and store the result for usage as normal
364+
```cpp
365+
#include "Imgui/interface/ImGuiImplSDL.hpp"
366+
367+
// Store somewhere
368+
std::unique_ptr<ImGuiImplDiligent> imgui_impl{};
369+
370+
// Create swapchain...
371+
372+
// Create the ImGui implementation object using the device and swapchain description
373+
auto CI = ImGuiDiligentCreateInfo{*device, SCDesc};
374+
imgui_impl = ImGui::ImplSDL::Create(CI, sdl_window);
375+
```
376+
377+
#### Rendering
378+
Before creating an imgui window for the current frame call `ImGuiImplDiligent::NewFrame()`
379+
```cpp
380+
auto SCDesc = swapchain->GetDesc();
381+
382+
// You may want to check here if the render size given by SDL is different then
383+
// the swapchain's. For example if the window was just resized and the values
384+
// given by SDL_Vulkan_GetDrawableSize() don't match the swapchain's, you might
385+
// want to return early for this frame otherwise ImGui may assert.
386+
387+
imgui_impl->NewFrame(SCDesc.Width, SCDesc.Height, SCDesc.PreTransform);
388+
389+
// Begin drawing with ImGui
390+
ImGui::Begin("Debug Menu");
391+
```
392+
Then finally at the end of your other render passes then call `ImGuiImplDiligent::Render()`
393+
```cpp
394+
imgui_impl->Render(immediate_context);
395+
// Finish rendering
396+
swapchain->Present();
397+
```
398+
399+
### Passing input events
400+
At the start of your main event loop, add a call to `ImGui::ImplSDL::HandleSDLEvent()`. It is not necessary to save the return value
401+
```cpp
402+
SDL_Event event{};
403+
auto& io = ImGui::GetIO();
404+
// Set flags you want
405+
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
406+
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
407+
408+
while (true)
409+
{
410+
dynamic_cast<ImGuiImplSDL*>(imgui_impl.get())->HandleSDLEvent(&event);
411+
// ...
412+
}
413+
```
414+
Then afterward in the loop you can filter ImGui events as normal
415+
```cpp
416+
switch(event)
417+
{
418+
case SDL_KeyDown:
419+
if (!io.WantCaptureKeyboard)
420+
{
421+
// Do your normal non-imgui logic here
422+
}
423+
break;
424+
case SDL_KeyUp:
425+
// ditto
426+
case SDL_MOUSEMOTION:
427+
if (!io.WantCaptureMouse)
428+
{
429+
// Do your normal non-imgui logic here
430+
}
431+
case SDL_MOUSEBUTTONDOWN:
432+
// ditto
433+
case SDL_MOUSEBUTTONUP:
434+
// ditto
435+
case SDL_MOUSEWHEEL:
436+
// ditto
437+
}
438+
```
439+
440+
It's possible to use gamepad input as well, but you will have to come up with a way to filter the events

0 commit comments

Comments
 (0)