iMSTK
Interactive Medical Simulation Toolkit
imstkFpsTxtCounter.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkFpsTxtCounter.h"
8 #include "imstkEntity.h"
9 #include "imstkScene.h"
10 #include "imstkSceneManager.h"
11 #include "imstkTextVisualModel.h"
12 #include "imstkViewer.h"
13 
14 namespace imstk
15 {
16 FpsTxtCounter::FpsTxtCounter(const std::string& name) : SceneBehaviour(name),
17  m_fpsTextVisualModel(std::make_shared<TextVisualModel>("FpsCounterTxt"))
18 {
19  m_fpsTextVisualModel->setPosition(TextVisualModel::DisplayPosition::LowerLeft);
20  m_fpsTextVisualModel->setFontSize(30.0);
21 }
22 
23 void
25 {
26  // Add a visual representation for the object
27  std::shared_ptr<Entity> entity = m_entity.lock();
28  CHECK(entity != nullptr) << "FpsTxtCounter must have entity to initialize";
29  if (!entity->containsComponent(m_fpsTextVisualModel))
30  {
31  m_fpsTextVisualModel->setName(entity->getName() + "_FpsCounterTxt");
32  entity->addComponent(m_fpsTextVisualModel);
33  }
34  CHECK(m_viewer.lock() != nullptr) << "FpsTxtCounter must have a Viewer to track";
35 }
36 
37 void
38 FpsTxtCounter::visualUpdate(const double& dt)
39 {
40  std::shared_ptr<Viewer> viewer = m_viewer.lock();
41  const int infoLevel = viewer->getInfoLevel();
42  if (infoLevel != m_prevInfoLevel)
43  {
44  if (infoLevel == 0)
45  {
46  m_fpsTextVisualModel->setVisibility(false);
47  }
48  else if (infoLevel == 1 || infoLevel == 2)
49  {
50  m_fpsTextVisualModel->setVisibility(true);
51  }
52  }
53  m_prevInfoLevel = infoLevel;
54 
55  m_lastUpdate += dt;
56 
57  // Only update when visible
58  if (m_fpsTextVisualModel->getVisibility() && m_lastUpdate > m_fpsUpdateDelay)
59  {
60  std::shared_ptr<SceneManager> sceneManager = m_sceneManager.lock();
61 
62  std::string fpsVisualStr = "V: " +
63  std::to_string(static_cast<int>(viewer->getVisualFps())) + " | " +
64  "P: " +
65  std::to_string(sceneManager->getActiveScene()->getFrameTime() * 1000.0) + " ms";
66  m_fpsTextVisualModel->setText(fpsVisualStr);
67  m_lastUpdate = 0.0;
68  }
69 }
70 } // namespace imstk
void visualUpdate(const double &dt)
Update the display of the last frames update times.
Compound Geometry.
void init() override
Initialize the component, called at a later time after all component construction is complete...