nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
round_trip.cc
Go to the documentation of this file.
1#include "client/round_trip.h"
2
3#include <imgui.h>
4
5#include <cfloat>
6#include <cmath>
7#include <iterator>
8#include <string>
9
10#include "absl/strings/substitute.h"
11#include "client/theme.h"
12#include "client/translations.h"
13
14namespace nanommo {
15
16namespace {
17
18// How much each new round trip moves the one shown, so that it doesn't jump
19// with each tick.
20constexpr float kSmoothing = 0.1f;
21
22} // namespace
23
24void RoundTrip::Sent(uint32_t sequence, double now) { sent_[sequence] = now; }
25
26void RoundTrip::Acked(uint32_t acked, double now) {
27 const auto sent = sent_.find(acked);
28 if (sent == sent_.end()) {
29 return;
30 }
31 const float measured = static_cast<float>((now - sent->second) * 1000.0);
32 milliseconds_ = milliseconds_ == 0.0f
33 ? measured
34 : milliseconds_ + (measured - milliseconds_) * kSmoothing;
35 // Those before it won't be acked on their own any more.
36 sent_.erase(sent_.begin(), std::next(sent));
37}
38
39float DrawRoundTrip(float milliseconds, float top) {
40 if (milliseconds == 0.0f) {
41 return 0.0f;
42 }
43 const std::string text = absl::Substitute(
44 Tr("$0 ms"), static_cast<int>(std::lround(milliseconds)));
45 ImFont* font = GetFonts().bold;
46 const float size = Ui(13.0f);
47 const ImVec2 extent = font->CalcTextSizeA(size, FLT_MAX, 0.0f, text.c_str());
48 const float right = ImGui::GetIO().DisplaySize.x - Ui(24.0f);
49 DrawShadowedText(ImGui::GetBackgroundDrawList(), font, size,
50 {right - extent.x, top + Ui(6.0f)}, colors::kMuted,
51 text.c_str());
52 return extent.y + Ui(6.0f);
53}
54
55} // namespace nanommo
void Sent(uint32_t sequence, double now)
We sent the input of that sequence, at the time, in seconds.
Definition round_trip.cc:24
void Acked(uint32_t acked, double now)
The world says the server applied the inputs up to acked.
Definition round_trip.cc:26
constexpr ImU32 kMuted
Definition theme.h:27
Definition chat.cc:9
float Ui(float points)
Definition theme.h:67
const char * Tr(const char *text)
float DrawRoundTrip(float milliseconds, float top)
Definition round_trip.cc:39
void DrawShadowedText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 color, const char *text)
Text with a shadow under it.
Definition theme.cc:272
const Fonts & GetFonts()
Definition theme.cc:66
ImFont * bold
Definition theme.h:43