0

How can i draw a simple countdown from 80 to 0 in dear imgui?

i tried like this but for some reason its lag a lot like 3 fps


AddText(ImVec2(1700, 400), ImColor(255, 255, 255), "80");
Sleep(1000);
AddText(ImVec2(1700, 400), ImColor(255, 255, 255), "79");
Sleep(1000);
AddText(ImVec2(1700, 400), ImColor(255, 255, 255), "78");

some one know how to do it better maybe like where the numbers are change like from 80 to 79 and from 79 to 78 and so on

1 Answers1

0

The Sleep call blocks your entire application, especially the loop that needs to refresh the screen. You need to use an actual clock or timer and compare the current time against some arbitrary point in the future:

// at startup
const auto deadline = std::chrono::high_resolution_clock::now() + std::chrono::seconds(80);

// in your game loop
const auto now = std::chrono::high_resolution_clock::now();
if (now < deadline) {
  auto delta = std::chrono::duration_cast<std::chrono::seconds>(t2 - t1);
  AddText(ImVec2(1700, 400), ImColor(255, 255, 255), std::to_string(delta).c_str());
}

If you want to have a less static countdown you can switch to measuring milliseconds and then rounding that to the nearest hundredth of a second (eg XX.YY)

Botje
  • 26,269
  • 3
  • 31
  • 41