Skip to content

Commit d60e6e7

Browse files
authored
Provide example ImGui implementation for SDL
1 parent 4350541 commit d60e6e7

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

doc/SDL2.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,100 @@ 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+
For all ImGui features to work properly, ```ImGuiImplDiligent``` should be overridden to use the SDL backend for ImGui.
359+
360+
### Example Implementation
361+
362+
#### Header
363+
```cpp
364+
// ImGuiImplSDL.hpp
365+
366+
#include "Imgui/interface/ImGuiImplDiligent.hpp"
367+
368+
extern "C" struct SDL_Window;
369+
extern "C" union SDL_Event;
370+
371+
namespace Diligent
372+
{
373+
class ImGuiImplSDL final : public ImGuiImplDiligent
374+
{
375+
public:
376+
static std::unique_ptr<ImGuiImplSDL>
377+
Create(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow);
378+
379+
ImGuiImplSDL(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow);
380+
~ImGuiImplSDL();
381+
382+
ImGuiImplSDL(const ImGuiImplSDL&) = delete;
383+
ImGuiImplSDL& operator=(const ImGuiImplSDL&) = delete;
384+
ImGuiImplSDL(ImGuiImplSDL&&) = delete;
385+
ImGuiImplSDL& operator=(ImGuiImplSDL&&) = delete;
386+
387+
virtual void NewFrame(Uint32 RenderSurfaceWidth,
388+
Uint32 RenderSurfaceHeight,
389+
SURFACE_TRANSFORM SurfacePreTransform) override final;
390+
virtual void Render(IDeviceContext* pCtx) override final;
391+
bool HandleSDLEvent(const SDL_Event * ev);
392+
};
393+
} // namespace Diligent
394+
395+
#endif // IMGUIIMPLSDL_HPP
396+
```
397+
398+
#### Source
399+
```cpp
400+
// ImGuiImplSDL.cpp
401+
402+
#include "ImGuiImplSDL.hpp"
403+
#include "backends/imgui_impl_sdl.h"
404+
405+
#include <SDL_video.h>
406+
#include <memory>
407+
408+
namespace Diligent
409+
{
410+
411+
std::unique_ptr<ImGuiImplSDL>
412+
ImGuiImplSDL::Create(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow)
413+
{
414+
return std::make_unique<ImGuiImplSDL>(CI, pWindow);
415+
}
416+
417+
ImGuiImplSDL::ImGuiImplSDL(const ImGuiDiligentCreateInfo& CI,
418+
SDL_Window* pWindow)
419+
: ImGuiImplDiligent(CI)
420+
{
421+
#if _WIN32
422+
ImGui_ImplSDL2_InitForD3D(pWindow);
423+
#elif __ANDROID__
424+
ImGui_ImplSDL2_InitForOpenGL(pWindow);
425+
#elif __APPLE__
426+
ImGui_ImplSDL2_InitForVulkan(pWindow);
427+
#endif
428+
}
429+
430+
ImGuiImplSDL::~ImGuiImplSDL() { ImGui_ImplSDL2_Shutdown(); }
431+
432+
void ImGuiImplSDL::NewFrame(Uint32 RenderSurfaceWidth,
433+
Uint32 RenderSurfaceHeight,
434+
SURFACE_TRANSFORM SurfacePreTransform)
435+
{
436+
ImGui_ImplSDL2_NewFrame();
437+
ImGuiImplDiligent::NewFrame(RenderSurfaceWidth, RenderSurfaceHeight,
438+
SurfacePreTransform);
439+
}
440+
441+
void ImGuiImplSDL::Render(IDeviceContext* pCtx)
442+
{
443+
ImGuiImplDiligent::Render(pCtx);
444+
}
445+
446+
bool ImGuiImplSDL::HandleSDLEvent(const SDL_Event* ev)
447+
{
448+
return ImGui_ImplSDL2_ProcessEvent(ev);
449+
}
450+
451+
} // namespace Diligent
452+
```

0 commit comments

Comments
 (0)