diff --git a/SofaGLFW/src/SofaGLFW/NullGUIEngine.cpp b/SofaGLFW/src/SofaGLFW/NullGUIEngine.cpp index 306106d15b..8f8130b287 100644 --- a/SofaGLFW/src/SofaGLFW/NullGUIEngine.cpp +++ b/SofaGLFW/src/SofaGLFW/NullGUIEngine.cpp @@ -29,19 +29,42 @@ namespace sofaglfw void NullGUIEngine::init() { - + m_lastTime = glfwGetTime(); + m_lastDisplayTime = m_lastTime; + m_avgFrameTime = 0.0; } -void NullGUIEngine::initBackend(GLFWwindow*) +void NullGUIEngine::initBackend(GLFWwindow* window) { - + m_window = window; } void NullGUIEngine::startFrame(SofaGLFWBaseGUI*) { - } void NullGUIEngine::endFrame() { - + constexpr double displayRefreshInterval = 0.1; + constexpr double smoothingFactor = 0.05; + + const double now = glfwGetTime(); + const double dt = now - m_lastTime; + m_lastTime = now; + + if (dt > 0.0) + { + if (m_avgFrameTime <= 0.0) + m_avgFrameTime = dt; + else + m_avgFrameTime += smoothingFactor * (dt - m_avgFrameTime); + } + + if (now - m_lastDisplayTime >= displayRefreshInterval) + { + const double fps = (m_avgFrameTime > 0.0) ? 1.0 / m_avgFrameTime : 0.0; + char title_string[32]; + std::snprintf(title_string, sizeof(title_string), "FPS: %.1f", fps); + glfwSetWindowTitle(m_window, title_string); + m_lastDisplayTime = now; + } } void NullGUIEngine::beforeDraw(GLFWwindow* window) diff --git a/SofaGLFW/src/SofaGLFW/NullGUIEngine.h b/SofaGLFW/src/SofaGLFW/NullGUIEngine.h index b33c543c3b..e39140fa99 100644 --- a/SofaGLFW/src/SofaGLFW/NullGUIEngine.h +++ b/SofaGLFW/src/SofaGLFW/NullGUIEngine.h @@ -44,6 +44,11 @@ class NullGUIEngine : public BaseGUIEngine bool dispatchMouseEvents() override; void resetCounter() override; sofa::type::Vec2i getFrameBufferPixels(std::vector& pixels) override; +private: + GLFWwindow* m_window{ nullptr }; + double m_lastTime{ 0.0 }; + double m_lastDisplayTime{ 0.0 }; + double m_avgFrameTime{ 0.0 }; }; } // namespace sofaglfw diff --git a/SofaImGui/CMakeLists.txt b/SofaImGui/CMakeLists.txt index fb86e73562..d7e5c1368c 100644 --- a/SofaImGui/CMakeLists.txt +++ b/SofaImGui/CMakeLists.txt @@ -150,8 +150,12 @@ set(IMGUI_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/resources ${imgui_SOURCE_DIR} $ add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${IMGUI_SOURCE_FILES}) target_include_directories(${PROJECT_NAME} PUBLIC "$") target_link_libraries(${PROJECT_NAME} PUBLIC SofaGLFW Sofa.GL.Component.Rendering3D ${CMAKE_DL_LIBS}) -target_link_libraries(${PROJECT_NAME} PRIVATE nfd) target_link_libraries(${PROJECT_NAME} PRIVATE SimpleIni::SimpleIni) +if (EXISTS nfd) + target_link_libraries(${PROJECT_NAME} PRIVATE nfd) +else() + target_link_libraries(${PROJECT_NAME} PRIVATE nfd::nfd) +endif() # setup the same API exports for imgui target_compile_definitions(${PROJECT_NAME} PUBLIC IMGUI_API=SOFAIMGUI_API)