Skip to content

Commit 333b64b

Browse files
HelpersImGui: use the standard ImGui backend for GLFW
1 parent c3c899b commit 333b64b

File tree

6 files changed

+44
-37
lines changed

6 files changed

+44
-37
lines changed

lvk/HelpersImGui.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
2-
* LightweightVK
3-
*
4-
* This source code is licensed under the MIT license found in the
5-
* LICENSE file in the root directory of this source tree.
6-
*/
2+
* LightweightVK
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
77

88
#include "HelpersImGui.h"
99

@@ -16,6 +16,10 @@
1616
#include "implot/implot_items.cpp"
1717
#endif // LVK_WITH_IMPLOT
1818

19+
#if LVK_WITH_GLFW
20+
#include "imgui/backends/imgui_impl_glfw.cpp"
21+
#endif // LVK_WITH_GLFW
22+
1923
#include <math.h>
2024

2125
static const char* codeVS = R"(
@@ -105,7 +109,8 @@ lvk::Holder<lvk::RenderPipelineHandle> ImGuiRenderer::createNewPipelineState(con
105109
nullptr);
106110
}
107111

108-
ImGuiRenderer::ImGuiRenderer(lvk::IContext& device, const char* defaultFontTTF, float fontSizePixels) : ctx_(device) {
112+
ImGuiRenderer::ImGuiRenderer(lvk::IContext& device, lvk::LVKwindow* window, const char* defaultFontTTF, float fontSizePixels)
113+
: ctx_(device) {
109114
ImGui::CreateContext();
110115
#if defined(LVK_WITH_IMPLOT)
111116
ImPlot::CreateContext();
@@ -114,9 +119,15 @@ ImGuiRenderer::ImGuiRenderer(lvk::IContext& device, const char* defaultFontTTF,
114119
ImGuiIO& io = ImGui::GetIO();
115120
io.BackendRendererName = "imgui-lvk";
116121
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;
122+
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
123+
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
124+
125+
#if LVK_WITH_GLFW
126+
ImGui_ImplGlfw_InitForOther(window, window ? true : false);
127+
#endif // LVK_WITH_GLFW
117128

118129
updateFont(defaultFontTTF, fontSizePixels);
119-
130+
120131
vert_ = ctx_.createShaderModule({codeVS, Stage_Vert, "Shader Module: imgui (vert)"});
121132
frag_ = ctx_.createShaderModule({codeFS, Stage_Frag, "Shader Module: imgui (frag)"});
122133
samplerClamp_ = ctx_.createSampler({
@@ -129,6 +140,9 @@ ImGuiRenderer::ImGuiRenderer(lvk::IContext& device, const char* defaultFontTTF,
129140
ImGuiRenderer::~ImGuiRenderer() {
130141
ImGuiIO& io = ImGui::GetIO();
131142
io.Fonts->TexRef = ImTextureRef();
143+
#if LVK_WITH_GLFW
144+
ImGui_ImplGlfw_Shutdown();
145+
#endif // LVK_WITH_GLFW
132146
#if defined(LVK_WITH_IMPLOT)
133147
ImPlot::DestroyContext();
134148
#endif // LVK_WITH_IMPLOT
@@ -179,6 +193,9 @@ void ImGuiRenderer::beginFrame(const lvk::Framebuffer& desc) {
179193
if (pipeline_.empty()) {
180194
pipeline_ = createNewPipelineState(desc);
181195
}
196+
#if LVK_WITH_GLFW
197+
ImGui_ImplGlfw_NewFrame();
198+
#endif // LVK_WITH_GLFW
182199
ImGui::NewFrame();
183200
}
184201

lvk/HelpersImGui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace lvk {
1616

1717
class ImGuiRenderer {
1818
public:
19-
explicit ImGuiRenderer(lvk::IContext& device, const char* defaultFontTTF = nullptr, float fontSizePixels = 24.0f);
19+
explicit ImGuiRenderer(lvk::IContext& device, lvk::LVKwindow* window, const char* defaultFontTTF = nullptr, float fontSizePixels = 24.0f);
2020
~ImGuiRenderer();
2121

2222
void updateFont(const char* defaultFontTTF, float fontSizePixels);

samples/004_YUV.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ void createDemo(const char* name, lvk::Format format, const char* fileName) {
127127
});
128128
}
129129

130-
void init() {
131-
res_.imgui = std::make_unique<lvk::ImGuiRenderer>(*ctx_, nullptr, float(height_) / 70.0f);
130+
void init(lvk::LVKwindow* window) {
131+
res_.imgui = std::make_unique<lvk::ImGuiRenderer>(*ctx_, window, nullptr, float(height_) / 70.0f);
132132

133133
res_.vert = ctx_->createShaderModule({codeVS, lvk::Stage_Vert, "Shader Module: main (vert)"});
134134
res_.frag = ctx_->createShaderModule({codeFS, lvk::Stage_Frag, "Shader Module: main (frag)"});
@@ -205,7 +205,7 @@ int main(int argc, char* argv[]) {
205205
if (!ctx_) {
206206
return 1;
207207
}
208-
init();
208+
init(window);
209209

210210
glfwSetFramebufferSizeCallback(window, [](GLFWwindow*, int width, int height) {
211211
width_ = width;

samples/007_RayTracingAO.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ bool initModel();
466466
void createPipelines();
467467
void createOffscreenFramebuffer();
468468

469-
bool init() {
469+
bool init(lvk::LVKwindow* window) {
470470
for (uint32_t i = 0; i != kNumBufferedFrames; i++) {
471471
ubPerFrame_.push_back(ctx_->createBuffer({.usage = lvk::BufferUsageBits_Uniform,
472472
.storage = lvk::StorageType_HostVisible,
@@ -513,7 +513,7 @@ bool init() {
513513
createPipelines();
514514

515515
imgui_ = std::make_unique<lvk::ImGuiRenderer>(
516-
*ctx_, (folderThirdParty + "3D-Graphics-Rendering-Cookbook/data/OpenSans-Light.ttf").c_str(), float(height_) / 70.0f);
516+
*ctx_, window, (folderThirdParty + "3D-Graphics-Rendering-Cookbook/data/OpenSans-Light.ttf").c_str(), float(height_) / 70.0f);
517517

518518
if (!initModel()) {
519519
return false;
@@ -1123,7 +1123,7 @@ int main(int argc, char* argv[]) {
11231123
return EXIT_FAILURE;
11241124
}
11251125

1126-
if (!init()) {
1126+
if (!init(window)) {
11271127
return EXIT_FAILURE;
11281128
}
11291129

samples/Tiny_Mesh.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ UniformsPerObject perObject[kNumCubes];
196196
void initObjects();
197197
std::filesystem::path getPathToContentFolder();
198198

199-
void init() {
199+
void init(lvk::LVKwindow* window) {
200200
// Vertex buffer, Index buffer and Vertex Input. Buffers are allocated in GPU memory.
201201
vb0_ = ctx_->createBuffer({.usage = lvk::BufferUsageBits_Storage,
202202
.storage = lvk::StorageType_Device,
@@ -296,7 +296,7 @@ void init() {
296296

297297
initObjects();
298298

299-
imgui_ = std::make_unique<lvk::ImGuiRenderer>(*ctx_);
299+
imgui_ = std::make_unique<lvk::ImGuiRenderer>(*ctx_, window);
300300
}
301301

302302
void destroy() {
@@ -448,7 +448,7 @@ int main(int argc, char* argv[]) {
448448
return 1;
449449
}
450450

451-
init();
451+
init(window);
452452

453453
glfwSetCursorPosCallback(window, [](auto* window, double x, double y) { ImGui::GetIO().MousePos = ImVec2(x, y); });
454454
glfwSetMouseButtonCallback(window, [](auto* window, int button, int action, int mods) {
@@ -515,7 +515,7 @@ void handle_cmd(android_app* app, int32_t cmd) {
515515
width_ = ANativeWindow_getWidth(app->window);
516516
height_ = ANativeWindow_getHeight(app->window);
517517
ctx_ = lvk::createVulkanContextWithSwapchain(app->window, width_, height_, {});
518-
init();
518+
init(nullptr);
519519
}
520520
break;
521521
case APP_CMD_TERM_WINDOW:

samples/Tiny_MeshLarge.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ void createPipelines();
669669
void createShadowMap();
670670
void createOffscreenFramebuffer();
671671

672-
bool init() {
672+
bool init(lvk::LVKwindow* window) {
673673
{
674674
const uint32_t pixel = 0xFFFFFFFF;
675675
textureDummyWhite_ = ctx_->createTexture(
@@ -757,7 +757,7 @@ bool init() {
757757
createPipelines();
758758

759759
imgui_ = std::make_unique<lvk::ImGuiRenderer>(
760-
*ctx_, (folderThirdParty + "3D-Graphics-Rendering-Cookbook/data/OpenSans-Light.ttf").c_str(), float(height_) / 70.0f);
760+
*ctx_, window, (folderThirdParty + "3D-Graphics-Rendering-Cookbook/data/OpenSans-Light.ttf").c_str(), float(height_) / 70.0f);
761761

762762
queryPoolTimestamps_ = ctx_->createQueryPool(GPUTimestamp_NUM_TIMESTAMPS, "queryPoolTimestamps_");
763763

@@ -2084,6 +2084,7 @@ double getCurrentTimestamp() {
20842084
}
20852085

20862086
GLFWkeyfun g_PrevKeyCallback = nullptr;
2087+
GLFWmousebuttonfun g_PrevMouseButtonCallback = nullptr;
20872088

20882089
int main(int argc, char* argv[]) {
20892090
minilog::initialize(nullptr, {.threadNames = false});
@@ -2122,7 +2123,7 @@ int main(int argc, char* argv[]) {
21222123
printf("Compressing textures... It can take a while in debug builds...(needs to be done once)\n");
21232124
}
21242125

2125-
if (!init()) {
2126+
if (!init(window)) {
21262127
return EXIT_FAILURE;
21272128
}
21282129

@@ -2141,7 +2142,7 @@ int main(int argc, char* argv[]) {
21412142
}
21422143
});
21432144

2144-
glfwSetMouseButtonCallback(window, [](auto* window, int button, int action, int mods) {
2145+
g_PrevMouseButtonCallback = glfwSetMouseButtonCallback(window, [](auto* window, int button, int action, int mods) {
21452146
if (!ImGui::GetIO().WantCaptureMouse) {
21462147
if (button == GLFW_MOUSE_BUTTON_LEFT) {
21472148
mousePressed_ = (action == GLFW_PRESS);
@@ -2150,20 +2151,9 @@ int main(int argc, char* argv[]) {
21502151
// release the mouse
21512152
mousePressed_ = false;
21522153
}
2153-
double xpos, ypos;
2154-
glfwGetCursorPos(window, &xpos, &ypos);
2155-
const ImGuiMouseButton_ imguiButton = (button == GLFW_MOUSE_BUTTON_LEFT)
2156-
? ImGuiMouseButton_Left
2157-
: (button == GLFW_MOUSE_BUTTON_RIGHT ? ImGuiMouseButton_Right : ImGuiMouseButton_Middle);
2158-
ImGuiIO& io = ImGui::GetIO();
2159-
io.MousePos = ImVec2((float)xpos, (float)ypos);
2160-
io.MouseDown[imguiButton] = action == GLFW_PRESS;
2161-
});
2162-
2163-
glfwSetScrollCallback(window, [](GLFWwindow* window, double dx, double dy) {
2164-
ImGuiIO& io = ImGui::GetIO();
2165-
io.MouseWheelH = (float)dx;
2166-
io.MouseWheel = (float)dy;
2154+
// call the previous installed callback
2155+
if (g_PrevMouseButtonCallback)
2156+
g_PrevMouseButtonCallback(window, button, action, mods);
21672157
});
21682158

21692159
g_PrevKeyCallback = glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods) {
@@ -2290,7 +2280,7 @@ void handle_cmd(android_app* app, int32_t cmd) {
22902280
},
22912281
kPreferIntegratedGPU ? lvk::HWDeviceType_Integrated :
22922282
lvk::HWDeviceType_Discrete);
2293-
if (!init()) {
2283+
if (!init(nullptr)) {
22942284
LLOGW("Failed to initialize the app\n");
22952285
std::terminate();
22962286
}

0 commit comments

Comments
 (0)