fps_counter.cxx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. using namespace engine;
  15. fps_counter::fps_counter(std::shared_ptr<text_engine> text_engine,
  16. std::size_t precision)
  17. : digits_(precision), magnitude_(std::pow(10, digits_)),
  18. text_engine_(text_engine), glyphs_() {}
  19. fps_counter::~fps_counter() {}
  20. std::string fps_counter::fps(env::clock::duration const & since) const {
  21. auto fps = magnitude_ * env::clock::duration::period::den / since.count();
  22. std::string fps_str = std::to_string(fps);
  23. fps_str.insert(fps_str.size() - digits_, ".");
  24. return fps_str;
  25. }
  26. void fps_counter::set_frame_step(env::clock::duration const & since) {
  27. if (++counter_ != change_after_) { return; }
  28. counter_ = 0;
  29. text_engine_->create_text_cells(
  30. glyphs_, {make_vector(5.f, 680.f), make_vector(10.f, 20.f), fps(since)});
  31. }