Skip to content

Commit bb683af

Browse files
author
James Johnson
committed
Provide ImGui implementation for SDL
Add header Add newline at end of file Fix for format
1 parent 9ab25fb commit bb683af

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

Imgui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(SOURCE
66
src/ImGuiDiligentRenderer.cpp
77
src/ImGuiImplDiligent.cpp
88
src/ImGuiUtils.cpp
9+
src/ImGuiImplSDL.cpp
910
)
1011

1112
set(IMGUIZMO_QUAT_SOURCE
@@ -17,6 +18,7 @@ set(INTERFACE
1718
interface/ImGuiDiligentRenderer.hpp
1819
interface/ImGuiImplDiligent.hpp
1920
interface/ImGuiUtils.hpp
21+
interface/ImGuiImplSDL.hpp
2022
)
2123

2224
if(PLATFORM_WIN32)

Imgui/interface/ImGuiImplSDL.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include "ImGuiImplDiligent.hpp"
4+
5+
extern "C" struct SDL_Window;
6+
extern "C" union SDL_Event;
7+
8+
namespace Diligent
9+
{
10+
class ImGuiImplSDL final : public ImGuiImplDiligent
11+
{
12+
public:
13+
static std::unique_ptr<ImGuiImplSDL>
14+
Create(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow);
15+
16+
ImGuiImplSDL(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow);
17+
~ImGuiImplSDL();
18+
19+
// clang-format off
20+
ImGuiImplSDL (const ImGuiImplSDL&) = delete;
21+
ImGuiImplSDL ( ImGuiImplSDL&&) = delete;
22+
ImGuiImplSDL& operator = (const ImGuiImplSDL&) = delete;
23+
ImGuiImplSDL& operator = ( ImGuiImplSDL&&) = delete;
24+
// clang-format on
25+
26+
virtual void NewFrame(Uint32 RenderSurfaceWidth,
27+
Uint32 RenderSurfaceHeight,
28+
SURFACE_TRANSFORM SurfacePreTransform) override final;
29+
virtual void Render(IDeviceContext* pCtx) override final;
30+
bool HandleSDLEvent(const SDL_Event* ev);
31+
};
32+
} // namespace Diligent

Imgui/src/ImGuiImplSDL.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2025 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#include "ImGuiImplSDL.hpp"
28+
29+
#include "Errors.hpp"
30+
#include "RenderDevice.h"
31+
#include "backends/imgui_impl_sdl.h"
32+
33+
#include <memory>
34+
35+
namespace Diligent
36+
{
37+
38+
std::unique_ptr<ImGuiImplSDL>
39+
ImGuiImplSDL::Create(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow)
40+
{
41+
return std::make_unique<ImGuiImplSDL>(CI, pWindow);
42+
}
43+
44+
ImGuiImplSDL::ImGuiImplSDL(const ImGuiDiligentCreateInfo& CI,
45+
SDL_Window* pWindow) :
46+
ImGuiImplDiligent(CI)
47+
{
48+
switch (CI.pDevice->GetDeviceInfo().Type)
49+
{
50+
case RENDER_DEVICE_TYPE_UNDEFINED:
51+
LOG_ERROR_AND_THROW("Undefined device type");
52+
break;
53+
case RENDER_DEVICE_TYPE_D3D11:
54+
case RENDER_DEVICE_TYPE_D3D12:
55+
ImGui_ImplSDL2_InitForD3D(pWindow);
56+
break;
57+
case RENDER_DEVICE_TYPE_GL:
58+
case RENDER_DEVICE_TYPE_GLES:
59+
ImGui_ImplSDL2_InitForOpenGL(pWindow, nullptr);
60+
break;
61+
case RENDER_DEVICE_TYPE_VULKAN:
62+
ImGui_ImplSDL2_InitForVulkan(pWindow);
63+
break;
64+
case RENDER_DEVICE_TYPE_METAL:
65+
ImGui_ImplSDL2_InitForMetal(pWindow);
66+
break;
67+
case RENDER_DEVICE_TYPE_WEBGPU:
68+
LOG_ERROR_AND_THROW("WebGPU not supported");
69+
break;
70+
case RENDER_DEVICE_TYPE_COUNT:
71+
LOG_ERROR_AND_THROW("Unsupported device type");
72+
break;
73+
}
74+
}
75+
76+
ImGuiImplSDL::~ImGuiImplSDL() { ImGui_ImplSDL2_Shutdown(); }
77+
78+
void ImGuiImplSDL::NewFrame(Uint32 RenderSurfaceWidth,
79+
Uint32 RenderSurfaceHeight,
80+
SURFACE_TRANSFORM SurfacePreTransform)
81+
{
82+
ImGui_ImplSDL2_NewFrame();
83+
ImGuiImplDiligent::NewFrame(RenderSurfaceWidth, RenderSurfaceHeight,
84+
SurfacePreTransform);
85+
}
86+
87+
void ImGuiImplSDL::Render(IDeviceContext* pCtx)
88+
{
89+
ImGuiImplDiligent::Render(pCtx);
90+
}
91+
92+
bool ImGuiImplSDL::HandleSDLEvent(const SDL_Event* ev)
93+
{
94+
return ImGui_ImplSDL2_ProcessEvent(ev);
95+
}
96+
97+
} // namespace Diligent

0 commit comments

Comments
 (0)