fps_counter.cxx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // fps_counter.cxx
  3. // engine
  4. //
  5. // Created by Sam Jaffe on 5/24/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include "game/engine/fps_counter.hpp"
  9. #include <json/value.h>
  10. #include <unordered_map>
  11. #include "game/engine/serial.hpp"
  12. #include "game/engine/text_engine.hpp"
  13. #include "game/graphics/object.hpp"
  14. #include "game/graphics/renderer.hpp"
  15. using namespace engine;
  16. fps_counter::fps_counter(std::shared_ptr<text_engine> text_engine,
  17. std::size_t precision)
  18. : digits_(precision), magnitude_(std::pow(10, digits_)),
  19. text_engine_(text_engine), glyphs_() {}
  20. fps_counter::~fps_counter() {}
  21. void fps_counter::render(graphics::renderer & renderer) const {
  22. for (auto & obj : glyphs()) {
  23. renderer.draw(obj);
  24. }
  25. }
  26. std::string fps_counter::fps(env::clock::duration const & since) const {
  27. auto fps = magnitude_ * env::clock::duration::period::den / since.count();
  28. std::string fps_str = std::to_string(fps);
  29. fps_str.insert(fps_str.size() - digits_, ".");
  30. return fps_str;
  31. }
  32. void fps_counter::set_frame_step(env::clock::duration const & since) {
  33. if (++counter_ != change_after_) { return; }
  34. counter_ = 0;
  35. text_engine_->create_text_cells(glyphs_,
  36. {{5.f, 780.f}, {10.f, 20.f}, fps(since)});
  37. }