Skip to content

Commit 5cf5830

Browse files
triplejamJames Johnson
andauthored
Split ImGuiImplSDL class into two for the SDL2 and SDL3 backends. (#257)
* Split ImGuiImplSDL class into two for the SDL2 and SDL3 backends. * Wrap the auxiliary functions for ImGuiImplSDL2 and 3. --------- Co-authored-by: James Johnson <[email protected]>
1 parent fe07949 commit 5cf5830

File tree

5 files changed

+242
-22
lines changed

5 files changed

+242
-22
lines changed

Imgui/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ set(INTERFACE
2020
)
2121

2222
if(EXISTS "${DILIGENT_DEAR_IMGUI_PATH}/backends")
23-
list(APPEND SOURCE src/ImGuiImplSDL.cpp)
24-
list(APPEND INTERFACE interface/ImGuiImplSDL.hpp)
23+
list(APPEND SOURCE src/ImGuiImplSDL2.cpp)
24+
list(APPEND SOURCE src/ImGuiImplSDL3.cpp)
25+
list(APPEND INTERFACE interface/ImGuiImplSDL2.hpp)
26+
list(APPEND INTERFACE interface/ImGuiImplSDL3.hpp)
2527
if(PLATFORM_WIN32)
2628
list(APPEND SOURCE src/ImGuiImplWin32.cpp)
2729
list(APPEND INTERFACE interface/ImGuiImplWin32.hpp)

Imgui/interface/ImGuiImplSDL2.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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" struct _SDL_GameController;
34+
extern "C" union SDL_Event;
35+
36+
namespace Diligent
37+
{
38+
class ImGuiImplSDL2 final : public ImGuiImplDiligent
39+
{
40+
public:
41+
static std::unique_ptr<ImGuiImplSDL2>
42+
Create(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow);
43+
44+
ImGuiImplSDL2(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow);
45+
~ImGuiImplSDL2();
46+
47+
// clang-format off
48+
ImGuiImplSDL2 (const ImGuiImplSDL2&) = delete;
49+
ImGuiImplSDL2 ( ImGuiImplSDL2&&) = delete;
50+
ImGuiImplSDL2& operator = (const ImGuiImplSDL2&) = delete;
51+
ImGuiImplSDL2& operator = ( ImGuiImplSDL2&&) = delete;
52+
// clang-format on
53+
54+
virtual void NewFrame(Uint32 RenderSurfaceWidth,
55+
Uint32 RenderSurfaceHeight,
56+
SURFACE_TRANSFORM SurfacePreTransform) override final;
57+
virtual void Render(IDeviceContext* pCtx) override final;
58+
bool HandleSDLEvent(const SDL_Event* ev);
59+
float GetContentScaleForWindow(SDL_Window* pWindow);
60+
float GetContentScaleForDisplay(int DisplayIndex);
61+
enum GAMEPAD_MODE
62+
{
63+
GAMEPAD_MODE_AUTO_FIRST,
64+
GAMEPAD_MODE_AUTO_ALL,
65+
GAMEPAD_MODE_MANUAL
66+
};
67+
void SetGamepadMode(GAMEPAD_MODE mode, _SDL_GameController** ppManualGamepadsArray = nullptr, int ManualGamepadsCount = -1);
68+
};
69+
} // namespace Diligent
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,38 @@
3030
#include "ImGuiImplDiligent.hpp"
3131

3232
extern "C" struct SDL_Window;
33+
extern "C" struct SDL_Gamepad;
3334
extern "C" union SDL_Event;
3435

3536
namespace Diligent
3637
{
37-
class ImGuiImplSDL final : public ImGuiImplDiligent
38+
class ImGuiImplSDL3 final : public ImGuiImplDiligent
3839
{
3940
public:
40-
static std::unique_ptr<ImGuiImplSDL>
41+
static std::unique_ptr<ImGuiImplSDL3>
4142
Create(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow);
4243

43-
ImGuiImplSDL(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow);
44-
~ImGuiImplSDL();
44+
ImGuiImplSDL3(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow);
45+
~ImGuiImplSDL3();
4546

4647
// clang-format off
47-
ImGuiImplSDL (const ImGuiImplSDL&) = delete;
48-
ImGuiImplSDL ( ImGuiImplSDL&&) = delete;
49-
ImGuiImplSDL& operator = (const ImGuiImplSDL&) = delete;
50-
ImGuiImplSDL& operator = ( ImGuiImplSDL&&) = delete;
48+
ImGuiImplSDL3 (const ImGuiImplSDL3&) = delete;
49+
ImGuiImplSDL3 ( ImGuiImplSDL3&&) = delete;
50+
ImGuiImplSDL3& operator = (const ImGuiImplSDL3&) = delete;
51+
ImGuiImplSDL3& operator = ( ImGuiImplSDL3&&) = delete;
5152
// clang-format on
5253

5354
virtual void NewFrame(Uint32 RenderSurfaceWidth,
5455
Uint32 RenderSurfaceHeight,
5556
SURFACE_TRANSFORM SurfacePreTransform) override final;
5657
virtual void Render(IDeviceContext* pCtx) override final;
5758
bool HandleSDLEvent(const SDL_Event* ev);
59+
enum GAMEPAD_MODE
60+
{
61+
GAMEPAD_MODE_AUTO_FIRST,
62+
GAMEPAD_MODE_AUTO_ALL,
63+
GAMEPAD_MODE_MANUAL
64+
};
65+
void SetGamepadMode(GAMEPAD_MODE mode, SDL_Gamepad** ppManualGamepadsArray = nullptr, int ManualGamepadsCount = -1);
5866
};
5967
} // namespace Diligent
Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* of the possibility of such damages.
2525
*/
2626

27-
#include "ImGuiImplSDL.hpp"
27+
#include "ImGuiImplSDL2.hpp"
2828

2929
#include "Errors.hpp"
3030
#include "RenderDevice.h"
@@ -33,14 +33,14 @@
3333
namespace Diligent
3434
{
3535

36-
std::unique_ptr<ImGuiImplSDL>
37-
ImGuiImplSDL::Create(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow)
36+
std::unique_ptr<ImGuiImplSDL2>
37+
ImGuiImplSDL2::Create(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow)
3838
{
39-
return std::make_unique<ImGuiImplSDL>(CI, pWindow);
39+
return std::make_unique<ImGuiImplSDL2>(CI, pWindow);
4040
}
4141

42-
ImGuiImplSDL::ImGuiImplSDL(const ImGuiDiligentCreateInfo& CI,
43-
SDL_Window* pWindow) :
42+
ImGuiImplSDL2::ImGuiImplSDL2(const ImGuiDiligentCreateInfo& CI,
43+
SDL_Window* pWindow) :
4444
ImGuiImplDiligent(CI)
4545
{
4646
switch (CI.pDevice->GetDeviceInfo().Type)
@@ -71,25 +71,53 @@ ImGuiImplSDL::ImGuiImplSDL(const ImGuiDiligentCreateInfo& CI,
7171
}
7272
}
7373

74-
ImGuiImplSDL::~ImGuiImplSDL() { ImGui_ImplSDL2_Shutdown(); }
74+
ImGuiImplSDL2::~ImGuiImplSDL2() { ImGui_ImplSDL2_Shutdown(); }
7575

76-
void ImGuiImplSDL::NewFrame(Uint32 RenderSurfaceWidth,
77-
Uint32 RenderSurfaceHeight,
78-
SURFACE_TRANSFORM SurfacePreTransform)
76+
void ImGuiImplSDL2::NewFrame(Uint32 RenderSurfaceWidth,
77+
Uint32 RenderSurfaceHeight,
78+
SURFACE_TRANSFORM SurfacePreTransform)
7979
{
8080
ImGui_ImplSDL2_NewFrame();
8181
ImGuiImplDiligent::NewFrame(RenderSurfaceWidth, RenderSurfaceHeight,
8282
SurfacePreTransform);
8383
}
8484

85-
void ImGuiImplSDL::Render(IDeviceContext* pCtx)
85+
void ImGuiImplSDL2::Render(IDeviceContext* pCtx)
8686
{
8787
ImGuiImplDiligent::Render(pCtx);
8888
}
8989

90-
bool ImGuiImplSDL::HandleSDLEvent(const SDL_Event* ev)
90+
bool ImGuiImplSDL2::HandleSDLEvent(const SDL_Event* ev)
9191
{
9292
return ImGui_ImplSDL2_ProcessEvent(ev);
9393
}
9494

95+
float ImGuiImplSDL2::GetContentScaleForWindow(SDL_Window* pWindow)
96+
{
97+
return ImGui_ImplSDL2_GetContentScaleForWindow(pWindow);
98+
}
99+
100+
float ImGuiImplSDL2::GetContentScaleForDisplay(int DisplayIndex)
101+
{
102+
return ImGui_ImplSDL2_GetContentScaleForDisplay(DisplayIndex);
103+
}
104+
105+
void ImGuiImplSDL2::SetGamepadMode(GAMEPAD_MODE GamepadMode, _SDL_GameController** ppManualGamepadsArray, int ManualGamepadsCount)
106+
{
107+
ImGui_ImplSDL2_GamepadMode imgGamepadMode{};
108+
switch (GamepadMode)
109+
{
110+
case GAMEPAD_MODE_AUTO_FIRST:
111+
imgGamepadMode = ImGui_ImplSDL2_GamepadMode_AutoFirst;
112+
break;
113+
case GAMEPAD_MODE_AUTO_ALL:
114+
imgGamepadMode = ImGui_ImplSDL2_GamepadMode_AutoAll;
115+
break;
116+
case GAMEPAD_MODE_MANUAL:
117+
imgGamepadMode = ImGui_ImplSDL2_GamepadMode_Manual;
118+
break;
119+
}
120+
ImGui_ImplSDL2_SetGamepadMode(imgGamepadMode, ppManualGamepadsArray, ManualGamepadsCount);
121+
}
122+
95123
} // namespace Diligent

Imgui/src/ImGuiImplSDL3.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 "ImGuiImplSDL3.hpp"
28+
29+
#include "Errors.hpp"
30+
#include "RenderDevice.h"
31+
#include "backends/imgui_impl_sdl3.h"
32+
33+
namespace Diligent
34+
{
35+
36+
std::unique_ptr<ImGuiImplSDL3>
37+
ImGuiImplSDL3::Create(const ImGuiDiligentCreateInfo& CI, SDL_Window* pWindow)
38+
{
39+
return std::make_unique<ImGuiImplSDL3>(CI, pWindow);
40+
}
41+
42+
ImGuiImplSDL3::ImGuiImplSDL3(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_ImplSDL3_InitForD3D(pWindow);
54+
break;
55+
case RENDER_DEVICE_TYPE_GL:
56+
case RENDER_DEVICE_TYPE_GLES:
57+
ImGui_ImplSDL3_InitForOpenGL(pWindow, nullptr);
58+
break;
59+
case RENDER_DEVICE_TYPE_VULKAN:
60+
ImGui_ImplSDL3_InitForVulkan(pWindow);
61+
break;
62+
case RENDER_DEVICE_TYPE_METAL:
63+
ImGui_ImplSDL3_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+
ImGuiImplSDL3::~ImGuiImplSDL3() { ImGui_ImplSDL3_Shutdown(); }
75+
76+
void ImGuiImplSDL3::NewFrame(Uint32 RenderSurfaceWidth,
77+
Uint32 RenderSurfaceHeight,
78+
SURFACE_TRANSFORM SurfacePreTransform)
79+
{
80+
ImGui_ImplSDL3_NewFrame();
81+
ImGuiImplDiligent::NewFrame(RenderSurfaceWidth, RenderSurfaceHeight,
82+
SurfacePreTransform);
83+
}
84+
85+
void ImGuiImplSDL3::Render(IDeviceContext* pCtx)
86+
{
87+
ImGuiImplDiligent::Render(pCtx);
88+
}
89+
90+
bool ImGuiImplSDL3::HandleSDLEvent(const SDL_Event* ev)
91+
{
92+
return ImGui_ImplSDL3_ProcessEvent(ev);
93+
}
94+
95+
void ImGuiImplSDL3::SetGamepadMode(GAMEPAD_MODE GamepadMode, SDL_Gamepad** ppManualGamepadsArray, int ManualGamepadsCount)
96+
{
97+
ImGui_ImplSDL3_GamepadMode imgGamepadMode{};
98+
switch (GamepadMode)
99+
{
100+
case GAMEPAD_MODE_AUTO_FIRST:
101+
imgGamepadMode = ImGui_ImplSDL3_GamepadMode_AutoFirst;
102+
break;
103+
case GAMEPAD_MODE_AUTO_ALL:
104+
imgGamepadMode = ImGui_ImplSDL3_GamepadMode_AutoAll;
105+
break;
106+
case GAMEPAD_MODE_MANUAL:
107+
imgGamepadMode = ImGui_ImplSDL3_GamepadMode_Manual;
108+
break;
109+
}
110+
ImGui_ImplSDL3_SetGamepadMode(imgGamepadMode, ppManualGamepadsArray, ManualGamepadsCount);
111+
}
112+
113+
} // namespace Diligent

0 commit comments

Comments
 (0)