|
|
@@ -12,65 +12,29 @@
|
|
|
#include <unordered_map>
|
|
|
|
|
|
#include "game/engine/serial.hpp"
|
|
|
+#include "game/engine/text_engine.hpp"
|
|
|
#include "game/graphics/object.hpp"
|
|
|
|
|
|
using namespace engine;
|
|
|
|
|
|
-static graphics::object create(graphics::manager const & mgr) {
|
|
|
- Json::Value value;
|
|
|
- value["material"]["texture"]["file"] = "data/images/font.bmp";
|
|
|
- value["material"]["texture"]["uniform"] = "u_normalMap";
|
|
|
- return engine::to_object(value, mgr);
|
|
|
-}
|
|
|
-
|
|
|
-static std::size_t digits(env::clock::duration const & fpscap) {
|
|
|
- auto fps = env::clock::duration::period::den / fpscap.count();
|
|
|
- std::size_t digits = 1;
|
|
|
- while (fps /= 10)
|
|
|
- ++digits;
|
|
|
- return digits;
|
|
|
-}
|
|
|
-
|
|
|
-std::unordered_map<char, math::vec2> const indices{
|
|
|
- {'.', make_vector(6.f, 6.f)}, {'0', make_vector(0.f, 5.f)},
|
|
|
- {'1', make_vector(1.f, 5.f)}, {'2', make_vector(2.f, 5.f)},
|
|
|
- {'3', make_vector(3.f, 5.f)}, {'4', make_vector(4.f, 5.f)},
|
|
|
- {'5', make_vector(5.f, 5.f)}, {'6', make_vector(6.f, 5.f)},
|
|
|
- {'7', make_vector(7.f, 5.f)}, {'8', make_vector(0.f, 4.f)},
|
|
|
- {'9', make_vector(1.f, 4.f)}};
|
|
|
-
|
|
|
-math::vec2 index(char value) {
|
|
|
- auto it = indices.find(value);
|
|
|
- return it != indices.end() ? it->second : make_vector(0.f, 0.f);
|
|
|
-}
|
|
|
-
|
|
|
-math::vec2 number_index(int num) { return index(char(num + '0')); }
|
|
|
-
|
|
|
fps_counter::fps_counter(env::clock::duration const & fpscap,
|
|
|
- graphics::manager const & mgr, std::size_t precision)
|
|
|
+ std::shared_ptr<text_engine> text_engine,
|
|
|
+ std::size_t precision)
|
|
|
: digits_(precision), magnitude_(std::pow(10, digits_)),
|
|
|
- glyphs_(digits(fpscap) + digits_ + 1, create(mgr)) {
|
|
|
- auto pixels = make_vector(0.125f, 0.125f);
|
|
|
- for (std::size_t i = 0; i < glyphs_.size(); ++i) {
|
|
|
- glyphs_[i].location.origin =
|
|
|
- make_vector((glyphs_.size() - i) * 10.f - 5.f, 680.f);
|
|
|
- glyphs_[i].location.size = make_vector(20.f, 20.f);
|
|
|
- glyphs_[i].points = glyphs_[i].location;
|
|
|
- glyphs_[i].frame.size = pixels;
|
|
|
- }
|
|
|
- glyphs_[digits_].frame.origin = index('.') * pixels;
|
|
|
-}
|
|
|
+ text_engine_(text_engine), glyphs_() {}
|
|
|
|
|
|
fps_counter::~fps_counter() {}
|
|
|
|
|
|
+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_ + 1, ".");
|
|
|
+ return fps_str;
|
|
|
+}
|
|
|
+
|
|
|
void fps_counter::set_frame_step(env::clock::duration const & since) {
|
|
|
if (++counter_ != change_after_) { return; }
|
|
|
counter_ = 0;
|
|
|
- auto pixels = make_vector(0.125f, 0.125f);
|
|
|
- auto fps = magnitude_ * env::clock::duration::period::den / since.count();
|
|
|
- for (std::size_t i = 0; i < glyphs_.size(); ++i) {
|
|
|
- if (i == digits_) continue;
|
|
|
- glyphs_[i].frame.origin = number_index(fps % 10) * pixels;
|
|
|
- fps /= 10;
|
|
|
- }
|
|
|
+ text_engine_->create_text_cells(
|
|
|
+ glyphs_, {make_vector(5.f, 680.f), make_vector(20.f, 20.f), fps(since)});
|
|
|
}
|