| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- //
- // fps_counter.cxx
- // engine
- //
- // Created by Sam Jaffe on 5/24/19.
- // Copyright © 2019 Sam Jaffe. All rights reserved.
- //
- #include "game/engine/fps_counter.hpp"
- #include <json/value.h>
- #include <unordered_map>
- #include "game/engine/serial.hpp"
- #include "game/engine/text_engine.hpp"
- #include "game/graphics/object.hpp"
- #include "game/graphics/renderer.hpp"
- using namespace engine;
- fps_counter::fps_counter(std::shared_ptr<text_engine> text_engine,
- std::size_t precision)
- : digits_(precision), magnitude_(std::pow(10, digits_)),
- text_engine_(text_engine), glyphs_() {}
- fps_counter::~fps_counter() {}
- void fps_counter::render(graphics::renderer & renderer) const {
- for (auto & obj : glyphs()) {
- renderer.draw(obj);
- }
- }
- std::string fps_counter::fps(env::clock::duration const & since) const {
- auto fps = magnitude_ * env::clock::duration::period::den / since.count();
- std::string fps_str = std::to_string(fps);
- fps_str.insert(fps_str.size() - digits_, ".");
- return fps_str;
- }
- void fps_counter::set_frame_step(env::clock::duration const & since) {
- if (++counter_ != change_after_) { return; }
- counter_ = 0;
- text_engine_->create_text_cells(glyphs_,
- {{5.f, 780.f}, {10.f, 20.f}, fps(since)});
- }
|