nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
loading.cc
Go to the documentation of this file.
1#include "client/loading.h"
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <array>
7#include <cfloat>
8#include <string>
9
10#include "client/theme.h"
11
12namespace nanommo {
13
14namespace {
15
16// A frame that took long, such as the one that built the land, counts as a
17// short one, so that the fog doesn't jump.
18constexpr float kLongestFrame = 1.0f / 30.0f;
19
20// Fast at first, then slower as it ends.
21float EaseOut(float share) {
22 const float left = 1.0f - std::clamp(share, 0.0f, 1.0f);
23 return 1.0f - left * left * left;
24}
25
26} // namespace
27
29 if (stage_ != Stage::kCovered) {
30 stage_ = Stage::kCovering;
31 }
32 time_ = 0.0f;
33}
34
36 stage_ = Stage::kLifting;
37 time_ = 0.0f;
38}
39
40void Loading::Update(float seconds) {
41 switch (stage_) {
42 case Stage::kCovering:
43 stage_ = Stage::kCovered;
44 break;
45 case Stage::kLifting:
46 time_ += std::min(seconds, kLongestFrame);
47 if (time_ >= kLift) {
48 stage_ = Stage::kNone;
49 }
50 break;
51 case Stage::kNone:
52 case Stage::kCovered:
53 break;
54 }
55}
56
57float Loading::cover() const {
58 switch (stage_) {
59 case Stage::kCovering:
60 case Stage::kCovered:
61 return 1.0f;
62 case Stage::kLifting:
63 return 1.0f - std::clamp(time_ / kFade, 0.0f, 1.0f);
64 case Stage::kNone:
65 break;
66 }
67 return 0.0f;
68}
69
70float Loading::clear() const {
71 switch (stage_) {
72 case Stage::kCovering:
73 case Stage::kCovered:
74 return 0.0f;
75 case Stage::kLifting:
76 return EaseOut(time_ / kLift);
77 case Stage::kNone:
78 break;
79 }
80 return 1.0f;
81}
82
83void DrawLoadingScreen(float cover, const std::array<float, 3>& sky,
84 const std::string& title, const std::string& line) {
85 if (cover <= 0.0f) {
86 return;
87 }
88 ImDrawList* draw = ImGui::GetForegroundDrawList();
89 const ImVec2 screen = ImGui::GetIO().DisplaySize;
90 const auto byte = [](float value) {
91 return static_cast<int>(std::clamp(value, 0.0f, 1.0f) * 255.0f);
92 };
93 draw->AddRectFilled(
94 {0.0f, 0.0f}, screen,
95 IM_COL32(byte(sky[0]), byte(sky[1]), byte(sky[2]), byte(cover)));
96 // The words stay a little longer than the sky behind them, on a dark band
97 // across the middle, so that they read on a light sky and a dark one.
98 const float alpha = std::clamp(cover * 1.5f, 0.0f, 1.0f);
99 const float middle = screen.y * 0.45f;
100 const float band = Ui(110.0f);
101 const ImU32 dark = Faded(colors::kNight, 0.7f * alpha);
102 const ImU32 clear = Faded(colors::kNight, 0.0f);
103 draw->AddRectFilledMultiColor({0.0f, middle - band}, {screen.x, middle},
104 clear, clear, dark, dark);
105 draw->AddRectFilledMultiColor({0.0f, middle}, {screen.x, middle + band}, dark,
106 dark, clear, clear);
107 ImFont* title_font = GetFonts().title;
108 const float title_size = Ui(58.0f);
109 const ImVec2 title_extent =
110 title_font->CalcTextSizeA(title_size, FLT_MAX, 0.0f, title.c_str());
111 const float top = middle - title_extent.y * 0.75f;
112 DrawGradientText(draw, title_font, title_size,
113 {(screen.x - title_extent.x) / 2.0f, top},
115 Faded(colors::kGoldDark, alpha), title.c_str());
116 if (alpha >= 0.99f) {
117 DrawDivider(draw,
118 {screen.x / 2.0f - title_extent.x * 0.6f,
119 top + title_extent.y + Ui(4.0f)},
120 title_extent.x * 1.2f);
121 }
122 ImFont* text = GetFonts().text;
123 const float line_size = Ui(20.0f);
124 const ImVec2 line_extent =
125 text->CalcTextSizeA(line_size, FLT_MAX, 0.0f, line.c_str());
127 draw, text, line_size,
128 {(screen.x - line_extent.x) / 2.0f, top + title_extent.y + Ui(14.0f)},
129 Faded(colors::kPaper, alpha * 0.85f), line.c_str());
130}
131
132} // namespace nanommo
static constexpr float kLift
Definition loading.h:18
float clear() const
Definition loading.cc:70
void Lift()
The land is built, so the cover fades and the fog lifts.
Definition loading.cc:35
float cover() const
How much of the screen the cover hides, from 0 to 1.
Definition loading.cc:57
void Cover()
Covers the world from the next frame on.
Definition loading.cc:28
static constexpr float kFade
In seconds.
Definition loading.h:17
void Update(float seconds)
Moves on by the frame's time, once the frame is drawn.
Definition loading.cc:40
constexpr ImU32 kGoldDark
Definition theme.h:23
constexpr ImU32 kGoldLight
Definition theme.h:22
constexpr ImU32 kNight
Definition theme.h:14
constexpr ImU32 kPaper
Definition theme.h:26
Definition chat.cc:9
void DrawDivider(ImDrawList *draw, ImVec2 left, float width)
Definition theme.cc:257
float Ui(float points)
Definition theme.h:67
void DrawLoadingScreen(float cover, const std::array< float, 3 > &sky, const std::string &title, const std::string &line)
Definition loading.cc:83
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
Definition theme.cc:59
void DrawGradientText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 top, ImU32 bottom, const char *text)
Definition theme.cc:283
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 * text
Definition theme.h:42
ImFont * title
Definition theme.h:44