Skip to content

Commit 361ee2b

Browse files
ImGui: Added ImGuiDiligentFileIO to route file operations through Diligent FileSystem
1 parent 9f4fa28 commit 361ee2b

File tree

3 files changed

+143
-1
lines changed

3 files changed

+143
-1
lines changed

Imgui/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ project(Diligent-Imgui CXX)
55
set(SOURCE
66
src/ImGuiDiligentRenderer.cpp
77
src/ImGuiImplDiligent.cpp
8+
src/ImGuiDiligentFileIO.cpp
89
src/ImGuiUtils.cpp
910
)
1011

@@ -126,7 +127,7 @@ if(PLATFORM_UNIVERSAL_WINDOWS)
126127
target_compile_definitions(Diligent-Imgui PRIVATE IMGUI_DISABLE_WIN32_FUNCTIONS)
127128
endif()
128129

129-
target_compile_definitions(Diligent-Imgui PUBLIC IMGUI_DEFINE_MATH_OPERATORS)
130+
target_compile_definitions(Diligent-Imgui PUBLIC IMGUI_USER_CONFIG="ImGuiDiligentConfig.h")
130131

131132
if(PLATFORM_WIN32 AND MINGW_BUILD)
132133
# Link with dwmapi.lib as imgui_impl_win32.cpp skips
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2026 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+
#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS
30+
#define IMGUI_DEFINE_MATH_OPERATORS
31+
32+
namespace Diligent
33+
{
34+
35+
#if PLATFORM_WIN32
36+
class WindowsFile;
37+
using ImGuiFile = WindowsFile;
38+
#elif PLATFORM_UNIVERSAL_WINDOWS
39+
class WindowsStoreFile;
40+
using ImGuiFile = WindowsStoreFile;
41+
#elif PLATFORM_ANDROID
42+
class AndroidFile;
43+
using ImGuiFile = AndroidFile;
44+
#else
45+
class StandardFile;
46+
using ImGuiFile = StandardFile;
47+
#endif
48+
49+
} // namespace Diligent
50+
51+
using ImFileHandle = Diligent::ImGuiFile*;
52+
using ImU64 = unsigned long long;
53+
54+
ImFileHandle ImFileOpen(const char* pFileName, const char* pMode);
55+
bool ImFileClose(ImFileHandle pFile);
56+
ImU64 ImFileGetSize(ImFileHandle pFile);
57+
ImU64 ImFileRead(void* pData, ImU64 Size, ImU64 Count, ImFileHandle pFile);
58+
ImU64 ImFileWrite(const void* pData, ImU64 Size, ImU64 Count, ImFileHandle pFile);

Imgui/src/ImGuiDiligentFileIO.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2026 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 "ImGuiDiligentConfig.h"
28+
#include "imgui.h"
29+
#include "imgui_internal.h"
30+
#include "FileSystem.hpp"
31+
32+
using namespace Diligent;
33+
34+
ImFileHandle ImFileOpen(const char* pFileName, const char* pMode)
35+
{
36+
if (!pFileName || !pMode || !pMode[0])
37+
return nullptr;
38+
39+
const bool HasPlus = std::strchr(pMode, '+') != nullptr;
40+
41+
EFileAccessMode AccessMode = EFileAccessMode::Read;
42+
43+
switch (pMode[0])
44+
{
45+
case 'w': AccessMode = HasPlus ? EFileAccessMode::OverwriteUpdate : EFileAccessMode::Overwrite; break;
46+
case 'a': AccessMode = HasPlus ? EFileAccessMode::AppendUpdate : EFileAccessMode::Append; break;
47+
case 'r': AccessMode = HasPlus ? EFileAccessMode::ReadUpdate : EFileAccessMode::Read; break;
48+
default: AccessMode = HasPlus ? EFileAccessMode::ReadUpdate : EFileAccessMode::Read; break;
49+
}
50+
51+
return static_cast<ImFileHandle>(FileSystem::OpenFile({pFileName, AccessMode}));
52+
}
53+
54+
bool ImFileClose(ImFileHandle pFile)
55+
{
56+
if (!pFile)
57+
return false;
58+
delete pFile;
59+
return true;
60+
}
61+
62+
ImU64 ImFileGetSize(ImFileHandle pFile)
63+
{
64+
if (!pFile)
65+
return static_cast<ImU64>(-1);
66+
return static_cast<ImU64>(pFile->GetSize());
67+
}
68+
69+
ImU64 ImFileRead(void* pData, ImU64 ElemSize, ImU64 ElemCount, ImFileHandle pFile)
70+
{
71+
if (!pFile || ElemSize == 0 || ElemCount == 0)
72+
return 0;
73+
const size_t TotalSize = static_cast<size_t>(ElemSize * ElemCount);
74+
return pFile->Read(pData, TotalSize) ? ElemCount : 0;
75+
}
76+
77+
ImU64 ImFileWrite(const void* pData, ImU64 ElemSize, ImU64 ElemCount, ImFileHandle pFile)
78+
{
79+
if (!pFile || ElemSize == 0 || ElemCount == 0)
80+
return 0;
81+
const size_t TotalSize = static_cast<size_t>(ElemSize * ElemCount);
82+
return pFile->Write(pData, TotalSize) ? ElemCount : 0;
83+
}

0 commit comments

Comments
 (0)