Skip to content

Commit fbd6c10

Browse files
James JohnsonTheMostDiligent
authored andcommitted
Provide ImGui implementation for SDL
Add header Add newline at end of file Fix for format Add header Move include to header
1 parent 9ab25fb commit fbd6c10

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
#pragma once
28+
29+
#include <memory>
30+
#include "ImGuiImplDiligent.hpp"
31+
32+
extern "C" struct SDL_Window;
33+
extern "C" union SDL_Event;
34+
35+
namespace Diligent
36+
{
37+
class ImGuiImplSDL final : public ImGuiImplDiligent
38+
{
39+
public:
40+
static std::unique_ptr<ImGuiImplSDL>
41+
Create(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow);
42+
43+
ImGuiImplSDL(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow);
44+
~ImGuiImplSDL();
45+
46+
// clang-format off
47+
ImGuiImplSDL (const ImGuiImplSDL&) = delete;
48+
ImGuiImplSDL ( ImGuiImplSDL&&) = delete;
49+
ImGuiImplSDL& operator = (const ImGuiImplSDL&) = delete;
50+
ImGuiImplSDL& operator = ( ImGuiImplSDL&&) = delete;
51+
// clang-format on
52+
53+
virtual void NewFrame(Uint32 RenderSurfaceWidth,
54+
Uint32 RenderSurfaceHeight,
55+
SURFACE_TRANSFORM SurfacePreTransform) override final;
56+
virtual void Render(IDeviceContext* pCtx) override final;
57+
bool HandleSDLEvent(const SDL_Event* ev);
58+
};
59+
} // namespace Diligent

Imgui/src/ImGuiImplSDL.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
namespace Diligent
34+
{
35+
36+
std::unique_ptr<ImGuiImplSDL>
37+
ImGuiImplSDL::Create(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow)
38+
{
39+
return std::make_unique<ImGuiImplSDL>(CI, pWindow);
40+
}
41+
42+
ImGuiImplSDL::ImGuiImplSDL(const ImGuiDiligentCreateInfo& CI,
43+
SDL_Window* pWindow) :
44+
ImGuiImplDiligent(CI)
45+
{
46+
switch (CI.pDevice->GetDeviceInfo().Type)
47+
{
48+
case RENDER_DEVICE_TYPE_UNDEFINED:
49+
LOG_ERROR_AND_THROW("Undefined device type");
50+
break;
51+
case RENDER_DEVICE_TYPE_D3D11:
52+
case RENDER_DEVICE_TYPE_D3D12:
53+
ImGui_ImplSDL2_InitForD3D(pWindow);
54+
break;
55+
case RENDER_DEVICE_TYPE_GL:
56+
case RENDER_DEVICE_TYPE_GLES:
57+
ImGui_ImplSDL2_InitForOpenGL(pWindow, nullptr);
58+
break;
59+
case RENDER_DEVICE_TYPE_VULKAN:
60+
ImGui_ImplSDL2_InitForVulkan(pWindow);
61+
break;
62+
case RENDER_DEVICE_TYPE_METAL:
63+
ImGui_ImplSDL2_InitForMetal(pWindow);
64+
break;
65+
case RENDER_DEVICE_TYPE_WEBGPU:
66+
LOG_ERROR_AND_THROW("WebGPU not supported");
67+
break;
68+
case RENDER_DEVICE_TYPE_COUNT:
69+
LOG_ERROR_AND_THROW("Unsupported device type");
70+
break;
71+
}
72+
}
73+
74+
ImGuiImplSDL::~ImGuiImplSDL() { ImGui_ImplSDL2_Shutdown(); }
75+
76+
void ImGuiImplSDL::NewFrame(Uint32 RenderSurfaceWidth,
77+
Uint32 RenderSurfaceHeight,
78+
SURFACE_TRANSFORM SurfacePreTransform)
79+
{
80+
ImGui_ImplSDL2_NewFrame();
81+
ImGuiImplDiligent::NewFrame(RenderSurfaceWidth, RenderSurfaceHeight,
82+
SurfacePreTransform);
83+
}
84+
85+
void ImGuiImplSDL::Render(IDeviceContext* pCtx)
86+
{
87+
ImGuiImplDiligent::Render(pCtx);
88+
}
89+
90+
bool ImGuiImplSDL::HandleSDLEvent(const SDL_Event* ev)
91+
{
92+
return ImGui_ImplSDL2_ProcessEvent(ev);
93+
}
94+
95+
} // namespace Diligent

0 commit comments

Comments
 (0)